Exemple #1
0
Bundle Bundle::fromPath(const QString &path)
{
    Bundle bundle;

    QFileInfo bundlePath(path);
    if (!bundlePath.isReadable()) {
        qCWarning(l) << "bundle" << bundlePath.absolutePath() << "is not readable";
        return Bundle();
    }

    bundle.b->path = path;

    QScopedPointer<QIODevice> manifestJSON(bundle.openFile(Bundle::MANIFEST, QIODevice::Text));
    if (!manifestJSON) {
        qCWarning(l) << "cannot find" << path << "manifest json";
        return Bundle();
    }

    QJsonParseError parseError;
    QJsonDocument doc = QJsonDocument::fromJson(manifestJSON->readAll(), &parseError);
    if (parseError.error != QJsonParseError::NoError) {
        qCWarning(l) << "cannot parse" << path << "manifest json" << parseError.errorString();
        return Bundle();
    }
    manifestJSON->close();
    bundle.b->manifest = doc.object();

    bundle.b->isValid = true;
    return bundle;
}
Exemple #2
0
/**
 * Check the bad raw exception.
 * Create a primary block a payload block and a raw canonical without any flags.
 * Add all of them into a raw bundle.
 * The bundle must throw an exception, because no last block flag is found.
 * Create the canonical block with the raw data, set the flag. Create the new raw
 * bundle, now no exception will be through.
 */
TEST(BundleTest, BadRawException) {
  std::pair<uint64_t, uint64_t> time = TimestampManager::getInstance()
      ->getTimestamp();
  PrimaryBlock pb = PrimaryBlock("Source", "Destination", time.first,
                                 time.second);
  PayloadBlock pb1 = PayloadBlock("This is a payload");
  std::stringstream ss;
  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::string canonical = ss.str();
  ss.str(std::string());
  ss << pb.toRaw() << pb1.toRaw() << canonical;
  ASSERT_THROW(Bundle(ss.str()), BundleCreationException);
  // Add last block flag to canonical.
  ss.str(std::string());
  ss << static_cast<uint8_t>(2) << SDNV::encode(std::bitset<7>().to_ulong())
     << SDNV::encode(data.size()) << data;
  CanonicalBlock cb = CanonicalBlock(ss.str());
  cb.setProcFlag(CanonicalBlockControlFlags::LAST_BLOCK);
  ss.str(std::string());
  ss << pb.toRaw() << pb1.toRaw() << cb.toRaw();
  ASSERT_NO_THROW(Bundle(ss.str()));
  // Check Exception when two payload blocks are present
  ss.str(std::string());
  ss << pb.toRaw() << pb1.toRaw() << pb1.toRaw() << cb.toRaw();
  ASSERT_THROW(Bundle(ss.str()), BundleCreationException);
}
Exemple #3
0
					AnyValue CVDSMModel::toAnyValue() const
					{
						return Bundle({
							{"name", _name},
							{"categories", toAnyArray(_categories)}
						});
					}
Exemple #4
0
		// Geerbt über Serializable
		virtual AnyValue toAnyValue() const override
		{
			return Bundle({
				{ "id", id },
				{ "value", value }
			});
		}
					AnyValue CVImageCaption::toAnyValue() const
					{
						return Bundle({
							{ "text", _text },
							{ "confidence", _confidence }
						});
					}
Exemple #6
0
/**
 * Check that a bundle id is correctly generated.
 * Bundle id format:
 * <Source><CreationTimestamp><TimestampSeqNumber>
 */
TEST(BundleTest, BundleId) {
  Bundle b = Bundle("Source", "Destination", "This is a payload");
  std::stringstream ss;
  ss << b.getPrimaryBlock()->getSource() << "_"
     << b.getPrimaryBlock()->getCreationTimestamp() << "_"
     << b.getPrimaryBlock()->getCreationTimestampSeqNumber();
  ASSERT_EQ(ss.str(), b.getId());
}
Exemple #7
0
/**
 * Check the constructor with parameters.
 * The primary block must contain the source and the destination.
 * The bundle must contain a payloadBlock with the payload value.
 */
TEST(BundleTest, FilledConstructor) {
  Bundle b = Bundle("source", "destination", "payload");
  ASSERT_EQ("source", b.getPrimaryBlock()->getSource());
  ASSERT_EQ("destination", b.getPrimaryBlock()->getDestination());
  ASSERT_EQ(static_cast<uint8_t>(2), b.getBlocks().size());
  ASSERT_EQ(static_cast<uint8_t>(CanonicalBlockTypes::PAYLOAD_BLOCK),
            b.getPayloadBlock()->getBlockType());
}
Exemple #8
0
/**
 * Check the raw constructor.
 * Generate a bundle, convert it to raw, and generate a new bundle from that
 * raw.
 * The blocks must be the same.
 */
