ctest_buffer.c 11.2 KB
Newer Older
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
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
 * Copyright 2018,      Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>

#include "ctest_lib.h"

#include "../src/buffer.h"
#include "../src/refobject.h"

static void test_create_ref_unref(void)
{
    buffer_t *a;

    a = buffer_new(-1, NULL, NULL, REFOBJECT_NULL);
    ctest_test("buffer created", a != NULL);
    ctest_test("un-referenced", refobject_unref(a) == 0);

    a = buffer_new_simple();
    ctest_test("buffer created", a != NULL);
    ctest_test("un-referenced", refobject_unref(a) == 0);

}

static void test_name(void)
{
    buffer_t *a;
    const char *name = "test object name";
    const char *ret;

    a = buffer_new(-1, NULL, name, REFOBJECT_NULL);
    ctest_test("buffer created", a != NULL);

    ret = refobject_get_name(a);
    ctest_test("get name", ret != NULL);
    ctest_test("name match", strcmp(name, ret) == 0);

    ctest_test("un-referenced", refobject_unref(a) == 0);
}

static void test_userdata(void)
{
    buffer_t *a;
    int tmp = 0;
    void *userdata = &tmp;
    void *ret;

    a = buffer_new(-1, NULL, NULL, REFOBJECT_NULL);
    ctest_test("buffer created", a != NULL);
    ret = refobject_get_userdata(a);
    ctest_test("get userdata", ret == NULL);
    ctest_test("set userdata", refobject_set_userdata(a, userdata) == 0);
    ret = refobject_get_userdata(a);
    ctest_test("get userdata", ret == userdata);
    ctest_test("clearing userdata", refobject_set_userdata(a, NULL) == 0);
    ret = refobject_get_userdata(a);
    ctest_test("get userdata", ret == NULL);

    ctest_test("un-referenced", refobject_unref(a) == 0);

    a = buffer_new(-1, userdata, NULL, REFOBJECT_NULL);
    ctest_test("buffer created", a != NULL);
    ctest_test("refobject created", !REFOBJECT_IS_NULL(a));
    ret = refobject_get_userdata(a);
    ctest_test("get userdata", ret == userdata);
    ctest_test("clearing userdata", refobject_set_userdata(a, NULL) == 0);
    ret = refobject_get_userdata(a);
    ctest_test("get userdata", ret == NULL);
    ctest_test("un-referenced", refobject_unref(a) == 0);
}

static void test_associated(void)
{
83
    refobject_base_t *a;
84 85
    buffer_t *b;

86
    a = refobject_new(refobject_base_t);
87 88 89 90 91 92 93 94 95 96
    ctest_test("refobject created", !REFOBJECT_IS_NULL(a));


    b = buffer_new(-1, NULL, NULL, a);
    ctest_test("buffer created with associated", !REFOBJECT_IS_NULL(b));

    ctest_test("un-referenced (1 of 2)", refobject_unref(b) == 0);
    ctest_test("un-referenced (2 of 2)", refobject_unref(a) == 0);
}

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
static void test_empty(void)
{
    buffer_t *a;
    const void *data = &data;
    size_t length = 5;
    const char *string;
    int ret;

    a = buffer_new_simple();
    ctest_test("buffer created", a != NULL);

    ret = buffer_get_data(a, &data, &length);
    ctest_test("got data and length from buffer", ret == 0);
    if (ret == 0) {
        ctest_test("data is updated", data != &data);
        ctest_test("length is zero", length == 0);
    }

    data = &data;
    ret = buffer_get_data(a, &data, NULL);
    ctest_test("got data from buffer", ret == 0);
    if (ret == 0) {
        ctest_test("data is updated", data != &data);
    }

    length = 5;
    ret = buffer_get_data(a, NULL, &length);
    ctest_test("got length from buffer", ret == 0);
    if (ret == 0) {
        ctest_test("length is zero", length == 0);
    }

    ret = buffer_get_string(a, &string);
    ctest_test("got string from buffer", ret == 0);
    if (ret == 0) {
        ctest_test("string is non-NULL", string != NULL);
        if (string != NULL) {
            ctest_test("string is empty", *string == 0);
        }
    }

    ctest_test("un-referenced", refobject_unref(a) == 0);
}

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
static void test_string(void)
{
    buffer_t *a;
    const char *hw = "Hello World!";
    const char *count = "0 1 2 3 4";
    const char *combined = "Hello World!" "0 1 2 3 4";
    const char *string = NULL;
    int ret;

    a = buffer_new_simple();
    ctest_test("buffer created", a != NULL);
    ctest_test("pushed string", buffer_push_string(a, hw) == 0);
    ret = buffer_get_string(a, &string);
    ctest_test("got strong", ret == 0);
    if (ret == 0) {
        ctest_test("string is non-NULL", string != NULL);
        if (string != NULL) {
            ctest_test("string matches input", strcmp(string, hw) == 0);
        }
    }

    ctest_test("pushed string", buffer_push_string(a, count) == 0);
    string = NULL;
    ret = buffer_get_string(a, &string);
    ctest_test("got strong", ret == 0);
    if (ret == 0) {
        ctest_test("string is non-NULL", string != NULL);
        if (string != NULL) {
            ctest_test("string matches combined input", strcmp(string, combined) == 0);
        }
    }

    ctest_test("un-referenced", refobject_unref(a) == 0);
}

