| 1 |
#include "unittest.h" |
| 2 |
#include <stdio.h> |
| 3 |
#include <string.h> |
| 4 |
#include <cfgparser.h> |
| 5 |
#include <configuration.h> |
| 6 |
#include "platform.h" |
| 7 |
|
| 8 |
#if defined(_AIX) |
| 9 |
const char *__progname = "configdump"; |
| 10 |
#endif |
| 11 |
|
| 12 |
static const char test1[] = "foo = 1\nbar = 2\n"; |
| 13 |
static const char test2[] = "foo = 1 bar = 2\n"; |
| 14 |
static const char test3[] = "foo {\n bar = 1\n buzz = 2\n}\n"; |
| 15 |
static const char test4[] = "foo {\n bar = 1 buzz = 2\n}\n"; |
| 16 |
static const char test5[] = "foo ( bar, buzz )\n"; |
| 17 |
static const char test6[] = "foo (\n bar,\n buzz\n )\n"; |
| 18 |
static const char test7[] = "foo\n(\n bar,\n buzz\n)\n"; |
| 19 |
static const char nested1[] = "nested {\n foo (\n bar,\n buzz\n )\n}\n"; |
| 20 |
static const char nested2[] = "nested {\n foo\n (\n bar,\n buzz\n )\n}\n"; |
| 21 |
static const char numpair1[] = "foo\n 1 2\n 3 4\nbar\n"; |
| 22 |
static const char bare1[] = "foo\n"; |
| 23 |
static const char bare2[] = "foo bar\n"; |
| 24 |
static const char bare3[] = "foo\nbar\n"; |
| 25 |
static const char bare4[] = "\"foo\"\n"; |
| 26 |
static const char bare5[] = "\"foo\"\n\"bar\"\n"; |
| 27 |
static const char bare6[] = "foo \"bar\"\n"; |
| 28 |
static char outbuf[1024]; |
| 29 |
|
| 30 |
static const char *curtest; |
| 31 |
|
| 32 |
static FILE *opentest(const char *test) |
| 33 |
{ |
| 34 |
FILE *res = fmemopen((char *)test, strlen(test), "r"); |
| 35 |
if (!res) |
| 36 |
err(TEST_FAIL, "Could not fmemopen test string \"%s\"", test); |
| 37 |
return res; |
| 38 |
} |
| 39 |
|
| 40 |
static FILE *openout(void) |
| 41 |
{ |
| 42 |
FILE *res = fmemopen(outbuf, sizeof(outbuf), "w"); |
| 43 |
if (!res) |
| 44 |
err(TEST_FAIL, "Could not fmemopen output"); |
| 45 |
return res; |
| 46 |
} |
| 47 |
|
| 48 |
static void parse_error(int line, int col, const char *msg) |
| 49 |
{ |
| 50 |
errx(TEST_FAIL, "Parsing \"%s\", line %d, col %d: %s\n", |
| 51 |
curtest, line, col, msg); |
| 52 |
} |
| 53 |
|
| 54 |
static void runparsedumptest(const char *test) |
| 55 |
{ |
| 56 |
struct ConfigBaseNode *config; |
| 57 |
FILE *in, *out; |
| 58 |
int ret; |
| 59 |
|
| 60 |
curtest = test; |
| 61 |
in = opentest(test); |
| 62 |
out = openout(); |
| 63 |
ret = parse_configlib_file(in, &config, parse_error, 1); |
| 64 |
fclose(in); |
| 65 |
if (ret) |
| 66 |
errx(TEST_FAIL, "Failed to parse \"%s\"", test); |
| 67 |
confignode_dump(out, config, NULL, 2); |
| 68 |
fclose(out); |
| 69 |
confignode_deepfree(config); |
| 70 |
if (strcmp(test, outbuf)) |
| 71 |
errx(TEST_FAIL, "Expected \"%s\", but got \"%s\"!", test, outbuf); |
| 72 |
} |
| 73 |
|
| 74 |
int main(void) |
| 75 |
{ |
| 76 |
runparsedumptest(test1); |
| 77 |
runparsedumptest(test2); |
| 78 |
runparsedumptest(test3); |
| 79 |
runparsedumptest(test4); |
| 80 |
runparsedumptest(test5); |
| 81 |
runparsedumptest(test6); |
| 82 |
runparsedumptest(test7); |
| 83 |
runparsedumptest(nested1); |
| 84 |
runparsedumptest(nested2); |
| 85 |
runparsedumptest(numpair1); |
| 86 |
runparsedumptest(bare1); |
| 87 |
runparsedumptest(bare2); |
| 88 |
runparsedumptest(bare3); |
| 89 |
runparsedumptest(bare4); |
| 90 |
runparsedumptest(bare5); |
| 91 |
runparsedumptest(bare6); |
| 92 |
return TEST_PASS; |
| 93 |
} |