Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 :
4 : util_str testing
5 :
6 : Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
7 :
8 : This program is free software; you can redistribute it and/or modify
9 : it under the terms of the GNU General Public License as published by
10 : the Free Software Foundation; either version 3 of the License, or
11 : (at your option) any later version.
12 :
13 : This program is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : GNU General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with this program. If not, see <http://www.gnu.org/licenses/>.
20 : */
21 :
22 : #include "includes.h"
23 : #include "torture/torture.h"
24 : #include "torture/local/proto.h"
25 :
26 1 : static bool test_string_sub_simple(struct torture_context *tctx)
27 : {
28 1 : char tmp[100];
29 1 : strlcpy(tmp, "foobar", sizeof(tmp));
30 1 : string_sub(tmp, "foo", "bar", sizeof(tmp));
31 1 : torture_assert_str_equal(tctx, tmp, "barbar", "invalid sub");
32 0 : return true;
33 : }
34 :
35 1 : static bool test_string_sub_multiple(struct torture_context *tctx)
36 : {
37 1 : char tmp[100];
38 1 : strlcpy(tmp, "fooblafoo", sizeof(tmp));
39 1 : string_sub(tmp, "foo", "bar", sizeof(tmp));
40 1 : torture_assert_str_equal(tctx, tmp, "barblabar", "invalid sub");
41 0 : return true;
42 : }
43 :
44 1 : static bool test_string_sub_longer(struct torture_context *tctx)
45 : {
46 1 : char tmp[100];
47 1 : strlcpy(tmp, "foobla", sizeof(tmp));
48 1 : string_sub(tmp, "foo", "blie", sizeof(tmp));
49 1 : torture_assert_str_equal(tctx, tmp, "bliebla", "invalid sub");
50 0 : return true;
51 : }
52 :
53 1 : static bool test_string_sub_shorter(struct torture_context *tctx)
54 : {
55 1 : char tmp[100];
56 1 : strlcpy(tmp, "foobla", sizeof(tmp));
57 1 : string_sub(tmp, "foo", "bl", sizeof(tmp));
58 1 : torture_assert_str_equal(tctx, tmp, "blbla", "invalid sub");
59 0 : return true;
60 : }
61 :
62 1 : static bool test_string_sub_special_char(struct torture_context *tctx)
63 : {
64 1 : char tmp[100];
65 1 : strlcpy(tmp, "foobla", sizeof(tmp));
66 1 : string_sub(tmp, "foo", "%b;l", sizeof(tmp));
67 1 : torture_assert_str_equal(tctx, tmp, "_b_lbla", "invalid sub");
68 0 : return true;
69 : }
70 :
71 1 : static bool test_talloc_string_sub_simple(struct torture_context *tctx)
72 : {
73 1 : char *t;
74 :
75 1 : t = talloc_string_sub(tctx, "foobla", "foo", "bl");
76 :
77 1 : torture_assert_str_equal(tctx, t, "blbla", "invalid sub");
78 :
79 0 : return true;
80 : }
81 :
82 1 : static bool test_talloc_string_sub_multiple(struct torture_context *tctx)
83 : {
84 1 : char *t;
85 :
86 1 : t = talloc_string_sub(tctx, "fooblafoo", "foo", "aapnootmies");
87 :
88 1 : torture_assert_str_equal(tctx, t, "aapnootmiesblaaapnootmies",
89 : "invalid sub");
90 :
91 0 : return true;
92 : }
93 :
94 : /*
95 : * with these next three tests, the failure is that the pattern looks like
96 : * "+++" because the \x.. bytes encode a zero byte in UTF-8. If we are not
97 : * careful with these strings we will see crashes instead of failures.
98 : */
99 :
100 1 : static bool test_talloc_string_sub_tricky_utf8_4(struct torture_context *tctx)
101 : {
102 1 : const char string[] = "++++--\xD8\xBB";
103 1 : const char pattern[] = "+++\xF0\x80\x80\x80++";
104 1 : const char replace[] = "...";
105 :
106 1 : char *t = talloc_string_sub(tctx, string, pattern, replace);
107 1 : torture_assert_str_equal(tctx, t, string,
108 : "should reject 4 byte NUL char");
109 1 : talloc_free(t);
110 1 : return true;
111 : }
112 :
113 1 : static bool test_talloc_string_sub_tricky_utf8_3(struct torture_context *tctx)
114 : {
115 1 : const char string[] = "++++--\xD8\xBB";
116 1 : const char pattern[] = "+++\xE0\x80\x80++";
117 1 : const char replace[] = "...";
118 :
119 1 : char *t = talloc_string_sub(tctx, string, pattern, replace);
120 1 : torture_assert_str_equal(tctx, t, string,
121 : "should reject 3 byte NUL char");
122 1 : talloc_free(t);
123 1 : return true;
124 : }
125 :
126 1 : static bool test_talloc_string_sub_tricky_utf8_2(struct torture_context *tctx)
127 : {
128 1 : const char string[] = "++++--\xD8\xBB";
129 1 : const char pattern[] = "+++\xC0\x80++";
130 1 : const char replace[] = "...";
131 :
132 1 : char *t = talloc_string_sub(tctx, string, pattern, replace);
133 1 : torture_assert_str_equal(tctx, t, string,
134 : "should reject 2 byte NUL char");
135 1 : talloc_free(t);
136 1 : return true;
137 : }
138 :
139 :
140 :
141 :
142 2354 : struct torture_suite *torture_local_util_str(TALLOC_CTX *mem_ctx)
143 : {
144 2354 : struct torture_suite *suite = torture_suite_create(mem_ctx, "str");
145 :
146 2354 : torture_suite_add_simple_test(suite, "string_sub_simple",
147 : test_string_sub_simple);
148 :
149 2354 : torture_suite_add_simple_test(suite, "string_sub_multiple",
150 : test_string_sub_multiple);
151 :
152 2354 : torture_suite_add_simple_test(suite, "string_sub_shorter",
153 : test_string_sub_shorter);
154 :
155 2354 : torture_suite_add_simple_test(suite, "string_sub_longer",
156 : test_string_sub_longer);
157 :
158 2354 : torture_suite_add_simple_test(suite, "string_sub_special_chars",
159 : test_string_sub_special_char);
160 :
161 2354 : torture_suite_add_simple_test(suite, "talloc_string_sub_simple",
162 : test_talloc_string_sub_simple);
163 :
164 2354 : torture_suite_add_simple_test(suite, "string_sub_talloc_multiple",
165 : test_talloc_string_sub_multiple);
166 :
167 2354 : torture_suite_add_simple_test(suite,
168 : "test_talloc_string_sub_tricky_utf8_4",
169 : test_talloc_string_sub_tricky_utf8_4);
170 :
171 2354 : torture_suite_add_simple_test(suite,
172 : "test_talloc_string_sub_tricky_utf8_3",
173 : test_talloc_string_sub_tricky_utf8_3);
174 :
175 2354 : torture_suite_add_simple_test(suite,
176 : "test_talloc_string_sub_tricky_utf8_2",
177 : test_talloc_string_sub_tricky_utf8_2);
178 :
179 2354 : return suite;
180 : }
|