Philipp Schafft's avatar
Philipp Schafft committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
static void test_binary(void)
{
    buffer_t *a;
    char pattern_a[8] = {0x01, 0x10, 0x80, 0xFF,  0x00, 0x55, 0xEE, 0xAA};
    char pattern_b[9] = {0x02, 0x03, 0xF0, 0x80,  0x0F, 0x04, 0x1A, 0x7F, 0x33};
    int ret;
    size_t length;
    const void *data;

    a = buffer_new_simple();
    ctest_test("buffer created", a != NULL);

    ctest_test("pushed data pattern a", buffer_push_data(a, pattern_a, sizeof(pattern_a)) == 0);
    length = sizeof(pattern_a) + 42;
    data = &data;
    ret = buffer_get_data(a, &data, &length);
    ctest_test("got data", ret == 0);
    if (ret == 0) {
        ctest_test("correct length was returned", length == sizeof(pattern_a));
        ctest_test("data is non-NULL", data != NULL);
        ctest_test("data has been set", data != &data);
        if (length == sizeof(pattern_a) && data != NULL && data != &data) {
            ctest_test("data matches pattern", memcmp(data, pattern_a, sizeof(pattern_a)) == 0);
        }
    }

    ctest_test("pushed data pattern b", buffer_push_data(a, pattern_b, sizeof(pattern_b)) == 0);
    length = sizeof(pattern_a) + sizeof(pattern_b) + 42;
    data = &data;
    ret = buffer_get_data(a, &data, &length);
    ctest_test("got data", ret == 0);
    if (ret == 0) {
        ctest_test("correct length was returned", length == (sizeof(pattern_a) + sizeof(pattern_b)));
        ctest_test("data is non-NULL", data != NULL);
        ctest_test("data has been set", data != &data);
        if (length == (sizeof(pattern_a) + sizeof(pattern_b)) && data != NULL && data != &data) {
            ctest_test("data matches combined pattern", memcmp(data, pattern_a, sizeof(pattern_a)) == 0 && memcmp(data + sizeof(pattern_a), pattern_b, sizeof(pattern_b)) == 0);
        }
    }


    ctest_test("un-referenced", refobject_unref(a) == 0);
}

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
static void test__compare_to_string(buffer_t *a, const char *testname, const char *pattern)
{
    const char *string = NULL;
    int ret;

    ret = buffer_get_string(a, &string);
    ctest_test("got strong", ret == 0);
    if (ret == 0) {
        ctest_test("string is non-NULL", string != NULL);
        if (string != NULL) {
            ctest_test(testname, strcmp(string, pattern) == 0);
            ctest_diagnostic_printf("string=\"%s\", pattern=\"%s\"", string, pattern);
        }
    }
}

