TEST_F (Simple, RemoveFile)
{
	using namespace kdb;
	KDB kdb;
	KeySet ks;
	kdb.get (ks, testRoot);
	ks.append (Key ("system" + testRoot + "remove", KEY_END));
	ASSERT_EQ (ks.size (), 1) << "could not append key\n" << ks;
	kdb.set (ks, testRoot);
	ASSERT_EQ (ks.size (), 1) << "key gone after kdb.set?\n" << ks;

	struct stat buf;
	ASSERT_EQ (stat (mp->systemConfigFile.c_str (), &buf), 0) << "found no file";

	Key parentKey;
	kdb.close (parentKey);
	kdb.open (parentKey);

	kdb.get (ks, testRoot);
	ks.clear ();
	ASSERT_EQ (ks.size (), 0) << "keyset should be empty after clearing it\n" << ks;
	kdb.set (ks, testRoot);

	ASSERT_EQ (stat (mp->systemConfigFile.c_str (), &buf), -1) << "found wrong file";
}
TEST_F (Simple, SetSystemGetAppend2)
{
	using namespace kdb;
	KDB kdb;
	KeySet ks;
	Key parentKey (testRoot, KEY_END);
	ks.append (Key ("system" + testRoot + "key", KEY_VALUE, "value1", KEY_END));
	kdb.get (ks, parentKey);
	ASSERT_EQ (ks.size (), 1) << "got keys from freshly mounted backends";
	ks.rewind ();
	ks.next ();
	EXPECT_EQ (ks.current ().getName (), "system/tests/kdb/key") << "name of element in keyset wrong";
	EXPECT_EQ (ks.current ().getString (), "value1") << "string of element in keyset wrong";
	kdb.set (ks, parentKey);
	kdb.close (parentKey);

	KeySet ks2;
	ks2.append (Key ("system" + testRoot + "key2", KEY_VALUE, "value2", KEY_END));
	kdb.open (parentKey);
	kdb.get (ks2, parentKey);
	ks2.rewind ();
	ks2.next ();
	ASSERT_EQ (ks2.size (), 1) << "wrong size";
	EXPECT_EQ (ks2.current ().getName (), "system/tests/kdb/key") << "name of element in keyset wrong";
	EXPECT_EQ (ks2.current ().getString (), "value1") << "string of element in keyset wrong";
}
TEST_F (Simple, GetSystem)
{
	using namespace kdb;
	KDB kdb;
	KeySet ks;
	Key parentKey ("system" + testRoot, KEY_END);
	ks.append (Key (parentKey.getName () + "/key", KEY_END));
	EXPECT_NE (kdb.get (ks, parentKey), -1);
	ASSERT_EQ (ks.size (), 1) << "no key stayed" << ks;
	ks.rewind ();
	ks.next ();
	EXPECT_EQ (ks.current ().getName (), "system/tests/kdb/key") << "name of element in keyset wrong";
	EXPECT_EQ (ks.current ().getString (), "") << "string of element in keyset wrong";

	ASSERT_NE (kdb.set (ks, parentKey), -1);
	ks.rewind ();
	ks.next ();
	EXPECT_EQ (ks.current ().getName (), "system/tests/kdb/key") << "name of element in keyset wrong";
	EXPECT_EQ (ks.current ().getString (), "") << "string of element in keyset wrong";
	kdb.close (parentKey);

	KeySet ks2;
	kdb.open (parentKey);
	kdb.get (ks2, parentKey);
	ks.rewind ();
	ks.next ();
	EXPECT_EQ (ks.current ().getName (), "system/tests/kdb/key") << "name of element in keyset wrong";
	EXPECT_EQ (ks.current ().getString (), "") << "string of element in keyset wrong";
}
kdb::Key mountBackend (int iteration)
{
	using namespace kdb;
	using namespace kdb::tools;

	Key mp = getMountpointForIteration<VARIANT> (iteration);
	std::string cf = "benchmark_" + plugin_variant_names[VARIANT] + "_" + std::to_string (iteration) + ".ecf";
	unlink (cf.c_str ());

	KDB kdb;
	KeySet mountConfig;
	kdb.get (mountConfig, "system/elektra/mountpoints");

	MountBackendBuilder b;
	b.setMountpoint (mp, KeySet (0, KS_END));
	b.addPlugin (PluginSpec ("resolver"));
	b.useConfigFile (cf);

	b.addPlugin (PluginSpec ("dump"));
	if (VARIANT != NO_CRYPTO)
	{
		KeySet pluginConfig;
		pluginConfig.append (Key ("user/encrypt/key", KEY_VALUE, GPG_TEST_KEY_ID, KEY_END));
		pluginConfig.append (Key ("user/gpg/unit_test", KEY_VALUE, "1", KEY_END));
		b.addPlugin (PluginSpec (plugin_variant_names[VARIANT], pluginConfig));
	}

	b.validated ();
	b.serialize (mountConfig);
	kdb.set (mountConfig, "system/elektra/mountpoints");
	kdb.close ();
	return mp;
}
Example #5
0
int main()
{
	using namespace kdb;

	KDB kdb;
	KeySet ks;
	Context c;
	kdb.get(ks, "/test/lift");
	kdb.get(ks, "/test/material_lift");
	kdb.get(ks, "/test/heavy_material_lift");
	kdb.get(ks, "/test/person_lift");

	Parameters par(ks,c);

	std::cout << std::boolalpha;
	std::cout << "delay: " << par.test.lift.emergency.delay << std::endl;
	std::cout << "stops: " << par.test.lift.emergency.action.stops << std::endl;
	kdb::test::Lift const & lift = par.test.lift;
	std::cout << "height #3: " << lift.floor.n3.height << std::endl;
	std::cout << "limit: " << par.test.lift.limit << std::endl;

	bool write = lift.write;
	par.test.lift.write = false;

	// write back to user/test/lift, see comments in lift.c
	if(write)
	{
		kdb.set(ks, "user/test/lift");
	}

	return 0;
}
Example #6
0
int main(int argc, char**argv)
{
	using namespace kdb;

	KDB kdb;
	KeySet ks;
	kdb.get(ks, "/test/lift");
	kdb.get(ks, "/test/material_lift");
	kdb.get(ks, "/test/heavy_material_lift");
	kdb.get(ks, "/test/person_lift");

	Parameters par(ks);

	std::cout << std::boolalpha;
	std::cout << "delay: " << par.getTestLiftEmergencyDelay() << std::endl;
	std::cout << "stops: " << par.getTestLiftEmergencyActionStops() << std::endl;
	// std::cout << "algorithm: " << par.getTestLiftAlgorithm() << std::endl;
	std::cout << "height #3: " << par.getTestLiftFloor3Height() << std::endl;
	std::cout << "limit: " << par.getTestLiftLimit() << std::endl;

	bool write = par.getTestLiftWrite();
	par.setTestLiftWrite(false);

	// write back to user/test/lift, see comments in lift.c
	if (write)
	{
		kdb.set(ks, "user/test/lift");
	}

	return 0;
}
Example #7
0
void TestCommand::doBasicTest ()
{
	{
		KDB kdb;
		Key t = root.dup ();
		t.addBaseName ("basic");
		t.setString ("BasicString");
		KeySet basic;
		basic.append (t);

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

	{
		KDB kdb;
		Key t = root.dup ();
		t.addBaseName ("basic");
		t.setString ("BasicString");

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

		nrTest++;
		if (!test.lookup (t))
		{
			nrError++;
			cerr << "Basic test failed" << endl;
		}
	}
}
TEST_F (Simple, GetAppendMeta)
{
	using namespace kdb;
	KDB kdb;
	KeySet ks;
	ks.append (Key ("meta/key", KEY_META_NAME, KEY_END));
	Key parentKey (testRoot, KEY_END);
	kdb.get (ks, parentKey);
	ASSERT_EQ (ks.size (), 1) << "no key stayed";
	ks.rewind ();
	ks.next ();
	EXPECT_EQ (ks.current ().getName (), "meta/key") << "name of element in keyset wrong";
	EXPECT_EQ (ks.current ().getString (), "") << "string of element in keyset wrong";
	kdb.set (ks, parentKey);
	ks.rewind ();
	ks.next ();
	EXPECT_EQ (ks.current ().getName (), "meta/key") << "name of element in keyset wrong";
	EXPECT_EQ (ks.current ().getString (), "") << "string of element in keyset wrong";
	kdb.close (parentKey);

	KeySet ks2;
	kdb.open (parentKey);
	kdb.get (ks2, parentKey);
	ASSERT_EQ (ks2.size (), 0) << "got keys from freshly mounted backends";
}
int main()
{
	using namespace kdb;

	KDB kdb;
	KeySet ks;
	kdb.get(ks, "/test/lift");
	kdb.get(ks, "/test/material_lift");
	kdb.get(ks, "/test/heavy_material_lift");
	kdb.get(ks, "/test/person_lift");

	Parameters par(ks);

	std::cout << std::boolalpha;
	std::cout << "delay: " << par.test().lift().emergency().getDelay() << std::endl;
	std::cout << "stops: " << par.test().lift().emergency().action().getStops() << std::endl;
	// std::cout << "algorithm: " << par.getTestLiftAlgorithm() << std::endl;
	kdb::test::Lift const & lift = par.test().lift();
	std::cout << "height #3: " << lift.floor().n3().getHeight() << std::endl;
	std::cout << "limit: " << par.test().lift().getLimit() << std::endl;

	bool write = lift.getWrite();
	par.test().lift().setWrite(false);

	// write back to user/test/lift, see comments in lift.c
	if(write)
	{
		kdb.set(ks, "user/test/lift");
	}

	return 0;
}
void test_kdbGetSet()
{
	cout << "testing kdbSet() and kdbGet()" << endl;

	{
		KeySet ks_set (5,
			*Key ("user/tests/key3", KEY_DIR, KEY_END),
			*Key ("user/tests/key3/1", KEY_END),
			*Key ("user/tests/key3/2", KEY_END),
			*Key ("user/tests/key3/3", KEY_VALUE, "value", KEY_END),
			KS_END);
		KeySet ks;
		KDB kdb;
		kdb.get (ks, "user/tests/key3");
		ks.append(ks_set);
		kdb.set (ks, "user/tests/key3");
	}

	// check if they were written
	{
		KDB kdb;
		KeySet ks;
		kdb.get (ks, "user/tests/key3");
		exit_if_fail(ks.lookup("user/tests/key3/3"), "could not find previously written key");
		succeed_if(ks.lookup("user/tests/key3/3").get<std::string>() == "value", "could not get value");
		succeed_if(!ks.lookup("user/tests/key3/3").needSync(), "should not need sync");
	}

	// now remove keys (cleanup)
	{
		KeySet ks;
		KDB kdb;
		kdb.get (ks, "user/tests/key3");
		ks.cut(Key("user/tests/key3", KEY_END));
		kdb.set (ks, "user/tests/key3");
	}

	// check if its gone now
	{
		KDB kdb;
		KeySet ks;
		kdb.get (ks, "user/tests/key3");
		succeed_if(!ks.lookup("user/tests/key3/3"), "key was not removed");
	}

}
TEST_F (Simple, WrongStateCascading)
{
	using namespace kdb;
	KDB kdb;
	KeySet ks;
	Key parentKey (testRoot, KEY_END);
	EXPECT_THROW (kdb.set (ks, parentKey), kdb::KDBException) << "kdb set without prior kdb get should have 107 Wrong State";
	ASSERT_EQ (ks.size (), 0) << "got keys from freshly mounted backends" << ks;
}
Example #12
0
int main ()
{
	using namespace kdb;
	KDB kdb;
	KeySet conf;
	kdb.get (conf, "/");
	std::cout << conf;
	kdb.set (conf, "/");
}
TEST_F (Simple, WrongParent)
{
	using namespace kdb;
	KDB kdb;
	Key parent ("meta", KEY_META_NAME, KEY_END);
	KeySet ks;
	EXPECT_THROW (kdb.set (ks, parent), kdb::KDBException);
	ASSERT_EQ (ks.size (), 0) << "got keys from freshly mounted backends" << ks;
}
Example #14
0
void TestCommand::doStringTest ()
{
	vector<string> teststrings;
	teststrings.push_back ("");
	teststrings.push_back ("value");
	teststrings.push_back ("value with spaces");
	teststrings.push_back (" a very long value with many spaces and basically very very long, but only text ... ");
	for (int i = 1; i < 256; ++i)
		teststrings.back () += " very very long, but only text ... ";


	for (auto & teststring : teststrings)
	{
		{
			KDB kdb;
			Key t = root.dup ();
			t.addBaseName ("string");
			t.setString (teststring);

			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 ("string");

			Key res = test.lookup (t);

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

			nrTest++;
			if (res.getString () != teststring)
			{
				nrError++;
				cerr << "String test failed (value is not equal)" << endl;
				cerr << "We got: \"" << res.getString () << "\"" << endl;
				cerr << "We wanted: \"" << teststring << "\"" << endl;
			}
		}
	}
}
TEST_F (Simple, SetNothing)
{
	using namespace kdb;
	KDB kdb;
	KeySet ks;
	kdb.get (ks, testRoot);
	ASSERT_EQ (ks.size (), 0) << "got keys from freshly mounted backends" << ks;
	kdb.set (ks, testRoot);
	ASSERT_EQ (ks.size (), 0) << "got keys from freshly mounted backends" << ks;
	struct stat buf;
	ASSERT_EQ (stat (mp->systemConfigFile.c_str (), &buf), -1) << "found wrong file";
}
TEST_F (Simple, GetCascading)
{
	using namespace kdb;
	KDB kdb;
	KeySet ks;
	Key parentKey (testRoot, KEY_END);
	kdb.get (ks, parentKey);
	ASSERT_EQ (ks.size (), 0) << "got keys from freshly mounted backends" << ks;

	Key setParentKey ("system" + testRoot, KEY_END);
	kdb.set (ks, setParentKey);
	kdb.close (parentKey);
}
/**
 * @brief Allows for updating of a database entry.
 *
 * Will renew the entry and all its subkeys (configuration).
 *
 * @param entry A custom Entry object holding current information.
 * @return true if the entry was updated, false if not
 * @throw kdbrest::exception::EntryNotFoundException in case the entry
 * to update does not exist.
 */
bool StorageEngine::updateEntry (model::Entry & entry)
{
    using namespace kdb;

    // register exclusive access
    boost::unique_lock<boost::shared_mutex> lock (m_mutex_entryCache);

    bool found = false;
    std::vector<model::Entry> & entries = this->m_entryCache;
    unsigned int i = 0;
    while (i < entries.size ())
    {
        if (entries[i].getName ().compare (entry.getName ()) == 0)
        {
            found = true;
            break;
        }
        i++;
    }

    if (!found)
    {
        throw exception::EntryNotFoundException ();
    }

    KDB kdb;
    KeySet ks;
    kdb.get (ks, entry.getName ());

    Key k = ks.lookup (entry.getName ());
    if (!k)
    {
        throw kdbrest::exception::EntryNotFoundException ();
    }

    ks.cut (entry);
    ks.append (entry);
    ks.append (entry.getSubkeys ());

    if (kdb.set (ks, entry.getName ()) >= 1)
    {
        entries.erase (entries.begin () + i);
        entries.push_back (entry);
        return true;
    }
    else
    {
        return false;
    }
}
TEST_F (Simple, TriggerError)
{
	using namespace kdb;
	KDB kdb;
	KeySet ks;
	EXPECT_EQ (kdb.get (ks, testRoot), 0) << "nothing to do in get";
	ks.append (Key ("system" + testRoot + "a", KEY_END));
	ks.append (Key ("system" + testRoot + "k", KEY_META, "trigger/error", "10", KEY_END));
	ks.append (Key ("system" + testRoot + "z", KEY_END));
	struct stat buf;
	ASSERT_EQ (stat (mp->systemConfigFile.c_str (), &buf), -1) << "found wrong file";
	EXPECT_THROW (kdb.set (ks, testRoot), kdb::KDBException) << "could not trigger error";
	ASSERT_EQ (ks.size (), 3) << "key suddenly missing";
	EXPECT_EQ (ks.current ().getName (), "system" + testRoot + "k") << "ks should point to error key";
	ASSERT_EQ (stat (mp->systemConfigFile.c_str (), &buf), -1) << "created file even though error triggered";
}
/**
 * @brief Allows for deleting of an user entry.
 *
 * Will delete the entry iteself as well as all subkeys (additional user information).
 *
 * @param user A custom User object that should be deleted.
 * @return true if the user was deleted successfully, false otherwise
 * @throw kdbrest::exception::UserNotFoundException in case the user
 * to delete does not exist.
 */
bool StorageEngine::deleteUser (model::User & user)
{
    using namespace kdb;

    // register exclusive access
    boost::unique_lock<boost::shared_mutex> lock (m_mutex_userCache);

    bool found = false;
    std::vector<model::User> & users = this->m_userCache;
    unsigned int i = 0;
    while (i < users.size ())
    {
        if (users[i].getName ().compare (user.getName ()) == 0)
        {
            found = true;
            break;
        }
        i++;
    }

    if (!found)
    {
        throw exception::UserNotFoundException ();
    }

    KDB kdb;
    KeySet ks;
    kdb.get (ks, user.getName ());

    Key k = ks.lookup (user.getName ());
    if (!k)
    {
        throw kdbrest::exception::UserNotFoundException ();
    }

    ks.cut (user);

    if (kdb.set (ks, user.getName ()) >= 1)
    {
        users.erase (users.begin () + i);
        return true;
    }
    else
    {
        return false;
    }
}
Example #20
0
int TestCommand::execute(Cmdline const& cl)
{
	if (cl.arguments.size() < 1)
	{
		throw invalid_argument ("need at least one argument");
	}

	// do a basic check on every argument
	for (size_t i=1; i<cl.arguments.size(); ++i)
	{
		string name = " ";
		name += cl.arguments[i];
		name += " ";
		if (testNames.find(name) == std::string::npos)
		{
			throw invalid_argument ("test name " +
					cl.arguments[i] +
					" does not exist in:" +
					testNames);
		}
	}

	printWarnings(cerr, root);

	root = cl.createKey(0);

	KDB kdb;
	KeySet original;
	kdb.get(original, root);
	original.rewind();

	doTests(cl.arguments);

	cerr << "We got " << nrError << " errors in " << nrTest << " testcases." << endl;

	cout << "Test suite is now finished." << endl;
	cout << "Now restoring the original keyset." << endl;
	kdb.set(original, root);

	printWarnings(cerr, root);

	return nrError;
}
TEST_F (Simple, MetaInSet)
{
	using namespace kdb;
	KDB kdb;
	KeySet ks;
	Key parent (testRoot, KEY_END);
	kdb.get (ks, parent);
	ASSERT_EQ (ks.size (), 0) << "got keys from freshly mounted backend" << ks;

	ks.append (Key ("meta" + testRoot + "wrong_meta_key", KEY_META_NAME, KEY_END));

	ASSERT_EQ (ks.size (), 1) << "key not inserted:\n" << ks;
	kdb.set (ks, parent);
	printError (std::cout, parent);
	printWarnings (std::cout, parent);
	ASSERT_EQ (ks.size (), 1) << "got wrong keys:\n" << ks;
	struct stat buf;
	ASSERT_EQ (stat (mp->systemConfigFile.c_str (), &buf), -1) << "did find config file";
}
TEST_F (Simple, GetAppendCascading)
{
	using namespace kdb;
	KDB kdb;
	KeySet ks;
	ks.append (Key (testRoot + "key", KEY_END));
	Key parentKey (testRoot, KEY_END);
	std::string myRoot = testRoot.substr (0, testRoot.length () - 1);
	EXPECT_EQ (parentKey.getName (), myRoot);
	EXPECT_EQ (parentKey.getString (), "");
	kdb.get (ks, parentKey);
	EXPECT_EQ (parentKey.getName (), myRoot);
	std::string fn = parentKey.getString ();
	EXPECT_EQ (fn.substr (fn.find_last_of ('/') + 1), "kdbFile.dump");
	parentKey.setString ("");

	ASSERT_EQ (ks.size (), 1) << "no key stayed" << ks;
	ks.rewind ();
	ks.next ();
	EXPECT_EQ (ks.current ().getName (), "/tests/kdb/key") << "name of element in keyset wrong";
	EXPECT_EQ (ks.current ().getString (), "") << "string of element in keyset wrong";
	kdb.set (ks, parentKey);
	EXPECT_EQ (parentKey.getName (), myRoot);
	EXPECT_EQ (parentKey.getString (), "");
	ks.rewind ();
	ks.next ();
	EXPECT_EQ (ks.current ().getName (), "/tests/kdb/key") << "name of element in keyset wrong";
	EXPECT_EQ (ks.current ().getString (), "") << "string of element in keyset wrong";
	kdb.close (parentKey);
	EXPECT_EQ (parentKey.getName (), myRoot);
	EXPECT_EQ (parentKey.getString (), "");

	KeySet ks2;
	kdb.open (parentKey);
	EXPECT_EQ (parentKey.getName (), myRoot);
	EXPECT_EQ (parentKey.getString (), "");
	kdb.get (ks2, parentKey);
	EXPECT_EQ (parentKey.getName (), myRoot);
	fn = parentKey.getString ();
	EXPECT_EQ (fn.substr (fn.find_last_of ('/') + 1), "kdbFile.dump");
	ASSERT_EQ (ks2.size (), 0) << "got keys from freshly mounted backends";
}
TEST_F (Simple, TryChangeAfterSet)
{
	using namespace kdb;
	KDB kdb;
	KeySet ks;
	std::string name = "system" + testRoot + "try_change";
	Key k (name, KEY_END);
	EXPECT_EQ (k.getName (), name);
	ks.append (k);
	EXPECT_THROW (k.setName ("user/x"), kdb::KeyInvalidName);
	EXPECT_EQ (k.getName (), name);
	kdb.get (ks, testRoot);
	ASSERT_EQ (ks.size (), 1) << "lost keys in get\n" << ks;
	kdb.set (ks, testRoot);
	EXPECT_THROW (k.setName ("user/x"), kdb::KeyInvalidName);
	EXPECT_EQ (k.getName (), name);
	ASSERT_EQ (ks.size (), 1) << "got no keys\n" << ks;
	struct stat buf;
	ASSERT_EQ (stat (mp->systemConfigFile.c_str (), &buf), 0) << "did not find config file";
}
TEST_F (Simple, EverythingInGetSet)
{
	using namespace kdb;
	KDB kdb;
	KeySet ks = getAll ();
	Key parent (testRoot, KEY_END);
	kdb.get (ks, parent);
	ASSERT_EQ (ks.size (), 714) << "did not keep" << ks;

	kdb.set (ks, parent);
	printError (std::cout, parent);
	printWarnings (std::cout, parent);
	ASSERT_EQ (ks.size (), 714) << "got wrong keys:\n" << ks;
	// KeySet cmp = getAll();
	// ASSERT_EQ(ks, cmp);
	// ASSERT_THAT(ks, ContainerEq(cmp));
	compareKeySet (ks, getAll ());
	struct stat buf;
	ASSERT_EQ (stat (mp->systemConfigFile.c_str (), &buf), -1) << "did find config file";
}
int main ()
{
	KeySet config;
	KDB kdb;
	kdb.get (config, "/sw/MyApp");

	Key k = config.lookup ("/sw/MyApp/mykey");
	if (k)
	{
		k.set<int> (k.get<int> () + 1);
	}
	else
	{
		Key n;
		n.setName ("user/sw/MyApp/mykey");
		n.set<int> (0);
		config.append (n);
	}

	kdb.set (config, "/sw/MyApp");
}
/**
 * @brief Can be used to create an entry in the database.
 *
 * Will add the entry and all subkeys to the database (configuration).
 *
 * @param entry A custom Entry object holding information to store.
 * @return true if the entry was stored, false if something went wrong
 * @throws kdbrest::exception::EntryAlreadyExistsException in case an
 * entry with the given name does already exist in the key database.
 */
bool StorageEngine::createEntry (model::Entry & entry)
{
    using namespace kdb;

    // register exclusive access
    boost::unique_lock<boost::shared_mutex> lock (m_mutex_entryCache);

    std::vector<model::Entry> & entries = this->m_entryCache;
    for (auto & elem : entries)
    {
        if (elem.getName ().compare (entry.getName ()) == 0)
        {
            throw exception::EntryAlreadyExistsException ();
        }
    }

    KDB kdb;
    KeySet ks;
    kdb.get (ks, entry.getName ());

    Key k = ks.lookup (entry.getName ());
    if (k)
    {
        throw kdbrest::exception::EntryAlreadyExistsException ();
    }

    ks.append (entry);
    ks.append (entry.getSubkeys ());

    if (kdb.set (ks, entry.getName ()) >= 1)
    {
        entries.push_back (entry);
        return true;
    }
    else
    {
        return false;
    }
}
/**
 * @brief Can be used to create an user entry in the database.
 *
 * Will add the user and all subkeys to the database (additional information).
 *
 * @param user A custom User object holding information to store.
 * @return true if the user was stored, false if something went wrong
 * @throws kdbrest::exception::UserAlreadyExistsException in case an
 * user with the given name does already exist in the key database.
 */
bool StorageEngine::createUser (model::User & user)
{
    using namespace kdb;

    // register exclusive access
    boost::unique_lock<boost::shared_mutex> lock (m_mutex_userCache);

    std::vector<model::User> & users = this->m_userCache;
    for (auto & elem : users)
    {
        if (elem.getName ().compare (user.getName ()) == 0)
        {
            throw exception::UserAlreadyExistsException ();
        }
    }

    KDB kdb;
    KeySet ks;
    kdb.get (ks, user.getName ());

    Key k = ks.lookup (user.getName ());
    if (k)
    {
        throw exception::UserAlreadyExistsException ();
    }

    ks.append (user);
    ks.append (user.getSubkeys ());

    if (kdb.set (ks, user.getName ()) >= 1)
    {
        users.push_back (user);
        return true;
    }
    else
    {
        return false;
    }
}
__attribute__ ((noinline)) void benchmark_crypto_set (int iteration)
{
	using namespace kdb;
	using namespace kdb::tools;
	static Timer t (plugin_variant_names[VARIANT]);

	Key mp = mountBackend<VARIANT> (iteration);

	{
		KDB kdb;
		KeySet ks;

		kdb.get (ks, mp);
		for (int i = 0; i < nr_keys; ++i)
		{
			// clang-format off
			ks.append (Key (mp.getName () + "/k" + std::to_string (i),
					KEY_VALUE, "value",
					KEY_META, "crypto/encrypt", "1",
					KEY_END));
			// clang-format on
		}

		/***************************************************************************
		 * start of measurement
		 **************************************************************************/
		t.start ();
		kdb.set (ks, mp);
		t.stop ();
		/***************************************************************************
		 * end of measurement
		 **************************************************************************/

		kdb.close ();
	}

	std::cout << t;
}
Example #29
0
int main()
{
    using namespace kdb;

    KDB kdb;
    KeySet ks;
    Coordinator c;
    ThreadContext tc(c);
    kdb.get(ks, "/test/lift");
    kdb.get(ks, "/test/material_lift");
    kdb.get(ks, "/test/heavy_material_lift");
    kdb.get(ks, "/test/person_lift");

    Environment <ContextPolicyIs<ThreadContext>> env(ks,tc);
    // Environment <ContextPolicyIs<ThreadContext>, WritePolicyIs<ReadOnlyPolicy>> env(ks,tc);
    std::cout << std::boolalpha;
    std::cout << "delay: " << env.test.lift.emergency.delay << std::endl;
    std::cout << "stops: " << env.test.lift.emergency.action.stops << std::endl;
    // kdb::test::Lift <ContextPolicyIs<ThreadContext>, WritePolicyIs<ReadOnlyPolicy>> const & lift = env.test.lift;
    kdb::test::Lift <ContextPolicyIs<ThreadContext>> const & lift = env.test.lift;
    std::cout << "height #3: " << lift.floor.n3.height << std::endl;
    std::cout << "limit: " << env.test.lift.limit << std::endl;

    // kdb::test::lift::emergency::Delay <ContextPolicyIs<ThreadContext>, WritePolicyIs<ReadOnlyPolicy>> delay(ks, tc);
    // delay = 20; // read only value!

    bool write = lift.write;
    env.test.lift.write = false;

    // write back to user/test/lift, see comments in lift.c
    if (write)
    {
        kdb.set(ks, "user/test/lift");
    }

    return 0;
}
Example #30
0
void TestCommand::doMetaTest ()
{
	vector<string> teststrings;
	teststrings.push_back ("");
	teststrings.push_back ("value");
	teststrings.push_back ("value with spaces");
	teststrings.push_back (" a very long value with many spaces and basically very very long, but only text ... ");
	for (int i = 1; i < 256; ++i)
		teststrings.back () += " very very long, but only text ... ";
	teststrings.push_back ("ascii umlauts !\"§$%&/()=?`\\}][{");
	teststrings.push_back ("utf8 umlauts ¸¬½¼³²¹ł€¶øæßð𳽫»¢“”nµ─·");
	teststrings.push_back ("all chars:");
	for (int i = 1; i < 256; ++i)
		teststrings.back ().push_back (i);
	teststrings.push_back ("€");
	for (int i = 1; i < 256; ++i)
	{
		string s;
		s.push_back (i);
		teststrings.push_back (s);
	}

	vector<string> testnames;
	testnames.push_back ("keyname");
	testnames.push_back ("deep/below/keyname");
	testnames.push_back ("keyname with spaces");
	testnames.push_back ("deep/belowkeyname with spaces");
	testnames.push_back (" a very long value with many spaces and basically very very long, but only text ");
	for (int i = 1; i < 256; ++i)
		testnames.back () += "/ very very long, but only text ... ";
	testnames.push_back ("ascii umlauts !\"§$%&/()=?`\\}][{");
	testnames.push_back ("utf8 umlauts ¸¬½¼³²¹ł€¶øæßð𳽫»¢“”nµ─·");
	testnames.push_back ("all chars:");
	for (int i = 1; i < 256; ++i)
		testnames.back ().push_back (i);
	testnames.push_back ("€");
	for (int i = 1; i < 256; ++i)
	{
		if (i == 46) continue; // ignore .
		string s;
		s.push_back (i);
		testnames.push_back (s);
	}


	for (auto & testname : testnames)
		for (auto & teststring : teststrings)
		{
			{
				KDB kdb;
				Key t = root.dup ();
				t.addBaseName (testname);
				t.setMeta<string> ("key", teststring);

				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 (testname);
				Key res = test.lookup (t);

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

				std::string meta = res.getMeta<std::string> ("key");

				nrTest++;
				if (meta != teststring)
				{
					nrError++;
					cerr << "Meta test failed (name is not equal)" << endl;
					cerr << "We got: \"" << meta << "\"" << endl;
					cerr << "We wanted: \"" << teststring << "\"" << endl;
				}
			}
		}
}