2 Unix SMB/CIFS implementation.
6 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
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.
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.
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/>.
23 #include "torture/torture.h"
24 #include "torture/local/proto.h"
26 static bool test_string_sub_simple(struct torture_context
*tctx
)
29 strlcpy(tmp
, "foobar", sizeof(tmp
));
30 string_sub(tmp
, "foo", "bar", sizeof(tmp
));
31 torture_assert_str_equal(tctx
, tmp
, "barbar", "invalid sub");
35 static bool test_string_sub_multiple(struct torture_context
*tctx
)
38 strlcpy(tmp
, "fooblafoo", sizeof(tmp
));
39 string_sub(tmp
, "foo", "bar", sizeof(tmp
));
40 torture_assert_str_equal(tctx
, tmp
, "barblabar", "invalid sub");
44 static bool test_string_sub_longer(struct torture_context
*tctx
)
47 strlcpy(tmp
, "foobla", sizeof(tmp
));
48 string_sub(tmp
, "foo", "blie", sizeof(tmp
));
49 torture_assert_str_equal(tctx
, tmp
, "bliebla", "invalid sub");
53 static bool test_string_sub_shorter(struct torture_context
*tctx
)
56 strlcpy(tmp
, "foobla", sizeof(tmp
));
57 string_sub(tmp
, "foo", "bl", sizeof(tmp
));
58 torture_assert_str_equal(tctx
, tmp
, "blbla", "invalid sub");
62 static bool test_string_sub_special_char(struct torture_context
*tctx
)
65 strlcpy(tmp
, "foobla", sizeof(tmp
));
66 string_sub(tmp
, "foo", "%b;l", sizeof(tmp
));
67 torture_assert_str_equal(tctx
, tmp
, "_b_lbla", "invalid sub");
71 static bool test_talloc_string_sub_simple(struct torture_context
*tctx
)
75 t
= talloc_string_sub(tctx
, "foobla", "foo", "bl");
77 torture_assert_str_equal(tctx
, t
, "blbla", "invalid sub");
82 static bool test_talloc_string_sub_multiple(struct torture_context
*tctx
)
86 t
= talloc_string_sub(tctx
, "fooblafoo", "foo", "aapnootmies");
88 torture_assert_str_equal(tctx
, t
, "aapnootmiesblaaapnootmies",
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.
100 static bool test_talloc_string_sub_tricky_utf8_4(struct torture_context
*tctx
)
102 const char string
[] = "++++--\xD8\xBB";
103 const char pattern
[] = "+++\xF0\x80\x80\x80++";
104 const char replace
[] = "...";
106 char *t
= talloc_string_sub(tctx
, string
, pattern
, replace
);
107 torture_assert_str_equal(tctx
, t
, string
,
108 "should reject 4 byte NUL char");
113 static bool test_talloc_string_sub_tricky_utf8_3(struct torture_context
*tctx
)
115 const char string
[] = "++++--\xD8\xBB";
116 const char pattern
[] = "+++\xE0\x80\x80++";
117 const char replace
[] = "...";
119 char *t
= talloc_string_sub(tctx
, string
, pattern
, replace
);
120 torture_assert_str_equal(tctx
, t
, string
,
121 "should reject 3 byte NUL char");
126 static bool test_talloc_string_sub_tricky_utf8_2(struct torture_context
*tctx
)
128 const char string
[] = "++++--\xD8\xBB";
129 const char pattern
[] = "+++\xC0\x80++";
130 const char replace
[] = "...";
132 char *t
= talloc_string_sub(tctx
, string
, pattern
, replace
);
133 torture_assert_str_equal(tctx
, t
, string
,
134 "should reject 2 byte NUL char");
142 struct torture_suite
*torture_local_util_str(TALLOC_CTX
*mem_ctx
)
144 struct torture_suite
*suite
= torture_suite_create(mem_ctx
, "str");
146 torture_suite_add_simple_test(suite
, "string_sub_simple",
147 test_string_sub_simple
);
149 torture_suite_add_simple_test(suite
, "string_sub_multiple",
150 test_string_sub_multiple
);
152 torture_suite_add_simple_test(suite
, "string_sub_shorter",
153 test_string_sub_shorter
);
155 torture_suite_add_simple_test(suite
, "string_sub_longer",
156 test_string_sub_longer
);
158 torture_suite_add_simple_test(suite
, "string_sub_special_chars",
159 test_string_sub_special_char
);
161 torture_suite_add_simple_test(suite
, "talloc_string_sub_simple",
162 test_talloc_string_sub_simple
);
164 torture_suite_add_simple_test(suite
, "string_sub_talloc_multiple",
165 test_talloc_string_sub_multiple
);
167 torture_suite_add_simple_test(suite
,
168 "test_talloc_string_sub_tricky_utf8_4",
169 test_talloc_string_sub_tricky_utf8_4
);
171 torture_suite_add_simple_test(suite
,
172 "test_talloc_string_sub_tricky_utf8_3",
173 test_talloc_string_sub_tricky_utf8_3
);
175 torture_suite_add_simple_test(suite
,
176 "test_talloc_string_sub_tricky_utf8_2",
177 test_talloc_string_sub_tricky_utf8_2
);