TEST(BundleTest, RawFunctions) {
  Bundle b = Bundle("Source", "Destination", "This is a payload");
  std::string raw = b.toRaw();
  Bundle b1 = Bundle(raw);
  ASSERT_EQ(b.getPrimaryBlock()->getSource(),
            b1.getPrimaryBlock()->getSource());
  ASSERT_EQ(b.getPrimaryBlock()->getDestination(),
            b1.getPrimaryBlock()->getDestination());
  ASSERT_EQ(b.getBlocks().size(), b1.getBlocks().size());
  ASSERT_EQ(
      std::static_pointer_cast<CanonicalBlock>(b.getBlocks()[1])->getBlockType(),
      std::static_pointer_cast<CanonicalBlock>(b1.getBlocks()[1])->getBlockType());
  std::shared_ptr<CanonicalBlock> PB = std::static_pointer_cast<CanonicalBlock>(
      b.getBlocks()[1]);
  std::shared_ptr<CanonicalBlock> PB1 =
      std::static_pointer_cast<CanonicalBlock>(b1.getBlocks()[1]);
  ASSERT_EQ(std::static_pointer_cast<PayloadBlock>(PB)->getPayload(),
            std::static_pointer_cast<PayloadBlock>(PB1)->getPayload());
}
Exemple #9
0
/**
 * 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());
}
Exemple #10
0
Bundle Bundle::split(StandardSegment &s)
{
	/**
	This will return the left/smaller part of split
	and modify the current tree to hold the right/bigger part
	*/
	Bundle rv = Bundle(STree::split(s, STree::ITEM_NOT_FOUND), NULL, bottom_line);
	rv.top_line = (StandardSegment*)&rv.findMax();
	bottom_line = (STree::root->element);
	return rv;
}
	TEST(PHPSessionSerializer, Serialize)
	{
		Bundle b({
			{ "bool", true },
			{ "string", "Hallo" },
			{ "array", Bundle({ { "0", std::string("hallo") } }) },
			{ "null", nullptr },
			{ "double", 10.0 }
		});
		std::string res = Serialize::PHPSessionSerializer().serialize(b);
		ASSERT_EQ("array|a:1:{s:1:\"0\";s:5:\"hallo\";}bool|b:1;double|d:10.000000;null|N;string|s:5:\"Hallo\";", res);
	}
Exemple #12
0
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);
}
Exemple #13
0
Bundle BundleHooks::FilterBundle(const BundleContext& context,
                                 const Bundle& bundle) const
{
  if (!bundle) {
    return bundle;
  }

  std::vector<ServiceRegistrationBase> srl;
  coreCtx->services.Get(us_service_interface_iid<BundleFindHook>(), srl);
  if (srl.empty()) {
    return bundle;
  } else {
    std::vector<Bundle> ml;
    ml.push_back(bundle);
    this->FilterBundles(context, ml);
    return ml.empty() ? Bundle() : bundle;
  }
}
Exemple #14
0
/**
 * Send a bundle to check it with the wireshark.
 * The bundle must be valid.
 * It will appears under UDP.
 */
TEST(BundleTest, WiresharkTest) {
  Bundle b = Bundle("node100", "node101", "This is a test payload");
  std::string raw = b.toRaw();
  sockaddr_in remote = { 0 };
  remote.sin_family = AF_INET;
  remote.sin_port = htons(0);
  remote.sin_addr.s_addr = htonl(INADDR_ANY);
  int sock = socket(AF_INET, SOCK_DGRAM, 0);
  EXPECT_LE(0,
            bind(sock, reinterpret_cast<sockaddr*>(&remote), sizeof(remote)));
  sockaddr_in destination = { 0 };
  destination.sin_family = AF_INET;
  destination.sin_port = htons(4556);
  inet_aton("127.0.0.1", &destination.sin_addr);
  EXPECT_LT(
      0,
      sendto(sock, raw.c_str(), raw.size(), 0,
             reinterpret_cast<sockaddr*>(&destination), sizeof(destination)));
}
Exemple #15
0
UkrWord::UkrWord(QGraphicsView * view):
    UkrWord(view,Bundle())
{
}
Exemple #16
0
/**
 * Check that toRaw saves the raw into the bundle.
 */
