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 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 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
|
#include <stdio.h>
#include <stdbool.h>
#include "globus_i_gridftp_server_config.h"
#include "globus_i_gfs_ipc.h"
typedef struct
{
char *name;
int (*test)(void);
}
test_case;
#define TEST_CASE(x) { .name = #x, .test = (x) }
#define TEST_ASSERT(x) \
if (!(x)) \
{ \
fprintf(stderr, " [%s:%d] %s\n", __func__, __LINE__, (#x)); \
testres = 1; \
goto cleanup; \
}
int
test_uint32(void)
{
char *start = NULL;
char *buf = NULL;
size_t len = 0;
uint32_t test_uints[] = { 0, UINT32_C(1)<< 25, UINT32_C(1)<<30, UINT32_MAX };
uint32_t test_uints_out[4];
void *test_uintps_out[4];
int testres = 0;
start = buf = malloc(len = 8);
TEST_ASSERT(start != NULL);
TEST_ASSERT(buf != NULL);
for (int i = 0; i < sizeof(test_uints) / sizeof(*test_uints); i++)
{
GFSEncodeUInt32(start, len, buf, test_uints[i]);
}
buf = start;
for (int i = 0; i < sizeof(test_uints_out) / sizeof(*test_uints_out); i++)
{
GFSDecodeUInt32(buf, len, test_uints_out[i]);
}
buf = start;
for (int i = 0; i < sizeof(test_uintps_out) / sizeof(*test_uintps_out); i++)
{
GFSDecodeUInt32P(buf, len, test_uintps_out[i]);
}
for (int i = 0; i < sizeof(test_uints) / sizeof(*test_uints); i++)
{
TEST_ASSERT(test_uints[i] == test_uints_out[i]);
TEST_ASSERT((intptr_t) test_uints[i] == (intptr_t) test_uintps_out[i]);
}
cleanup:
return testres;
decode_err:
TEST_ASSERT(false);
}
int
test_uint64(void)
{
char *start = NULL;
char *buf = NULL;
size_t len = 0;
uint64_t test_uints[] = { 0, 1<<25, UINT64_C(1)<<30, UINT64_C(1)<<34, UINT64_C(1)<<60, UINT64_MAX };
uint64_t test_uints_out[6];
int testres = 0;
start = buf = malloc(len = 8);
TEST_ASSERT(start != NULL);
TEST_ASSERT(buf != NULL);
for (int i = 0; i < sizeof(test_uints) / sizeof(*test_uints); i++)
{
GFSEncodeUInt64(start, len, buf, test_uints[i]);
}
buf = start;
for (int i = 0; i < sizeof(test_uints_out) / sizeof(*test_uints_out); i++)
{
GFSDecodeUInt64(buf, len, test_uints_out[i]);
}
for (int i = 0; i < sizeof(test_uints) / sizeof(*test_uints); i++)
{
TEST_ASSERT(test_uints[i] == test_uints_out[i]);
}
cleanup:
return testres;
decode_err:
TEST_ASSERT(false);
}
int
test_char(void)
{
char *start = NULL;
char *buf = NULL;
size_t len = 0;
char test_chars[] = { 0, 1, 2, 3, 124, 127 };
char test_chars_out[6];
int testres = 0;
start = buf = malloc(len = 8);
TEST_ASSERT(start != NULL);
TEST_ASSERT(buf != NULL);
for (int i = 0; i < sizeof(test_chars) / sizeof(*test_chars); i++)
{
GFSEncodeChar(start, len, buf, test_chars[i]);
}
buf = start;
for (int i = 0; i < sizeof(test_chars_out) / sizeof(*test_chars_out); i++)
{
GFSDecodeChar(buf, len, test_chars_out[i]);
}
for (int i = 0; i < sizeof(test_chars) / sizeof(*test_chars); i++)
{
TEST_ASSERT(test_chars[i] == test_chars_out[i]);
}
cleanup:
return testres;
decode_err:
TEST_ASSERT(false);
}
int
test_string(void)
{
char *start = NULL;
char *buf = NULL;
size_t len = 0;
char* test_strings[] = { "hello", "", "\n" };
char *test_strings_out[3];
int testres = 0;
start = buf = malloc(len = 8);
TEST_ASSERT(start != NULL);
TEST_ASSERT(buf != NULL);
for (int i = 0; i < sizeof(test_strings) / sizeof(*test_strings); i++)
{
GFSEncodeString(start, len, buf, test_strings[i]);
}
buf = start;
for (int i = 0; i < sizeof(test_strings_out) / sizeof(*test_strings_out); i++)
{
GFSDecodeString(buf, len, test_strings_out[i]);
}
for (int i = 0; i < sizeof(test_strings) / sizeof(*test_strings); i++)
{
TEST_ASSERT(strcmp(test_strings[i], test_strings_out[i]) == 0);
}
cleanup:
return testres;
decode_err:
TEST_ASSERT(false);
}
int main()
{
test_case tests[] =
{
TEST_CASE(test_uint32),
TEST_CASE(test_uint64),
TEST_CASE(test_char),
TEST_CASE(test_string)
};
int failed = 0;
int rc = 0;
#ifdef WORDS_BIGENDIAN
printf("# big endian test\n");
#else
printf("# little endian test\n");
#endif
printf("1..%d\n", (int) (sizeof(tests) / sizeof(*tests)));
for (int i = 0; i < sizeof(tests) / sizeof(*tests); i++)
{
rc = tests[i].test();
if (rc == 0)
{
printf("ok %d - %s\n", i+1, tests[i].name);
}
else
{
printf("not ok %d - %s\n", i+1, tests[i].name);
failed++;
}
}
return failed;
}
|