1024: Verify server certificate hostname with OpenSSL
[elinks.git] / src / network / ssl / test / match-hostname-test.c
blobfbdf6fa6f197df4b0c7ad9f3fbd13aff3b2cb2e1
1 /* Test match_hostname_pattern() */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <stdio.h>
8 #include <stdlib.h>
10 #include "elinks.h"
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;
19 int match;
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 },
42 { NULL, NULL, 0 }
45 int
46 main(void)
48 const struct match_hostname_pattern_test_case *test;
49 int count_ok = 0;
50 int count_fail = 0;
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);
58 return EXIT_FAILURE;
61 for (test = match_hostname_pattern_test_cases; test->pattern; test++) {
62 int match;
64 match = match_hostname_pattern(
65 test->hostname,
66 strlen(test->hostname),
67 test->pattern,
68 strlen(test->pattern));
69 if (!match == !test->match) {
70 /* Test OK */
71 count_ok++;
72 } else {
73 fprintf(stderr, "match_hostname_pattern() test failed\n"
74 "\tHostname: %s\n"
75 "\tPattern: %s\n"
76 "\tActual result: %d\n"
77 "\tCorrect result: %d\n",
78 test->hostname,
79 test->pattern,
80 match,
81 test->match);
82 count_fail++;
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(
94 hostname_str.source,
95 strlen(test->hostname),
96 pattern_str.source,
97 strlen(test->pattern));
98 if (!match == !test->match) {
99 /* Test OK */
100 count_ok++;
101 } else {
102 fprintf(stderr, "match_hostname_pattern() test failed\n"
103 "\tVariant: Strings were not null-terminated.\n"
104 "\tHostname: %s\n"
105 "\tPattern: %s\n"
106 "\tActual result: %d\n"
107 "\tCorrect result: %d\n",
108 test->hostname,
109 test->pattern,
110 match,
111 test->match);
112 count_fail++;
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;