From 72e96e3c1d53b4e8d54b1aa0d7b2146d18fa622d Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Sat, 3 Mar 2018 18:40:02 +0100 Subject: [PATCH] Added first json test case --- tests/CMakeLists.txt | 1 + tests/io/json_tests.cpp | 117 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 tests/io/json_tests.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 48f7a05..fe1b412 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -49,6 +49,7 @@ add_executable( all_tests all_tests.cpp crypto/dh_test.cpp crypto/rand_test.cpp crypto/sha_tests.cpp + io/json_tests.cpp io/stream_tests.cpp network/http/websocket_test.cpp thread/task_cancel.cpp diff --git a/tests/io/json_tests.cpp b/tests/io/json_tests.cpp new file mode 100644 index 0000000..8db3e08 --- /dev/null +++ b/tests/io/json_tests.cpp @@ -0,0 +1,117 @@ +#include + +#include +#include +#include +#include +#include +#include + +#include + +BOOST_AUTO_TEST_SUITE(json_tests) + +static void replace_some( std::string& str ) +{ + for( size_t i = 0; i < str.length(); i++ ) + if( str[i] == '\1' ) str[i] = '\0'; + else if( str[i] == '\'' ) str[i] = '"'; +} + +static void test_fail_string( const std::string& str ) +{ + try { + fc::json::from_string( str ); + BOOST_FAIL( "json::from_string('" + str + "') failed" ); + } catch( const fc::parse_error_exception& ) { // ignore, ok + } catch( const fc::eof_exception& ) { // ignore, ok + } FC_CAPTURE_LOG_AND_RETHROW( ("json::from_string failed")(str) ) +} + +static void test_fail_stream( const std::string& str ) +{ + fc::temp_file file( fc::temp_directory_path(), true ); + { + std::fstream init( file.path().to_native_ansi_path(), std::fstream::out | std::fstream::trunc ); + init.write( str.c_str(), str.length() ); + init.close(); + } + try { + fc::istream_ptr in( new fc::ifstream( file.path() ) ); + fc::buffered_istream bin( in ); + fc::json::from_stream( bin ); + BOOST_FAIL( "json::from_stream('" + str + "') failed using ifstream" ); + } catch( const fc::parse_error_exception& ) { // ignore, ok + } catch( const fc::eof_exception& ) { // ignore, ok + } FC_CAPTURE_LOG_AND_RETHROW( ("json::from_stream failed using ifstream")(str) ) + try { + fc::istream_ptr in( new fc::stringstream( str ) ); + fc::buffered_istream bin( in ); + fc::json::from_stream( bin ); + BOOST_FAIL( "json::from_stream('" + str + "') failed using stringstream" ); + } catch( const fc::parse_error_exception& ) { // ignore, ok + } catch( const fc::eof_exception& ) { // ignore, ok + } FC_CAPTURE_LOG_AND_RETHROW( ("json::from_stream failed using stringstream")(str) ) +} + +static void test_fail_file( const std::string& str ) +{ + fc::temp_file file( fc::temp_directory_path(), true ); + { + std::fstream init( file.path().to_native_ansi_path(), std::fstream::out | std::fstream::trunc ); + init.write( str.c_str(), str.length() ); + init.close(); + } + try { + fc::json::from_file( file.path() ); + BOOST_FAIL( "json::from_file('" + str + "') failed using" ); + } catch( const fc::parse_error_exception& ) { // ignore, ok + } catch( const fc::eof_exception& ) { // ignore, ok + } FC_CAPTURE_LOG_AND_RETHROW( ("json::from_file failed")(str) ) +} + +BOOST_AUTO_TEST_CASE(imbalanced_test) +{ + std::vector tests + { // for easier handling and better readability, in the following test + // strings ' is used instead of " and \1 instead of \0 + "{", + "{'", + "{'}", + "{'a'", + "{'a':", + "{'a':5", + "[", + "['", + "[']", + "[ 13", + "' end", + "{ 13: }", + "{\1", + "{\1}", + "{'\1", + "{'\1}", + "{'a'\1", + "{'a'\1}", + "{'a': \1", + "{'a': \1}", + "[\1", + "[\1]", + "['\1", + "['\1]", + "[ 13\1", + "[ 13\1]", + "' end\1" + }; + + for( std::string test : tests ) + { + replace_some( test ); + test_fail_string( test ); + test_fail_stream( test ); + test_fail_file( test ); + } +} + + +BOOST_AUTO_TEST_SUITE_END()