// // Created by Stepan Usatiuk on 14.04.2023. // #include "ChunkTest.h" #include "../../src/repo/Serialize.h" #include "../utils/Cleaner.h" std::unique_ptr ChunkTest::operator()() { auto tg = std::make_unique("Chunk tests"); tg->addTest(std::make_unique("De/serialize test", std::function([]() { std::vector s1, s2; { std::vector data1{'a', 'b', 'c', 'e'}; Chunk o1(666, {0}, data1); std::vector data2{'q', 'w', 'e', 'r', 'b'}; Chunk o2(777, {1}, data2); s1 = Serialize::serialize(o1); s2 = Serialize::serialize(o2); } { auto s1b = s1.cbegin(); auto s2b = s2.cbegin(); Chunk o1(s1b, s1.cend()), o2(s2b, s2.cend()); std::vector data1{'a', 'b', 'c', 'e'}; Chunk o1e(666, {0}, data1); std::vector data2{'q', 'w', 'e', 'r', 'b'}; Chunk o2e(777, {1}, data2); HelpfulAssert()(o1.id, o1e.id); HelpfulAssert()(o2.id, o2e.id); HelpfulAssert()((int) o1.type, (int) o1e.type); HelpfulAssert()((int) o2.type, (int) o2e.type); HelpfulAssert()(o1.data.size(), o1e.data.size()); HelpfulAssert()(o2.data.size(), o2e.data.size()); for (int i = 0; i < o1.data.size(); i++) { HelpfulAssert()(o1.data[i], o1e.data[i]); } for (int i = 0; i < o2.data.size(); i++) { HelpfulAssert()(o2.data[i], o2e.data[i]); } for (int i = 0; i < o1.md5.size(); i++) { HelpfulAssert()(o1.md5[i], o1e.md5[i]); } for (int i = 0; i < o2.md5.size(); i++) { HelpfulAssert()(o2.md5[i], o2e.md5[i]); } } return true; }))); tg->addTest(std::make_unique("Garbage throw test", std::function([]() { std::vector e{'a', 'b'}; auto eb = e.cbegin(); try { Chunk o1(eb, e.cend()); } catch (...) { return true; } throw Exception("Object constructed with garbage data!"); }))); tg->addTest(std::make_unique("Garbage throw test2", std::function([]() { std::vector e{'s', 'e', 'm', 'b', 'a', 'a'}; auto eb = e.cbegin(); try { Chunk o1(eb, e.cend()); } catch (...) { return true; } throw Exception("Object constructed with garbage data!"); }))); tg->addTest(std::make_unique("Garbage throw test3", std::function([]() { std::vector s1, s2; { std::vector data1{'a', 'b', 'c', 'e'}; Chunk o1(666, {0}, data1); std::vector data2{'q', 'w', 'e', 'r', 'b'}; Chunk o2(777, {1}, data2); s1 = Serialize::serialize(o1); s2 = Serialize::serialize(o2); } { s1.resize(s1.size() - 1); s2.resize(s1.size() - 2); bool fail = false; try { Chunk o1 = Serialize::deserialize(s1); fail = true; } catch (...) { } try { Chunk o2 = Serialize::deserialize(s2); fail = true; } catch (...) { } if (fail) throw Exception("Object constructed with garbage data!"); } return true; }))); return tg; }