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
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <gmssl/oid.h>
15 #include <gmssl/x509_str.h>
16 #include <gmssl/x509.h>
17 #include <gmssl/rand.h>
18 #include <gmssl/error.h>
19
test_x509_directory_name(void)20 static int test_x509_directory_name(void)
21 {
22 uint8_t str[] = { 'a', 'b', 'c', 0 };
23 uint8_t buf[256];
24 uint8_t *p = buf;
25 const uint8_t *cp = buf;
26 size_t len = 0;
27 int tag;
28 const uint8_t *d;
29 size_t dlen;
30
31 if (x509_directory_name_check_ex(ASN1_TAG_UTF8String, str, 3, 1, 10) != 1 // str,4 will fail
32 || x509_directory_name_to_der(ASN1_TAG_UTF8String, str, 3, &p, &len) != 1
33 || x509_directory_name_from_der(&tag, &d, &dlen, &cp, &len) != 1
34 || asn1_check(tag == ASN1_TAG_UTF8String) != 1
35 || asn1_check(dlen == 3) != 1
36 || asn1_check(memcmp(str, d, dlen) == 0) != 1
37 || asn1_length_is_zero(len) != 1) {
38 error_print();
39 return -1;
40 }
41 printf("%s() ok\n", __FUNCTION__);
42 return 1;
43 }
44
test_x509_display_text(void)45 static int test_x509_display_text(void)
46 {
47 uint8_t str[] = { 'a', 'b', 'c', 0 };
48 uint8_t buf[256];
49 uint8_t *p = buf;
50 const uint8_t *cp = buf;
51 size_t len = 0;
52 int tag;
53 const uint8_t *d;
54 size_t dlen;
55
56 if (x509_display_text_check(ASN1_TAG_UTF8String, str, 3) != 1 // str,4 will fail
57 || x509_display_text_to_der(ASN1_TAG_UTF8String, str, 3, &p, &len) != 1
58 || x509_display_text_from_der(&tag, &d, &dlen, &cp, &len) != 1
59 || asn1_check(tag == ASN1_TAG_UTF8String) != 1
60 || asn1_check(dlen == 3) != 1
61 || asn1_check(memcmp(str, d, dlen) == 0) != 1
62 || asn1_length_is_zero(len) != 1) {
63 error_print();
64 return -1;
65 }
66 printf("%s() ok\n", __FUNCTION__);
67 return 1;
68 }
69
main(void)70 int main(void)
71 {
72 if (test_x509_directory_name() != 1) goto err;
73 if (test_x509_display_text() != 1) goto err;
74 printf("%s all tests passed!\n", __FILE__);
75 return 0;
76 err:
77 error_print();
78 return 1;
79 }
80