<commit_before>/**
 * \file
 *
 * \brief A plugin that makes use of libaugeas to read and write configuration files
 *
 * \copyright BSD License (see doc/COPYING or http://www.libelektra.org)
 *
 */

#ifdef HAVE_KDBCONFIG_H
#include "kdbconfig.h"
#endif

/* used for asprintf */
#define _GNU_SOURCE

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include <tests_plugin.h>
#include "rename.h"


static KeySet *createSimpleTestKeys()
{
	return ksNew (20,
			keyNew("user/tests/rename/will/be/stripped/key1", KEY_VALUE, "value1", KEY_END),
			keyNew("user/tests/rename/will/be/stripped/key2", KEY_VALUE, "value2", KEY_END),
			keyNew("user/tests/rename/will/be/stripped", KEY_VALUE, "value3", KEY_END),
			keyNew("user/tests/rename/will/not/be/stripped/key4", KEY_VALUE, "value4", KEY_END),
			KS_END);
}

static KeySet *createSimpleMetaTestKeys()
{
	return ksNew (20,
			keyNew("user/tests/rename/will/be/stripped/key1",
					KEY_VALUE, "value1",
					KEY_META, "rename/cut", "will/be/stripped",
					KEY_END),
			keyNew("user/tests/rename/will/be/stripped/key2",
					KEY_VALUE, "value2",
					KEY_META, "rename/cut", "will/be/stripped",
					KEY_END),
			keyNew("user/tests/rename/will/be/stripped", KEY_VALUE, "value3", KEY_END),
			keyNew("user/tests/rename/will/not/be/stripped/key4", KEY_VALUE, "value4", KEY_END),
			KS_END);
}

static void checkSimpleTestKeys(KeySet* ks)
{
	/* the first two keys should have been renamed */
	Key* key = ksLookupByName (ks, "user/tests/rename/key1", KDB_O_NONE);
	succeed_if(key, "key1 was not correctly renamed");
	key = ksLookupByName (ks, "user/tests/rename/key2", KDB_O_NONE);
	succeed_if(key, "key2 was not correctly renamed");
	/* the third key was not renamed because it would replace the parent key */
	key = ksLookupByName (ks, "user/tests/rename/will/be/stripped", KDB_O_NONE);
	succeed_if(key, "key3 was renamed but would replace the parent key");
	/* the fourth key was not renamed because the prefix did not match */
	key = ksLookupByName (ks, "user/tests/rename/will/not/be/stripped/key4", KDB_O_NONE);
	succeed_if(key, "key4 was renamed although its prefix did not match");
}

static void compareKeySets(KeySet* ks, KeySet* expected)
{
	succeed_if(ksGetSize (expected) == ksGetSize (ks), "KeySet on set does not contain the same amount of keys");
	Key* current;
	ksRewind (expected);
	while ((current = ksNext (expected)))
	{
		Key *key = ksLookup (ks, current, KDB_O_NONE);
		succeed_if (key, "Expected key was not found in KeySet");
		succeed_if (!strcmp(keyString(key), keyString(current)), "Value of key was modified");
	}
}

static void test_simpleCutOnGet () {
	Key *parentKey = keyNew ("user/tests/rename", KEY_END);
	KeySet *conf = ksNew (20,
			keyNew ("system/cut", KEY_VALUE, "will/be/stripped", KEY_END), KS_END);
	PLUGIN_OPEN("rename");

	KeySet *ks = createSimpleTestKeys();
	ksAppendKey(ks, parentKey);

	succeed_if(plugin->kdbGet (plugin, ks, parentKey) >= 1,
			"call to kdbGet was not successful");
	succeed_if(output_error (parentKey), "error in kdbGet");
	succeed_if(output_warnings (parentKey), "warnings in kdbGet");

	checkSimpleTestKeys (ks);

	keyDel (parentKey);
	ksDel(ks);
	PLUGIN_CLOSE ();
}


static void test_metaCutOnGet()
{
	Key *parentKey = keyNew ("user/tests/rename", KEY_END);
	KeySet *conf = ksNew (0);
	PLUGIN_OPEN("rename");

	KeySet *ks = createSimpleMetaTestKeys();
	ksAppendKey(ks, parentKey);

	succeed_if(plugin->kdbGet (plugin, ks, parentKey) >= 1,
			"call to kdbGet was not successful");
	succeed_if(output_error (parentKey), "error in kdbGet");
	succeed_if(output_warnings (parentKey), "warnings in kdbGet");

	checkSimpleTestKeys(ks);

	keyDel (parentKey);
	ksDel(ks);
	PLUGIN_CLOSE ();

}

