[Pkg-bitcoin-commits] [libunivalue] 02/76: Import vjson tests, and test harness.
Jonas Smedegaard
dr at jones.dk
Mon Apr 4 09:18:26 UTC 2016
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository libunivalue.
commit ab0d0f7dbd2feabd892b682d2e8dd1fdc792f258
Author: Jeff Garzik <jgarzik at bitpay.com>
Date: Thu Jun 12 17:34:10 2014 -0400
Import vjson tests, and test harness.
---
.gitignore | 2 +
Makefile.am | 45 +++++++++++++++++++++
fail1.json | 1 +
fail10.json | 1 +
fail11.json | 1 +
fail12.json | 1 +
fail13.json | 1 +
fail14.json | 1 +
fail15.json | 1 +
fail16.json | 1 +
fail17.json | 1 +
fail18.json | 1 +
fail19.json | 1 +
fail2.json | 1 +
fail20.json | 1 +
fail21.json | 1 +
fail22.json | 1 +
fail23.json | 1 +
fail24.json | 1 +
fail25.json | 1 +
fail26.json | 1 +
fail27.json | 2 +
fail28.json | 2 +
fail29.json | 1 +
fail3.json | 1 +
fail30.json | 1 +
fail31.json | 1 +
fail32.json | 1 +
fail33.json | 1 +
fail4.json | 1 +
fail5.json | 1 +
fail6.json | 1 +
fail7.json | 1 +
fail8.json | 1 +
fail9.json | 1 +
pass1.json | 58 +++++++++++++++++++++++++++
pass2.json | 1 +
pass3.json | 6 +++
unitester.cpp | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
univalue.h | 3 ++
univalue_read.cpp | 24 +++++++++---
41 files changed, 284 insertions(+), 5 deletions(-)
diff --git a/.gitignore b/.gitignore
index e14b9d9..6770a50 100644
--- a/.gitignore
+++ b/.gitignore
@@ -13,6 +13,8 @@ install-sh
missing
stamp-h1
univalue-config.h*
+test-driver
*.o
libunivalue.a
+unitester
diff --git a/Makefile.am b/Makefile.am
index 7d7bc3d..74f58e6 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -8,3 +8,48 @@ libunivalue_a_SOURCES = \
univalue_read.cpp \
univalue_write.cpp
+TESTS = unitester
+
+noinst_PROGRAMS = unitester
+
+unitester_SOURCES = unitester.cpp
+unitester_LDADD = libunivalue.a
+
+EXTRA_DIST = \
+ fail10.json \
+ fail11.json \
+ fail12.json \
+ fail13.json \
+ fail14.json \
+ fail15.json \
+ fail16.json \
+ fail17.json \
+ fail18.json \
+ fail19.json \
+ fail1.json \
+ fail20.json \
+ fail21.json \
+ fail22.json \
+ fail23.json \
+ fail24.json \
+ fail25.json \
+ fail26.json \
+ fail27.json \
+ fail28.json \
+ fail29.json \
+ fail2.json \
+ fail30.json \
+ fail31.json \
+ fail32.json \
+ fail33.json \
+ fail3.json \
+ fail4.json \
+ fail5.json \
+ fail6.json \
+ fail7.json \
+ fail8.json \
+ fail9.json \
+ pass1.json \
+ pass2.json \
+ pass3.json
+
diff --git a/fail1.json b/fail1.json
new file mode 100644
index 0000000..6216b86
--- /dev/null
+++ b/fail1.json
@@ -0,0 +1 @@
+"A JSON payload should be an object or array, not a string."
\ No newline at end of file
diff --git a/fail10.json b/fail10.json
new file mode 100644
index 0000000..5d8c004
--- /dev/null
+++ b/fail10.json
@@ -0,0 +1 @@
+{"Extra value after close": true} "misplaced quoted value"
\ No newline at end of file
diff --git a/fail11.json b/fail11.json
new file mode 100644
index 0000000..76eb95b
--- /dev/null
+++ b/fail11.json
@@ -0,0 +1 @@
+{"Illegal expression": 1 + 2}
\ No newline at end of file
diff --git a/fail12.json b/fail12.json
new file mode 100644
index 0000000..77580a4
--- /dev/null
+++ b/fail12.json
@@ -0,0 +1 @@
+{"Illegal invocation": alert()}
\ No newline at end of file
diff --git a/fail13.json b/fail13.json
new file mode 100644
index 0000000..379406b
--- /dev/null
+++ b/fail13.json
@@ -0,0 +1 @@
+{"Numbers cannot have leading zeroes": 013}
\ No newline at end of file
diff --git a/fail14.json b/fail14.json
new file mode 100644
index 0000000..0ed366b
--- /dev/null
+++ b/fail14.json
@@ -0,0 +1 @@
+{"Numbers cannot be hex": 0x14}
\ No newline at end of file
diff --git a/fail15.json b/fail15.json
new file mode 100644
index 0000000..fc8376b
--- /dev/null
+++ b/fail15.json
@@ -0,0 +1 @@
+["Illegal backslash escape: \x15"]
\ No newline at end of file
diff --git a/fail16.json b/fail16.json
new file mode 100644
index 0000000..3fe21d4
--- /dev/null
+++ b/fail16.json
@@ -0,0 +1 @@
+[\naked]
\ No newline at end of file
diff --git a/fail17.json b/fail17.json
new file mode 100644
index 0000000..62b9214
--- /dev/null
+++ b/fail17.json
@@ -0,0 +1 @@
+["Illegal backslash escape: \017"]
\ No newline at end of file
diff --git a/fail18.json b/fail18.json
new file mode 100644
index 0000000..edac927
--- /dev/null
+++ b/fail18.json
@@ -0,0 +1 @@
+[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]
\ No newline at end of file
diff --git a/fail19.json b/fail19.json
new file mode 100644
index 0000000..3b9c46f
--- /dev/null
+++ b/fail19.json
@@ -0,0 +1 @@
+{"Missing colon" null}
\ No newline at end of file
diff --git a/fail2.json b/fail2.json
new file mode 100644
index 0000000..6b7c11e
--- /dev/null
+++ b/fail2.json
@@ -0,0 +1 @@
+["Unclosed array"
\ No newline at end of file
diff --git a/fail20.json b/fail20.json
new file mode 100644
index 0000000..27c1af3
--- /dev/null
+++ b/fail20.json
@@ -0,0 +1 @@
+{"Double colon":: null}
\ No newline at end of file
diff --git a/fail21.json b/fail21.json
new file mode 100644
index 0000000..6247457
--- /dev/null
+++ b/fail21.json
@@ -0,0 +1 @@
+{"Comma instead of colon", null}
\ No newline at end of file
diff --git a/fail22.json b/fail22.json
new file mode 100644
index 0000000..a775258
--- /dev/null
+++ b/fail22.json
@@ -0,0 +1 @@
+["Colon instead of comma": false]
\ No newline at end of file
diff --git a/fail23.json b/fail23.json
new file mode 100644
index 0000000..494add1
--- /dev/null
+++ b/fail23.json
@@ -0,0 +1 @@
+["Bad value", truth]
\ No newline at end of file
diff --git a/fail24.json b/fail24.json
new file mode 100644
index 0000000..caff239
--- /dev/null
+++ b/fail24.json
@@ -0,0 +1 @@
+['single quote']
\ No newline at end of file
diff --git a/fail25.json b/fail25.json
new file mode 100644
index 0000000..8b7ad23
--- /dev/null
+++ b/fail25.json
@@ -0,0 +1 @@
+[" tab character in string "]
\ No newline at end of file
diff --git a/fail26.json b/fail26.json
new file mode 100644
index 0000000..845d26a
--- /dev/null
+++ b/fail26.json
@@ -0,0 +1 @@
+["tab\ character\ in\ string\ "]
\ No newline at end of file
diff --git a/fail27.json b/fail27.json
new file mode 100644
index 0000000..6b01a2c
--- /dev/null
+++ b/fail27.json
@@ -0,0 +1,2 @@
+["line
+break"]
\ No newline at end of file
diff --git a/fail28.json b/fail28.json
new file mode 100644
index 0000000..621a010
--- /dev/null
+++ b/fail28.json
@@ -0,0 +1,2 @@
+["line\
+break"]
\ No newline at end of file
diff --git a/fail29.json b/fail29.json
new file mode 100644
index 0000000..47ec421
--- /dev/null
+++ b/fail29.json
@@ -0,0 +1 @@
+[0e]
\ No newline at end of file
diff --git a/fail3.json b/fail3.json
new file mode 100644
index 0000000..168c81e
--- /dev/null
+++ b/fail3.json
@@ -0,0 +1 @@
+{unquoted_key: "keys must be quoted"}
\ No newline at end of file
diff --git a/fail30.json b/fail30.json
new file mode 100644
index 0000000..8ab0bc4
--- /dev/null
+++ b/fail30.json
@@ -0,0 +1 @@
+[0e+]
\ No newline at end of file
diff --git a/fail31.json b/fail31.json
new file mode 100644
index 0000000..1cce602
--- /dev/null
+++ b/fail31.json
@@ -0,0 +1 @@
+[0e+-1]
\ No newline at end of file
diff --git a/fail32.json b/fail32.json
new file mode 100644
index 0000000..45cba73
--- /dev/null
+++ b/fail32.json
@@ -0,0 +1 @@
+{"Comma instead if closing brace": true,
\ No newline at end of file
diff --git a/fail33.json b/fail33.json
new file mode 100644
index 0000000..ca5eb19
--- /dev/null
+++ b/fail33.json
@@ -0,0 +1 @@
+["mismatch"}
\ No newline at end of file
diff --git a/fail4.json b/fail4.json
new file mode 100644
index 0000000..9de168b
--- /dev/null
+++ b/fail4.json
@@ -0,0 +1 @@
+["extra comma",]
\ No newline at end of file
diff --git a/fail5.json b/fail5.json
new file mode 100644
index 0000000..ddf3ce3
--- /dev/null
+++ b/fail5.json
@@ -0,0 +1 @@
+["double extra comma",,]
\ No newline at end of file
diff --git a/fail6.json b/fail6.json
new file mode 100644
index 0000000..ed91580
--- /dev/null
+++ b/fail6.json
@@ -0,0 +1 @@
+[ , "<-- missing value"]
\ No newline at end of file
diff --git a/fail7.json b/fail7.json
new file mode 100644
index 0000000..8a96af3
--- /dev/null
+++ b/fail7.json
@@ -0,0 +1 @@
+["Comma after the close"],
\ No newline at end of file
diff --git a/fail8.json b/fail8.json
new file mode 100644
index 0000000..b28479c
--- /dev/null
+++ b/fail8.json
@@ -0,0 +1 @@
+["Extra close"]]
\ No newline at end of file
diff --git a/fail9.json b/fail9.json
new file mode 100644
index 0000000..5815574
--- /dev/null
+++ b/fail9.json
@@ -0,0 +1 @@
+{"Extra comma": true,}
\ No newline at end of file
diff --git a/pass1.json b/pass1.json
new file mode 100644
index 0000000..70e2685
--- /dev/null
+++ b/pass1.json
@@ -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"]
\ No newline at end of file
diff --git a/pass2.json b/pass2.json
new file mode 100644
index 0000000..d3c63c7
--- /dev/null
+++ b/pass2.json
@@ -0,0 +1 @@
+[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]
\ No newline at end of file
diff --git a/pass3.json b/pass3.json
new file mode 100644
index 0000000..4528d51
--- /dev/null
+++ b/pass3.json
@@ -0,0 +1,6 @@
+{
+ "JSON Test Pattern pass3": {
+ "The outermost value": "must be an object or array.",
+ "In this test": "It is an object."
+ }
+}
diff --git a/unitester.cpp b/unitester.cpp
new file mode 100644
index 0000000..b796ed2
--- /dev/null
+++ b/unitester.cpp
@@ -0,0 +1,115 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <cassert>
+#include <string>
+#include "univalue.h"
+
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+#endif
+
+using namespace std;
+
+static void runtest(const char *filename, const string& jdata)
+{
+ fprintf(stderr, "test %s\n", filename);
+
+ string filename_(filename);
+ string prefix = filename_.substr(0, 4);
+
+ bool wantPass = (prefix == "pass");
+ bool wantFail = (prefix == "fail");
+ assert(wantPass || wantFail);
+
+ bool testResult = true;
+ try {
+ UniValue val;
+ val.read(jdata);
+ }
+ catch (std::exception& e) {
+ string strPrint = string("error: ") + e.what();
+ fprintf(stderr, "%s\n", strPrint.c_str());
+ testResult = false;
+ }
+ catch (...) {
+ string strPrint = string("unknown exception");
+ fprintf(stderr, "%s\n", strPrint.c_str());
+ testResult = false;
+ }
+
+ if (wantPass) {
+ assert(testResult == true);
+ } else {
+ assert(testResult == false);
+ }
+}
+
+static void runtest_file(const char *filename)
+{
+ FILE *f = fopen(filename, "r");
+ assert(f != NULL);
+
+ string jdata;
+
+ char buf[4096];
+ while (!feof(f)) {
+ int bread = fread(buf, 1, sizeof(buf), f);
+ assert(!ferror(f));
+
+ string s(buf, bread);
+ jdata += s;
+ }
+
+ assert(!ferror(f));
+ fclose(f);
+
+ runtest(filename, jdata);
+}
+
+static const char *filenames[] = {
+ "fail10.json",
+ "fail11.json",
+ "fail12.json",
+ "fail13.json",
+ "fail14.json",
+ "fail15.json",
+ "fail16.json",
+ "fail17.json",
+ "fail18.json",
+ "fail19.json",
+ "fail1.json",
+ "fail20.json",
+ "fail21.json",
+ "fail22.json",
+ "fail23.json",
+ "fail24.json",
+ "fail25.json",
+ "fail26.json",
+ "fail27.json",
+ "fail28.json",
+ "fail29.json",
+ "fail2.json",
+ "fail30.json",
+ "fail31.json",
+ "fail32.json",
+ //"fail33.json",
+ "fail3.json",
+ "fail4.json",
+ "fail5.json",
+ "fail6.json",
+ "fail7.json",
+ "fail8.json",
+ "fail9.json",
+ "pass1.json",
+ "pass2.json",
+ "pass3.json",
+};
+
+int main (int argc, char *argv[])
+{
+ for (unsigned int fidx = 0; fidx < ARRAY_SIZE(filenames); fidx++) {
+ runtest_file(filenames[fidx]);
+ }
+}
+
diff --git a/univalue.h b/univalue.h
index 5260f71..189674a 100644
--- a/univalue.h
+++ b/univalue.h
@@ -63,6 +63,9 @@ public:
unsigned int indentLevel = 0);
void read(const char *raw);
+ void read(std::string rawStr) {
+ read(rawStr.c_str());
+ }
private:
UniValue::VType typ;
diff --git a/univalue_read.cpp b/univalue_read.cpp
index 63d5f9b..af6b1f2 100644
--- a/univalue_read.cpp
+++ b/univalue_read.cpp
@@ -13,6 +13,7 @@ void UniValue::read(const char *raw)
clear();
bool expectName = false;
+ bool expectColon = false;
vector<UniValue*> stack;
while (*raw) {
@@ -42,7 +43,7 @@ void UniValue::read(const char *raw)
case '}':
case ']': {
- if (!stack.size())
+ if (!stack.size() || expectColon)
throw runtime_error("json parse: unexpected }]");
VType utyp = (*raw == '}' ? VOBJ : VARR);
@@ -58,19 +59,21 @@ void UniValue::read(const char *raw)
}
case ':': {
- if (!stack.size() || expectName)
+ if (!stack.size() || expectName || !expectColon)
throw runtime_error("json parse: : stack empty or want name");
UniValue *top = stack.back();
if (top->getType() != VOBJ)
throw runtime_error("json parse: : parent not object");
+ expectColon = false;
+
raw++;
break;
}
case ',': {
- if (!stack.size() || expectName)
+ if (!stack.size() || expectName || expectColon)
throw runtime_error("json parse: , stack empty or want name");
UniValue *top = stack.back();
@@ -84,7 +87,7 @@ void UniValue::read(const char *raw)
case 'n':
case 't':
case 'f': {
- if (!stack.size() || expectName)
+ if (!stack.size() || expectName || expectColon)
throw runtime_error("json parse: ntf stack empty or want name");
VType utyp;
@@ -118,13 +121,20 @@ void UniValue::read(const char *raw)
case '7':
case '8':
case '9': {
- if (!stack.size() || expectName)
+ if (!stack.size() || expectName || expectColon)
throw runtime_error("json parse digits: stack empty or want name");
// part 1: int
string numStr;
const char *first = raw;
+
+ const char *firstDigit = first;
+ if (!isdigit(*firstDigit))
+ firstDigit++;
+ if (*firstDigit == '0')
+ throw runtime_error("json parse digits 1.5: first digit bad");
+
numStr += *raw; // copy first char
raw++;
@@ -226,6 +236,7 @@ void UniValue::read(const char *raw)
if (expectName) {
top->keys.push_back(valStr);
expectName = false;
+ expectColon = true;
} else {
UniValue tmpVal(VSTR, valStr);
top->values.push_back(tmpVal);
@@ -241,6 +252,9 @@ void UniValue::read(const char *raw)
case '\n':
raw++; // skip whitespace
break;
+
+ default:
+ throw runtime_error("json parse string: illegal expression");
}
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-bitcoin/libunivalue.git
More information about the Pkg-bitcoin-commits
mailing list