x509_strtest.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <gmssl/oid.h>
  13. #include <gmssl/x509_ext.h>
  14. #include <gmssl/x509.h>
  15. #include <gmssl/rand.h>
  16. #include <gmssl/error.h>
  17. static int test_x509_directory_name(void)
  18. {
  19. uint8_t str[] = { 'a', 'b', 'c', 0 };
  20. uint8_t buf[256];
  21. uint8_t *p = buf;
  22. const uint8_t *cp = buf;
  23. size_t len = 0;
  24. int tag;
  25. const uint8_t *d;
  26. size_t dlen;
  27. if (x509_directory_name_check_ex(ASN1_TAG_UTF8String, str, 3, 1, 10) != 1 // str,4 will fail
  28. || x509_directory_name_to_der(ASN1_TAG_UTF8String, str, 3, &p, &len) != 1
  29. || x509_directory_name_from_der(&tag, &d, &dlen, &cp, &len) != 1
  30. || asn1_check(tag == ASN1_TAG_UTF8String) != 1
  31. || asn1_check(dlen == 3) != 1
  32. || asn1_check(memcmp(str, d, dlen) == 0) != 1
  33. || asn1_length_is_zero(len) != 1) {
  34. error_print();
  35. return -1;
  36. }
  37. printf("%s() ok\n", __FUNCTION__);
  38. return 1;
  39. }
  40. static int test_x509_display_text(void)
  41. {
  42. uint8_t str[] = { 'a', 'b', 'c', 0 };
  43. uint8_t buf[256];
  44. uint8_t *p = buf;
  45. const uint8_t *cp = buf;
  46. size_t len = 0;
  47. int tag;
  48. const uint8_t *d;
  49. size_t dlen;
  50. if (x509_display_text_check(ASN1_TAG_UTF8String, str, 3) != 1 // str,4 will fail
  51. || x509_display_text_to_der(ASN1_TAG_UTF8String, str, 3, &p, &len) != 1
  52. || x509_display_text_from_der(&tag, &d, &dlen, &cp, &len) != 1
  53. || asn1_check(tag == ASN1_TAG_UTF8String) != 1
  54. || asn1_check(dlen == 3) != 1
  55. || asn1_check(memcmp(str, d, dlen) == 0) != 1
  56. || asn1_length_is_zero(len) != 1) {
  57. error_print();
  58. return -1;
  59. }
  60. printf("%s() ok\n", __FUNCTION__);
  61. return 1;
  62. }
  63. int main(void)
  64. {
  65. if (test_x509_directory_name() != 1) goto err;
  66. if (test_x509_display_text() != 1) goto err;
  67. printf("%s all tests passed!\n", __FILE__);
  68. return 0;
  69. err:
  70. error_print();
  71. return 1;
  72. }