aboutsummaryrefslogtreecommitdiff
path: root/tests/unittests/torture_misc.c
diff options
context:
space:
mode:
authorAnderson Toshiyuki Sasaki <ansasaki@redhat.com>2019-11-04 15:35:15 +0100
committerAndreas Schneider <asn@cryptomilk.org>2019-12-09 16:08:03 +0100
commitc9ce8fa40b45af7fca2c75fe5f0614d881e35a84 (patch)
tree0d44bde9d4cf545f5c4ff57d2517357351a6b619 /tests/unittests/torture_misc.c
parent6c79ed980163d4692281c73adf569c0112885f3e (diff)
downloadlibssh-c9ce8fa40b45af7fca2c75fe5f0614d881e35a84.tar.gz
libssh-c9ce8fa40b45af7fca2c75fe5f0614d881e35a84.tar.xz
libssh-c9ce8fa40b45af7fca2c75fe5f0614d881e35a84.zip
misc: Add a function to encode newlines
Given a string, the added function encodes existing newline characters ('\n') as the string "\\n" and puts into a given output buffer. The output buffer must have at least 2 times the length of the input string plus 1 for the terminating '\0'. In the worst case, each character can be replaced by 2 characters. Fixes T189 Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org> Reviewed-by: Jakub Jelen <jjelen@redhat.com>
Diffstat (limited to 'tests/unittests/torture_misc.c')
-rw-r--r--tests/unittests/torture_misc.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/tests/unittests/torture_misc.c b/tests/unittests/torture_misc.c
index eff93532..ef06d65f 100644
--- a/tests/unittests/torture_misc.c
+++ b/tests/unittests/torture_misc.c
@@ -637,6 +637,24 @@ static void torture_ssh_quote_file_name(UNUSED_PARAM(void **state))
assert_int_equal(rc, SSH_ERROR);
}
+static void torture_ssh_newline_vis(UNUSED_PARAM(void **state))
+{
+ int rc;
+ char buffer[1024];
+
+ rc = ssh_newline_vis("\n", buffer, 1024);
+ assert_int_equal(rc, 2);
+ assert_string_equal(buffer, "\\n");
+
+ rc = ssh_newline_vis("\n\n\n\n", buffer, 1024);
+ assert_int_equal(rc, 8);
+ assert_string_equal(buffer, "\\n\\n\\n\\n");
+
+ rc = ssh_newline_vis("a\nb\n", buffer, 1024);
+ assert_int_equal(rc, 6);
+ assert_string_equal(buffer, "a\\nb\\n");
+}
+
int torture_run_tests(void) {
int rc;
struct CMUnitTest tests[] = {
@@ -656,6 +674,7 @@ int torture_run_tests(void) {
cmocka_unit_test(torture_timeout_update),
cmocka_unit_test(torture_ssh_analyze_banner),
cmocka_unit_test(torture_ssh_dir_writeable),
+ cmocka_unit_test(torture_ssh_newline_vis),
cmocka_unit_test(torture_ssh_mkdirs),
cmocka_unit_test(torture_ssh_quote_file_name),
};