chunk: Print chunks without separator if + modifier is used
authorTobias Brunner <tobias@strongswan.org>
Tue, 6 Aug 2013 15:27:35 +0000 (17:27 +0200)
committerAndreas Steffen <andreas.steffen@strongswan.org>
Sat, 24 Aug 2013 14:22:51 +0000 (16:22 +0200)
src/libstrongswan/tests/test_chunk.c
src/libstrongswan/utils/chunk.c
src/libstrongswan/utils/chunk.h

index 7f07d05..8e29971 100644 (file)
@@ -756,11 +756,12 @@ END_TEST
 static struct {
        chunk_t in;
        char *out;
+       char *out_plus;
 } printf_hook_data[] = {
-       {chunk_from_chars(), ""},
-       {chunk_from_chars(0x00), "00"},
-       {chunk_from_chars(0x00, 0x01), "00:01"},
-       {chunk_from_chars(0x00, 0x01, 0x02), "00:01:02"},
+       {chunk_from_chars(), "", ""},
+       {chunk_from_chars(0x00), "00", "00"},
+       {chunk_from_chars(0x00, 0x01), "00:01", "0001"},
+       {chunk_from_chars(0x00, 0x01, 0x02), "00:01:02", "000102"},
 };
 
 START_TEST(test_printf_hook_hash)
@@ -774,6 +775,17 @@ START_TEST(test_printf_hook_hash)
 }
 END_TEST
 
+START_TEST(test_printf_hook_plus)
+{
+       char buf[16];
+       int len;
+
+       len = snprintf(buf, sizeof(buf), "%+B", &printf_hook_data[_i].in);
+       ck_assert(len >= 0 && len < sizeof(buf));
+       ck_assert_str_eq(buf, printf_hook_data[_i].out_plus);
+}
+END_TEST
+
 START_TEST(test_printf_hook)
 {
        char buf[128], mem[128];
@@ -856,6 +868,7 @@ Suite *chunk_suite_create()
 
        tc = tcase_create("printf_hook");
        tcase_add_loop_test(tc, test_printf_hook_hash, 0, countof(printf_hook_data));
+       tcase_add_loop_test(tc, test_printf_hook_plus, 0, countof(printf_hook_data));
        tcase_add_loop_test(tc, test_printf_hook, 0, countof(printf_hook_data));
        suite_add_tcase(s, tc);
 
index 04f3eea..f5b3ac6 100644 (file)
@@ -797,7 +797,7 @@ int chunk_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
        chunk_t copy = *chunk;
        int written = 0;
 
-       if (!spec->hash)
+       if (!spec->hash && !spec->plus)
        {
                u_int chunk_len = chunk->len;
                const void *new_args[] = {&chunk->ptr, &chunk_len};
@@ -810,7 +810,7 @@ int chunk_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
                {
                        first = FALSE;
                }
-               else
+               else if (!spec->plus)
                {
                        written += print_in_hook(data, ":");
                }
index 34ba773..36f6f26 100644 (file)
@@ -368,6 +368,7 @@ u_int64_t chunk_mac(chunk_t chunk, u_char *key);
  * Arguments are:
  *     chunk_t *chunk
  * Use #-modifier to print a compact version
+ * Use +-modifier to print a compact version without separator
  */
 int chunk_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
                                          const void *const *args);