Squashed 'src/univalue/' content from commit 9ef5b78
git-subtree-dir: src/univalue git-subtree-split: 9ef5b78c1998509b8f1ccd76f0aee15140e384be
This commit is contained in:
6
test/.gitignore
vendored
Normal file
6
test/.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
unitester
|
||||
test_json
|
||||
no_nul
|
||||
|
||||
*.trs
|
||||
*.log
|
||||
1
test/fail1.json
Normal file
1
test/fail1.json
Normal file
@@ -0,0 +1 @@
|
||||
"This is a string that never ends, yes it goes on and on, my friends.
|
||||
1
test/fail10.json
Normal file
1
test/fail10.json
Normal file
@@ -0,0 +1 @@
|
||||
{"Extra value after close": true} "misplaced quoted value"
|
||||
1
test/fail11.json
Normal file
1
test/fail11.json
Normal file
@@ -0,0 +1 @@
|
||||
{"Illegal expression": 1 + 2}
|
||||
1
test/fail12.json
Normal file
1
test/fail12.json
Normal file
@@ -0,0 +1 @@
|
||||
{"Illegal invocation": alert()}
|
||||
1
test/fail13.json
Normal file
1
test/fail13.json
Normal file
@@ -0,0 +1 @@
|
||||
{"Numbers cannot have leading zeroes": 013}
|
||||
1
test/fail14.json
Normal file
1
test/fail14.json
Normal file
@@ -0,0 +1 @@
|
||||
{"Numbers cannot be hex": 0x14}
|
||||
1
test/fail15.json
Normal file
1
test/fail15.json
Normal file
@@ -0,0 +1 @@
|
||||
["Illegal backslash escape: \x15"]
|
||||
1
test/fail16.json
Normal file
1
test/fail16.json
Normal file
@@ -0,0 +1 @@
|
||||
[\naked]
|
||||
1
test/fail17.json
Normal file
1
test/fail17.json
Normal file
@@ -0,0 +1 @@
|
||||
["Illegal backslash escape: \017"]
|
||||
1
test/fail18.json
Normal file
1
test/fail18.json
Normal file
@@ -0,0 +1 @@
|
||||
[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]
|
||||
1
test/fail19.json
Normal file
1
test/fail19.json
Normal file
@@ -0,0 +1 @@
|
||||
{"Missing colon" null}
|
||||
1
test/fail2.json
Normal file
1
test/fail2.json
Normal file
@@ -0,0 +1 @@
|
||||
["Unclosed array"
|
||||
1
test/fail20.json
Normal file
1
test/fail20.json
Normal file
@@ -0,0 +1 @@
|
||||
{"Double colon":: null}
|
||||
1
test/fail21.json
Normal file
1
test/fail21.json
Normal file
@@ -0,0 +1 @@
|
||||
{"Comma instead of colon", null}
|
||||
1
test/fail22.json
Normal file
1
test/fail22.json
Normal file
@@ -0,0 +1 @@
|
||||
["Colon instead of comma": false]
|
||||
1
test/fail23.json
Normal file
1
test/fail23.json
Normal file
@@ -0,0 +1 @@
|
||||
["Bad value", truth]
|
||||
1
test/fail24.json
Normal file
1
test/fail24.json
Normal file
@@ -0,0 +1 @@
|
||||
['single quote']
|
||||
1
test/fail25.json
Normal file
1
test/fail25.json
Normal file
@@ -0,0 +1 @@
|
||||
[" tab character in string "]
|
||||
1
test/fail26.json
Normal file
1
test/fail26.json
Normal file
@@ -0,0 +1 @@
|
||||
["tab\ character\ in\ string\ "]
|
||||
2
test/fail27.json
Normal file
2
test/fail27.json
Normal file
@@ -0,0 +1,2 @@
|
||||
["line
|
||||
break"]
|
||||
2
test/fail28.json
Normal file
2
test/fail28.json
Normal file
@@ -0,0 +1,2 @@
|
||||
["line\
|
||||
break"]
|
||||
1
test/fail29.json
Normal file
1
test/fail29.json
Normal file
@@ -0,0 +1 @@
|
||||
[0e]
|
||||
1
test/fail3.json
Normal file
1
test/fail3.json
Normal file
@@ -0,0 +1 @@
|
||||
{unquoted_key: "keys must be quoted"}
|
||||
1
test/fail30.json
Normal file
1
test/fail30.json
Normal file
@@ -0,0 +1 @@
|
||||
[0e+]
|
||||
1
test/fail31.json
Normal file
1
test/fail31.json
Normal file
@@ -0,0 +1 @@
|
||||
[0e+-1]
|
||||
1
test/fail32.json
Normal file
1
test/fail32.json
Normal file
@@ -0,0 +1 @@
|
||||
{"Comma instead if closing brace": true,
|
||||
1
test/fail33.json
Normal file
1
test/fail33.json
Normal file
@@ -0,0 +1 @@
|
||||
["mismatch"}
|
||||
1
test/fail34.json
Normal file
1
test/fail34.json
Normal file
@@ -0,0 +1 @@
|
||||
{} garbage
|
||||
1
test/fail35.json
Normal file
1
test/fail35.json
Normal file
@@ -0,0 +1 @@
|
||||
[ true true true [] [] [] ]
|
||||
1
test/fail36.json
Normal file
1
test/fail36.json
Normal file
@@ -0,0 +1 @@
|
||||
{"a":}
|
||||
1
test/fail37.json
Normal file
1
test/fail37.json
Normal file
@@ -0,0 +1 @@
|
||||
{"a":1 "b":2}
|
||||
1
test/fail38.json
Normal file
1
test/fail38.json
Normal file
@@ -0,0 +1 @@
|
||||
["\ud834"]
|
||||
1
test/fail39.json
Normal file
1
test/fail39.json
Normal file
@@ -0,0 +1 @@
|
||||
["\udd61"]
|
||||
1
test/fail4.json
Normal file
1
test/fail4.json
Normal file
@@ -0,0 +1 @@
|
||||
["extra comma",]
|
||||
1
test/fail40.json
Normal file
1
test/fail40.json
Normal file
@@ -0,0 +1 @@
|
||||
["揣。"]
|
||||
1
test/fail41.json
Normal file
1
test/fail41.json
Normal file
@@ -0,0 +1 @@
|
||||
["<22><><EFBFBD>"]
|
||||
BIN
test/fail42.json
Normal file
BIN
test/fail42.json
Normal file
Binary file not shown.
1
test/fail5.json
Normal file
1
test/fail5.json
Normal file
@@ -0,0 +1 @@
|
||||
["double extra comma",,]
|
||||
1
test/fail6.json
Normal file
1
test/fail6.json
Normal file
@@ -0,0 +1 @@
|
||||
[ , "<-- missing value"]
|
||||
1
test/fail7.json
Normal file
1
test/fail7.json
Normal file
@@ -0,0 +1 @@
|
||||
["Comma after the close"],
|
||||
1
test/fail8.json
Normal file
1
test/fail8.json
Normal file
@@ -0,0 +1 @@
|
||||
["Extra close"]]
|
||||
1
test/fail9.json
Normal file
1
test/fail9.json
Normal file
@@ -0,0 +1 @@
|
||||
{"Extra comma": true,}
|
||||
8
test/no_nul.cpp
Normal file
8
test/no_nul.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include "univalue.h"
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
char buf[] = "___[1,2,3]___";
|
||||
UniValue val;
|
||||
return val.read(buf + 3, 7) ? 0 : 1;
|
||||
}
|
||||
58
test/pass1.json
Normal file
58
test/pass1.json
Normal 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": "" \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"]
|
||||
1
test/pass2.json
Normal file
1
test/pass2.json
Normal file
@@ -0,0 +1 @@
|
||||
[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]
|
||||
6
test/pass3.json
Normal file
6
test/pass3.json
Normal 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."
|
||||
}
|
||||
}
|
||||
1
test/round1.json
Normal file
1
test/round1.json
Normal file
@@ -0,0 +1 @@
|
||||
["\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u007f"]
|
||||
1
test/round2.json
Normal file
1
test/round2.json
Normal file
@@ -0,0 +1 @@
|
||||
["a§■𐎒𝅘𝅥𝅯"]
|
||||
1
test/round3.json
Normal file
1
test/round3.json
Normal file
@@ -0,0 +1 @@
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
1
test/round4.json
Normal file
1
test/round4.json
Normal file
@@ -0,0 +1 @@
|
||||
7
|
||||
1
test/round5.json
Normal file
1
test/round5.json
Normal file
@@ -0,0 +1 @@
|
||||
true
|
||||
1
test/round6.json
Normal file
1
test/round6.json
Normal file
@@ -0,0 +1 @@
|
||||
false
|
||||
1
test/round7.json
Normal file
1
test/round7.json
Normal file
@@ -0,0 +1 @@
|
||||
null
|
||||
24
test/test_json.cpp
Normal file
24
test/test_json.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
// Test program that can be called by the JSON test suite at
|
||||
// https://github.com/nst/JSONTestSuite.
|
||||
//
|
||||
// It reads JSON input from stdin and exits with code 0 if it can be parsed
|
||||
// successfully. It also pretty prints the parsed JSON value to stdout.
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "univalue.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
UniValue val;
|
||||
if (val.read(string(istreambuf_iterator<char>(cin),
|
||||
istreambuf_iterator<char>()))) {
|
||||
cout << val.write(1 /* prettyIndent */, 4 /* indentLevel */) << endl;
|
||||
return 0;
|
||||
} else {
|
||||
cerr << "JSON Parse Error." << endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
169
test/unitester.cpp
Normal file
169
test/unitester.cpp
Normal file
@@ -0,0 +1,169 @@
|
||||
// Copyright 2014 BitPay Inc.
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <cassert>
|
||||
#include <string>
|
||||
#include "univalue.h"
|
||||
|
||||
#ifndef JSON_TEST_SRC
|
||||
#error JSON_TEST_SRC must point to test source directory
|
||||
#endif
|
||||
|
||||
#ifndef ARRAY_SIZE
|
||||
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
string srcdir(JSON_TEST_SRC);
|
||||
static bool test_failed = false;
|
||||
|
||||
#define d_assert(expr) { if (!(expr)) { test_failed = true; fprintf(stderr, "%s failed\n", filename.c_str()); } }
|
||||
#define f_assert(expr) { if (!(expr)) { test_failed = true; fprintf(stderr, "%s failed\n", __func__); } }
|
||||
|
||||
static std::string rtrim(std::string s)
|
||||
{
|
||||
s.erase(s.find_last_not_of(" \n\r\t")+1);
|
||||
return s;
|
||||
}
|
||||
|
||||
static void runtest(string filename, const string& jdata)
|
||||
{
|
||||
string prefix = filename.substr(0, 4);
|
||||
|
||||
bool wantPass = (prefix == "pass") || (prefix == "roun");
|
||||
bool wantFail = (prefix == "fail");
|
||||
bool wantRoundTrip = (prefix == "roun");
|
||||
assert(wantPass || wantFail);
|
||||
|
||||
UniValue val;
|
||||
bool testResult = val.read(jdata);
|
||||
|
||||
if (wantPass) {
|
||||
d_assert(testResult == true);
|
||||
} else {
|
||||
d_assert(testResult == false);
|
||||
}
|
||||
|
||||
if (wantRoundTrip) {
|
||||
std::string odata = val.write(0, 0);
|
||||
assert(odata == rtrim(jdata));
|
||||
}
|
||||
}
|
||||
|
||||
static void runtest_file(const char *filename_)
|
||||
{
|
||||
string basename(filename_);
|
||||
string filename = srcdir + "/" + basename;
|
||||
FILE *f = fopen(filename.c_str(), "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(basename, jdata);
|
||||
}
|
||||
|
||||
static const char *filenames[] = {
|
||||
"fail10.json",
|
||||
"fail11.json",
|
||||
"fail12.json",
|
||||
"fail13.json",
|
||||
"fail14.json",
|
||||
"fail15.json",
|
||||
"fail16.json",
|
||||
"fail17.json",
|
||||
//"fail18.json", // investigate
|
||||
"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",
|
||||
"fail34.json",
|
||||
"fail35.json",
|
||||
"fail36.json",
|
||||
"fail37.json",
|
||||
"fail38.json", // invalid unicode: only first half of surrogate pair
|
||||
"fail39.json", // invalid unicode: only second half of surrogate pair
|
||||
"fail40.json", // invalid unicode: broken UTF-8
|
||||
"fail41.json", // invalid unicode: unfinished UTF-8
|
||||
"fail42.json", // valid json with garbage following a nul byte
|
||||
"fail3.json",
|
||||
"fail4.json", // extra comma
|
||||
"fail5.json",
|
||||
"fail6.json",
|
||||
"fail7.json",
|
||||
"fail8.json",
|
||||
"fail9.json", // extra comma
|
||||
"pass1.json",
|
||||
"pass2.json",
|
||||
"pass3.json",
|
||||
"round1.json", // round-trip test
|
||||
"round2.json", // unicode
|
||||
"round3.json", // bare string
|
||||
"round4.json", // bare number
|
||||
"round5.json", // bare true
|
||||
"round6.json", // bare false
|
||||
"round7.json", // bare null
|
||||
};
|
||||
|
||||
// Test \u handling
|
||||
void unescape_unicode_test()
|
||||
{
|
||||
UniValue val;
|
||||
bool testResult;
|
||||
// Escaped ASCII (quote)
|
||||
testResult = val.read("[\"\\u0022\"]");
|
||||
f_assert(testResult);
|
||||
f_assert(val[0].get_str() == "\"");
|
||||
// Escaped Basic Plane character, two-byte UTF-8
|
||||
testResult = val.read("[\"\\u0191\"]");
|
||||
f_assert(testResult);
|
||||
f_assert(val[0].get_str() == "\xc6\x91");
|
||||
// Escaped Basic Plane character, three-byte UTF-8
|
||||
testResult = val.read("[\"\\u2191\"]");
|
||||
f_assert(testResult);
|
||||
f_assert(val[0].get_str() == "\xe2\x86\x91");
|
||||
// Escaped Supplementary Plane character U+1d161
|
||||
testResult = val.read("[\"\\ud834\\udd61\"]");
|
||||
f_assert(testResult);
|
||||
f_assert(val[0].get_str() == "\xf0\x9d\x85\xa1");
|
||||
}
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
for (unsigned int fidx = 0; fidx < ARRAY_SIZE(filenames); fidx++) {
|
||||
runtest_file(filenames[fidx]);
|
||||
}
|
||||
|
||||
unescape_unicode_test();
|
||||
|
||||
return test_failed ? 1 : 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user