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-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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
std::pair<u8 *, size_t> p = get_corpus(fullpath);
|
|
|
|
// terrible hack but just to get it working
|
2018-08-21 05:27:25 +08:00
|
|
|
ParsedJson *pj_ptr = allocate_ParsedJson(p.second);
|
|
|
|
if(pj_ptr == NULL) {
|
|
|
|
std::cerr<< "can't allocate memory"<<std::endl;
|
2018-08-18 07:57:31 +08:00
|
|
|
return false;
|
|
|
|
}
|
2018-08-21 05:27:25 +08:00
|
|
|
ParsedJson &pj(*pj_ptr);
|
|
|
|
bool isok = json_parse(p.first, p.second, pj);
|
2018-08-18 07:57:31 +08:00
|
|
|
if (startsWith("pass", name)) {
|
|
|
|
if (!isok) {
|
|
|
|
printf("warning: file %s should pass but it fails.\n", name);
|
|
|
|
everythingfine = false;
|
|
|
|
}
|
|
|
|
} else if (startsWith("fail", name)) {
|
|
|
|
if (isok) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < c; ++i)
|
|
|
|
free(entry_list[i]);
|
|
|
|
free(entry_list);
|
|
|
|
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;
|
|
|
|
}
|