static void test_simpleCutRestoreOnSet () {
	Key *parentKey = keyNew ("user/tests/rename", KEY_END);
	Key *parentKeyCopy = keyDup(parentKey);
	KeySet *conf = ksNew (20,
			keyNew ("system/cut", KEY_VALUE, "will/be/stripped", KEY_END), KS_END);
	PLUGIN_OPEN("rename");

	KeySet *ks = createSimpleTestKeys();
	ksAppendKey(ks, parentKey);

	succeed_if(plugin->kdbGet (plugin, ks, parentKey) >= 1,
			"call to kdbGet was not successful");
	succeed_if(output_error (parentKey), "error in kdbGet");
	succeed_if(output_warnings (parentKey), "warnings in kdbGet");

	succeed_if(plugin->kdbSet (plugin, ks, parentKey) >= 1,
			"call to kdbSet was not successful");
	succeed_if(output_error (parentKey), "error in kdbSet");
	succeed_if(output_warnings (parentKey), "warnings in kdbSet");


	/* test that the keys have been correctly restored */
	KeySet *expected = createSimpleTestKeys();
	ksAppendKey(expected, parentKeyCopy);

	compareKeySets (ks, expected);
	keyDel (parentKey);
	keyDel (parentKeyCopy);
	ksDel(expected);
	ksDel(ks);
	PLUGIN_CLOSE ();
}

static void test_withoutConfig()
{
	Key *parentKey = keyNew ("user/tests/rename", KEY_END);
	Key *parentKeyCopy = keyDup(parentKey);
	KeySet *conf = ksNew (0);
	PLUGIN_OPEN("rename");

	KeySet *ks = createSimpleTestKeys();
	ksAppendKey(ks, parentKey);

	succeed_if(plugin->kdbGet (plugin, ks, parentKey) >= 1,
			"call to kdbGet was not successful");
	succeed_if(output_error (parentKey), "error in kdbGet");
	succeed_if(output_warnings (parentKey), "warnings in kdbGet");

	KeySet *expected = createSimpleTestKeys();
	ksAppendKey(expected, parentKeyCopy);

	compareKeySets(ks, expected);
	keyDel (parentKey);
	keyDel (parentKeyCopy);
	ksDel(expected);
	ksDel(ks);
	PLUGIN_CLOSE ();
}<commit_msg>add KS_END<commit_after>/**
 * \file
 *
 * \brief A plugin that makes use of libaugeas to read and write configuration files
 *
 * \copyright BSD License (see doc/COPYING or http://www.libelektra.org)
 *
 */

#ifdef HAVE_KDBCONFIG_H
#include "kdbconfig.h"
#endif

/* used for asprintf */
#define _GNU_SOURCE

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include <tests_plugin.h>
#include "rename.h"


static KeySet *createSimpleTestKeys()
{
	return ksNew (20,
			keyNew("user/tests/rename/will/be/stripped/key1", KEY_VALUE, "value1", KEY_END),
			keyNew("user/tests/rename/will/be/stripped/key2", KEY_VALUE, "value2", KEY_END),
			keyNew("user/tests/rename/will/be/stripped", KEY_VALUE, "value3", KEY_END),
			keyNew("user/tests/rename/will/not/be/stripped/key4", KEY_VALUE, "value4", KEY_END),
			KS_END);
}

static KeySet *createSimpleMetaTestKeys()
{
	return ksNew (20,
			keyNew("user/tests/rename/will/be/stripped/key1",
					KEY_VALUE, "value1",
					KEY_META, "rename/cut", "will/be/stripped",
					KEY_END),
			keyNew("user/tests/rename/will/be/stripped/key2",
					KEY_VALUE, "value2",
					KEY_META, "rename/cut", "will/be/stripped",
					KEY_END),
			keyNew("user/tests/rename/will/be/stripped", KEY_VALUE, "value3", KEY_END),
			keyNew("user/tests/rename/will/not/be/stripped/key4", KEY_VALUE, "value4", KEY_END),
			KS_END);
}

static void checkSimpleTestKeys(KeySet* ks)
{
	/* the first two keys should have been renamed */
	Key* key = ksLookupByName (ks, "user/tests/rename/key1", KDB_O_NONE);
	succeed_if(key, "key1 was not correctly renamed");
	key = ksLookupByName (ks, "user/tests/rename/key2", KDB_O_NONE);
	succeed_if(key, "key2 was not correctly renamed");
	/* the third key was not renamed because it would replace the parent key */
	key = ksLookupByName (ks, "user/tests/rename/will/be/stripped", KDB_O_NONE);
	succeed_if(key, "key3 was renamed but would replace the parent key");
	/* the fourth key was not renamed because the prefix did not match */
	key = ksLookupByName (ks, "user/tests/rename/will/not/be/stripped/key4", KDB_O_NONE);
	succeed_if(key, "key4 was renamed although its prefix did not match");
}

