summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorNeels Hofmeyr <neels@hofmeyr.de>2019-03-05 16:42:50 +0100
committerNeels Hofmeyr <neels@hofmeyr.de>2019-04-12 01:00:16 +0200
commitecef7ec3c36805e70e3da88ea694aeaf526e751d (patch)
tree85edb94effe46e05b4e802f38dbb17914d6c6d31 /tests
parent8531d6695f7363e5fa24141c53253e699d99ca0a (diff)
add osmo_{escape,quote}_str_buf2() for standard args ordering
To be able to append an escaped or quoted string using OSMO_STRBUF_APPEND_NOLEN(), the function signature must have the buf and len as first args, like most other *_buf() functions. Add osmo_escape_str_buf2() and osmo_quote_str_buf2() to match this signature. A recent patch [1] has changed the return value of osmo_escape_str_buf() to char*, removing the const. However, the functions may return const strings, hence re-add the const. The new signatures always return the non-const buffer. To avoid code duplication, implement osmo_quote_str_buf() and osmo_escape_str_buf() by calling the new functions. I decided to allow slight changes to the behavior for current osmo_escape_str() and osmo_escape_str_buf(), because impact on callers is minimal: (1) The new implementation uses OSMO_STRBUF_*, and in consequence osmo_quote_str() no longer prints an ending double quote after truncated strings; Before, a truncated output was, sic: "this string is trunca" and now this becomes, sic: "this string is truncat I decided to not keep the old behavior because it is questionable to begin with. It looks like the string actually ended at the truncation boundary instead of the reason being not enough space in the output buffer. (2) The new osmo_escape_str_buf2() function obviously cannot pass-thru an unchanged char* if no escaping was needed. Sacrifice this tiny optimization feature to avoid code duplication: - it is an unnoticeable optimization, - the caller anyway always passes a string buffer, - the feature caused handling strings and buffers differently depending on their content (i.e. code that usually writes out strings in full length "suddenly" truncates because a non-printable character is contained, etc.) I considered adding a skip_if_unescaped flag to the osmo_quote_str_buf2() function signature, but in the end decided that the API clutter is not worth having for all the above reasons. Adjust tests to accomodate above changes. [1] 4a62eda225ab7f3c9556990c81a6fc5e19b5eec8 Ibf85f79e93244f53b2684ff6f1095c5b41203e05 Change-Id: Id748b906b0083b1f1887f2be7a53cae705a8a9ae
Diffstat (limited to 'tests')
-rw-r--r--tests/utils/utils_test.c6
-rw-r--r--tests/utils/utils_test.ok6
2 files changed, 6 insertions, 6 deletions
diff --git a/tests/utils/utils_test.c b/tests/utils/utils_test.c
index 223f67d2..70d017fe 100644
--- a/tests/utils/utils_test.c
+++ b/tests/utils/utils_test.c
@@ -585,7 +585,7 @@ static void str_quote_test(void)
printf("- never passthru:\n");
res = osmo_quote_str(printable, -1);
- if (res != printable)
+ if (strcmp(res, printable))
printf("NOT passed through. '%s'\n", res);
else
printf("passed through unchanged '%s'\n", res);
@@ -596,14 +596,14 @@ static void str_quote_test(void)
printf("- truncation when too long:\n");
memset(in_buf, 'x', sizeof(in_buf));
in_buf[0] = '\a';
- in_buf[5] = 'E';
+ in_buf[6] = 'E';
memset(out_buf, 0x7f, sizeof(out_buf));
printf("'%s'\n", osmo_quote_str_buf((const char *)in_buf, sizeof(in_buf), out_buf, 10));
OSMO_ASSERT(out_buf[10] == 0x7f);
printf("- always truncation, even when no escaping needed:\n");
memset(in_buf, 'x', sizeof(in_buf));
- in_buf[6] = 'E'; /* dst has 10, less 2 quotes and nul, leaves 7, i.e. in[6] is last */
+ in_buf[7] = 'E'; /* dst has 10, less 1 quote and nul, leaves 8, i.e. in[7] is last */
in_buf[20] = '\0';
memset(out_buf, 0x7f, sizeof(out_buf));
printf("'%s'\n", osmo_quote_str_buf((const char *)in_buf, -1, out_buf, 10));
diff --git a/tests/utils/utils_test.ok b/tests/utils/utils_test.ok
index 587c6f09..c150a8d0 100644
--- a/tests/utils/utils_test.ok
+++ b/tests/utils/utils_test.ok
@@ -258,11 +258,11 @@ NOT passed through. '"printable"'
- zero length:
'""'
- truncation when too long:
-'"\axxxxE"'
+'"\axxxxxE'
- always truncation, even when no escaping needed:
-'"xxxxxxE"'
+'"xxxxxxxE'
- try to feed too little buf for quoting:
-'<buf-too-small>'
+'"'
- NULL string becomes a "NULL" literal:
'NULL'