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 1 : static bool test_log_escape_empty_string(struct torture_context *tctx) 27 : { 28 1 : char *result = log_escape( tctx, ""); 29 1 : torture_assert_str_equal(tctx, result, "", "Empty string handling"); 30 0 : return true; 31 : } 32 : 33 1 : static bool test_log_escape_null_string(struct torture_context *tctx) 34 : { 35 1 : char *result = log_escape( tctx, NULL); 36 1 : torture_assert(tctx, (result == NULL), "Empty string handling"); 37 0 : return true; 38 : } 39 : 40 1 : static bool test_log_escape_plain_string(struct torture_context *tctx) 41 : { 42 1 : const char *input = "a plain string with no escapable characters"; 43 1 : const char *expected = "a plain string with no escapable characters"; 44 : 45 1 : char *result = log_escape( tctx, input); 46 1 : torture_assert_str_equal(tctx, result, expected, 47 : "Plain string handling"); 48 0 : return true; 49 : } 50 : 51 1 : static bool test_log_escape_string(struct torture_context *tctx) 52 : { 53 1 : const char *input = "\a\b\f\n\r\t\v\\\x01"; 54 1 : const char *expected = "\\a\\b\\f\\n\\r\\t\\v\\\\\\x01"; 55 : 56 1 : char *result = log_escape( tctx, input); 57 1 : torture_assert_str_equal(tctx, result, expected, 58 : "Escapable characters in string"); 59 0 : return true; 60 : } 61 : 62 1 : static bool test_log_escape_hex_string(struct torture_context *tctx) 63 : { 64 1 : const char *input = "\x01\x1F "; 65 1 : const char *expected = "\\x01\\x1F "; 66 : 67 1 : char *result = log_escape( tctx, input); 68 1 : torture_assert_str_equal(tctx, result, expected, 69 : "hex escaping"); 70 0 : return true; 71 : } 72 2354 : struct torture_suite *torture_local_util_str_escape(TALLOC_CTX *mem_ctx) 73 : { 74 2354 : struct torture_suite *suite = torture_suite_create(mem_ctx, 75 : "util_str_escape"); 76 : 77 2354 : torture_suite_add_simple_test(suite, "log_escape_empty_string", 78 : test_log_escape_empty_string); 79 2354 : torture_suite_add_simple_test(suite, "log_escape_null_string", 80 : test_log_escape_null_string); 81 2354 : torture_suite_add_simple_test(suite, "log_escape_plain_string", 82 : test_log_escape_plain_string); 83 2354 : torture_suite_add_simple_test(suite, "log_escape_string", 84 : test_log_escape_string); 85 2354 : torture_suite_add_simple_test(suite, "log_escape_hex_string", 86 : test_log_escape_hex_string); 87 : 88 : 89 2354 : return suite; 90 : }