TEST(BundleTest, RawBundleTest) {
  Bundle b = Bundle("Source", "Destination", "This is a payload");
  ASSERT_EQ("", b.getRaw());
  std::string raw = b.toRaw();
  ASSERT_EQ(raw, b.getRaw());
}
Exemple #17
0
Bundle Bundle::subBundle(const Path& inRelativePath)
{
  return Bundle(_path / inRelativePath);
}
Exemple #18
0
/**
 * 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);
}
Exemple #19
0
		AnyValue BsonSerializer::deserialize(const std::string & str)
		{
			Bundle res;
			std::vector<uint8_t> data(str.begin(), str.end());
			BufferReader rdr(data);
			if (rdr.getSize() < 4)
				throw std::invalid_argument("Document must be at least 4 byte long");

			rdr.readInt32();
			while (true)
			{
				uint8_t field_type = rdr.readUInt8();
				if (field_type == 0x00)
					break;

				std::string name = rdr.readCString();

				if (field_type == 0x01)
				{
					res.set(name, rdr.readDouble());
				}
				else if (field_type == 0x02)
				{
					int32_t len = rdr.readInt32();
					res.set(name, rdr.readString(len - 1));
					rdr.readUInt8(); // skip trailing 0
				}
				else if (field_type == 0x03)
				{
					// Document
					int32_t len = rdr.peekInt32();
					const uint8_t* ptr = rdr.readBytes(len);
					std::string document(ptr, ptr + len);
					res.set(name, this->deserialize(document));
				}
				else if (field_type == 0x04)
				{
					// Array
					int32_t len = rdr.peekInt32();
					const uint8_t* ptr = rdr.readBytes(len);
					std::string document(ptr, ptr + len);
					std::vector<AnyValue> bdocres;
					for (auto& elem : this->deserialize(document).as<Bundle>()) bdocres.push_back(elem.second);

					res.set(name, bdocres);
				}
				else if (field_type == 0x05)
				{
					// Binary Data
					int32_t len = rdr.readInt32();
					rdr.readUInt8(); // Subtype
									 // TODO: What to do with subtype ?
					auto ptr = rdr.readBytes(len);
					res.set(name, std::vector<uint8_t>(ptr, ptr + len));
				}
				else if (field_type == 0x06)
				{
					// Deprecated
				}
				else if (field_type == 0x07)
				{
					// ObjectID
					// We don't know what to do with it in standard implementations.
					rdr.readBytes(12);
				}
				else if (field_type == 0x08)
				{
					// Boolean
					res.set(name, (rdr.readUInt8() == 1));
				}
				else if (field_type == 0x09)
				{
					// UTC Date int64
					res.set(name, rdr.readInt64());
				}
				else if (field_type == 0x0A)
				{
					res.set(name, nullptr);
				}
				else if (field_type == 0x0B)
				{
					rdr.readCString();
					rdr.readCString();
				}
				else if (field_type == 0x0C)
				{
					// DB Pointer
					// Deprecated
					int32_t len = rdr.readInt32();
					rdr.readBytes(len + 12);
				}
				else if (field_type == 0x0D)
				{
					// Javascript code
					// Is this a good idea ?
					Bundle js;
					int32_t len = rdr.readInt32();
					js.set("js", rdr.readString(len));
					rdr.readUInt8(); // skip trailing 0
					js.set("scope", Bundle());
					res.set(name, js);
				}
				else if (field_type == 0x0E)
				{
					// Deprecated
					int32_t len = rdr.readInt32();
					rdr.readBytes(len);
				}
				else if (field_type == 0x0F)
				{
					// Javascript code with scope
					// Is this a good idea ?
					Bundle js;
					rdr.readInt32(); // code length
					int32_t jslen = rdr.readInt32();
					js.set("js", rdr.readString(jslen));
					rdr.readUInt8(); // skip trailing 0
					int32_t doclen = rdr.peekInt32();
					auto docptr = rdr.readBytes(doclen);
					js.set("scope", this->deserialize(std::string(docptr, docptr + doclen)));
					res.set(name, js);
				}
				else if (field_type == 0x10)
				{
					// INT32
					res.set(name, rdr.readInt32());
				}
				else if (field_type == 0x11)
				{
					// Timestamp
					res.set(name, rdr.readInt64());
				}
				else if (field_type == 0x12)
				{
					// Int64
					res.set(name, rdr.readInt64());
				}
				else if (field_type == 0xFF)
				{
					// Min key
				}
				else if (field_type == 0x7F)
				{
					// Max key
				}
			}

			return res;
		}