Line data Source code
1 : /*
2 : Copyright (C) 2018 Red Hat, Inc.
3 :
4 : This library is free software; you can redistribute it and/or
5 : modify it under the terms of the GNU Lesser General Public
6 : License as published by the Free Software Foundation; either
7 : version 2.1 of the License, or (at your option) any later version.
8 :
9 : This library is distributed in the hope that it will be useful,
10 : but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : Lesser General Public License for more details.
13 :
14 : You should have received a copy of the GNU Lesser General Public
15 : License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 : */
17 : #include "common/ssl_verify.c"
18 :
19 : static gchar **result_set = NULL;
20 : static gchar **next_result = NULL;
21 : static int entry_count = 0;
22 :
23 : // set expected result for next test, these will be checked
24 : // results will be separated by ':' which is not a special character
25 16 : static void setup_results(const char *results)
26 : {
27 16 : g_assert_null(result_set);
28 16 : g_assert_null(next_result);
29 16 : result_set = g_strsplit_set(results, ":", -1);
30 16 : guint len = g_strv_length(result_set);
31 16 : g_assert_true(len % 2 == 0);
32 16 : next_result = result_set;
33 16 : entry_count = len / 2;
34 16 : }
35 :
36 : // cleanup results and prepare for next test
37 16 : static void tear_results(void)
38 : {
39 16 : g_assert_nonnull(next_result);
40 16 : g_assert_null(*next_result);
41 16 : g_strfreev(result_set);
42 16 : result_set = NULL;
43 16 : entry_count = 0;
44 16 : next_result = NULL;
45 16 : }
46 :
47 : // get next expected value
48 24 : static const char *get_next_result(void)
49 : {
50 24 : g_assert_nonnull(next_result);
51 24 : g_assert_nonnull(*next_result);
52 24 : return *next_result++;
53 : }
54 :
55 : // This override the OpenSSL function
56 : int X509_NAME_add_entry_by_txt(X509_NAME *name, const char *field, int type,
57 : const unsigned char *bytes, int len, int loc,
58 : int set)
59 : {
60 12 : g_assert_nonnull(name);
61 12 : g_assert_nonnull(field);
62 12 : g_assert_cmpint(type, ==, MBSTRING_UTF8);
63 12 : g_assert_nonnull(bytes);
64 12 : g_assert_cmpint(len, ==, -1);
65 12 : g_assert_cmpint(loc, ==, -1);
66 12 : g_assert_cmpint(set, ==, 0);
67 12 : g_assert_cmpstr(field, ==, get_next_result());
68 12 : g_assert_cmpstr((const char *)bytes, ==, get_next_result());
69 12 : return 1;
70 : }
71 :
72 : typedef struct {
73 : const char *input;
74 : const char *output;
75 : gboolean success;
76 : } TestGenericParams;
77 :
78 16 : static void test_generic(const void *arg)
79 : {
80 16 : const TestGenericParams *params = arg;
81 : X509_NAME *name;
82 : int num_entries;
83 :
84 16 : setup_results(params->output);
85 16 : name = subject_to_x509_name(params->input, &num_entries);
86 16 : if (params->success) {
87 11 : g_assert_cmpint(num_entries, ==, entry_count);
88 11 : g_assert_nonnull(name);
89 11 : X509_NAME_free(name);
90 : } else {
91 5 : g_assert_null(name);
92 : }
93 16 : tear_results();
94 16 : }
95 :
96 1 : int main(int argc, char *argv[])
97 : {
98 1 : g_test_init(&argc, &argv, NULL);
99 :
100 : #define TEST_SUCCESS(name, input, output) \
101 : const TestGenericParams test_ ## name = { input, output, TRUE }; \
102 : g_test_add_data_func("/ssl_verify/" #name, &test_ ## name, test_generic)
103 : #define TEST_ERROR(name, input, output) \
104 : const TestGenericParams test_ ## name = { input, output, FALSE }; \
105 : g_test_add_data_func("/ssl_verify/" #name, &test_ ## name, test_generic)
106 :
107 : // normal
108 1 : TEST_SUCCESS(easy1, "C=UK", "C:UK");
109 1 : TEST_SUCCESS(easy2, "a=b,c=d", "a:b:c:d");
110 :
111 : // check spaces before keys are ignored
112 1 : TEST_SUCCESS(space1, " C=UK", "C:UK");
113 1 : TEST_SUCCESS(space2, "C=UK, A=B", "C:UK:A:B");
114 :
115 : // empty key
116 1 : TEST_SUCCESS(empty1, "", "");
117 1 : TEST_SUCCESS(empty2, "a=b,", "a:b");
118 1 : TEST_SUCCESS(empty3, " ", "");
119 1 : TEST_SUCCESS(empty4, "a=b, ", "a:b");
120 :
121 : // empty value
122 1 : TEST_ERROR(empty5, "a=", "");
123 :
124 : // quoting
125 1 : TEST_SUCCESS(quote1, "\\,=a", ",:a");
126 1 : TEST_SUCCESS(quote2, "\\\\=a", "\\:a");
127 1 : TEST_SUCCESS(quote3, "a=\\,b,c=d", "a:,b:c:d");
128 1 : TEST_ERROR(quote4, ",", "");
129 1 : TEST_ERROR(quote5, "a\\w=x", "");
130 :
131 1 : TEST_ERROR(no_value1, "a", "");
132 1 : TEST_ERROR(no_value2, "a,b=c", "");
133 :
134 1 : return g_test_run();
135 : }
|