1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
/*
* Copyright (c) 2015-2016 Balabit
* Copyright (c) 2015-2016 Balázs Scheidler
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "str-repr/encode.h"
#include <criterion/criterion.h>
#include <criterion/parameterized.h>
#include "stopwatch.h"
GString *value;
typedef struct _EncodeTestStr
{
const gchar *actual;
const gchar *expected;
} EncodeTestStr;
typedef struct _EncodeTestForbidden
{
const gchar *actual;
const gchar *forbidden;
const gchar *expected;
} EncodeTestForbidden;
static void
assert_encode_equals(const gchar *input, const gchar *expected)
{
GString *str = g_string_new("");
str_repr_encode(str, input, -1, NULL);
cr_assert_str_eq(str->str, expected, "Encoded value does not match expected; actual: %s, expected: %s", str->str,
expected);
str_repr_encode(str, input, strlen(input), NULL);
cr_assert_str_eq(str->str, expected, "Encoded value does not match expected; actual: %s, expected: %s", str->str,
expected);
gchar *space_ended_input = g_strdup_printf("%s ", input);
str_repr_encode(str, space_ended_input, strlen(input), ",");
cr_assert_str_eq(str->str, expected, "Encoded value does not match expected; actual: %s, expected: %s", str->str,
expected);
g_free(space_ended_input);
g_string_free(str, TRUE);
}
static void
assert_encode_with_forbidden_equals(const gchar *input, const gchar *forbidden_chars, const gchar *expected)
{
GString *str = g_string_new("");
str_repr_encode(str, input, -1, forbidden_chars);
cr_assert_str_eq(str->str, expected, "Encoded value does not match expected; actual: %s, expected: %s", str->str,
expected);
g_string_free(str, TRUE);
}
ParameterizedTestParameters(encode, test_strings)
{
static EncodeTestStr test_cases[] =
{
{"", "\"\""},
{"a", "a"},
{"alma", "alma"},
{"al\nma", "\"al\\nma\""},
{"foo bar", "\"foo bar\""},
/* embedded quote */
{"\"value1", "'\"value1'"},
{"'value1", "\"'value1\""},
/* control sequences */
{"\b \f \n \r \t \\", "\"\\b \\f \\n \\r \\t \\\\\""}
};
return cr_make_param_array(EncodeTestStr, test_cases, sizeof(test_cases) / sizeof(test_cases[0]));
}
ParameterizedTest(EncodeTestStr *test_cases, encode, test_strings)
{
assert_encode_equals(test_cases->actual, test_cases->expected);
}
ParameterizedTestParameters(encode, test_encode_strings_that_need_quotation)
{
static EncodeTestForbidden test_cases[] =
{
{"foo,", ",", "\"foo,\""},
{"\"'foo,", ",", "\"\\\"'foo,\""}
};
return cr_make_param_array(EncodeTestForbidden, test_cases, sizeof(test_cases) / sizeof(test_cases[0]));
}
ParameterizedTest(EncodeTestForbidden *test_cases, encode, test_encode_strings_that_need_quotation)
{
assert_encode_with_forbidden_equals(test_cases->actual, test_cases->forbidden, test_cases->expected);
}
#define ITERATION_NUMBER 100000
static void
_perftest(const gchar *value_to_encode)
{
gint iteration_index = 0;
GString *result = g_string_sized_new(64);
gsize value_len = strlen(value_to_encode);
start_stopwatch();
for (iteration_index = 0; iteration_index < ITERATION_NUMBER; iteration_index++)
{
str_repr_encode(result, value_to_encode, value_len, ",");
}
stop_stopwatch_and_display_result(iteration_index, "%.64s...", value_to_encode);
g_string_free(result, TRUE);
}
Test(encode, test_performance)
{
_perftest("This is a long value with spaces and control characters\n"
" ");
_perftest("This is 'a long' value with spaces and control characters\n");
_perftest("This is \"a long\" value with spaces and control characters\n");
}
|