Fabcoin Core  0.16.2
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 http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <cassert>
9 #include <string>
10 #include "univalue.h"
11 
12 #ifndef JSON_TEST_SRC
13 #error JSON_TEST_SRC must point to test source directory
14 #endif
15 
16 #ifndef ARRAY_SIZE
17 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
18 #endif
19 
20 using namespace std;
21 string srcdir(JSON_TEST_SRC);
22 static bool test_failed = false;
23 
24 #define d_assert(expr) { if (!(expr)) { test_failed = true; fprintf(stderr, "%s failed\n", filename.c_str()); } }
25 #define f_assert(expr) { if (!(expr)) { test_failed = true; fprintf(stderr, "%s failed\n", __func__); } }
26 
27 static std::string rtrim(std::string s)
28 {
29  s.erase(s.find_last_not_of(" \n\r\t")+1);
30  return s;
31 }
32 
33 static void runtest(string filename, const string& jdata)
34 {
35  string prefix = filename.substr(0, 4);
36 
37  bool wantPass = (prefix == "pass") || (prefix == "roun");
38  bool wantFail = (prefix == "fail");
39  bool wantRoundTrip = (prefix == "roun");
40  assert(wantPass || wantFail);
41 
42  UniValue val;
43  bool testResult = val.read(jdata);
44 
45  if (wantPass) {
46  d_assert(testResult == true);
47  } else {
48  d_assert(testResult == false);
49  }
50 
51  if (wantRoundTrip) {
52  std::string odata = val.write(0, 0);
53  assert(odata == rtrim(jdata));
54  }
55 }
56 
57 static void runtest_file(const char *filename_)
58 {
59  string basename(filename_);
60  string filename = srcdir + "/" + basename;
61  FILE *f = fopen(filename.c_str(), "r");
62  assert(f != NULL);
63 
64  string jdata;
65 
66  char buf[4096];
67  while (!feof(f)) {
68  int bread = fread(buf, 1, sizeof(buf), f);
69  assert(!ferror(f));
70 
71  string s(buf, bread);
72  jdata += s;
73  }
74 
75  assert(!ferror(f));
76  fclose(f);
77 
78  runtest(basename, jdata);
79 }
80 
81 static const char *filenames[] = {
82  "fail10.json",
83  "fail11.json",
84  "fail12.json",
85  "fail13.json",
86  "fail14.json",
87  "fail15.json",
88  "fail16.json",
89  "fail17.json",
90  //"fail18.json", // investigate
91  "fail19.json",
92  "fail1.json",
93  "fail20.json",
94  "fail21.json",
95  "fail22.json",
96  "fail23.json",
97  "fail24.json",
98  "fail25.json",
99  "fail26.json",
100  "fail27.json",
101  "fail28.json",
102  "fail29.json",
103  "fail2.json",
104  "fail30.json",
105  "fail31.json",
106  "fail32.json",
107  "fail33.json",
108  "fail34.json",
109  "fail35.json",
110  "fail36.json",
111  "fail37.json",
112  "fail38.json", // invalid unicode: only first half of surrogate pair
113  "fail39.json", // invalid unicode: only second half of surrogate pair
114  "fail40.json", // invalid unicode: broken UTF-8
115  "fail41.json", // invalid unicode: unfinished UTF-8
116  "fail42.json", // valid json with garbage following a nul byte
117  "fail44.json", // unterminated string
118  "fail3.json",
119  "fail4.json", // extra comma
120  "fail5.json",
121  "fail6.json",
122  "fail7.json",
123  "fail8.json",
124  "fail9.json", // extra comma
125  "pass1.json",
126  "pass2.json",
127  "pass3.json",
128  "round1.json", // round-trip test
129  "round2.json", // unicode
130  "round3.json", // bare string
131  "round4.json", // bare number
132  "round5.json", // bare true
133  "round6.json", // bare false
134  "round7.json", // bare null
135 };
136 
137 // Test \u handling
139 {
140  UniValue val;
141  bool testResult;
142  // Escaped ASCII (quote)
143  testResult = val.read("[\"\\u0022\"]");
144  f_assert(testResult);
145  f_assert(val[0].get_str() == "\"");
146  // Escaped Basic Plane character, two-byte UTF-8
147  testResult = val.read("[\"\\u0191\"]");
148  f_assert(testResult);
149  f_assert(val[0].get_str() == "\xc6\x91");
150  // Escaped Basic Plane character, three-byte UTF-8
151  testResult = val.read("[\"\\u2191\"]");
152  f_assert(testResult);
153  f_assert(val[0].get_str() == "\xe2\x86\x91");
154  // Escaped Supplementary Plane character U+1d161
155  testResult = val.read("[\"\\ud834\\udd61\"]");
156  f_assert(testResult);
157  f_assert(val[0].get_str() == "\xf0\x9d\x85\xa1");
158 }
159 
160 int main (int argc, char *argv[])
161 {
162  for (unsigned int fidx = 0; fidx < ARRAY_SIZE(filenames); fidx++) {
163  runtest_file(filenames[fidx]);
164  }
165 
167 
168  return test_failed ? 1 : 0;
169 }
170 
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:5
int main(int argc, char *argv[])
Definition: unitester.cpp:160
#define ARRAY_SIZE(arr)
Definition: unitester.cpp:17
bool read(const char *raw, size_t len)
const char * prefix
Definition: rest.cpp:623
std::hash for asio::adress
Definition: Common.h:323
assert(len-trim+(2 *lenIndices)<=WIDTH)
#define d_assert(expr)
Definition: unitester.cpp:24
void unescape_unicode_test()
Definition: unitester.cpp:138
#define f(x)
Definition: gost.cpp:57
string srcdir(JSON_TEST_SRC)
#define f_assert(expr)
Definition: unitester.cpp:25
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
std::string get_str(std::string::const_iterator begin, std::string::const_iterator end)