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.

131 lines
4.2 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. new TlsCertificate.from_files (cert_path, key_path);
  30. } catch (Error e) {
  31. warning ("load from files failed: %s", e.message);
  32. Test.fail ();
  33. }
  34. }
  35. void test_custom_cn () {
  36. string key_path = "/tmp/test-key-vala.pem";
  37. string cert_path = "/tmp/test-cert-vala.pem";
  38. FileUtils.remove (key_path);
  39. FileUtils.remove (cert_path);
  40. try {
  41. Crypt.generate_key_cert (key_path, cert_path, "custom-cn");
  42. } catch (Error e) {
  43. warning ("generate failed: %s", e.message);
  44. Test.fail ();
  45. }
  46. uint8[] data;
  47. try {
  48. File.new_for_path (cert_path).load_contents (null, out data, null);
  49. } catch (Error e) {
  50. warning ("load contents failed: %s", e.message);
  51. Test.fail ();
  52. }
  53. var datum = GnuTLS.Datum () {
  54. data = data, size = data.length
  55. };
  56. var cert = GnuTLS.X509.Certificate.create ();
  57. var res = cert.import (ref datum, GnuTLS.X509.CertificateFormat.PEM);
  58. assert (res == GnuTLS.ErrorCode.SUCCESS);
  59. // verify DN
  60. var dn = new uint8[1024];
  61. size_t sz = dn.length;
  62. cert.get_dn (dn, ref sz);
  63. debug ("dn: %s\n", (string) dn);
  64. var issuer_dn = new uint8[1024];
  65. sz = issuer_dn.length;
  66. cert.get_issuer_dn (issuer_dn, ref sz);
  67. debug ("dn: %s\n", (string) issuer_dn);
  68. var subject = (string) dn;
  69. var issuer = (string) issuer_dn;
  70. // verify that the certificate is self signed
  71. assert (subject == issuer);
  72. //
  73. assert ("CN=custom-cn" in subject);
  74. }
  75. void test_fingerprint () {
  76. var pem = """-----BEGIN CERTIFICATE-----
  77. MIIC8jCCAdoCAQowDQYJKoZIhvcNAQEFBQAwPzERMA8GA1UEChMIbWNvbm5lY3Qx
  78. ETAPBgNVBAsTCG1jb25uZWN0MRcwFQYDVQQDDA5tYWNpZWtAY29yc2FpcjAeFw0x
  79. NzA5MjQxOTU3NDVaFw0yNzA5MjQxOTU3NDVaMD8xETAPBgNVBAoTCG1jb25uZWN0
  80. MREwDwYDVQQLEwhtY29ubmVjdDEXMBUGA1UEAwwObWFjaWVrQGNvcnNhaXIwggEi
  81. MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDBWMM09dCCGXjY1aZ261nCa8+q
  82. 3ejDOHf21+Mt6yJnoWjPGvTK21MbRFPkeQe62FJHF3q8iXe3sSEdFk/f56G8ZZ4t
  83. Qw/ST//kxtf/CKHPuoZeAFgQpYEKE3GVUX5M3b8+4YSKSRXs3FE4D5awQVdstI31
  84. N53plyOLdJe5IeK1kESsT0UgVo2RTHCOByel9WB2zcalQVTl8UxGKJcgrSuXj2f2
  85. 1SBxHupOX0Ej7vTr+gIBNBRbwbVdsafEW/gRngyCWIO30cPKoaUSkoCppXQ+6hYd
  86. /aOt+6/bBOISGJdy6uyM74jqoEbEMdhUXHfPFNCelIABxUMez0SNrRmdag2VAgMB
  87. AAEwDQYJKoZIhvcNAQEFBQADggEBAEnJ+IsjGvBmlWAg5vlmWUY0OVMJa8Gl9ept
  88. HWLIjK16XARAOwIcePNfDPyITWaxT5YV+MZotm1m6HkY5rPCeOjV7nzHrHjTjZqO
  89. sCmsSGcb9ZkEQfRNGTmFFthkcnfTU9mKh8oGc/a9r0DDgYcPSCgqERt2fgiBrt85
  90. 85PVl16fCSObVwOu5u5TtrfWkHpEHbjBU9AX52+IOYg7RsM7I4OnH+5svhmWqAxW
  91. /PXFBB3q2nX2XXqFRhqeN9eOlHBQ5EoZh8GUp7vJyxp5eAS9g2KVtCBwTDElQt4D
  92. 4hu+QuzzEmoWY9w1R+hblNu/37mWkzFFrLqYlkNU2vbKkuWMOTg=
  93. -----END CERTIFICATE-----""";
  94. var expected = "eb2611a447085322b206fa61d4bc5869b4a55657";
  95. var fingerprint = Crypt.fingerprint_certificate (pem);
  96. // SHA1
  97. assert (fingerprint.length == 20);
  98. var sb = new StringBuilder.sized (20 * 2);
  99. foreach (var b in fingerprint) {
  100. sb.append_printf ("%02x", b);
  101. }
  102. assert (sb.str == expected);
  103. }
  104. public static void main (string[] args) {
  105. Test.init (ref args);
  106. Test.add_func ("/mconn-crypt-vala/generated", test_generate);
  107. Test.add_func ("/mconn-crypt-vala/load", test_generate_load);
  108. Test.add_func ("/mconn-crypt-vala/verify-cn", test_custom_cn);
  109. Test.add_func ("/mconn-crypt-vala/fingerprint", test_fingerprint);
  110. Test.run ();
  111. }