2019-02-24 00:28:20 +08:00
|
|
|
#include <cassert>
|
2018-08-18 07:57:31 +08:00
|
|
|
#include <cstring>
|
2018-12-31 10:00:19 +08:00
|
|
|
#ifndef _MSC_VER
|
2018-08-18 07:57:31 +08:00
|
|
|
#include <dirent.h>
|
2018-12-31 10:00:19 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#else
|
|
|
|
// Microsoft can't be bothered to provide standard utils.
|
|
|
|
#include <dirent_portable.h>
|
|
|
|
#endif
|
2019-02-24 00:28:20 +08:00
|
|
|
#include <cinttypes>
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2018-08-18 07:57:31 +08:00
|
|
|
|
2018-11-30 22:37:57 +08:00
|
|
|
#include "simdjson/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, '.');
|
2019-02-24 00:28:20 +08:00
|
|
|
return ((ext != nullptr) && (strcmp(ext, extension) == 0));
|
2018-08-18 07:57:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2019-02-24 00:28:20 +08:00
|
|
|
return (strstr(str, pre) != nullptr);
|
2018-10-24 08:19:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-18 07:57:31 +08:00
|
|
|
bool validate(const char *dirname) {
|
|
|
|
bool everythingfine = true;
|
|
|
|
const char *extension = ".json";
|
|
|
|
size_t dirlen = strlen(dirname);
|
|
|
|
struct dirent **entry_list;
|
2019-02-24 00:28:20 +08:00
|
|
|
int c = scandir(dirname, &entry_list, nullptr, alphasort);
|
2018-08-18 07:57:31 +08:00
|
|
|
if (c < 0) {
|
2018-12-31 10:00:19 +08:00
|
|
|
fprintf(stderr, "error accessing %s \n", dirname);
|
2018-08-18 07:57:31 +08:00
|
|
|
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];
|
2019-02-24 00:28:20 +08:00
|
|
|
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-12-01 10:31:05 +08:00
|
|
|
printf("validating: file %s ", name);
|
2019-02-24 00:28:20 +08:00
|
|
|
fflush(nullptr);
|
2018-08-18 07:57:31 +08:00
|
|
|
size_t filelen = strlen(name);
|
2019-02-24 00:31:38 +08:00
|
|
|
char *fullpath = static_cast<char *>(malloc(dirlen + filelen + 1 + 1));
|
2018-08-18 07:57:31 +08:00
|
|
|
strcpy(fullpath, dirname);
|
|
|
|
if (needsep) {
|
|
|
|
fullpath[dirlen] = '/';
|
|
|
|
strcpy(fullpath + dirlen + 1, name);
|
|
|
|
} else {
|
|
|
|
strcpy(fullpath + dirlen, name);
|
|
|
|
}
|
2018-12-01 09:27:16 +08:00
|
|
|
std::string_view p;
|
2018-11-28 03:37:59 +08:00
|
|
|
try {
|
|
|
|
p = get_corpus(fullpath);
|
2018-12-31 10:00:19 +08:00
|
|
|
} catch (const std::exception& e) {
|
|
|
|
std::cerr << "Could not load the file " << fullpath << std::endl;
|
2018-11-28 03:37:59 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2018-12-01 09:27:16 +08:00
|
|
|
ParsedJson pj;
|
|
|
|
bool allocok = pj.allocateCapacity(p.size(), 1024);
|
|
|
|
if(!allocok) {
|
2018-12-31 10:00:19 +08:00
|
|
|
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;
|
2019-03-03 06:18:45 +08:00
|
|
|
const int parseRes = json_parse(p, pj);
|
2018-12-31 10:00:19 +08:00
|
|
|
aligned_free((void*)p.data());
|
2019-03-03 06:18:45 +08:00
|
|
|
printf("%s\n", parseRes == 0 ? "ok" : "invalid");
|
2018-10-24 08:19:33 +08:00
|
|
|
if(contains("EXCLUDE",name)) {
|
|
|
|
// skipping
|
|
|
|
howmany--;
|
2019-03-03 06:18:45 +08:00
|
|
|
} else if (startsWith("pass", name) && parseRes != 0) {
|
2018-11-28 03:37:59 +08:00
|
|
|
isfileasexpected[i] = false;
|
2019-03-03 06:18:45 +08:00
|
|
|
printf("warning: file %s should pass but it fails. Error is: %s\n", name, simdjson::errorMsg(parseRes).data());
|
2018-08-18 07:57:31 +08:00
|
|
|
everythingfine = false;
|
2019-03-03 06:18:45 +08:00
|
|
|
} else if (startsWith("fail", name) && parseRes == 0) {
|
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;
|
2018-12-31 10:00:19 +08:00
|
|
|
}
|
2018-08-18 07:57:31 +08:00
|
|
|
free(fullpath);
|
|
|
|
}
|
|
|
|
}
|
2018-11-28 03:37:59 +08:00
|
|
|
printf("%zu files checked.\n", howmany);
|
|
|
|
if(everythingfine) {
|
|
|
|
printf("All ok!\n");
|
|
|
|
} else {
|
2018-12-31 10:00:19 +08:00
|
|
|
fprintf(stderr, "There were problems! Consider reviewing the following files:\n");
|
2018-11-28 03:37:59 +08:00
|
|
|
for(int i = 0; i < c; i++) {
|
2019-02-24 00:28:20 +08:00
|
|
|
if(!isfileasexpected[i]) { fprintf(stderr, "%s \n", entry_list[i]->d_name);
|
|
|
|
}
|
2018-11-28 03:37:59 +08:00
|
|
|
}
|
|
|
|
}
|
2019-02-24 00:28:20 +08:00
|
|
|
for (int i = 0; i < c; ++i) {
|
2018-08-18 07:57:31 +08:00
|
|
|
free(entry_list[i]);
|
2019-02-24 00:28:20 +08:00
|
|
|
}
|
2018-08-18 07:57:31 +08:00
|
|
|
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;
|
2018-12-29 02:04:38 +08:00
|
|
|
#ifndef SIMDJSON_TEST_DATA_DIR
|
2018-08-18 07:57:31 +08:00
|
|
|
std::cout
|
|
|
|
<< "We are going to assume you mean to use the 'jsonchecker' directory."
|
|
|
|
<< std::endl;
|
|
|
|
return validate("jsonchecker/") ? EXIT_SUCCESS : EXIT_FAILURE;
|
2018-12-29 02:04:38 +08:00
|
|
|
#else
|
|
|
|
std::cout
|
|
|
|
<< "We are going to assume you mean to use the '"<< SIMDJSON_TEST_DATA_DIR <<"' directory."
|
|
|
|
<< std::endl;
|
|
|
|
return validate(SIMDJSON_TEST_DATA_DIR) ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
#endif
|
2018-08-18 07:57:31 +08:00
|
|
|
}
|
|
|
|
return validate(argv[1]) ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
}
|