memxor_test.c 3.17 KB
Newer Older
Davide Galassi's avatar
Davide Galassi committed
1
#include "test.h"
2
#include <cry/memxor.h>
3 4 5 6 7 8 9 10
#include <string.h>

#define S1 "HeLLoWoRld01x"
#define S2 "0231487516295"
#define L1 (sizeof(S1)-1)
#define L2 (sizeof(S2)-1)

#define USED_MAX (L1+L2+1)
Davide Galassi's avatar
Davide Galassi committed
11 12


13
#define XOR_RES \
14 15 16 17 18 19 20 21 22 23 24
    "\x78\x57\x7f\x7d\x5b\x6f\x58\x67\x5d\x52\x02\x08\x4d"

#define XOR_RES1 \
    "\x78\x57\x7f\x7d\x5b\x6f\x58\x67\x5d\x52\x02\x08\x05"

#define XOR_RES2 \
    "\x78\x57\x7f\x01\x06\x0b\x06\x01\x09\x01\x07\x08\x03"


static void mem1_before_mem2(void)
{
25 26
    memcpy(g_buf, S1, L1);
    memcpy(g_buf+L1+1, S2, L2);
27

28
    cry_memxor(g_buf, g_buf+L1+1, L1);
29

30
    ASSERT_EQ_BUF(g_buf, XOR_RES, L1);
31 32 33 34
}

static void mem1_after_mem2(void)
{
35 36
    memcpy(g_buf, S1, L1);
    memcpy(g_buf+L1+1, S2, L2);
37

38
    cry_memxor(g_buf+L1+1, g_buf, L1);
39

40
    ASSERT_EQ_BUF(g_buf+L1+1, XOR_RES, L1);
41 42 43 44
}

static void memxor2_mem1_before_mem2(void)
{
45 46
    memcpy(g_buf, S1, L1);
    memcpy(g_buf+L1+1, S2, L2);
47

48
    cry_memxor2(g_buf, g_buf, g_buf+L1+1, L1);
49

50
    ASSERT_EQ_BUF(g_buf, XOR_RES, L1);
51 52 53 54
}

static void memxor2_mem1_after_mem2(void)
{
55 56
    memcpy(g_buf, S1, L1);
    memcpy(g_buf+L1+1, S2, L2);
57

58
    cry_memxor2(g_buf+L1+1, g_buf+L1+1, g_buf, L1);
59

60
    ASSERT_EQ_BUF(g_buf+L1+1, XOR_RES, L1);
61 62 63 64
}

static void memxor2_mem1_before_mem2_overlap(void)
{
65 66
    memcpy(g_buf, S1, L1);
    memcpy(g_buf+L1-1, S2, L2);
67

68
    cry_memxor2(g_buf, g_buf, g_buf+L1-1, L1);
69

70
    ASSERT_EQ_BUF(g_buf, XOR_RES1, L1);
71 72 73 74
}

static void memxor2_mem1_after_mem2_overlap(void)
{
75 76
    memcpy(g_buf, S1, L1);
    memcpy(g_buf+L1-1, S2, L2);
77

78
    cry_memxor2(g_buf+L1-1, g_buf+L1-1, g_buf, L1);
79

80
    ASSERT_EQ_BUF(g_buf+L1-1, XOR_RES1, L1);
81 82 83 84
}

static void memxor2_mem1_before_mem2_dst_in_the_gap_after_mem1_end(void)
{
85 86
    memcpy(g_buf, S1, L1);
    memcpy(g_buf+L1+1, S2, L2);
87

88
    cry_memxor2(g_buf+L1, g_buf, g_buf+L1+1, L1);
89

90
    ASSERT_EQ_BUF(g_buf+L1, XOR_RES, L1);
91 92 93 94
}

static void memxor2_mem1_before_mem2_dst_in_the_gap_before_mem1_end(void)
{
95 96
    memcpy(g_buf, S1, L1);
    memcpy(g_buf+L1+1, S2, L2);
97

98
    cry_memxor2(g_buf+L1-3, g_buf, g_buf+L1+1, L1);
99

100
    ASSERT_EQ_BUF(g_buf+L1-3, XOR_RES, L1);
101 102 103 104
}

static void memxor2_mem1_before_mem2_dst_in_the_mid(void)
{
105 106
    memcpy(g_buf, S1, L1);
    memcpy(g_buf+3, S2, L2);
107

108
    cry_memxor2(g_buf+1, g_buf, g_buf+3, L1);
109

110
    ASSERT_EQ_BUF(g_buf+1, XOR_RES2, L1);
111 112 113 114 115
}


static void setup(void)
{
116
    memset(g_buf, 0, USED_MAX);
117 118
}

119 120 121 122 123 124 125 126 127 128 129 130 131 132

static struct test_case tests[] = {
    { "M1 < M2 and dst = M1", mem1_before_mem2 },
    { "M1 > M2 and dst = M1", mem1_after_mem2 },
    { "M1 < M2", memxor2_mem1_before_mem2 },
    { "M1 > M2", memxor2_mem1_after_mem2 },
    { "M1 < M2 and overlap", memxor2_mem1_before_mem2_overlap },
    { "M1 > M2 and overlap", memxor2_mem1_after_mem2_overlap },
    { "M1 < M2 and M1.end <= dst <= M2", memxor2_mem1_before_mem2_dst_in_the_gap_after_mem1_end },
    { "M1 < M2 and M1 <= dst <= M1.end", memxor2_mem1_before_mem2_dst_in_the_gap_before_mem1_end },
    { "M1 < M2 and M1.end <= dst <= M2", memxor2_mem1_before_mem2_dst_in_the_mid }
};

#define NTESTS (sizeof(tests)/sizeof(tests[0]))
Davide Galassi's avatar
Davide Galassi committed
133 134 135

void memxor_test(void)
{
136
    int i;
Davide Galassi's avatar
Davide Galassi committed
137

138
    printf("* XOR\n");
139 140 141
    ASSERT(USED_MAX < BUFSIZ);
    for (i = 0; i < NTESTS; i++)
        run(tests[i].name, tests[i].func, setup, NULL);
142
    printf("\n");
Davide Galassi's avatar
Davide Galassi committed
143
}