Bitcoin Core  0.20.99
P2P Digital Currency
object.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014 BitPay Inc.
2 // Copyright (c) 2014-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <stdint.h>
7 #include <vector>
8 #include <string>
9 #include <map>
10 #include <cassert>
11 #include <stdexcept>
12 #include <univalue.h>
13 
14 #define BOOST_FIXTURE_TEST_SUITE(a, b)
15 #define BOOST_AUTO_TEST_CASE(funcName) void funcName()
16 #define BOOST_AUTO_TEST_SUITE_END()
17 #define BOOST_CHECK(expr) assert(expr)
18 #define BOOST_CHECK_EQUAL(v1, v2) assert((v1) == (v2))
19 #define BOOST_CHECK_THROW(stmt, excMatch) { \
20  try { \
21  (stmt); \
22  assert(0 && "No exception caught"); \
23  } catch (excMatch & e) { \
24  } catch (...) { \
25  assert(0 && "Wrong exception caught"); \
26  } \
27  }
28 #define BOOST_CHECK_NO_THROW(stmt) { \
29  try { \
30  (stmt); \
31  } catch (...) { \
32  assert(0); \
33  } \
34  }
35 
36 BOOST_FIXTURE_TEST_SUITE(univalue_tests, BasicTestingSetup)
37 
38 BOOST_AUTO_TEST_CASE(univalue_constructor)
39 {
40  UniValue v1;
41  BOOST_CHECK(v1.isNull());
42 
44  BOOST_CHECK(v2.isStr());
45 
46  UniValue v3(UniValue::VSTR, "foo");
47  BOOST_CHECK(v3.isStr());
48  BOOST_CHECK_EQUAL(v3.getValStr(), "foo");
49 
50  UniValue numTest;
51  BOOST_CHECK(numTest.setNumStr("82"));
52  BOOST_CHECK(numTest.isNum());
53  BOOST_CHECK_EQUAL(numTest.getValStr(), "82");
54 
55  uint64_t vu64 = 82;
56  UniValue v4(vu64);
57  BOOST_CHECK(v4.isNum());
58  BOOST_CHECK_EQUAL(v4.getValStr(), "82");
59 
60  int64_t vi64 = -82;
61  UniValue v5(vi64);
62  BOOST_CHECK(v5.isNum());
63  BOOST_CHECK_EQUAL(v5.getValStr(), "-82");
64 
65  int vi = -688;
66  UniValue v6(vi);
67  BOOST_CHECK(v6.isNum());
68  BOOST_CHECK_EQUAL(v6.getValStr(), "-688");
69 
70  double vd = -7.21;
71  UniValue v7(vd);
72  BOOST_CHECK(v7.isNum());
73  BOOST_CHECK_EQUAL(v7.getValStr(), "-7.21");
74 
75  std::string vs("yawn");
76  UniValue v8(vs);
77  BOOST_CHECK(v8.isStr());
78  BOOST_CHECK_EQUAL(v8.getValStr(), "yawn");
79 
80  const char *vcs = "zappa";
81  UniValue v9(vcs);
82  BOOST_CHECK(v9.isStr());
83  BOOST_CHECK_EQUAL(v9.getValStr(), "zappa");
84 }
85 
86 BOOST_AUTO_TEST_CASE(univalue_typecheck)
87 {
88  UniValue v1;
89  BOOST_CHECK(v1.setNumStr("1"));
90  BOOST_CHECK(v1.isNum());
91  BOOST_CHECK_THROW(v1.get_bool(), std::runtime_error);
92 
93  UniValue v2;
94  BOOST_CHECK(v2.setBool(true));
95  BOOST_CHECK_EQUAL(v2.get_bool(), true);
96  BOOST_CHECK_THROW(v2.get_int(), std::runtime_error);
97 
98  UniValue v3;
99  BOOST_CHECK(v3.setNumStr("32482348723847471234"));
100  BOOST_CHECK_THROW(v3.get_int64(), std::runtime_error);
101  BOOST_CHECK(v3.setNumStr("1000"));
102  BOOST_CHECK_EQUAL(v3.get_int64(), 1000);
103 
104  UniValue v4;
105  BOOST_CHECK(v4.setNumStr("2147483648"));
106  BOOST_CHECK_EQUAL(v4.get_int64(), 2147483648);
107  BOOST_CHECK_THROW(v4.get_int(), std::runtime_error);
108  BOOST_CHECK(v4.setNumStr("1000"));
109  BOOST_CHECK_EQUAL(v4.get_int(), 1000);
110  BOOST_CHECK_THROW(v4.get_str(), std::runtime_error);
111  BOOST_CHECK_EQUAL(v4.get_real(), 1000);
112  BOOST_CHECK_THROW(v4.get_array(), std::runtime_error);
113  BOOST_CHECK_THROW(v4.getKeys(), std::runtime_error);
114  BOOST_CHECK_THROW(v4.getValues(), std::runtime_error);
115  BOOST_CHECK_THROW(v4.get_obj(), std::runtime_error);
116 
117  UniValue v5;
118  BOOST_CHECK(v5.read("[true, 10]"));
119  BOOST_CHECK_NO_THROW(v5.get_array());
120  std::vector<UniValue> vals = v5.getValues();
121  BOOST_CHECK_THROW(vals[0].get_int(), std::runtime_error);
122  BOOST_CHECK_EQUAL(vals[0].get_bool(), true);
123 
124  BOOST_CHECK_EQUAL(vals[1].get_int(), 10);
125  BOOST_CHECK_THROW(vals[1].get_bool(), std::runtime_error);
126 }
127 
128 BOOST_AUTO_TEST_CASE(univalue_set)
129 {
130  UniValue v(UniValue::VSTR, "foo");
131  v.clear();
132  BOOST_CHECK(v.isNull());
133  BOOST_CHECK_EQUAL(v.getValStr(), "");
134 
135  BOOST_CHECK(v.setObject());
136  BOOST_CHECK(v.isObject());
137  BOOST_CHECK_EQUAL(v.size(), 0);
139  BOOST_CHECK(v.empty());
140 
141  BOOST_CHECK(v.setArray());
142  BOOST_CHECK(v.isArray());
143  BOOST_CHECK_EQUAL(v.size(), 0);
144 
145  BOOST_CHECK(v.setStr("zum"));
146  BOOST_CHECK(v.isStr());
147  BOOST_CHECK_EQUAL(v.getValStr(), "zum");
148 
149  BOOST_CHECK(v.setFloat(-1.01));
150  BOOST_CHECK(v.isNum());
151  BOOST_CHECK_EQUAL(v.getValStr(), "-1.01");
152 
153  BOOST_CHECK(v.setInt((int)1023));
154  BOOST_CHECK(v.isNum());
155  BOOST_CHECK_EQUAL(v.getValStr(), "1023");
156 
157  BOOST_CHECK(v.setInt((int64_t)-1023LL));
158  BOOST_CHECK(v.isNum());
159  BOOST_CHECK_EQUAL(v.getValStr(), "-1023");
160 
161  BOOST_CHECK(v.setInt((uint64_t)1023ULL));
162  BOOST_CHECK(v.isNum());
163  BOOST_CHECK_EQUAL(v.getValStr(), "1023");
164 
165  BOOST_CHECK(v.setNumStr("-688"));
166  BOOST_CHECK(v.isNum());
167  BOOST_CHECK_EQUAL(v.getValStr(), "-688");
168 
169  BOOST_CHECK(v.setBool(false));
170  BOOST_CHECK_EQUAL(v.isBool(), true);
171  BOOST_CHECK_EQUAL(v.isTrue(), false);
172  BOOST_CHECK_EQUAL(v.isFalse(), true);
173  BOOST_CHECK_EQUAL(v.getBool(), false);
174 
175  BOOST_CHECK(v.setBool(true));
176  BOOST_CHECK_EQUAL(v.isBool(), true);
177  BOOST_CHECK_EQUAL(v.isTrue(), true);
178  BOOST_CHECK_EQUAL(v.isFalse(), false);
179  BOOST_CHECK_EQUAL(v.getBool(), true);
180 
181  BOOST_CHECK(!v.setNumStr("zombocom"));
182 
183  BOOST_CHECK(v.setNull());
184  BOOST_CHECK(v.isNull());
185 }
186 
187 BOOST_AUTO_TEST_CASE(univalue_array)
188 {
190 
191  UniValue v((int64_t)1023LL);
192  BOOST_CHECK(arr.push_back(v));
193 
194  std::string vStr("zippy");
195  BOOST_CHECK(arr.push_back(vStr));
196 
197  const char *s = "pippy";
198  BOOST_CHECK(arr.push_back(s));
199 
200  std::vector<UniValue> vec;
201  v.setStr("boing");
202  vec.push_back(v);
203 
204  v.setStr("going");
205  vec.push_back(v);
206 
207  BOOST_CHECK(arr.push_backV(vec));
208 
209  BOOST_CHECK(arr.push_back((uint64_t) 400ULL));
210  BOOST_CHECK(arr.push_back((int64_t) -400LL));
211  BOOST_CHECK(arr.push_back((int) -401));
212  BOOST_CHECK(arr.push_back(-40.1));
213 
214  BOOST_CHECK_EQUAL(arr.empty(), false);
215  BOOST_CHECK_EQUAL(arr.size(), 9);
216 
217  BOOST_CHECK_EQUAL(arr[0].getValStr(), "1023");
218  BOOST_CHECK_EQUAL(arr[1].getValStr(), "zippy");
219  BOOST_CHECK_EQUAL(arr[2].getValStr(), "pippy");
220  BOOST_CHECK_EQUAL(arr[3].getValStr(), "boing");
221  BOOST_CHECK_EQUAL(arr[4].getValStr(), "going");
222  BOOST_CHECK_EQUAL(arr[5].getValStr(), "400");
223  BOOST_CHECK_EQUAL(arr[6].getValStr(), "-400");
224  BOOST_CHECK_EQUAL(arr[7].getValStr(), "-401");
225  BOOST_CHECK_EQUAL(arr[8].getValStr(), "-40.1");
226 
227  BOOST_CHECK_EQUAL(arr[999].getValStr(), "");
228 
229  arr.clear();
230  BOOST_CHECK(arr.empty());
231  BOOST_CHECK_EQUAL(arr.size(), 0);
232 }
233 
234 BOOST_AUTO_TEST_CASE(univalue_object)
235 {
237  std::string strKey, strVal;
238  UniValue v;
239 
240  strKey = "age";
241  v.setInt(100);
242  BOOST_CHECK(obj.pushKV(strKey, v));
243 
244  strKey = "first";
245  strVal = "John";
246  BOOST_CHECK(obj.pushKV(strKey, strVal));
247 
248  strKey = "last";
249  const char *cVal = "Smith";
250  BOOST_CHECK(obj.pushKV(strKey, cVal));
251 
252  strKey = "distance";
253  BOOST_CHECK(obj.pushKV(strKey, (int64_t) 25));
254 
255  strKey = "time";
256  BOOST_CHECK(obj.pushKV(strKey, (uint64_t) 3600));
257 
258  strKey = "calories";
259  BOOST_CHECK(obj.pushKV(strKey, (int) 12));
260 
261  strKey = "temperature";
262  BOOST_CHECK(obj.pushKV(strKey, (double) 90.012));
263 
264  strKey = "moon";
265  BOOST_CHECK(obj.pushKV(strKey, true));
266 
267  strKey = "spoon";
268  BOOST_CHECK(obj.pushKV(strKey, false));
269 
270  UniValue obj2(UniValue::VOBJ);
271  BOOST_CHECK(obj2.pushKV("cat1", 9000));
272  BOOST_CHECK(obj2.pushKV("cat2", 12345));
273 
274  BOOST_CHECK(obj.pushKVs(obj2));
275 
276  BOOST_CHECK_EQUAL(obj.empty(), false);
277  BOOST_CHECK_EQUAL(obj.size(), 11);
278 
279  BOOST_CHECK_EQUAL(obj["age"].getValStr(), "100");
280  BOOST_CHECK_EQUAL(obj["first"].getValStr(), "John");
281  BOOST_CHECK_EQUAL(obj["last"].getValStr(), "Smith");
282  BOOST_CHECK_EQUAL(obj["distance"].getValStr(), "25");
283  BOOST_CHECK_EQUAL(obj["time"].getValStr(), "3600");
284  BOOST_CHECK_EQUAL(obj["calories"].getValStr(), "12");
285  BOOST_CHECK_EQUAL(obj["temperature"].getValStr(), "90.012");
286  BOOST_CHECK_EQUAL(obj["moon"].getValStr(), "1");
287  BOOST_CHECK_EQUAL(obj["spoon"].getValStr(), "");
288  BOOST_CHECK_EQUAL(obj["cat1"].getValStr(), "9000");
289  BOOST_CHECK_EQUAL(obj["cat2"].getValStr(), "12345");
290 
291  BOOST_CHECK_EQUAL(obj["nyuknyuknyuk"].getValStr(), "");
292 
293  BOOST_CHECK(obj.exists("age"));
294  BOOST_CHECK(obj.exists("first"));
295  BOOST_CHECK(obj.exists("last"));
296  BOOST_CHECK(obj.exists("distance"));
297  BOOST_CHECK(obj.exists("time"));
298  BOOST_CHECK(obj.exists("calories"));
299  BOOST_CHECK(obj.exists("temperature"));
300  BOOST_CHECK(obj.exists("moon"));
301  BOOST_CHECK(obj.exists("spoon"));
302  BOOST_CHECK(obj.exists("cat1"));
303  BOOST_CHECK(obj.exists("cat2"));
304 
305  BOOST_CHECK(!obj.exists("nyuknyuknyuk"));
306 
307  std::map<std::string, UniValue::VType> objTypes;
308  objTypes["age"] = UniValue::VNUM;
309  objTypes["first"] = UniValue::VSTR;
310  objTypes["last"] = UniValue::VSTR;
311  objTypes["distance"] = UniValue::VNUM;
312  objTypes["time"] = UniValue::VNUM;
313  objTypes["calories"] = UniValue::VNUM;
314  objTypes["temperature"] = UniValue::VNUM;
315  objTypes["moon"] = UniValue::VBOOL;
316  objTypes["spoon"] = UniValue::VBOOL;
317  objTypes["cat1"] = UniValue::VNUM;
318  objTypes["cat2"] = UniValue::VNUM;
319  BOOST_CHECK(obj.checkObject(objTypes));
320 
321  objTypes["cat2"] = UniValue::VSTR;
322  BOOST_CHECK(!obj.checkObject(objTypes));
323 
324  obj.clear();
325  BOOST_CHECK(obj.empty());
326  BOOST_CHECK_EQUAL(obj.size(), 0);
328 
329  BOOST_CHECK_EQUAL(obj.setObject(), true);
330  UniValue uv;
331  uv.setInt(42);
332  obj.__pushKV("age", uv);
333  BOOST_CHECK_EQUAL(obj.size(), 1);
334  BOOST_CHECK_EQUAL(obj["age"].getValStr(), "42");
335 
336  uv.setInt(43);
337  obj.pushKV("age", uv);
338  BOOST_CHECK_EQUAL(obj.size(), 1);
339  BOOST_CHECK_EQUAL(obj["age"].getValStr(), "43");
340 
341  obj.pushKV("name", "foo bar");
342 
343  std::map<std::string,UniValue> kv;
344  obj.getObjMap(kv);
345  BOOST_CHECK_EQUAL(kv["age"].getValStr(), "43");
346  BOOST_CHECK_EQUAL(kv["name"].getValStr(), "foo bar");
347 
348 }
349 
350 static const char *json1 =
351 "[1.10000000,{\"key1\":\"str\\u0000\",\"key2\":800,\"key3\":{\"name\":\"martian http://test.com\"}}]";
352 
353 BOOST_AUTO_TEST_CASE(univalue_readwrite)
354 {
355  UniValue v;
356  BOOST_CHECK(v.read(json1));
357 
358  std::string strJson1(json1);
359  BOOST_CHECK(v.read(strJson1));
360 
361  BOOST_CHECK(v.isArray());
362  BOOST_CHECK_EQUAL(v.size(), 2);
363 
364  BOOST_CHECK_EQUAL(v[0].getValStr(), "1.10000000");
365 
366  UniValue obj = v[1];
367  BOOST_CHECK(obj.isObject());
368  BOOST_CHECK_EQUAL(obj.size(), 3);
369 
370  BOOST_CHECK(obj["key1"].isStr());
371  std::string correctValue("str");
372  correctValue.push_back('\0');
373  BOOST_CHECK_EQUAL(obj["key1"].getValStr(), correctValue);
374  BOOST_CHECK(obj["key2"].isNum());
375  BOOST_CHECK_EQUAL(obj["key2"].getValStr(), "800");
376  BOOST_CHECK(obj["key3"].isObject());
377 
378  BOOST_CHECK_EQUAL(strJson1, v.write());
379 
380  /* Check for (correctly reporting) a parsing error if the initial
381  JSON construct is followed by more stuff. Note that whitespace
382  is, of course, exempt. */
383 
384  BOOST_CHECK(v.read(" {}\n "));
385  BOOST_CHECK(v.isObject());
386  BOOST_CHECK(v.read(" []\n "));
387  BOOST_CHECK(v.isArray());
388 
389  BOOST_CHECK(!v.read("@{}"));
390  BOOST_CHECK(!v.read("{} garbage"));
391  BOOST_CHECK(!v.read("[]{}"));
392  BOOST_CHECK(!v.read("{}[]"));
393  BOOST_CHECK(!v.read("{} 42"));
394 }
395 
397 
398 int main (int argc, char *argv[])
399 {
400  univalue_constructor();
401  univalue_typecheck();
402  univalue_set();
403  univalue_array();
404  univalue_object();
405  univalue_readwrite();
406  return 0;
407 }
408 
bool isObject() const
Definition: univalue.h:84
bool setNumStr(const std::string &val)
Definition: univalue.cpp:46
bool isBool() const
Definition: univalue.h:80
#define BOOST_CHECK_THROW(stmt, excMatch)
Definition: object.cpp:19
bool get_bool() const
int main(int argc, char *argv[])
Definition: object.cpp:398
#define BOOST_AUTO_TEST_CASE(funcName)
Definition: object.cpp:15
bool read(const char *raw, size_t len)
enum VType getType() const
Definition: univalue.h:64
bool isNum() const
Definition: univalue.h:82
bool isStr() const
Definition: univalue.h:81
bool pushKVs(const UniValue &obj)
Definition: univalue.cpp:146
const std::string & getValStr() const
Definition: univalue.h:65
static const char * json1
Definition: object.cpp:350
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
bool isFalse() const
Definition: univalue.h:79
void getObjMap(std::map< std::string, UniValue > &kv) const
Definition: univalue.cpp:157
bool exists(const std::string &key) const
Definition: univalue.h:75
bool empty() const
Definition: univalue.h:66
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
bool setFloat(double val)
Definition: univalue.cpp:75
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
bool setNull()
Definition: univalue.cpp:23
bool isNull() const
Definition: univalue.h:77
bool isTrue() const
Definition: univalue.h:78
bool getBool() const
Definition: univalue.h:70
bool checkObject(const std::map< std::string, UniValue::VType > &memberTypes) const
Definition: univalue.cpp:179
bool setStr(const std::string &val)
Definition: univalue.cpp:86
bool setInt(uint64_t val)
Definition: univalue.cpp:57
bool setArray()
Definition: univalue.cpp:94
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
bool push_backV(const std::vector< UniValue > &vec)
Definition: univalue.cpp:117
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
bool setObject()
Definition: univalue.cpp:101
bool setBool(bool val)
Definition: univalue.cpp:29
#define BOOST_CHECK_NO_THROW(stmt)
Definition: object.cpp:28
void clear()
Definition: univalue.cpp:15
size_t size() const
Definition: univalue.h:68
void __pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:127
bool isArray() const
Definition: univalue.h:83
#define BOOST_CHECK(expr)
Definition: object.cpp:17