/** * Check the raw functions when a canonical block is added. * Generate a bundle, add a valid canonical block, convert all to raw. * Create a new bundle from that raw, check that all the blocks are correct. */ TEST(BundleTest, ConstructorWithCanonical) { Bundle b = Bundle("Source", "Destination", "This is a payload"); std::stringstream ss; // Block Type ss << static_cast<uint8_t>(2) << SDNV::encode(std::bitset<7>().to_ulong()); std::string data = std::to_string(rand() + 1); ss << SDNV::encode(data.size()) << data; std::shared_ptr<CanonicalBlock> cb = std::shared_ptr<CanonicalBlock>( new CanonicalBlock(ss.str())); ASSERT_EQ(ss.str(), cb->toRaw()); b.addBlock(cb); ASSERT_EQ(static_cast<uint8_t>(3), b.getBlocks().size()); std::string raw = b.toRaw(); Bundle b1 = Bundle(raw); ASSERT_EQ(b.getBlocks().size(), b1.getBlocks().size()); std::shared_ptr<CanonicalBlock> cb1 = std::static_pointer_cast<CanonicalBlock>(b1.getBlocks()[2]); ASSERT_EQ(cb->toRaw(), cb1->toRaw()); ASSERT_EQ(cb->getBlockType(), cb1->getBlockType()); }
TEST(BundleTest, GetFrameworkExtension) { Bundle b = Bundle("Source", "Destination", "This is a payload"); uint8_t fwkId = 1; std::map<uint8_t, std::shared_ptr<FrameworkExtension>> extensions; nlohmann::json state = {"source", "me"}; uint8_t fwkExtId = 2; std::string code = "code"; std::shared_ptr<FrameworkExtension> ext = std::shared_ptr<FrameworkExtension>( new FrameworkExtension(fwkExtId, code)); extensions.insert(std::pair<uint8_t, std::shared_ptr<FrameworkExtension>> (fwkExtId, ext)); std::shared_ptr<FrameworkMEB> fmeb = std::shared_ptr<FrameworkMEB>(new FrameworkMEB(fwkId, extensions, state)); b.addBlock(fmeb); std::shared_ptr<FrameworkExtension> fe = b.getFwkExt(fwkId, fwkExtId); ASSERT_EQ(fe->getCodeLength(), static_cast<uint16_t>(code.length())); ASSERT_EQ(fe->getFwkExtId(), fwkExtId); ASSERT_EQ(fe->getSwSrcCode(), code); Bundle b2 = Bundle("Source", "Destination", "This is a payload"); ASSERT_THROW(b2.getFwkExt(fwkId, fwkExtId), FrameworkNotFoundException); b2.addBlock(fmeb); uint8_t fwkId2 = 3; uint8_t fwkExtId2 = 4; ASSERT_THROW(b2.getFwkExt(fwkId, fwkExtId2), FrameworkNotFoundException); ASSERT_THROW(b2.getFwkExt(fwkId2, fwkExtId), FrameworkNotFoundException); ASSERT_THROW(b2.getFwkExt(fwkId2, fwkExtId2), FrameworkNotFoundException); }
/** * Check an exception throw when a second payload is inserted. */ TEST(BundleTest, ConstructorWithTwoPayload) { Bundle b = Bundle("Source", "Destination", "This is a payload"); std::shared_ptr<CanonicalBlock> payload = std::shared_ptr<CanonicalBlock>( new PayloadBlock("This is a new payload", false)); ASSERT_THROW(b.addBlock(payload), BundleException); }