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.

78 lines
2.0 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. Crypt.generate_key_cert(key_path, cert_path, "foo");
  9. assert(FileUtils.test(key_path, FileTest.EXISTS) == true);
  10. assert(FileUtils.test(cert_path, FileTest.EXISTS) == true);
  11. }
  12. void test_generate_load() {
  13. string key_path = "/tmp/test-key-vala.pem";
  14. string cert_path = "/tmp/test-cert-vala.pem";
  15. FileUtils.remove(key_path);
  16. FileUtils.remove(cert_path);
  17. Crypt.generate_key_cert(key_path, cert_path, "bar");
  18. try {
  19. var cert = new TlsCertificate.from_files(cert_path,
  20. key_path);
  21. } catch (Error e) {
  22. Test.fail();
  23. }
  24. }
  25. void test_custom_cn() {
  26. string key_path = "/tmp/test-key-vala.pem";
  27. string cert_path = "/tmp/test-cert-vala.pem";
  28. FileUtils.remove(key_path);
  29. FileUtils.remove(cert_path);
  30. Crypt.generate_key_cert(key_path, cert_path, "custom-cn");
  31. uint8[] data;
  32. try {
  33. File.new_for_path(cert_path).load_contents(null, out data, null);
  34. } catch (Error e) {
  35. Test.fail();
  36. }
  37. var datum = GnuTLS.Datum() { data=data, size=data.length };
  38. var cert = GnuTLS.X509.Certificate.create();
  39. var res = cert.import(ref datum, GnuTLS.X509.CertificateFormat.PEM);
  40. assert(res == GnuTLS.ErrorCode.SUCCESS);
  41. // verify DN
  42. var dn = new uint8[1024];
  43. size_t sz = dn.length;
  44. cert.get_dn(dn, ref sz);
  45. debug("dn: %s\n", (string)dn);
  46. var issuer_dn = new uint8[1024];
  47. sz = issuer_dn.length;
  48. cert.get_issuer_dn(issuer_dn, ref sz);
  49. debug("dn: %s\n", (string)issuer_dn);
  50. var subject = (string)dn;
  51. var issuer = (string)issuer_dn;
  52. // verify that the certificate is self signed
  53. assert(subject == issuer);
  54. //
  55. assert("CN=custom-cn" in subject);
  56. }
  57. public static void main(string[] args) {
  58. Test.init(ref args);
  59. Test.add_func("/mconn-crypt-vala/generated", test_generate);
  60. Test.add_func("/mconn-crypt-vala/load", test_generate_load);
  61. Test.add_func("/mconn-crypt-vala/verify-cn", test_custom_cn);
  62. Test.run();
  63. }