Debian Codegraph @

src/microhttpd/test_str_token.c

raw
1 /*
2 This file is part of libmicrohttpd
3 Copyright (C) 2017 Karlson2k (Evgeny Grin)
4
5 This test tool is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2, or
8 (at your option) any later version.
9
10 This test tool is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /**
21 * @file microhttpd/test_str_token.c
22 * @brief Unit tests for some mhd_str functions
23 * @author Karlson2k (Evgeny Grin)
24 */
25
26 #include "mhd_options.h"
27 #include <stdio.h>
28 #include "mhd_str.h"
29
30
31 static int
32 expect_found_n (const char *str, const char *token, size_t token_len)
33 {
34 if (! MHD_str_has_token_caseless_ (str, token, token_len))
35 {
36 fprintf (stderr,
37 "MHD_str_has_token_caseless_() FAILED:\n\tMHD_str_has_token_caseless_(%s, %s, %lu) return false\n",
38 str, token, (unsigned long) token_len);
39 return 1;
40 }
41 return 0;
42 }
43
44
45 #define expect_found(s,t) expect_found_n ((s),(t),MHD_STATICSTR_LEN_ (t))
46
47 static int
48 expect_not_found_n (const char *str, const char *token, size_t token_len)
49 {
50 if (MHD_str_has_token_caseless_ (str, token, token_len))
51 {
52 fprintf (stderr,
53 "MHD_str_has_token_caseless_() FAILED:\n\tMHD_str_has_token_caseless_(%s, %s, %lu) return true\n",
54 str, token, (unsigned long) token_len);
55 return 1;
56 }
57 return 0;
58 }
59
60
61 #define expect_not_found(s,t) expect_not_found_n ((s),(t),MHD_STATICSTR_LEN_ ( \
62 t))
63
64 static int
65 check_match (void)
66 {
67 int errcount = 0;
68 errcount += expect_found ("string", "string");
69 errcount += expect_found ("String", "string");
70 errcount += expect_found ("string", "String");
71 errcount += expect_found ("strinG", "String");
72 errcount += expect_found ("\t strinG", "String");
73 errcount += expect_found ("strinG\t ", "String");
74 errcount += expect_found (" \t tOkEn ", "toKEN");
75 errcount += expect_found ("not token\t, tOkEn ", "toKEN");
76 errcount += expect_found ("not token,\t tOkEn, more token", "toKEN");
77 errcount += expect_found ("not token,\t tOkEn\t, more token", "toKEN");
78 errcount += expect_found (",,,,,,test,,,,", "TESt");
79 errcount += expect_found (",,,,,\t,test,,,,", "TESt");
80 errcount += expect_found (",,,,,,test, ,,,", "TESt");
81 errcount += expect_found (",,,,,, test,,,,", "TESt");
82 errcount += expect_found (",,,,,, test not,test,,", "TESt");
83 errcount += expect_found (",,,,,, test not,,test,,", "TESt");
84 errcount += expect_found (",,,,,, test not ,test,,", "TESt");
85 errcount += expect_found (",,,,,, test", "TESt");
86 errcount += expect_found (",,,,,, test ", "TESt");
87 errcount += expect_found ("no test,,,,,, test ", "TESt");
88 return errcount;
89 }
90
91
92 static int
93 check_not_match (void)
94 {
95 int errcount = 0;
96 errcount += expect_not_found ("strin", "string");
97 errcount += expect_not_found ("Stringer", "string");
98 errcount += expect_not_found ("sstring", "String");
99 errcount += expect_not_found ("string", "Strin");
100 errcount += expect_not_found ("\t( strinG", "String");
101 errcount += expect_not_found (")strinG\t ", "String");
102 errcount += expect_not_found (" \t tOkEn t ", "toKEN");
103 errcount += expect_not_found ("not token\t, tOkEner ", "toKEN");
104 errcount += expect_not_found ("not token,\t tOkEns, more token", "toKEN");
105 errcount += expect_not_found ("not token,\t tOkEns\t, more token", "toKEN");
106 errcount += expect_not_found (",,,,,,testing,,,,", "TESt");
107 errcount += expect_not_found (",,,,,\t,test,,,,", "TESting");
108 errcount += expect_not_found ("tests,,,,,,quest, ,,,", "TESt");
109 errcount += expect_not_found (",,,,,, testы,,,,", "TESt");
110 errcount += expect_not_found (",,,,,, test not,хtest,,", "TESt");
111 errcount += expect_not_found ("testing,,,,,, test not,,test2,,", "TESt");
112 errcount += expect_not_found (",testi,,,,, test not ,test,,", "TESting");
113 errcount += expect_not_found (",,,,,,2 test", "TESt");
114 errcount += expect_not_found (",,,,,,test test ", "test");
115 errcount += expect_not_found ("no test,,,,,, test test", "test");
116 return errcount;
117 }
118
119
120 int
121 main (int argc, char *argv[])
122 {
123 int errcount = 0;
124 (void) argc; (void) argv; /* Unused. Silent compiler warning. */
125 errcount += check_match ();
126 errcount += check_not_match ();
127 return errcount == 0 ? 0 : 1;
128 }