Merge branch 'master' of git://repo.or.cz/unleashed
[unleashed.git] / contrib / libjeffpc / test_padding.c
blob2f551603a14d9cd4feed90a29de4dafffcf5c9e0
1 /*
2 * Copyright (c) 2016 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
23 #include <jeffpc/padding.h>
24 #include <jeffpc/types.h>
26 #include "test.c"
28 struct run {
29 const uint8_t *in;
30 size_t inlen;
31 uint8_t byte;
32 bool success;
35 #define TEST_ENTRY(i, b, s) \
36 { \
37 .in = (i), \
38 .inlen = sizeof(i), \
39 .byte = (b), \
40 .success = (s), \
43 static const uint8_t a4[] = { 'A', 'A', 'A', 'A' };
44 static const uint8_t null4[] = { '\0', '\0', '\0', '\0' };
45 static const uint8_t mid5[] = { 'A', 'A', 'X', 'A', 'A' };
46 static const uint8_t end4[] = { 'A', 'A', 'A', 'X' };
48 static const struct run runs[] = {
49 TEST_ENTRY(a4, 'A', true),
50 TEST_ENTRY(a4, 'X', false),
51 TEST_ENTRY(a4, 'a', false),
52 TEST_ENTRY(null4, '\0', true),
53 TEST_ENTRY(null4, 'X', false),
54 TEST_ENTRY(mid5, 'X', false),
55 TEST_ENTRY(mid5, 'A', false),
56 TEST_ENTRY(end4, 'A', false),
57 TEST_ENTRY(end4, 'X', false),
60 static void __test(int idx, const uint8_t *in, size_t inlen, uint8_t byte,
61 bool success)
63 fprintf(stderr, "%d: check_padding(%p, %#x, %zu)...", idx, in, byte,
64 inlen);
66 if (check_padding(in, byte, inlen) != success)
67 fail("%s when it shouldn't have",
68 success ? "failed" : "success");
70 fprintf(stderr, "%s...ok\n", success ? "true" : "false");
73 void test(void)
75 int i;
77 for (i = 0; i < ARRAY_LEN(runs) ; i++)
78 __test(i, runs[i].in, runs[i].inlen, runs[i].byte,
79 runs[i].success);