Exemple #1
0
void TestCommand::doBinaryTest ()
{
	vector<string> teststrings;
	teststrings.push_back ("binary value");
	teststrings.push_back ("binary value with null");
	teststrings.back ().push_back ('\0');

	teststrings.push_back ("binary value with null");
	teststrings.back ().push_back ('\0');
	teststrings.back () += "in the middle";

	teststrings.push_back (" a very long value with many spaces and basically very very long, but only binary text ... ");
	for (int i = 0; i < 256; ++i)
	{
		teststrings.back ().push_back ('\0');
		teststrings.back () += " very very long, but only binary text ... ";
	}

	teststrings.push_back ("all chars:");
	for (int i = 0; i < 256; ++i)
		teststrings.back ().push_back (i);
	teststrings.push_back ("€");
	for (int i = 0; i < 256; ++i)
	{
		string s;
		s.push_back (i);
		teststrings.push_back (s);
	}


	for (auto & teststring : teststrings)
	{
		{
			KDB kdb;
			Key t = root.dup ();
			t.addBaseName ("binary");
			t.setBinary (teststring.c_str (), teststring.length ());

			KeySet basic;
			basic.append (t);

			KeySet test;
			kdb.get (test, root);
			kdb.set (basic, root);
		}

		{
			KDB kdb;

			KeySet test;
			kdb.get (test, root);

			Key t = root.dup ();
			t.addBaseName ("binary");

			Key res = test.lookup (t);
			nrTest++;
			if (!res)
			{
				nrError++;
				cerr << "Binary test failed (key not found)" << t.getName () << endl;
				continue;
			}

			nrTest++;
			if (res.getBinarySize () > 0 && static_cast<size_t> (res.getBinarySize ()) != teststring.length ())
			{
				nrError++;
				cerr << "Binary test failed (length is not equal)" << endl;
				cerr << "We got: \"" << res.getBinary () << "\"" << endl;
				cerr << "We wanted: \"" << teststring << "\"" << endl;
			}

			nrTest++;
			if (res.getBinary () != teststring)
			{
				nrError++;
				cerr << "Binary test failed (value is not equal)" << endl;
				cerr << "We got: \"" << res.getBinary () << "\"" << endl;
				cerr << "We wanted: \"" << teststring << "\"" << endl;
			}
		}
	}
}
Exemple #2
0
int GetCommand::execute (Cmdline const & cl)
{
	if (cl.arguments.size () != 1) throw invalid_argument ("Need one argument");

	KeySet conf;

	kdb::Key root = cl.createKey (0);
	kdb::KDB kdb (root);

	std::string n;
	if (cl.all)
	{
		n = root.getName ();
		root.setName ("/");
	}

	kdb.get (conf, root);

	if (cl.all)
	{
		root.setName (n);
	}

	// do a lookup without tracer to warm up default cache
	conf.lookup (root);

	root.setCallback (warnOnMeta);
	if (cl.verbose)
	{
		cout << "got " << conf.size () << " keys" << std::endl;
		root.setCallback (printTrace);
	}
	Key k = conf.lookup (root);

	int ret = 0;

	if (k)
	{
		if (cl.verbose)
		{
			cout << "The resulting keyname is " << k.getName () << std::endl;
			cout << "The resulting value size is " << k.getStringSize () << std::endl;
		}

		if (k.isBinary ())
		{
			if (cl.verbose)
			{
				if (k.getBinarySize () == 0)
				{
					cout << "The key is null." << std::endl;
				}
				else
				{
					cout << "The key is binary." << std::endl;
				}
			}
			cout << std::hex;
			const uint8_t * data = static_cast<const uint8_t *> (k.getValue ());
			for (auto position = 0; position < k.getBinarySize (); position++)
			{
				cout << "\\x" << unsigned(data[position]);
			}
			cout << std::dec;
		}
		else
		{
			cout << k.getString ();
		}
	}
	else
	{
		cerr << "Did not find key '" << root.getName () << "'";
		ret = 11;
	}

	if (!cl.noNewline)
	{
		cout << endl;
	}

	printWarnings (cerr, root);
	printError (cerr, root);

	return ret;
}