mconnect - KDE Connect protocol implementation in Vala/C
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.4 KiB

  1. using Mconn;
  2. void test_generate() {
  3. string key_path = "/tmp/test-key-vala.pem";
  4. string cert_path = "/tmp/test-cert-vala.pem";
  5. FileUtils.remove(key_path);
  6. FileUtils.remove(cert_path);
  7. assert(FileUtils.test(key_path, FileTest.EXISTS) == false);
  8. try {
  9. Crypt.generate_key_cert(key_path, cert_path, "foo");
  10. } catch (Error e) {
  11. warning("generate failed: %s", e.message);
  12. Test.fail();
  13. }
  14. assert(FileUtils.test(key_path, FileTest.EXISTS) == true);
  15. assert(FileUtils.test(cert_path, FileTest.EXISTS) == true);
  16. }
  17. void test_generate_load() {
  18. string key_path = "/tmp/test-key-vala.pem";
  19. string cert_path = "/tmp/test-cert-vala.pem";
  20. FileUtils.remove(key_path);
  21. FileUtils.remove(cert_path);
  22. try {
  23. Crypt.generate_key_cert(key_path, cert_path, "bar");
  24. } catch (Error e) {
  25. warning("generate failed: %s", e.message);
  26. Test.fail();
  27. }
  28. try {
  29. var cert = new TlsCertificate.from_files(cert_path,
  30. key_path);
  31. } catch (Error e) {
  32. warning("load from files failed: %s", e.message);
  33. Test.fail();
  34. }
  35. }
  36. void test_custom_cn() {
  37. string key_path = "/tmp/test-key-vala.pem";
  38. string cert_path = "/tmp/test-cert-vala.pem";
  39. FileUtils.remove(key_path);
  40. FileUtils.remove(cert_path);
  41. try {
  42. Crypt.generate_key_cert(key_path, cert_path, "custom-cn");
  43. } catch (Error e) {
  44. warning("generate failed: %s", e.message);
  45. Test.fail();
  46. }
  47. uint8[] data;
  48. try {
  49. File.new_for_path(cert_path).load_contents(null, out data, null);
  50. } catch (Error e) {
  51. warning("load contents failed: %s", e.message);
  52. Test.fail();
  53. }
  54. var datum = GnuTLS.Datum() { data=data, size=data.length };
  55. var cert = GnuTLS.X509.Certificate.create();
  56. var res = cert.import(ref datum, GnuTLS.X509.CertificateFormat.PEM);
  57. assert(res == GnuTLS.ErrorCode.SUCCESS);
  58. // verify DN
  59. var dn = new uint8[1024];
  60. size_t sz = dn.length;
  61. cert.get_dn(dn, ref sz);
  62. debug("dn: %s\n", (string)dn);
  63. var issuer_dn = new uint8[1024];
  64. sz = issuer_dn.length;
  65. cert.get_issuer_dn(issuer_dn, ref sz);
  66. debug("dn: %s\n", (string)issuer_dn);
  67. var subject = (string)dn;
  68. var issuer = (string)issuer_dn;
  69. // verify that the certificate is self signed
  70. assert(subject == issuer);
  71. //
  72. assert("CN=custom-cn" in subject);
  73. }
  74. public static void main(string[] args) {
  75. Test.init(ref args);
  76. Test.add_func("/mconn-crypt-vala/generated", test_generate);
  77. Test.add_func("/mconn-crypt-vala/load", test_generate_load);
  78. Test.add_func("/mconn-crypt-vala/verify-cn", test_custom_cn);
  79. Test.run();
  80. }