static void compareKeySets(KeySet* ks, KeySet* expected)
{
	succeed_if(ksGetSize (expected) == ksGetSize (ks), "KeySet on set does not contain the same amount of keys");
	Key* current;
	ksRewind (expected);
	while ((current = ksNext (expected)))
	{
		Key *key = ksLookup (ks, current, KDB_O_NONE);
		succeed_if (key, "Expected key was not found in KeySet");
		succeed_if (!strcmp(keyString(key), keyString(current)), "Value of key was modified");
	}
}

static void test_simpleCutOnGet () {
	Key *parentKey = keyNew ("user/tests/rename", KEY_END);
	KeySet *conf = ksNew (20,
			keyNew ("system/cut", KEY_VALUE, "will/be/stripped", KEY_END), KS_END);
	PLUGIN_OPEN("rename");

	KeySet *ks = createSimpleTestKeys();
	ksAppendKey(ks, parentKey);

	succeed_if(plugin->kdbGet (plugin, ks, parentKey) >= 1,
			"call to kdbGet was not successful");
	succeed_if(output_error (parentKey), "error in kdbGet");
	succeed_if(output_warnings (parentKey), "warnings in kdbGet");

	checkSimpleTestKeys (ks);

	keyDel (parentKey);
	ksDel(ks);
	PLUGIN_CLOSE ();
}


static void test_metaCutOnGet()
{
	Key *parentKey = keyNew ("user/tests/rename", KEY_END);
	KeySet *conf = ksNew (0, KS_END);
	PLUGIN_OPEN("rename");

	KeySet *ks = createSimpleMetaTestKeys();
	ksAppendKey(ks, parentKey);

	succeed_if(plugin->kdbGet (plugin, ks, parentKey) >= 1,
			"call to kdbGet was not successful");
	succeed_if(output_error (parentKey), "error in kdbGet");
	succeed_if(output_warnings (parentKey), "warnings in kdbGet");

	checkSimpleTestKeys(ks);

	keyDel (parentKey);
	ksDel(ks);
	PLUGIN_CLOSE ();

}

static void test_simpleCutRestoreOnSet () {
	Key *parentKey = keyNew ("user/tests/rename", KEY_END);
	Key *parentKeyCopy = keyDup(parentKey);
	KeySet *conf = ksNew (20,
			keyNew ("system/cut", KEY_VALUE, "will/be/stripped", KEY_END), KS_END);
	PLUGIN_OPEN("rename");

	KeySet *ks = createSimpleTestKeys();
	ksAppendKey(ks, parentKey);

	succeed_if(plugin->kdbGet (plugin, ks, parentKey) >= 1,
			"call to kdbGet was not successful");
	succeed_if(output_error (parentKey), "error in kdbGet");
	succeed_if(output_warnings (parentKey), "warnings in kdbGet");

	succeed_if(plugin->kdbSet (plugin, ks, parentKey) >= 1,
			"call to kdbSet was not successful");
	succeed_if(output_error (parentKey), "error in kdbSet");
	succeed_if(output_warnings (parentKey), "warnings in kdbSet");


	/* test that the keys have been correctly restored */
	KeySet *expected = createSimpleTestKeys();
	ksAppendKey(expected, parentKeyCopy);

	compareKeySets (ks, expected);
	keyDel (parentKey);
	keyDel (parentKeyCopy);
	ksDel(expected);
	ksDel(ks);
	PLUGIN_CLOSE ();
}

static void test_withoutConfig()
{
	Key *parentKey = keyNew ("user/tests/rename", KEY_END);
	Key *parentKeyCopy = keyDup(parentKey);
	KeySet *conf = ksNew (0, KS_END);
	PLUGIN_OPEN("rename");

	KeySet *ks = createSimpleTestKeys();
	ksAppendKey(ks, parentKey);

	succeed_if(plugin->kdbGet (plugin, ks, parentKey) >= 1,
			"call to kdbGet was not successful");
	succeed_if(output_error (parentKey), "error in kdbGet");
	succeed_if(output_warnings (parentKey), "warnings in kdbGet");

	KeySet *expected = createSimpleTestKeys();
	ksAppendKey(expected, parentKeyCopy);

	compareKeySets(ks, expected);
	keyDel (parentKey);
	keyDel (parentKeyCopy);
	ksDel(expected);
	ksDel(ks);
	PLUGIN_CLOSE ();
}
{'fraction_non_alphanumeric': 0.1417265532611662, 'fraction_numerical': 0.0065496224870372054, 'mean_word_length': 5.260820045558087, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1540391', 'n_tokens_mistral': 4241, 'n_tokens_neox': 4058, 'n_words': 971}