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.

129 lines
3.9 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. void test_fingerprint() {
  75. var pem = """-----BEGIN CERTIFICATE-----
  76. MIIC8jCCAdoCAQowDQYJKoZIhvcNAQEFBQAwPzERMA8GA1UEChMIbWNvbm5lY3Qx
  77. ETAPBgNVBAsTCG1jb25uZWN0MRcwFQYDVQQDDA5tYWNpZWtAY29yc2FpcjAeFw0x
  78. NzA5MjQxOTU3NDVaFw0yNzA5MjQxOTU3NDVaMD8xETAPBgNVBAoTCG1jb25uZWN0
  79. MREwDwYDVQQLEwhtY29ubmVjdDEXMBUGA1UEAwwObWFjaWVrQGNvcnNhaXIwggEi
  80. MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDBWMM09dCCGXjY1aZ261nCa8+q
  81. 3ejDOHf21+Mt6yJnoWjPGvTK21MbRFPkeQe62FJHF3q8iXe3sSEdFk/f56G8ZZ4t
  82. Qw/ST//kxtf/CKHPuoZeAFgQpYEKE3GVUX5M3b8+4YSKSRXs3FE4D5awQVdstI31
  83. N53plyOLdJe5IeK1kESsT0UgVo2RTHCOByel9WB2zcalQVTl8UxGKJcgrSuXj2f2
  84. 1SBxHupOX0Ej7vTr+gIBNBRbwbVdsafEW/gRngyCWIO30cPKoaUSkoCppXQ+6hYd
  85. /aOt+6/bBOISGJdy6uyM74jqoEbEMdhUXHfPFNCelIABxUMez0SNrRmdag2VAgMB
  86. AAEwDQYJKoZIhvcNAQEFBQADggEBAEnJ+IsjGvBmlWAg5vlmWUY0OVMJa8Gl9ept
  87. HWLIjK16XARAOwIcePNfDPyITWaxT5YV+MZotm1m6HkY5rPCeOjV7nzHrHjTjZqO
  88. sCmsSGcb9ZkEQfRNGTmFFthkcnfTU9mKh8oGc/a9r0DDgYcPSCgqERt2fgiBrt85
  89. 85PVl16fCSObVwOu5u5TtrfWkHpEHbjBU9AX52+IOYg7RsM7I4OnH+5svhmWqAxW
  90. /PXFBB3q2nX2XXqFRhqeN9eOlHBQ5EoZh8GUp7vJyxp5eAS9g2KVtCBwTDElQt4D
  91. 4hu+QuzzEmoWY9w1R+hblNu/37mWkzFFrLqYlkNU2vbKkuWMOTg=
  92. -----END CERTIFICATE-----""";
  93. var expected = "eb2611a447085322b206fa61d4bc5869b4a55657";
  94. var fingerprint = Crypt.fingerprint_certificate(pem);
  95. // SHA1
  96. assert(fingerprint.length == 20);
  97. var sb = new StringBuilder.sized(20*2);
  98. foreach(var b in fingerprint) {
  99. sb.append_printf("%02x", b);
  100. }
  101. assert(sb.str == expected);
  102. }
  103. public static void main(string[] args) {
  104. Test.init(ref args);
  105. Test.add_func("/mconn-crypt-vala/generated", test_generate);
  106. Test.add_func("/mconn-crypt-vala/load", test_generate_load);
  107. Test.add_func("/mconn-crypt-vala/verify-cn", test_custom_cn);
  108. Test.add_func("/mconn-crypt-vala/fingerprint", test_fingerprint);
  109. Test.run();
  110. }