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.3 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 () {
  55. data = data, size = data.length
  56. };
  57. var cert = GnuTLS.X509.Certificate.create ();
  58. var res = cert.import (ref datum, GnuTLS.X509.CertificateFormat.PEM);
  59. assert (res == GnuTLS.ErrorCode.SUCCESS);
  60. // verify DN
  61. var dn = new uint8[1024];
  62. size_t sz = dn.length;
  63. cert.get_dn (dn, ref sz);
  64. debug ("dn: %s\n", (string) dn);
  65. var issuer_dn = new uint8[1024];
  66. sz = issuer_dn.length;
  67. cert.get_issuer_dn (issuer_dn, ref sz);
  68. debug ("dn: %s\n", (string) issuer_dn);
  69. var subject = (string) dn;
  70. var issuer = (string) issuer_dn;
  71. // verify that the certificate is self signed
  72. assert (subject == issuer);
  73. //
  74. assert ("CN=custom-cn" in subject);
  75. }
  76. void test_fingerprint () {
  77. var pem = """-----BEGIN CERTIFICATE-----
  78. MIIC8jCCAdoCAQowDQYJKoZIhvcNAQEFBQAwPzERMA8GA1UEChMIbWNvbm5lY3Qx
  79. ETAPBgNVBAsTCG1jb25uZWN0MRcwFQYDVQQDDA5tYWNpZWtAY29yc2FpcjAeFw0x
  80. NzA5MjQxOTU3NDVaFw0yNzA5MjQxOTU3NDVaMD8xETAPBgNVBAoTCG1jb25uZWN0
  81. MREwDwYDVQQLEwhtY29ubmVjdDEXMBUGA1UEAwwObWFjaWVrQGNvcnNhaXIwggEi
  82. MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDBWMM09dCCGXjY1aZ261nCa8+q
  83. 3ejDOHf21+Mt6yJnoWjPGvTK21MbRFPkeQe62FJHF3q8iXe3sSEdFk/f56G8ZZ4t
  84. Qw/ST//kxtf/CKHPuoZeAFgQpYEKE3GVUX5M3b8+4YSKSRXs3FE4D5awQVdstI31
  85. N53plyOLdJe5IeK1kESsT0UgVo2RTHCOByel9WB2zcalQVTl8UxGKJcgrSuXj2f2
  86. 1SBxHupOX0Ej7vTr+gIBNBRbwbVdsafEW/gRngyCWIO30cPKoaUSkoCppXQ+6hYd
  87. /aOt+6/bBOISGJdy6uyM74jqoEbEMdhUXHfPFNCelIABxUMez0SNrRmdag2VAgMB
  88. AAEwDQYJKoZIhvcNAQEFBQADggEBAEnJ+IsjGvBmlWAg5vlmWUY0OVMJa8Gl9ept
  89. HWLIjK16XARAOwIcePNfDPyITWaxT5YV+MZotm1m6HkY5rPCeOjV7nzHrHjTjZqO
  90. sCmsSGcb9ZkEQfRNGTmFFthkcnfTU9mKh8oGc/a9r0DDgYcPSCgqERt2fgiBrt85
  91. 85PVl16fCSObVwOu5u5TtrfWkHpEHbjBU9AX52+IOYg7RsM7I4OnH+5svhmWqAxW
  92. /PXFBB3q2nX2XXqFRhqeN9eOlHBQ5EoZh8GUp7vJyxp5eAS9g2KVtCBwTDElQt4D
  93. 4hu+QuzzEmoWY9w1R+hblNu/37mWkzFFrLqYlkNU2vbKkuWMOTg=
  94. -----END CERTIFICATE-----""";
  95. var expected = "eb2611a447085322b206fa61d4bc5869b4a55657";
  96. var fingerprint = Crypt.fingerprint_certificate (pem);
  97. // SHA1
  98. assert (fingerprint.length == 20);
  99. var sb = new StringBuilder.sized (20 * 2);
  100. foreach (var b in fingerprint) {
  101. sb.append_printf ("%02x", b);
  102. }
  103. assert (sb.str == expected);
  104. }
  105. public static void main (string[] args) {
  106. Test.init (ref args);
  107. Test.add_func ("/mconn-crypt-vala/generated", test_generate);
  108. Test.add_func ("/mconn-crypt-vala/load", test_generate_load);
  109. Test.add_func ("/mconn-crypt-vala/verify-cn", test_custom_cn);
  110. Test.add_func ("/mconn-crypt-vala/fingerprint", test_fingerprint);
  111. Test.run ();
  112. }