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 208 209 210 211 212 213 214 215 216 217 218 219 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 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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <limits.h>
#include <errno.h>
#include <getopt.h>
#include <libmnl/libmnl.h> /*nlmsghdr*/
#include <libnftnl/ruleset.h>
#include <libnftnl/table.h>
#include <libnftnl/chain.h>
#include <libnftnl/rule.h>
#include <libnftnl/set.h>
enum {
TEST_XML_RULESET,
TEST_JSON_RULESET,
};
static bool update = false;
static void print_detail_error(char *a, char *b)
{
int i;
int from = -1;
for (i = 0; i < strlen(b); i++) {
if (from == -1 && a[i] != b[i]) {
from = i;
break;
}
}
if (from != -1) {
int k = from - 10;
if (k < 0)
k = 0;
fprintf(stderr, "from file: ");
for (i = k; i < from + 10; i++)
fprintf(stderr, "%c", a[i]);
fprintf(stderr, "\nfrom snprintf: ");
for (i = k; i < from + 10; i++)
fprintf(stderr, "%c", b[i]);
/* Don't look twice below this comment ;-) */
fprintf(stderr, "\n ");
for (i = k; i < from + 10; i++) {
if (i == from)
fprintf(stderr, "^");
else
fprintf(stderr, " ");
}
fprintf(stderr, "\n");
}
}
static int compare_test(uint32_t type, struct nftnl_ruleset *rs,
const char *filename, FILE *fp)
{
char orig[4096];
char out[4096];
switch (type) {
case TEST_XML_RULESET:
nftnl_ruleset_snprintf(out, sizeof(out), rs,
NFTNL_OUTPUT_XML, NFTNL_OF_EVENT_NEW);
break;
case TEST_JSON_RULESET:
nftnl_ruleset_snprintf(out, sizeof(out), rs,
NFTNL_OUTPUT_JSON, NFTNL_OF_EVENT_NEW);
break;
default:
errno = EINVAL;
return -1;
}
rewind(fp);
fgets(orig, sizeof(orig), fp);
if (strncmp(orig, out, strlen(out)) == 0) {
if (update)
printf("%s: No changes to update\n", filename);
return 0;
}
if (update) {
FILE *fout;
printf("%s: Updating test file\n", filename);
fout = fopen(filename, "w");
if (fout == NULL) {
printf("unable to open file %s: %s\n", filename,
strerror(errno));
return -1;
}
fprintf(fout, "%s\n", out);
fclose(fout);
return 0;
}
printf("validating %s: ", filename);
printf("\033[31mFAILED\e[0m\n");
print_detail_error(orig, out);
return -1;
}
static int test_json(const char *filename, struct nftnl_parse_err *err)
{
int ret = -1;
struct nftnl_ruleset *rs;
FILE *fp;
fp = fopen(filename, "r");
if (fp == NULL) {
printf("unable to open file %s: %s\n", filename,
strerror(errno));
return -1;
}
rs = nftnl_ruleset_alloc();
if (rs == NULL) {
perror("nftnl_ruleset_alloc");
return -1;
}
if (nftnl_ruleset_parse_file(rs, NFTNL_PARSE_JSON, fp, err) == 0)
ret = compare_test(TEST_JSON_RULESET, rs, filename, fp);
else
goto failparsing;
nftnl_ruleset_free(rs);
fclose(fp);
return ret;
failparsing:
fclose(fp);
printf("parsing %s: ", filename);
printf("\033[31mFAILED\e[0m (%s)\n", strerror(errno));
nftnl_parse_perror("Reason", err);
return -1;
}
static int execute_test(const char *dir_name)
{
DIR *d;
struct dirent *dent;
char path[PATH_MAX];
int ret = 0, exit_code = 0;
struct nftnl_parse_err *err;
d = opendir(dir_name);
if (d == NULL) {
perror("opendir");
exit(EXIT_FAILURE);
}
err = nftnl_parse_err_alloc();
if (err == NULL) {
perror("error");
exit(EXIT_FAILURE);
}
while ((dent = readdir(d)) != NULL) {
int len = strlen(dent->d_name);
if (strcmp(dent->d_name, ".") == 0 ||
strcmp(dent->d_name, "..") == 0)
continue;
snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
if (strcmp(&dent->d_name[len-5], ".json") == 0) {
if ((ret = test_json(path, err)) == 0) {
if (!update) {
printf("parsing and validating %s: ",
path);
printf("\033[32mOK\e[0m\n");
}
}
exit_code += ret;
}
}
closedir(d);
nftnl_parse_err_free(err);
if (exit_code != 0)
exit(EXIT_FAILURE);
return 0;
}
static int execute_test_file(const char *filename)
{
char path[PATH_MAX];
struct nftnl_parse_err *err;
int ret = 0, len;
err = nftnl_parse_err_alloc();
if (err == NULL) {
perror("error");
exit(EXIT_FAILURE);
}
snprintf(path, sizeof(path), "%s", filename);
len = strlen(filename);
if (strcmp(&filename[len-5], ".json") == 0) {
if ((ret = test_json(path, err)) == 0) {
if (!update) {
printf("parsing and validating %s: ",
path);
printf("\033[32mOK\e[0m\n");
}
}
nftnl_parse_err_free(err);
exit(EXIT_FAILURE);
}
nftnl_parse_err_free(err);
return 0;
}
static void show_help(const char *name)
{
printf(
"Usage: %s [option]\n"
"\n"
"Options:\n"
" -d/--dir <directory> Check test files from <directory>.\n"
" -u/--update <directory> Update test files from <directory>.\n"
" -f/--file <file> Check test file <file>\n"
"\n",
name);
}
int main(int argc, char *argv[])
{
int val;
int ret = 0;
int option_index = 0;
static struct option long_options[] = {
{ "dir", required_argument, 0, 'd' },
{ "update", required_argument, 0, 'u' },
{ "file", required_argument, 0, 'f' },
{ 0 }
};
if (argc != 3) {
show_help(argv[0]);
exit(EXIT_FAILURE);
}
while (1) {
val = getopt_long(argc, argv, "d:u:f:", long_options,
&option_index);
if (val == -1)
break;
switch (val) {
case 'd':
ret = execute_test(optarg);
break;
case 'u':
update = true;
ret = execute_test(optarg);
break;
case 'f':
ret = execute_test_file(optarg);
break;
default:
show_help(argv[0]);
break;
}
}
return ret;
}
|