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