Adding validating step.

This commit is contained in:
Daniel Lemire 2018-07-25 22:59:40 -04:00
parent 460b971473
commit 93f4e1d9b4
38 changed files with 181 additions and 1 deletions

View File

@ -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

View File

@ -0,0 +1 @@
"A JSON payload should be an object or array, not a string."

1
jsonchecker/fail02.json Normal file
View File

@ -0,0 +1 @@
["Unclosed array"

1
jsonchecker/fail03.json Normal file
View File

@ -0,0 +1 @@
{unquoted_key: "keys must be quoted"}

1
jsonchecker/fail04.json Normal file
View File

@ -0,0 +1 @@
["extra comma",]

1
jsonchecker/fail05.json Normal file
View File

@ -0,0 +1 @@
["double extra comma",,]

1
jsonchecker/fail06.json Normal file
View File

@ -0,0 +1 @@
[ , "<-- missing value"]

1
jsonchecker/fail07.json Normal file
View File

@ -0,0 +1 @@
["Comma after the close"],

1
jsonchecker/fail08.json Normal file
View File

@ -0,0 +1 @@
["Extra close"]]

1
jsonchecker/fail09.json Normal file
View File

@ -0,0 +1 @@
{"Extra comma": true,}

1
jsonchecker/fail10.json Normal file
View File

@ -0,0 +1 @@
{"Extra value after close": true} "misplaced quoted value"

1
jsonchecker/fail11.json Normal file
View File

@ -0,0 +1 @@
{"Illegal expression": 1 + 2}

1
jsonchecker/fail12.json Normal file
View File

@ -0,0 +1 @@
{"Illegal invocation": alert()}

1
jsonchecker/fail13.json Normal file
View File

@ -0,0 +1 @@
{"Numbers cannot have leading zeroes": 013}

1
jsonchecker/fail14.json Normal file
View File

@ -0,0 +1 @@
{"Numbers cannot be hex": 0x14}

1
jsonchecker/fail15.json Normal file
View File

@ -0,0 +1 @@
["Illegal backslash escape: \x15"]

1
jsonchecker/fail16.json Normal file
View File

@ -0,0 +1 @@
[\naked]

1
jsonchecker/fail17.json Normal file
View File

@ -0,0 +1 @@
["Illegal backslash escape: \017"]

View File

@ -0,0 +1 @@
[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]

1
jsonchecker/fail19.json Normal file
View File

@ -0,0 +1 @@
{"Missing colon" null}

1
jsonchecker/fail20.json Normal file
View File

@ -0,0 +1 @@
{"Double colon":: null}

1
jsonchecker/fail21.json Normal file
View File

@ -0,0 +1 @@
{"Comma instead of colon", null}

1
jsonchecker/fail22.json Normal file
View File

@ -0,0 +1 @@
["Colon instead of comma": false]

1
jsonchecker/fail23.json Normal file
View File

@ -0,0 +1 @@
["Bad value", truth]

1
jsonchecker/fail24.json Normal file
View File

@ -0,0 +1 @@
['single quote']

1
jsonchecker/fail25.json Normal file
View File

@ -0,0 +1 @@
[" tab character in string "]

1
jsonchecker/fail26.json Normal file
View File

@ -0,0 +1 @@
["tab\ character\ in\ string\ "]

2
jsonchecker/fail27.json Normal file
View File

@ -0,0 +1,2 @@
["line
break"]

2
jsonchecker/fail28.json Normal file
View File

@ -0,0 +1,2 @@
["line\
break"]

1
jsonchecker/fail29.json Normal file
View File

@ -0,0 +1 @@
[0e]

1
jsonchecker/fail30.json Normal file
View File

@ -0,0 +1 @@
[0e+]

1
jsonchecker/fail31.json Normal file
View File

@ -0,0 +1 @@
[0e+-1]

1
jsonchecker/fail32.json Normal file
View File

@ -0,0 +1 @@
{"Comma instead if closing brace": true,

1
jsonchecker/fail33.json Normal file
View File

@ -0,0 +1 @@
["mismatch"}

58
jsonchecker/pass01.json Normal file
View File

@ -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": "&#34; \u0022 %22 0x22 034 &#x22;",
"\/\\\"\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"]

1
jsonchecker/pass02.json Normal file
View File

@ -0,0 +1 @@
[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]

6
jsonchecker/pass03.json Normal file
View File

@ -0,0 +1,6 @@
{
"JSON Test Pattern pass3": {
"The outermost value": "must be an object or array.",
"In this test": "It is an object."
}
}

View File

@ -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]);