Bitcoin ABC 0.30.7
P2P Digital Currency
unitester.cpp
Go to the documentation of this file.
1// Copyright 2014 BitPay Inc.
2// Distributed under the MIT/X11 software license, see the accompanying
3// file COPYING or https://opensource.org/licenses/mit-license.php.
4
5#include <univalue.h>
6
7#include <cassert>
8#include <cstdio>
9#include <string>
10
11#ifndef JSON_TEST_SRC
12#error JSON_TEST_SRC must point to test source directory
13#endif
14
15std::string srcdir(JSON_TEST_SRC);
16
17static std::string rtrim(std::string s) {
18 s.erase(s.find_last_not_of(" \n\r\t") + 1);
19 return s;
20}
21
22static void runtest(std::string filename, const std::string &jdata) {
23 std::string prefix = filename.substr(0, 4);
24
25 bool wantPass = (prefix == "pass") || (prefix == "roun");
26 bool wantFail = (prefix == "fail");
27 bool wantRoundTrip = (prefix == "roun");
28 assert(wantPass || wantFail);
29
30 UniValue val;
31 bool testResult = val.read(jdata);
32
33 if (wantPass) {
34 assert(testResult == true);
35 } else {
36 assert(testResult == false);
37 }
38
39 if (wantRoundTrip) {
40 std::string odata = val.write(0, 0);
41 assert(odata == rtrim(jdata));
42 }
43}
44
45static void runtest_file(const char *filename_) {
46 std::string basename(filename_);
47 std::string filename = srcdir + "/" + basename;
48 FILE *f = fopen(filename.c_str(), "r");
49 assert(f != nullptr);
50
51 std::string jdata;
52
53 char buf[4096];
54 while (!feof(f)) {
55 int bread = fread(buf, 1, sizeof(buf), f);
56 assert(!ferror(f));
57
58 std::string s(buf, bread);
59 jdata += s;
60 }
61
62 assert(!ferror(f));
63 fclose(f);
64
65 runtest(basename, jdata);
66}
67
68static const char *filenames[] = {
69 "fail10.json", "fail11.json", "fail12.json", "fail13.json", "fail14.json",
70 "fail15.json", "fail16.json", "fail17.json",
71 //"fail18.json", // investigate
72 "fail19.json", "fail1.json", "fail20.json", "fail21.json", "fail22.json",
73 "fail23.json", "fail24.json", "fail25.json", "fail26.json", "fail27.json",
74 "fail28.json", "fail29.json", "fail2.json", "fail30.json", "fail31.json",
75 "fail32.json", "fail33.json", "fail34.json", "fail35.json", "fail36.json",
76 "fail37.json",
77 "fail38.json", // invalid unicode: only first half of surrogate pair
78 "fail39.json", // invalid unicode: only second half of surrogate pair
79 "fail40.json", // invalid unicode: broken UTF-8
80 "fail41.json", // invalid unicode: unfinished UTF-8
81 "fail42.json", // valid json with garbage following a nul byte
82 "fail44.json", // unterminated string
83 "fail45.json", // nested beyond max depth
84 "fail3.json",
85 "fail4.json", // extra comma
86 "fail5.json", "fail6.json", "fail7.json", "fail8.json",
87 "fail9.json", // extra comma
88 "pass1.json", "pass2.json", "pass3.json", "pass4.json",
89 "round1.json", // round-trip test
90 "round2.json", // unicode
91 "round3.json", // bare string
92 "round4.json", // bare number
93 "round5.json", // bare true
94 "round6.json", // bare false
95 "round7.json", // bare null
96};
97
98// Test \u handling
100 UniValue val;
101 bool testResult;
102 // Escaped ASCII (quote)
103 testResult = val.read("[\"\\u0022\"]");
104 assert(testResult);
105 assert(val[0].get_str() == "\"");
106 // Escaped Basic Plane character, two-byte UTF-8
107 testResult = val.read("[\"\\u0191\"]");
108 assert(testResult);
109 assert(val[0].get_str() == "\xc6\x91");
110 // Escaped Basic Plane character, three-byte UTF-8
111 testResult = val.read("[\"\\u2191\"]");
112 assert(testResult);
113 assert(val[0].get_str() == "\xe2\x86\x91");
114 // Escaped Supplementary Plane character U+1d161
115 testResult = val.read("[\"\\ud834\\udd61\"]");
116 assert(testResult);
117 assert(val[0].get_str() == "\xf0\x9d\x85\xa1");
118}
119
121 char buf[] = "___[1,2,3]___";
122 UniValue val;
123 assert(val.read({buf + 3, 7}));
124}
125
126int main(int argc, char *argv[]) {
127 for (const auto &f : filenames) {
128 runtest_file(f);
129 }
130
132 no_nul_test();
133
134 return 0;
135}
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
bool read(std::string_view raw)
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:30
const char * prefix
Definition: rest.cpp:817
int main(int argc, char *argv[])
Definition: unitester.cpp:126
static const char * filenames[]
Definition: unitester.cpp:68
static std::string rtrim(std::string s)
Definition: unitester.cpp:17
void unescape_unicode_test()
Definition: unitester.cpp:99
static void runtest(std::string filename, const std::string &jdata)
Definition: unitester.cpp:22
static void runtest_file(const char *filename_)
Definition: unitester.cpp:45
void no_nul_test()
Definition: unitester.cpp:120
std::string srcdir(JSON_TEST_SRC)
assert(!tx.IsCoinBase())