1 /* Test match_hostname_pattern() */
12 #include "network/ssl/match-hostname.h"
13 #include "util/string.h"
15 struct match_hostname_pattern_test_case
17 const unsigned char *pattern
;
18 const unsigned char *hostname
;
22 static const struct match_hostname_pattern_test_case match_hostname_pattern_test_cases
[] = {
23 { "*r*.example.org", "random.example.org", 1 },
24 { "*r*.example.org", "history.example.org", 1 },
25 { "*r*.example.org", "frozen.fruit.example.org", 0 },
26 { "*r*.example.org", "steamed.fruit.example.org", 0 },
28 { "ABC.def.Ghi", "abc.DEF.gHI", 1 },
30 { "*", "localhost", 1 },
31 { "*", "example.org", 0 },
32 { "*.*", "example.org", 1 },
33 { "*.*.*", "www.example.org", 1 },
34 { "*.*.*", "example.org", 0 },
36 { "assign", "assignee", 0 },
37 { "*peg", "arpeggiator", 0 },
38 { "*peg*", "arpeggiator", 1 },
39 { "*r*gi*", "arpeggiator", 1 },
40 { "*r*git*", "arpeggiator", 0 },
48 const struct match_hostname_pattern_test_case
*test
;
51 struct string hostname_str
= NULL_STRING
;
52 struct string pattern_str
= NULL_STRING
;
54 if (!init_string(&hostname_str
) || !init_string(&pattern_str
)) {
55 fputs("Out of memory.\n", stderr
);
56 done_string(&hostname_str
);
57 done_string(&pattern_str
);
61 for (test
= match_hostname_pattern_test_cases
; test
->pattern
; test
++) {
64 match
= match_hostname_pattern(
66 strlen(test
->hostname
),
68 strlen(test
->pattern
));
69 if (!match
== !test
->match
) {
73 fprintf(stderr
, "match_hostname_pattern() test failed\n"
76 "\tActual result: %d\n"
77 "\tCorrect result: %d\n",
85 /* Try with strings that are not null-terminated. */
86 hostname_str
.length
= 0;
87 add_to_string(&hostname_str
, test
->hostname
);
88 add_to_string(&hostname_str
, "ZZZ");
89 pattern_str
.length
= 0;
90 add_to_string(&pattern_str
, test
->pattern
);
91 add_to_string(&hostname_str
, "______");
93 match
= match_hostname_pattern(
95 strlen(test
->hostname
),
97 strlen(test
->pattern
));
98 if (!match
== !test
->match
) {
102 fprintf(stderr
, "match_hostname_pattern() test failed\n"
103 "\tVariant: Strings were not null-terminated.\n"
106 "\tActual result: %d\n"
107 "\tCorrect result: %d\n",
116 printf("Summary of match_hostname_pattern() tests: %d OK, %d failed.\n",
117 count_ok
, count_fail
);
119 done_string(&hostname_str
);
120 done_string(&pattern_str
);
121 return count_fail
? EXIT_FAILURE
: EXIT_SUCCESS
;