static void test_shift(void)
{
    buffer_t *a;
    const char *pattern = "AABBBCC";

    a = buffer_new_simple();
    ctest_test("buffer created", a != NULL);

    ctest_test("pushed string", buffer_push_string(a, pattern) == 0);
    test__compare_to_string(a, "string matches input", pattern);
    ctest_test("shifted data by 0 bytes", buffer_shift(a, 0) == 0);
    test__compare_to_string(a, "string matches input (no shift happened)", pattern);
    ctest_test("shifted data by 2 bytes", buffer_shift(a, 2) == 0);
    test__compare_to_string(a, "string matches shifted input", pattern + 2);
    ctest_test("shifted data by 3 bytes", buffer_shift(a, 3) == 0);
    test__compare_to_string(a, "string matches shifted input", pattern + 2 + 3);
    ctest_test("shifted data by 3 bytes", buffer_shift(a, 2) == 0);
    test__compare_to_string(a, "string matches shifted input", pattern + 2 + 3 + 2);
    ctest_test("shifted data beyond end (42 bytes)", buffer_shift(a, 42) != 0);

    ctest_test("un-referenced", refobject_unref(a) == 0);
}

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
static void test_length(void)
{
    buffer_t *a;
    const char *pattern = "AABBBCC";
    const char *match_a = "AABBB";
    const char *match_b = "AABB";
    const char *match_c = "";

    a = buffer_new_simple();
    ctest_test("buffer created", a != NULL);

    ctest_test("pushed string", buffer_push_string(a, pattern) == 0);
    test__compare_to_string(a, "string matches input", pattern);
    ctest_test("Set length to match pattern a", buffer_set_length(a, strlen(match_a)) == 0);
    test__compare_to_string(a, "string matches pattern a", match_a);
    ctest_test("Set length to match pattern b", buffer_set_length(a, strlen(match_b)) == 0);
    test__compare_to_string(a, "string matches pattern a", match_b);
    ctest_test("Set length to match pattern c", buffer_set_length(a, strlen(match_c)) == 0);
    test__compare_to_string(a, "string matches pattern a", match_c);
    ctest_test("Set length to match pattern a (again)", buffer_set_length(a, strlen(match_a)) != 0);
    test__compare_to_string(a, "string still matches pattern c", match_c);

    ctest_test("un-referenced", refobject_unref(a) == 0);
}

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
static void test_printf(void)
{
    buffer_t *a;
    const char *str = "Hello World!";
    const int num = -127;
    const char *match_a = ":Hello World!:";
    const char *match_b = ":Hello World!:<-127 >";
    const char *match_c = ":Hello World!:<-127 >? +127?";

    a = buffer_new_simple();
    ctest_test("buffer created", a != NULL);

    ctest_test("Set length to match pattern a", buffer_push_printf(a, ":%s:", str) == 0);
    test__compare_to_string(a, "string matches pattern a", match_a);
    ctest_test("Set length to match pattern a", buffer_push_printf(a, "<%-5i>", num) == 0);
    test__compare_to_string(a, "string matches pattern b", match_b);
    ctest_test("Set length to match pattern a", buffer_push_printf(a, "?%+5i?", -num) == 0);
    test__compare_to_string(a, "string matches pattern c", match_c);

    ctest_test("un-referenced", refobject_unref(a) == 0);
}

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
static void test_push_buffer(void)
{
    buffer_t *a;
    buffer_t *b;
    const char *pattern = "AABBBCC";
    const char *match_a = "AABBBCCAABBBCC";

    a = buffer_new_simple();
    ctest_test("buffer a created", a != NULL);
    b = buffer_new_simple();
    ctest_test("buffer b created", b != NULL);

    ctest_test("pushed string", buffer_push_string(a, pattern) == 0);
    test__compare_to_string(a, "string matches input", pattern);

    ctest_test("pushed buffer a to b", buffer_push_buffer(b, a) == 0);
    test__compare_to_string(b, "string matches input", pattern);

    ctest_test("pushed buffer a to b", buffer_push_buffer(b, a) == 0);
    test__compare_to_string(b, "string matches pattern a", match_a);

    ctest_test("un-referenced b", refobject_unref(b) == 0);
    ctest_test("un-referenced a", refobject_unref(a) == 0);
}

331 332 333 334 335 336 337 338 339 340 341
int main (void)
{
    ctest_init();


    test_create_ref_unref();

    test_name();
    test_userdata();
    test_associated();

342
    test_empty();
343
    test_string();
Philipp Schafft's avatar
Philipp Schafft committed
344
    test_binary();
345

346
    test_shift();
347
    test_length();
348

349
    test_printf();
350
    test_push_buffer();
351

352 353 354 355
    ctest_fin();

    return 0;
}