|
1 | 1 | // Copyright 2014 BitPay Inc. |
2 | | -// Distributed under the MIT/X11 software license, see the accompanying |
| 2 | +// Copyright (c) 2015-present The Bitcoin Core developers |
| 3 | +// Distributed under the MIT software license, see the accompanying |
3 | 4 | // file COPYING or https://opensource.org/licenses/mit-license.php. |
4 | 5 |
|
5 | 6 | #include <univalue.h> |
6 | 7 |
|
| 8 | +#include <univalue/test/fail1.json.h> |
| 9 | +#include <univalue/test/fail10.json.h> |
| 10 | +#include <univalue/test/fail11.json.h> |
| 11 | +#include <univalue/test/fail12.json.h> |
| 12 | +#include <univalue/test/fail13.json.h> |
| 13 | +#include <univalue/test/fail14.json.h> |
| 14 | +#include <univalue/test/fail15.json.h> |
| 15 | +#include <univalue/test/fail16.json.h> |
| 16 | +#include <univalue/test/fail17.json.h> |
| 17 | +#include <univalue/test/fail18.json.h> |
| 18 | +#include <univalue/test/fail19.json.h> |
| 19 | +#include <univalue/test/fail2.json.h> |
| 20 | +#include <univalue/test/fail20.json.h> |
| 21 | +#include <univalue/test/fail21.json.h> |
| 22 | +#include <univalue/test/fail22.json.h> |
| 23 | +#include <univalue/test/fail23.json.h> |
| 24 | +#include <univalue/test/fail24.json.h> |
| 25 | +#include <univalue/test/fail25.json.h> |
| 26 | +#include <univalue/test/fail26.json.h> |
| 27 | +#include <univalue/test/fail27.json.h> |
| 28 | +#include <univalue/test/fail28.json.h> |
| 29 | +#include <univalue/test/fail29.json.h> |
| 30 | +#include <univalue/test/fail3.json.h> |
| 31 | +#include <univalue/test/fail30.json.h> |
| 32 | +#include <univalue/test/fail31.json.h> |
| 33 | +#include <univalue/test/fail32.json.h> |
| 34 | +#include <univalue/test/fail33.json.h> |
| 35 | +#include <univalue/test/fail34.json.h> |
| 36 | +#include <univalue/test/fail35.json.h> |
| 37 | +#include <univalue/test/fail36.json.h> |
| 38 | +#include <univalue/test/fail37.json.h> |
| 39 | +#include <univalue/test/fail38.json.h> |
| 40 | +#include <univalue/test/fail39.json.h> |
| 41 | +#include <univalue/test/fail4.json.h> |
| 42 | +#include <univalue/test/fail40.json.h> |
| 43 | +#include <univalue/test/fail41.json.h> |
| 44 | +#include <univalue/test/fail42.json.h> |
| 45 | +#include <univalue/test/fail44.json.h> |
| 46 | +#include <univalue/test/fail45.json.h> |
| 47 | +#include <univalue/test/fail5.json.h> |
| 48 | +#include <univalue/test/fail6.json.h> |
| 49 | +#include <univalue/test/fail7.json.h> |
| 50 | +#include <univalue/test/fail8.json.h> |
| 51 | +#include <univalue/test/fail9.json.h> |
| 52 | +#include <univalue/test/pass1.json.h> |
| 53 | +#include <univalue/test/pass2.json.h> |
| 54 | +#include <univalue/test/pass3.json.h> |
| 55 | +#include <univalue/test/pass4.json.h> |
| 56 | +#include <univalue/test/round1.json.h> |
| 57 | +#include <univalue/test/round2.json.h> |
| 58 | +#include <univalue/test/round3.json.h> |
| 59 | +#include <univalue/test/round4.json.h> |
| 60 | +#include <univalue/test/round5.json.h> |
| 61 | +#include <univalue/test/round6.json.h> |
| 62 | +#include <univalue/test/round7.json.h> |
| 63 | + |
| 64 | +#include <array> |
7 | 65 | #include <cassert> |
8 | 66 | #include <cstdio> |
9 | 67 | #include <string> |
10 | 68 |
|
11 | | -#ifndef JSON_TEST_SRC |
12 | | -#error JSON_TEST_SRC must point to test source directory |
13 | | -#endif |
14 | | - |
15 | | -std::string srcdir(JSON_TEST_SRC); |
16 | | - |
17 | 69 | static std::string rtrim(std::string s) |
18 | 70 | { |
19 | 71 | s.erase(s.find_last_not_of(" \n\r\t")+1); |
@@ -44,87 +96,64 @@ static void runtest(std::string filename, const std::string& jdata) |
44 | 96 | } |
45 | 97 | } |
46 | 98 |
|
47 | | -static void runtest_file(const char *filename_) |
48 | | -{ |
49 | | - std::string basename(filename_); |
50 | | - std::string filename = srcdir + "/" + basename; |
51 | | - FILE *f = fopen(filename.c_str(), "r"); |
52 | | - assert(f != nullptr); |
53 | | - |
54 | | - std::string jdata; |
55 | | - |
56 | | - char buf[4096]; |
57 | | - while (!feof(f)) { |
58 | | - int bread = fread(buf, 1, sizeof(buf), f); |
59 | | - assert(!ferror(f)); |
60 | | - |
61 | | - std::string s(buf, bread); |
62 | | - jdata += s; |
63 | | - } |
64 | | - |
65 | | - assert(!ferror(f)); |
66 | | - fclose(f); |
67 | | - |
68 | | - runtest(basename, jdata); |
69 | | -} |
70 | | - |
71 | | -static const char *filenames[] = { |
72 | | - "fail1.json", |
73 | | - "fail10.json", |
74 | | - "fail11.json", |
75 | | - "fail12.json", |
76 | | - "fail13.json", |
77 | | - "fail14.json", |
78 | | - "fail15.json", |
79 | | - "fail16.json", |
80 | | - "fail17.json", |
81 | | - "fail18.json", |
82 | | - "fail19.json", |
83 | | - "fail2.json", |
84 | | - "fail20.json", |
85 | | - "fail21.json", |
86 | | - "fail22.json", |
87 | | - "fail23.json", |
88 | | - "fail24.json", |
89 | | - "fail25.json", |
90 | | - "fail26.json", |
91 | | - "fail27.json", |
92 | | - "fail28.json", |
93 | | - "fail29.json", |
94 | | - "fail3.json", |
95 | | - "fail30.json", |
96 | | - "fail31.json", |
97 | | - "fail32.json", |
98 | | - "fail33.json", |
99 | | - "fail34.json", |
100 | | - "fail35.json", |
101 | | - "fail36.json", |
102 | | - "fail37.json", |
103 | | - "fail38.json", // invalid unicode: only first half of surrogate pair |
104 | | - "fail39.json", // invalid unicode: only second half of surrogate pair |
105 | | - "fail4.json", // extra comma |
106 | | - "fail40.json", // invalid unicode: broken UTF-8 |
107 | | - "fail41.json", // invalid unicode: unfinished UTF-8 |
108 | | - "fail42.json", // valid json with garbage following a nul byte |
109 | | - "fail44.json", // unterminated string |
110 | | - "fail45.json", // nested beyond max depth |
111 | | - "fail5.json", |
112 | | - "fail6.json", |
113 | | - "fail7.json", |
114 | | - "fail8.json", |
115 | | - "fail9.json", // extra comma |
116 | | - "pass1.json", |
117 | | - "pass2.json", |
118 | | - "pass3.json", |
119 | | - "pass4.json", |
120 | | - "round1.json", // round-trip test |
121 | | - "round2.json", // unicode |
122 | | - "round3.json", // bare string |
123 | | - "round4.json", // bare number |
124 | | - "round5.json", // bare true |
125 | | - "round6.json", // bare false |
126 | | - "round7.json", // bare null |
127 | | -}; |
| 99 | +#define TEST_FILE(name) {#name, json_tests::name} |
| 100 | +inline constexpr std::array tests{std::to_array<std::tuple<std::string_view, std::string_view>>({ |
| 101 | + TEST_FILE(fail1), |
| 102 | + TEST_FILE(fail10), |
| 103 | + TEST_FILE(fail11), |
| 104 | + TEST_FILE(fail12), |
| 105 | + TEST_FILE(fail13), |
| 106 | + TEST_FILE(fail14), |
| 107 | + TEST_FILE(fail15), |
| 108 | + TEST_FILE(fail16), |
| 109 | + TEST_FILE(fail17), |
| 110 | + TEST_FILE(fail18), |
| 111 | + TEST_FILE(fail19), |
| 112 | + TEST_FILE(fail2), |
| 113 | + TEST_FILE(fail20), |
| 114 | + TEST_FILE(fail21), |
| 115 | + TEST_FILE(fail22), |
| 116 | + TEST_FILE(fail23), |
| 117 | + TEST_FILE(fail24), |
| 118 | + TEST_FILE(fail25), |
| 119 | + TEST_FILE(fail26), |
| 120 | + TEST_FILE(fail27), |
| 121 | + TEST_FILE(fail28), |
| 122 | + TEST_FILE(fail29), |
| 123 | + TEST_FILE(fail3), |
| 124 | + TEST_FILE(fail30), |
| 125 | + TEST_FILE(fail31), |
| 126 | + TEST_FILE(fail32), |
| 127 | + TEST_FILE(fail33), |
| 128 | + TEST_FILE(fail34), |
| 129 | + TEST_FILE(fail35), |
| 130 | + TEST_FILE(fail36), |
| 131 | + TEST_FILE(fail37), |
| 132 | + TEST_FILE(fail38), // invalid unicode: only first half of surrogate pair |
| 133 | + TEST_FILE(fail39), // invalid unicode: only second half of surrogate pair |
| 134 | + TEST_FILE(fail4), // extra comma |
| 135 | + TEST_FILE(fail40), // invalid unicode: broken UTF-8 |
| 136 | + TEST_FILE(fail41), // invalid unicode: unfinished UTF-8 |
| 137 | + TEST_FILE(fail42), // valid json with garbage following a nul byte |
| 138 | + TEST_FILE(fail44), // unterminated string |
| 139 | + TEST_FILE(fail45), // nested beyond max depth |
| 140 | + TEST_FILE(fail5), |
| 141 | + TEST_FILE(fail6), |
| 142 | + TEST_FILE(fail7), |
| 143 | + TEST_FILE(fail8), |
| 144 | + TEST_FILE(fail9), // extra comma |
| 145 | + TEST_FILE(pass1), |
| 146 | + TEST_FILE(pass2), |
| 147 | + TEST_FILE(pass3), |
| 148 | + TEST_FILE(pass4), |
| 149 | + TEST_FILE(round1), // round-trip test |
| 150 | + TEST_FILE(round2), // unicode |
| 151 | + TEST_FILE(round3), // bare string |
| 152 | + TEST_FILE(round4), // bare number |
| 153 | + TEST_FILE(round5), // bare true |
| 154 | + TEST_FILE(round6), // bare false |
| 155 | + TEST_FILE(round7), // bare null |
| 156 | +})}; |
128 | 157 |
|
129 | 158 | // Test \u handling |
130 | 159 | void unescape_unicode_test() |
@@ -158,13 +187,12 @@ void no_nul_test() |
158 | 187 |
|
159 | 188 | int main (int argc, char *argv[]) |
160 | 189 | { |
161 | | - for (const auto& f: filenames) { |
162 | | - runtest_file(f); |
| 190 | + for (const auto& [file, json] : tests) { |
| 191 | + runtest(std::string{file}, std::string{json}); |
163 | 192 | } |
164 | 193 |
|
165 | 194 | unescape_unicode_test(); |
166 | 195 | no_nul_test(); |
167 | 196 |
|
168 | 197 | return 0; |
169 | 198 | } |
170 | | - |
|
0 commit comments