Line data Source code
1 : /*
2 :
3 : util_str_escape testing
4 :
5 : Copyright (C) Andrew Bartlett <abartlet@samba.org> 2017
6 :
7 : This program is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3 of the License, or
10 : (at your option) any later version.
11 :
12 : This program is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with this program. If not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : #include "includes.h"
22 : #include "torture/torture.h"
23 : #include "torture/local/proto.h"
24 : #include "lib/util/util_str_escape.h"
25 :
26 0 : static bool test_log_escape_empty_string(struct torture_context *tctx)
27 : {
28 0 : char *result = log_escape( tctx, "");
29 0 : torture_assert_str_equal(tctx, result, "", "Empty string handling");
30 0 : return true;
31 : }
32 :
33 0 : static bool test_log_escape_null_string(struct torture_context *tctx)
34 : {
35 0 : char *result = log_escape( tctx, NULL);
36 0 : torture_assert(tctx, (result == NULL), "Empty string handling");
37 0 : return true;
38 : }
39 :
40 0 : static bool test_log_escape_plain_string(struct torture_context *tctx)
41 : {
42 0 : const char *input = "a plain string with no escapable characters";
43 0 : const char *expected = "a plain string with no escapable characters";
44 :
45 0 : char *result = log_escape( tctx, input);
46 0 : torture_assert_str_equal(tctx, result, expected,
47 : "Plain string handling");
48 0 : return true;
49 : }
50 :
51 0 : static bool test_log_escape_string(struct torture_context *tctx)
52 : {
53 0 : const char *input = "\a\b\f\n\r\t\v\\\x01";
54 0 : const char *expected = "\\a\\b\\f\\n\\r\\t\\v\\\\\\x01";
55 :
56 0 : char *result = log_escape( tctx, input);
57 0 : torture_assert_str_equal(tctx, result, expected,
58 : "Escapable characters in string");
59 0 : return true;
60 : }
61 :
62 0 : static bool test_log_escape_hex_string(struct torture_context *tctx)
63 : {
64 0 : const char *input = "\x01\x1F ";
65 0 : const char *expected = "\\x01\\x1F ";
66 :
67 0 : char *result = log_escape( tctx, input);
68 0 : torture_assert_str_equal(tctx, result, expected,
69 : "hex escaping");
70 0 : return true;
71 : }
72 964 : struct torture_suite *torture_local_util_str_escape(TALLOC_CTX *mem_ctx)
73 : {
74 964 : struct torture_suite *suite = torture_suite_create(mem_ctx,
75 : "util_str_escape");
76 :
77 964 : torture_suite_add_simple_test(suite, "log_escape_empty_string",
78 : test_log_escape_empty_string);
79 964 : torture_suite_add_simple_test(suite, "log_escape_null_string",
80 : test_log_escape_null_string);
81 964 : torture_suite_add_simple_test(suite, "log_escape_plain_string",
82 : test_log_escape_plain_string);
83 964 : torture_suite_add_simple_test(suite, "log_escape_string",
84 : test_log_escape_string);
85 964 : torture_suite_add_simple_test(suite, "log_escape_hex_string",
86 : test_log_escape_hex_string);
87 :
88 :
89 964 : return suite;
90 : }
|