From fd24b19337015a3c8b0f218fa99fb33b953dd649 Mon Sep 17 00:00:00 2001 From: Terraneo Federico <fede.tft@miosix.org> Date: Sun, 6 May 2018 23:02:43 +0200 Subject: [PATCH] Testing error code paths in stream serializers --- examples/5_stream_failtest.cpp | 128 +++++++++++++++++++++++++++++++++ examples/Makefile | 13 ++-- 2 files changed, 136 insertions(+), 5 deletions(-) create mode 100644 examples/5_stream_failtest.cpp diff --git a/examples/5_stream_failtest.cpp b/examples/5_stream_failtest.cpp new file mode 100644 index 0000000..cdbb7ce --- /dev/null +++ b/examples/5_stream_failtest.cpp @@ -0,0 +1,128 @@ + +#include <iostream> +#include <sstream> +#include <cassert> +#include <tscpp.h> +#include "types.h" + +using namespace std; +using namespace tscpp; + +int main() +{ + //eof during type name in InputArchive + { + stringstream ss; + InputArchive ia(ss); + Point3d p; + try { + ia>>p; + assert(false); + } catch(TscppException& ex) { + assert(string(ex.what())=="eof"); + assert(ex.name()==""); + assert(p.x==0 && p.y==0 && p.z==0); + } + } + + //wrong type in InputArchive + { + stringstream ss; + OutputArchive oa(ss); + Point3d p(1,2,3); + oa<<p; + + InputArchive ia(ss); + Point2d q; + try { + ia>>q; + assert(false); + } catch(TscppException& ex) { + assert(ss.tellg()==0); + assert(string(ex.what())=="wrong type"); + assert(ex.name()==typeid(p).name()); + assert(q.x==0 && q.y==0); + } + } + + //eof after type name in InputArchive + { + stringstream ss; + OutputArchive oa(ss); + Point3d p(1,2,3); + oa<<p; + + string s=ss.str(); + s.pop_back(); //Remove one char + ss.str(s); + + InputArchive ia(ss); + Point3d q; + try { + ia>>q; + assert(false); + } catch(TscppException& ex) { + assert(string(ex.what())=="eof"); + assert(ex.name()==""); + } + } + + TypePool tp; + tp.registerType<Point2d>([&](Point2d& t) { assert(false); }); + tp.registerType<Point3d>([&](Point3d& t) { assert(false); }); + + //eof during type name in UnknownInputArchive + { + + stringstream ss; + UnknownInputArchive ia(ss,tp); + try { + ia.unserialize(); + assert(false); + } catch(TscppException& ex) { + assert(string(ex.what())=="eof"); + assert(ex.name()==""); + } + } + + //unknown type in UnknownInputArchive + { + stringstream ss; + OutputArchive oa(ss); + MiscData md; + oa<<md; + + UnknownInputArchive ia(ss,tp); + try { + ia.unserialize(); + assert(false); + } catch(TscppException& ex) { + assert(ss.tellg()==0); + assert(string(ex.what())=="unknown type"); + assert(ex.name()==typeid(md).name()); + } + } + + //eof after type name in UnknownInputArchive + { + stringstream ss; + OutputArchive oa(ss); + Point3d p(1,2,3); + oa<<p; + + string s=ss.str(); + s.pop_back(); //Remove one char + ss.str(s); + + UnknownInputArchive ia(ss,tp); + try { + ia.unserialize(); + assert(false); + } catch(TscppException& ex) { + assert(string(ex.what())=="eof"); + assert(ex.name()==""); + } + } + + cout<<"Test passed"<<endl; +} diff --git a/examples/Makefile b/examples/Makefile index 5dc2456..2f227a4 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -3,14 +3,17 @@ CXX = g++ CXXFLAGS = -std=c++11 -O2 -Wall -I.. all: - $(CXX) $(CXXFLAGS) 1_stream_known.cpp ../tscpp.cpp -o 1_stream_known - $(CXX) $(CXXFLAGS) 2_stream_unknown.cpp ../tscpp.cpp -o 2_stream_unknown - $(CXX) $(CXXFLAGS) 3_buffer_known.cpp ../tscpp.cpp -o 3_buffer_known - $(CXX) $(CXXFLAGS) 4_buffer_unknown.cpp ../tscpp.cpp -o 4_buffer_unknown + $(CXX) $(CXXFLAGS) 1_stream_known.cpp ../tscpp.cpp -o 1_stream_known + $(CXX) $(CXXFLAGS) 2_stream_unknown.cpp ../tscpp.cpp -o 2_stream_unknown + $(CXX) $(CXXFLAGS) 3_buffer_known.cpp ../tscpp.cpp -o 3_buffer_known + $(CXX) $(CXXFLAGS) 4_buffer_unknown.cpp ../tscpp.cpp -o 4_buffer_unknown + $(CXX) $(CXXFLAGS) 5_stream_failtest.cpp ../tscpp.cpp -o 5_stream_failtest ./1_stream_known ./2_stream_unknown ./3_buffer_known ./4_buffer_unknown + ./5_stream_failtest clean: - rm -f 1_stream_known 2_stream_unknown 3_buffer_known 4_buffer_unknown + rm -f 1_stream_known 2_stream_unknown 3_buffer_known 4_buffer_unknown \ + 5_stream_failtest -- GitLab