2018-08-18 07:57:31 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <cstring>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2018-11-28 03:37:59 +08:00
|
|
|
#include <unistd.h>
|
2018-08-18 07:57:31 +08:00
|
|
|
|
2018-08-21 05:51:38 +08:00
|
|
|
#include "jsonparser/jsonparser.h"
|
2018-08-18 07:57:31 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Does the file filename ends with the given extension.
|
|
|
|
*/
|
|
|
|
static bool hasExtension(const char *filename, const char *extension) {
|
|
|
|
const char *ext = strrchr(filename, '.');
|
|
|
|
return (ext && !strcmp(ext, extension));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool startsWith(const char *pre, const char *str) {
|
|
|
|
size_t lenpre = strlen(pre), lenstr = strlen(str);
|
|
|
|
return lenstr < lenpre ? false : strncmp(pre, str, lenpre) == 0;
|
|
|
|
}
|
|
|
|
|
2018-10-24 08:19:33 +08:00
|
|
|
bool contains(const char *pre, const char *str) {
|
|
|
|
return (strstr(str, pre) != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-18 07:57:31 +08:00
|
|
|
bool validate(const char *dirname) {
|
|
|
|
bool everythingfine = true;
|
2018-08-21 05:27:25 +08:00
|
|
|
// init_state_machine(); // no longer necessary
|
2018-08-18 07:57:31 +08:00
|
|
|
const char *extension = ".json";
|
|
|
|
size_t dirlen = strlen(dirname);
|
|
|
|
struct dirent **entry_list;
|
|
|
|
int c = scandir(dirname, &entry_list, 0, alphasort);
|
|
|
|
if (c < 0) {
|
|
|
|
printf("error accessing %s \n", dirname);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (c == 0) {
|
|
|
|
printf("nothing in dir %s \n", dirname);
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-28 03:37:59 +08:00
|
|
|
bool * isfileasexpected = new bool[c];
|
|
|
|
for(int i = 0; i < c; i++) isfileasexpected[i] = true;
|
2018-10-24 08:19:33 +08:00
|
|
|
size_t howmany = 0;
|
2018-08-18 07:57:31 +08:00
|
|
|
bool needsep = (strlen(dirname) > 1) && (dirname[strlen(dirname) - 1] != '/');
|
|
|
|
for (int i = 0; i < c; i++) {
|
|
|
|
const char *name = entry_list[i]->d_name;
|
|
|
|
if (hasExtension(name, extension)) {
|
2018-09-28 08:26:27 +08:00
|
|
|
//printf("validating: file %s \n", name);
|
2018-08-18 07:57:31 +08:00
|
|
|
size_t filelen = strlen(name);
|
|
|
|
char *fullpath = (char *)malloc(dirlen + filelen + 1 + 1);
|
|
|
|
strcpy(fullpath, dirname);
|
|
|
|
if (needsep) {
|
|
|
|
fullpath[dirlen] = '/';
|
|
|
|
strcpy(fullpath + dirlen + 1, name);
|
|
|
|
} else {
|
|
|
|
strcpy(fullpath + dirlen, name);
|
|
|
|
}
|
2018-11-28 03:37:59 +08:00
|
|
|
std::pair<u8 *, size_t> p;
|
|
|
|
try {
|
|
|
|
p = get_corpus(fullpath);
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
std::cout << "Could not load the file " << fullpath << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2018-11-27 23:10:39 +08:00
|
|
|
ParsedJson *pj_ptr = allocate_ParsedJson(p.second, 1024);
|
2018-08-21 05:27:25 +08:00
|
|
|
if(pj_ptr == NULL) {
|
|
|
|
std::cerr<< "can't allocate memory"<<std::endl;
|
2018-08-18 07:57:31 +08:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-24 08:19:33 +08:00
|
|
|
++howmany;
|
2018-08-21 05:27:25 +08:00
|
|
|
ParsedJson &pj(*pj_ptr);
|
|
|
|
bool isok = json_parse(p.first, p.second, pj);
|
2018-10-24 08:19:33 +08:00
|
|
|
if(contains("EXCLUDE",name)) {
|
|
|
|
// skipping
|
|
|
|
howmany--;
|
|
|
|
} else if (startsWith("pass", name)) {
|
2018-08-18 07:57:31 +08:00
|
|
|
if (!isok) {
|
2018-11-28 03:37:59 +08:00
|
|
|
isfileasexpected[i] = false;
|
2018-08-18 07:57:31 +08:00
|
|
|
printf("warning: file %s should pass but it fails.\n", name);
|
|
|
|
everythingfine = false;
|
|
|
|
}
|
|
|
|
} else if (startsWith("fail", name)) {
|
|
|
|
if (isok) {
|
2018-11-28 03:37:59 +08:00
|
|
|
isfileasexpected[i] = false;
|
2018-08-18 07:57:31 +08:00
|
|
|
printf("warning: file %s should fail but it passes.\n", name);
|
|
|
|
everythingfine = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf("File %s %s.\n", name,
|
|
|
|
isok ? " is valid JSON " : " is not valid JSON");
|
|
|
|
}
|
|
|
|
free(p.first);
|
|
|
|
free(fullpath);
|
2018-08-21 05:27:25 +08:00
|
|
|
deallocate_ParsedJson(pj_ptr);
|
2018-08-18 07:57:31 +08:00
|
|
|
}
|
|
|
|
}
|
2018-11-28 03:37:59 +08:00
|
|
|
printf("%zu files checked.\n", howmany);
|
|
|
|
if(everythingfine) {
|
|
|
|
printf("All ok!\n");
|
|
|
|
} else {
|
|
|
|
printf("There were problems! Consider reviewing the following files:\n");
|
|
|
|
for(int i = 0; i < c; i++) {
|
|
|
|
if(!isfileasexpected[i]) printf("%s \n", entry_list[i]->d_name);
|
|
|
|
}
|
|
|
|
}
|
2018-08-18 07:57:31 +08:00
|
|
|
for (int i = 0; i < c; ++i)
|
|
|
|
free(entry_list[i]);
|
|
|
|
free(entry_list);
|
2018-11-28 03:37:59 +08:00
|
|
|
delete[] isfileasexpected;
|
|
|
|
|
2018-08-18 07:57:31 +08:00
|
|
|
return everythingfine;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
if (argc != 2) {
|
|
|
|
std::cerr << "Usage: " << argv[0] << " <directorywithjsonfiles>"
|
|
|
|
<< std::endl;
|
|
|
|
std::cout
|
|
|
|
<< "We are going to assume you mean to use the 'jsonchecker' directory."
|
|
|
|
<< std::endl;
|
|
|
|
return validate("jsonchecker/") ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
return validate(argv[1]) ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
}
|