Adding validating step.
This commit is contained in:
parent
460b971473
commit
93f4e1d9b4
3
Makefile
3
Makefile
|
@ -14,8 +14,9 @@ EXECUTABLES=parse
|
|||
|
||||
|
||||
all: $(EXECUTABLES)
|
||||
-./parse
|
||||
|
||||
parse: main.cpp common_defs.h vecdecode.h linux-perf-events.h
|
||||
parse: main.cpp common_defs.h linux-perf-events.h
|
||||
$(CXX) $(CXXFLAGS) -o parse main.cpp
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
"A JSON payload should be an object or array, not a string."
|
|
@ -0,0 +1 @@
|
|||
["Unclosed array"
|
|
@ -0,0 +1 @@
|
|||
{unquoted_key: "keys must be quoted"}
|
|
@ -0,0 +1 @@
|
|||
["extra comma",]
|
|
@ -0,0 +1 @@
|
|||
["double extra comma",,]
|
|
@ -0,0 +1 @@
|
|||
[ , "<-- missing value"]
|
|
@ -0,0 +1 @@
|
|||
["Comma after the close"],
|
|
@ -0,0 +1 @@
|
|||
["Extra close"]]
|
|
@ -0,0 +1 @@
|
|||
{"Extra comma": true,}
|
|
@ -0,0 +1 @@
|
|||
{"Extra value after close": true} "misplaced quoted value"
|
|
@ -0,0 +1 @@
|
|||
{"Illegal expression": 1 + 2}
|
|
@ -0,0 +1 @@
|
|||
{"Illegal invocation": alert()}
|
|
@ -0,0 +1 @@
|
|||
{"Numbers cannot have leading zeroes": 013}
|
|
@ -0,0 +1 @@
|
|||
{"Numbers cannot be hex": 0x14}
|
|
@ -0,0 +1 @@
|
|||
["Illegal backslash escape: \x15"]
|
|
@ -0,0 +1 @@
|
|||
[\naked]
|
|
@ -0,0 +1 @@
|
|||
["Illegal backslash escape: \017"]
|
|
@ -0,0 +1 @@
|
|||
[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]
|
|
@ -0,0 +1 @@
|
|||
{"Missing colon" null}
|
|
@ -0,0 +1 @@
|
|||
{"Double colon":: null}
|
|
@ -0,0 +1 @@
|
|||
{"Comma instead of colon", null}
|
|
@ -0,0 +1 @@
|
|||
["Colon instead of comma": false]
|
|
@ -0,0 +1 @@
|
|||
["Bad value", truth]
|
|
@ -0,0 +1 @@
|
|||
['single quote']
|
|
@ -0,0 +1 @@
|
|||
[" tab character in string "]
|
|
@ -0,0 +1 @@
|
|||
["tab\ character\ in\ string\ "]
|
|
@ -0,0 +1,2 @@
|
|||
["line
|
||||
break"]
|
|
@ -0,0 +1,2 @@
|
|||
["line\
|
||||
break"]
|
|
@ -0,0 +1 @@
|
|||
[0e]
|
|
@ -0,0 +1 @@
|
|||
[0e+]
|
|
@ -0,0 +1 @@
|
|||
[0e+-1]
|
|
@ -0,0 +1 @@
|
|||
{"Comma instead if closing brace": true,
|
|
@ -0,0 +1 @@
|
|||
["mismatch"}
|
|
@ -0,0 +1,58 @@
|
|||
[
|
||||
"JSON Test Pattern pass1",
|
||||
{"object with 1 member":["array with 1 element"]},
|
||||
{},
|
||||
[],
|
||||
-42,
|
||||
true,
|
||||
false,
|
||||
null,
|
||||
{
|
||||
"integer": 1234567890,
|
||||
"real": -9876.543210,
|
||||
"e": 0.123456789e-12,
|
||||
"E": 1.234567890E+34,
|
||||
"": 23456789012E66,
|
||||
"zero": 0,
|
||||
"one": 1,
|
||||
"space": " ",
|
||||
"quote": "\"",
|
||||
"backslash": "\\",
|
||||
"controls": "\b\f\n\r\t",
|
||||
"slash": "/ & \/",
|
||||
"alpha": "abcdefghijklmnopqrstuvwyz",
|
||||
"ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
|
||||
"digit": "0123456789",
|
||||
"0123456789": "digit",
|
||||
"special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?",
|
||||
"hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",
|
||||
"true": true,
|
||||
"false": false,
|
||||
"null": null,
|
||||
"array":[ ],
|
||||
"object":{ },
|
||||
"address": "50 St. James Street",
|
||||
"url": "http://www.JSON.org/",
|
||||
"comment": "// /* <!-- --",
|
||||
"# -- --> */": " ",
|
||||
" s p a c e d " :[1,2 , 3
|
||||
|
||||
,
|
||||
|
||||
4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7],
|
||||
"jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}",
|
||||
"quotes": "" \u0022 %22 0x22 034 "",
|
||||
"\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"
|
||||
: "A key can be any string"
|
||||
},
|
||||
0.5 ,98.6
|
||||
,
|
||||
99.44
|
||||
,
|
||||
|
||||
1066,
|
||||
1e1,
|
||||
0.1e1,
|
||||
1e-1,
|
||||
1e00,2e+00,2e-00
|
||||
,"rosebud"]
|
|
@ -0,0 +1 @@
|
|||
[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"JSON Test Pattern pass3": {
|
||||
"The outermost value": "must be an object or array.",
|
||||
"In this test": "It is an object."
|
||||
}
|
||||
}
|
79
main.cpp
79
main.cpp
|
@ -1,3 +1,10 @@
|
|||
#include <dirent.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <chrono>
|
||||
|
@ -1160,9 +1167,81 @@ void colorfuldisplay(ParsedJson & pj, const u8 * buf) {
|
|||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
void validate() {
|
||||
init_state_machine();// to be safe
|
||||
const char *dirname = "jsonchecker/"; // ugly, hardcoded, brittle
|
||||
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;
|
||||
}
|
||||
if (c == 0) {
|
||||
printf("nothing in dir %s \n", dirname);
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < c; i++) {
|
||||
const char *name = entry_list[i]->d_name;
|
||||
if (hasExtension(name, extension)) {
|
||||
size_t filelen = strlen(name);
|
||||
char *fullpath = (char *)malloc(dirlen + filelen + 1);
|
||||
strcpy(fullpath, dirname);
|
||||
strcpy(fullpath + dirlen, name);
|
||||
pair<u8 *, size_t> p = get_corpus(fullpath);
|
||||
ParsedJson pj;
|
||||
if (posix_memalign( (void **)&pj.structurals, 8, ROUNDUP_N(p.second, 64)/8)) {
|
||||
cerr << "Could not allocate memory\n";
|
||||
return;
|
||||
};
|
||||
pj.n_structural_indexes = 0;
|
||||
u32 max_structures = ROUNDUP_N(p.second, 64) + 2 + 7;
|
||||
pj.structural_indexes = new u32[max_structures];
|
||||
pj.nodes = new JsonNode[max_structures];
|
||||
find_structural_bits(p.first, p.second, pj);
|
||||
flatten_indexes(p.second, pj);
|
||||
bool isok = ape_machine(p.first, p.second, pj);
|
||||
if(isok)
|
||||
isok = shovel_machine(p.first, p.second, pj);
|
||||
if(startsWith("pass",name)) {
|
||||
if(!isok) printf("warning: file %s should pass but it fails.\n",name);
|
||||
}
|
||||
if(startsWith("fail",name)) {
|
||||
if(isok) printf("warning: file %s should fail but it passes.\n",name);
|
||||
}
|
||||
free(pj.structurals);
|
||||
free(p.first);
|
||||
delete[] pj.structural_indexes;
|
||||
delete[] pj.nodes;
|
||||
free(fullpath);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < c; ++i) free(entry_list[i]);
|
||||
free(entry_list);
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
if (argc != 2) {
|
||||
cerr << "Usage: " << argv[0] << " <jsonfile>\n";
|
||||
cout << "We are going to validate:\n" << std::endl;
|
||||
validate();
|
||||
exit(1);
|
||||
}
|
||||
pair<u8 *, size_t> p = get_corpus(argv[1]);
|
||||
|
|
Loading…
Reference in New Issue