mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-25 12:51:55 -05:00

a44caf65fe Merge bitcoin-core/univalue-subtree#28: Import fixes for sanitizer reported issues 135254331e Import fixes for sanitizer reported issues d5fb86940e refactor: use c++11 range based for loop in checkObject ff9c379304 refactor: Use nullptr (c++11) instead of NULL 08a99754d5 build: use ax_cxx_compile_stdcxx.m4 to check for C++11 support 66d3713ce7 Merge bitcoin-core/univalue#29: ci: travis -> cirrus 808d487292 ci: travis -> cirrus c390ac375f Merge bitcoin-core/univalue#19: Split sources for easier buildsystem integration 4a5b0a1c65 build: Move source entries out to sources.mk 6c7d94b33c build: cleanup wonky gen usage a222637c6d Merge #23: Merge changes from jgarzik/univalue@1ae6a23 f77d0f718d Merge commit '1ae6a231a0169938eb3972c1d48dd17cba5947e1' into HEAD 1ae6a231a0 Merge pull request #57 from MarcoFalke/test_fix 92bdd11f0b univalue_write: remove unneeded sstream.h include ffb621c130 Merge pull request #56 from drodil/remove_sstream_header f33acf9fe8 Merge commit '7890db9~' into HEAD 66e0adec4d Remove unnecessary sstream header from univalue.h 88967f6586 Version 1.0.4 1dc113dbef Merge pull request #50 from luke-jr/pushKV_bool 72392fb227 [tests] test pushKV for boolean values c23132bcf4 Pushing boolean value to univalue correctly 81faab26a1 Merge pull request #48 from fwolfst/47-UPDATE_MIT_LINK_TO_HTTPS b17634ef24 Update URLs to MIT license. 88ab64f6b5 Merge pull request #46 from jasonbcox/master 35ed96da31 Merge pull request #44 from MarcoFalke/Mf1709-univalue-cherrypick-explicit 420c226290 Merge pull request #45 from MarcoFalke/Mf1710-univalue-revert-test git-subtree-dir: src/univalue git-subtree-split: a44caf65fe55b9dd8ddb08f04c0f70409efd53b3
171 lines
4.9 KiB
C++
171 lines
4.9 KiB
C++
// Copyright 2014 BitPay Inc.
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
// file COPYING or https://opensource.org/licenses/mit-license.php.
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <cassert>
|
|
#include <string>
|
|
#include "univalue.h"
|
|
|
|
#ifndef JSON_TEST_SRC
|
|
#error JSON_TEST_SRC must point to test source directory
|
|
#endif
|
|
|
|
#ifndef ARRAY_SIZE
|
|
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
|
|
#endif
|
|
|
|
std::string srcdir(JSON_TEST_SRC);
|
|
static bool test_failed = false;
|
|
|
|
#define d_assert(expr) { if (!(expr)) { test_failed = true; fprintf(stderr, "%s failed\n", filename.c_str()); } }
|
|
#define f_assert(expr) { if (!(expr)) { test_failed = true; fprintf(stderr, "%s failed\n", __func__); } }
|
|
|
|
static std::string rtrim(std::string s)
|
|
{
|
|
s.erase(s.find_last_not_of(" \n\r\t")+1);
|
|
return s;
|
|
}
|
|
|
|
static void runtest(std::string filename, const std::string& jdata)
|
|
{
|
|
std::string prefix = filename.substr(0, 4);
|
|
|
|
bool wantPass = (prefix == "pass") || (prefix == "roun");
|
|
bool wantFail = (prefix == "fail");
|
|
bool wantRoundTrip = (prefix == "roun");
|
|
assert(wantPass || wantFail);
|
|
|
|
UniValue val;
|
|
bool testResult = val.read(jdata);
|
|
|
|
if (wantPass) {
|
|
d_assert(testResult == true);
|
|
} else {
|
|
d_assert(testResult == false);
|
|
}
|
|
|
|
if (wantRoundTrip) {
|
|
std::string odata = val.write(0, 0);
|
|
assert(odata == rtrim(jdata));
|
|
}
|
|
}
|
|
|
|
static void runtest_file(const char *filename_)
|
|
{
|
|
std::string basename(filename_);
|
|
std::string filename = srcdir + "/" + basename;
|
|
FILE *f = fopen(filename.c_str(), "r");
|
|
assert(f != nullptr);
|
|
|
|
std::string jdata;
|
|
|
|
char buf[4096];
|
|
while (!feof(f)) {
|
|
int bread = fread(buf, 1, sizeof(buf), f);
|
|
assert(!ferror(f));
|
|
|
|
std::string s(buf, bread);
|
|
jdata += s;
|
|
}
|
|
|
|
assert(!ferror(f));
|
|
fclose(f);
|
|
|
|
runtest(basename, jdata);
|
|
}
|
|
|
|
static const char *filenames[] = {
|
|
"fail10.json",
|
|
"fail11.json",
|
|
"fail12.json",
|
|
"fail13.json",
|
|
"fail14.json",
|
|
"fail15.json",
|
|
"fail16.json",
|
|
"fail17.json",
|
|
//"fail18.json", // investigate
|
|
"fail19.json",
|
|
"fail1.json",
|
|
"fail20.json",
|
|
"fail21.json",
|
|
"fail22.json",
|
|
"fail23.json",
|
|
"fail24.json",
|
|
"fail25.json",
|
|
"fail26.json",
|
|
"fail27.json",
|
|
"fail28.json",
|
|
"fail29.json",
|
|
"fail2.json",
|
|
"fail30.json",
|
|
"fail31.json",
|
|
"fail32.json",
|
|
"fail33.json",
|
|
"fail34.json",
|
|
"fail35.json",
|
|
"fail36.json",
|
|
"fail37.json",
|
|
"fail38.json", // invalid unicode: only first half of surrogate pair
|
|
"fail39.json", // invalid unicode: only second half of surrogate pair
|
|
"fail40.json", // invalid unicode: broken UTF-8
|
|
"fail41.json", // invalid unicode: unfinished UTF-8
|
|
"fail42.json", // valid json with garbage following a nul byte
|
|
"fail44.json", // unterminated string
|
|
"fail45.json", // nested beyond max depth
|
|
"fail3.json",
|
|
"fail4.json", // extra comma
|
|
"fail5.json",
|
|
"fail6.json",
|
|
"fail7.json",
|
|
"fail8.json",
|
|
"fail9.json", // extra comma
|
|
"pass1.json",
|
|
"pass2.json",
|
|
"pass3.json",
|
|
"pass4.json",
|
|
"round1.json", // round-trip test
|
|
"round2.json", // unicode
|
|
"round3.json", // bare string
|
|
"round4.json", // bare number
|
|
"round5.json", // bare true
|
|
"round6.json", // bare false
|
|
"round7.json", // bare null
|
|
};
|
|
|
|
// Test \u handling
|
|
void unescape_unicode_test()
|
|
{
|
|
UniValue val;
|
|
bool testResult;
|
|
// Escaped ASCII (quote)
|
|
testResult = val.read("[\"\\u0022\"]");
|
|
f_assert(testResult);
|
|
f_assert(val[0].get_str() == "\"");
|
|
// Escaped Basic Plane character, two-byte UTF-8
|
|
testResult = val.read("[\"\\u0191\"]");
|
|
f_assert(testResult);
|
|
f_assert(val[0].get_str() == "\xc6\x91");
|
|
// Escaped Basic Plane character, three-byte UTF-8
|
|
testResult = val.read("[\"\\u2191\"]");
|
|
f_assert(testResult);
|
|
f_assert(val[0].get_str() == "\xe2\x86\x91");
|
|
// Escaped Supplementary Plane character U+1d161
|
|
testResult = val.read("[\"\\ud834\\udd61\"]");
|
|
f_assert(testResult);
|
|
f_assert(val[0].get_str() == "\xf0\x9d\x85\xa1");
|
|
}
|
|
|
|
int main (int argc, char *argv[])
|
|
{
|
|
for (unsigned int fidx = 0; fidx < ARRAY_SIZE(filenames); fidx++) {
|
|
runtest_file(filenames[fidx]);
|
|
}
|
|
|
|
unescape_unicode_test();
|
|
|
|
return test_failed ? 1 : 0;
|
|
}
|
|
|