Exemple #1
0
void
test_cgroup_name_new (void)
{
	CGroupName     *cgname;
	nih_local char *parent = NULL;

	TEST_FUNCTION ("cgroup_name_new");

	parent = nih_strdup (NULL, "a parent object");
	TEST_NE_P (parent, NULL);

	TEST_FEATURE ("no parent, name");

	TEST_ALLOC_FAIL {
		cgname = cgroup_name_new (NULL, "foo.");

		if (test_alloc_failed) {
			TEST_EQ_P (cgname, NULL);
			continue;
		}

		TEST_NE_P (cgname, NULL);

		TEST_ALLOC_SIZE (cgname, sizeof (CGroupName));

		TEST_ALLOC_PARENT (cgname, NULL);

		TEST_EQ_STR (cgname->name, "foo.");
		TEST_ALLOC_SIZE (cgname->name, 1+strlen ("foo."));
		TEST_ALLOC_PARENT (cgname->name, cgname);

		TEST_LIST_EMPTY (&cgname->settings);
	}

	TEST_FEATURE ("parent, name");

	TEST_ALLOC_FAIL {
		cgname = cgroup_name_new (parent, "bar");

		if (test_alloc_failed) {
			TEST_EQ_P (cgname, NULL);
			continue;
		}

		TEST_NE_P (cgname, NULL);

		TEST_ALLOC_SIZE (cgname, sizeof (CGroupName));

		TEST_ALLOC_PARENT (cgname, parent);

		TEST_EQ_STR (cgname->name, "bar");
		TEST_ALLOC_SIZE (cgname->name, 1+strlen ("bar"));
		TEST_ALLOC_PARENT (cgname->name, cgname);

		TEST_LIST_EMPTY (&cgname->settings);
	}
}
Exemple #2
0
void
test_vsprintf (void)
{
    char *str1, *str2;

    TEST_FUNCTION ("nih_vsprintf");

    /* Check that we can create a formatted string for a va_list,
     * first with no parent.
     */
    TEST_FEATURE ("with no parent");
    TEST_ALLOC_FAIL {
        str1 = my_vsprintf (NULL, "this %s a test %d", "is", 54321);

        if (test_alloc_failed) {
            TEST_EQ_P (str1, NULL);
            continue;
        }

        TEST_ALLOC_PARENT (str1, NULL);
        TEST_ALLOC_SIZE (str1, strlen (str1) + 1);
        TEST_EQ_STR (str1, "this is a test 54321");

        nih_free (str1);
    }


    /* And then with a parent. */
    TEST_FEATURE ("with a parent");
    str1 = my_vsprintf (NULL, "this %s a test %d", "is", 54321);

    TEST_ALLOC_FAIL {
        str2 = my_vsprintf (str1, "another %d test %s",
        12345, "string");

        if (test_alloc_failed) {
            TEST_EQ_P (str2, NULL);
            continue;
        }

        TEST_ALLOC_PARENT (str2, str1);
        TEST_ALLOC_SIZE (str2, strlen (str2) + 1);
        TEST_EQ_STR (str2, "another 12345 test string");

        nih_free (str2);
    }

    nih_free (str1);
}
Exemple #3
0
void
test_sprintf (void)
{
    char *str1, *str2;

    TEST_FUNCTION ("nih_sprintf");

    /* Check that we can create a formatted string with no parent,
     * it should be allocated with nih_alloc and be the right length.
     */
    TEST_FEATURE ("with no parent");
    TEST_ALLOC_FAIL {
        str1 = nih_sprintf (NULL, "this %s a test %d", "is", 54321);

        if (test_alloc_failed) {
            TEST_EQ_P (str1, NULL);
            continue;
        }

        TEST_ALLOC_PARENT (str1, NULL);
        TEST_ALLOC_SIZE (str1, strlen (str1) + 1);
        TEST_EQ_STR (str1, "this is a test 54321");

        nih_free (str1);
    }


    /* Check that we can create a string with a parent. */
    TEST_FEATURE ("with a parent");
    str1 = nih_sprintf (NULL, "this %s a test %d", "is", 54321);

    TEST_ALLOC_FAIL {
        str2 = nih_sprintf (str1, "another %d test %s",
        12345, "string");

        if (test_alloc_failed) {
            TEST_EQ_P (str2, NULL);
            continue;
        }

        TEST_ALLOC_PARENT (str2, str1);
        TEST_ALLOC_SIZE (str2, strlen (str2) + 1);
        TEST_EQ_STR (str2, "another 12345 test string");

        nih_free (str2);
    }

    nih_free (str1);
}
Exemple #4
0
void
test_strdup (void)
{
    char *str1, *str2;

    TEST_FUNCTION ("nih_strdup");

    /* Check that we can create a duplicate of another string,
     * allocated with nih_alloc and no parent.
     */
    TEST_FEATURE ("with no parent");
    TEST_ALLOC_FAIL {
        str1 = nih_strdup (NULL, "this is a test");

        if (test_alloc_failed) {
            TEST_EQ_P (str1, NULL);
            continue;
        }

        TEST_ALLOC_PARENT (str1, NULL);
        TEST_ALLOC_SIZE (str1, strlen (str1) + 1);
        TEST_EQ_STR (str1, "this is a test");

        nih_free (str1);
    }


    /* And check we can allocate with a parent. */
    TEST_FEATURE ("with a parent");
    str1 = nih_strdup (NULL, "this is a test");

    TEST_ALLOC_FAIL {
        str2 = nih_strdup (str1, "another test string");

        if (test_alloc_failed) {
            TEST_EQ_P (str2, NULL);
            continue;
        }

        TEST_ALLOC_PARENT (str2, str1);
        TEST_ALLOC_SIZE (str2, strlen (str2) + 1);
        TEST_EQ_STR (str2, "another test string");

        nih_free (str2);
    }

    nih_free (str1);
}
Exemple #5
0
void
test_array_addn (void)
{
    char   **array, **ret;
    size_t   len;

    /* Check that we can append strings to a NULL-terminated array.
     */
    TEST_FUNCTION ("nih_str_array_addn");
    array = nih_str_array_new (NULL);
    len = 0;

    TEST_ALLOC_FAIL {
        ret = nih_str_array_addn (&array, NULL, &len, "testing", 4);

        if (test_alloc_failed) {
            TEST_EQ_P (ret, NULL);

            TEST_EQ (len, 1);
            TEST_EQ_STR (array[0], "test");
            TEST_EQ_P (array[1], NULL);
            continue;
        }

        TEST_NE_P (ret, NULL);

        TEST_EQ (len, 1);
        TEST_ALLOC_PARENT (array[0], array);
        TEST_ALLOC_SIZE (array[0], 5);
        TEST_EQ_STR (array[0], "test");
        TEST_EQ_P (array[1], NULL);
    }

    nih_free (array);
}
Exemple #6
0
void
test_local (void)
{
	void *parent;
	void *_ptr;

	TEST_GROUP ("nih_local");


	/* Make sure that when a variable goes out of scope, it's freed. */
	TEST_FEATURE ("with variable going out of scope");
	do {
		nih_local void *ptr;

		ptr = nih_alloc (NULL, 100);

		nih_alloc_set_destructor (ptr, destructor_called);
		destructor_was_called = 0;
	} while (0);

	TEST_TRUE (destructor_was_called);


	/* Make sure that if a variable is referenced while in scope, it
	 * is not freed.
	 */
	TEST_FEATURE ("with referenced variable");
	parent = nih_alloc (NULL, 100);

	do {
		nih_local void *ptr;

		ptr = nih_alloc (NULL, 100);
		nih_ref (ptr, parent);
		_ptr = ptr;

		nih_alloc_set_destructor (ptr, destructor_called);
		destructor_was_called = 0;
	} while (0);

	TEST_FALSE (destructor_was_called);
	TEST_ALLOC_PARENT (_ptr, parent);

	nih_free (parent);


	/* Make sure we don't need to allocate the variable. */
	TEST_FEATURE ("with NULL variable");
	do {
		nih_local void *ptr = NULL;
	} while (0);
}
Exemple #7
0
void
test_alloc (void)
{
	void *ptr1;
	void *ptr2;

	TEST_FUNCTION ("nih_alloc");

	/* Check allocation remembers the size, and is possible without
	 * a parent.
	 */
	TEST_FEATURE ("with no parent");
	ptr1 = nih_alloc (NULL, 8096);
	memset (ptr1, 'x', 8096);

	TEST_ALLOC_SIZE (ptr1, 8096);
	TEST_ALLOC_PARENT (ptr1, NULL);


	/* Check that allocation with a parent remembers the parent */
	TEST_FEATURE ("with a parent");
	ptr2 = nih_alloc (ptr1, 10);
	memset (ptr2, 'x', 10);

	TEST_ALLOC_SIZE (ptr2, 10);
	TEST_ALLOC_PARENT (ptr2, ptr1);

	nih_free (ptr1);


	/* Check that nih_alloc returns NULL if allocation fails. */
	TEST_FEATURE ("with failed allocation");
	__nih_malloc = malloc_null;
	ptr1 = nih_new (NULL, int);
	__nih_malloc = malloc;

	TEST_EQ_P (ptr1, NULL);
}
Exemple #8
0
void
test_new (void)
{
	void *ptr1;
	void *ptr2;

	TEST_FUNCTION ("nih_new");

	/* Check that nih_new works if we don't give it a parent, the block
	 * should be allocated with the size of the type given.
	 */
	TEST_FEATURE ("with no parent");
	ptr1 = nih_new (NULL, int);

	TEST_ALLOC_SIZE (ptr1, sizeof (int));
	TEST_ALLOC_PARENT (ptr1, NULL);


	/* Check that nih_new works if we do give a parent. */
	TEST_FEATURE ("with parent");
	ptr2 = nih_new (ptr1, char);

	TEST_ALLOC_SIZE (ptr2, sizeof (char));
	TEST_ALLOC_PARENT (ptr2, ptr1);

	nih_free (ptr1);


	/* Check that nih_new returns NULL if allocation fails. */
	TEST_FEATURE ("with failed allocation");
	__nih_malloc = malloc_null;
	ptr1 = nih_new (NULL, int);
	__nih_malloc = malloc;

	TEST_EQ_P (ptr1, NULL);
}
Exemple #9
0
void
test_set (void)
{
	char   **env = NULL, **ret;
	size_t   len = 0;

	TEST_FUNCTION ("environ_set");

	/* Check that an environment variable can be set from a format
	 * string.
	 */
	TEST_ALLOC_FAIL {
		TEST_ALLOC_SAFE {
			len = 0;
			env = nih_str_array_new (NULL);
		}

		ret = environ_set (&env, NULL, &len, TRUE, "FOO=%d", 1234);

		if (test_alloc_failed) {
			TEST_EQ_P (ret, NULL);

			TEST_EQ (len, 0);
			TEST_EQ_P (env[0], NULL);

			nih_free (env);
			continue;
		}

		TEST_NE_P (ret, NULL);

		TEST_EQ (len, 1);
		TEST_ALLOC_PARENT (env[0], env);
		TEST_ALLOC_SIZE (env[0], 9);
		TEST_EQ_STR (env[0], "FOO=1234");
		TEST_EQ_P (env[1], NULL);

		nih_free (env);
	}
}
Exemple #10
0
void
test_unref (void)
{
	void *ptr1;
	void *ptr2;
	void *ptr3;

	TEST_FUNCTION ("nih_unref");


	/* Check that we can remove a reference from an object with multiple
	 * parents, which means the object will not be freed.
	 */
	TEST_FEATURE ("with multiple parents");
	ptr1 = nih_alloc (NULL, 100);
	memset (ptr1, 'x', 100);

	ptr2 = nih_alloc (ptr1, 100);
	memset (ptr2, 'y', 100);

	ptr3 = nih_alloc (NULL, 100);
	memset (ptr2, 'z', 100);

	nih_ref (ptr2, ptr3);

	nih_alloc_set_destructor (ptr2, destructor_called);
	destructor_was_called = 0;

	nih_unref (ptr2, ptr1);

	TEST_FALSE (destructor_was_called);
	TEST_ALLOC_PARENT (ptr2, ptr3);

	nih_free (ptr1);
	nih_free (ptr3);


	/* Check that when we remove the last reference from an object,
	 * the object is freed.
	 */
	TEST_FEATURE ("with last parent");
	ptr1 = nih_alloc (NULL, 100);
	memset (ptr1, 'x', 100);

	ptr2 = nih_alloc (ptr1, 100);
	memset (ptr2, 'y', 100);

	nih_alloc_set_destructor (ptr2, destructor_called);
	destructor_was_called = 0;

	nih_unref (ptr2, ptr1);

	TEST_TRUE (destructor_was_called);

	nih_free (ptr1);


	/* Check that we have to remove the NULL reference from an object
	 * for it to be freed.
	 */
	TEST_FEATURE ("with only NULL parent");
	ptr1 = nih_alloc (NULL, 100);
	memset (ptr1, 'x', 100);

	nih_alloc_set_destructor (ptr1, destructor_called);
	destructor_was_called = 0;

	nih_unref (ptr1, NULL);

	TEST_TRUE (destructor_was_called);


	/* Check that we can remove the NULL reference leaving a reference
	 * to a different object.
	 */
	TEST_FEATURE ("with no parent and other parent");
	ptr1 = nih_alloc (NULL, 100);
	memset (ptr1, 'x', 100);

	ptr2 = nih_alloc (NULL, 100);
	memset (ptr2, 'y', 100);

	nih_ref (ptr2, ptr1);

	nih_alloc_set_destructor (ptr2, destructor_called);
	destructor_was_called = 0;

	nih_unref (ptr2, NULL);

	TEST_FALSE (destructor_was_called);

	TEST_ALLOC_PARENT (ptr2, ptr1);
	TEST_FALSE (nih_alloc_parent (ptr2, NULL));

	nih_free (ptr1);


	/* Check that an object with multiple NULL references must have
	 * them both removed before it will be freed.
	 */
	TEST_FEATURE ("with multiple NULL parents");
	ptr1 = nih_alloc (NULL, 100);
	memset (ptr1, 'x', 100);

	nih_ref (ptr1, NULL);

	nih_alloc_set_destructor (ptr1, destructor_called);
	destructor_was_called = 0;

	nih_unref (ptr1, NULL);

	TEST_FALSE (destructor_was_called);

	nih_unref (ptr1, NULL);

	TEST_TRUE (destructor_was_called);


	/* Check that an object with multiple identical references must have
	 * them both removed before it will be freed.
	 */
	TEST_FEATURE ("with multiple identical parents");
	ptr1 = nih_alloc (NULL, 100);
	memset (ptr1, 'x', 100);

	ptr2 = nih_alloc (ptr1, 100);
	memset (ptr2, 'y', 100);

	nih_ref (ptr2, ptr1);

	nih_alloc_set_destructor (ptr2, destructor_called);
	destructor_was_called = 0;

	nih_unref (ptr2, ptr1);

	TEST_FALSE (destructor_was_called);

	nih_unref (ptr2, ptr1);

	TEST_TRUE (destructor_was_called);

	nih_free (ptr1);
}
Exemple #11
0
void
test_ref (void)
{
	void *ptr1;
	void *ptr2;
	void *ptr3;

	TEST_FUNCTION ("nih_ref");


	/* Check that we can add a reference to an object that has no
	 * parent, and this does not remove the NULL reference.
	 */
	TEST_FEATURE ("with no parent");
	ptr1 = nih_alloc (NULL, 100);
	memset (ptr1, 'x', 100);

	ptr2 = nih_alloc (NULL, 100);
	memset (ptr2, 'y', 100);

	nih_ref (ptr1, ptr2);

	TEST_ALLOC_PARENT (ptr1, ptr2);
	TEST_ALLOC_PARENT (ptr1, NULL);

	nih_free (ptr1);
	nih_free (ptr2);


	/* Check that we can add a reference to an object that already has
	 * a parent, and that both shall be parents afterwards.
	 */
	TEST_FEATURE ("with existing parent");
	ptr1 = nih_alloc (NULL, 100);
	memset (ptr1, 'x', 100);

	ptr2 = nih_alloc (ptr1, 100);
	memset (ptr2, 'y', 100);

	ptr3 = nih_alloc (NULL, 100);
	memset (ptr2, 'z', 100);

	nih_ref (ptr2, ptr3);

	TEST_ALLOC_PARENT (ptr2, ptr1);
	TEST_ALLOC_PARENT (ptr2, ptr3);

	nih_free (ptr1);
	nih_free (ptr3);


	/* Check that we can add a new NULL reference to an object that
	 * already has a parent, and that both shall be parents afterwards.
	 */
	TEST_FEATURE ("with existing parent and new NULL");
	ptr1 = nih_alloc (NULL, 100);
	memset (ptr1, 'x', 100);

	ptr2 = nih_alloc (ptr1, 100);
	memset (ptr2, 'y', 100);

	nih_ref (ptr2, NULL);

	TEST_ALLOC_PARENT (ptr2, ptr1);
	TEST_ALLOC_PARENT (ptr2, NULL);

	nih_free (ptr1);
	nih_free (ptr2);


	/* Check that we can add a second NULL reference to an object that
	 * already has one.
	 */
	TEST_FEATURE ("with additional NULL parent");
	ptr1 = nih_alloc (NULL, 100);
	memset (ptr1, 'x', 100);

	nih_ref (ptr1, NULL);

	TEST_ALLOC_PARENT (ptr1, NULL);

	nih_free (ptr1);


	/* Check that we can add a second reference to an object that already
	 * has a reference from the same parent.
	 */
	TEST_FEATURE ("with additional existing parent");
	ptr1 = nih_alloc (NULL, 100);
	memset (ptr1, 'x', 100);

	ptr2 = nih_alloc (ptr1, 100);
	memset (ptr2, 'y', 100);

	nih_ref (ptr2, ptr1);

	TEST_ALLOC_PARENT (ptr2, ptr1);

	nih_free (ptr2);
	nih_free (ptr1);
}
Exemple #12
0
void
test_realloc (void)
{
	void *ptr1;
	void *ptr2;
	void *ptr3;

	TEST_FUNCTION ("nih_realloc");

	/* Check that nih_realloc behaves like nih_alloc if the pointer is
	 * NULL (it should, in fact, just call it)
	 */
	TEST_FEATURE ("as nih_alloc");
	ptr1 = nih_realloc (NULL, NULL, 4096);
	memset (ptr1, 'x', 4096);

	TEST_ALLOC_SIZE (ptr1, 4096);
	TEST_ALLOC_PARENT (ptr1, NULL);

	nih_free (ptr1);


	/* Check that nih_realloc works if the block doesn't have a parent.
	 */
	TEST_FEATURE ("with no parent");
	ptr1 = nih_alloc (NULL, 4096);
	memset (ptr1, 'x', 4096);

	ptr1 = nih_realloc (ptr1, NULL, 8096);
	memset (ptr1, 'x', 8096);

	TEST_ALLOC_SIZE (ptr1, 8096);
	TEST_ALLOC_PARENT (ptr1, NULL);


	/* Check that nih_realloc works if the block has a parent, the size
	 * should change but the parent should remain the same.
	 */
	TEST_FEATURE ("with a parent");
	ptr2 = nih_alloc (ptr1, 5);
	memset (ptr2, 'x', 5);

	ptr2 = nih_realloc (ptr2, ptr1, 10);
	memset (ptr2, 'x', 10);

	TEST_ALLOC_SIZE (ptr2, 10);
	TEST_ALLOC_PARENT (ptr2, ptr1);

	nih_free (ptr1);


	/* Check that nih_realloc works if the block being reallocated has
	 * a child.  This is fiddly as they need their parent pointers
	 * adjusted.
	 */
	TEST_FEATURE ("with a child");
	ptr1 = nih_alloc (NULL, 128);
	memset (ptr1, 'x', 128);

	ptr2 = nih_alloc (ptr1, 512);
	memset (ptr2, 'x', 512);

	ptr3 = nih_realloc (ptr1, NULL, 1024);
	memset (ptr3, 'x', 1024);

	TEST_ALLOC_PARENT (ptr2, ptr3);

	nih_free (ptr3);


	/* Check that nih_realloc returns NULL and doesn't alter the block
	 * if the allocator fails.
	 */
	TEST_FEATURE ("with failing realloc");
	ptr1 = nih_alloc (NULL, 10);
	assert (ptr1);
	memset (ptr1, 'x', 10);

	__nih_realloc = realloc_null;
	ptr2 = nih_realloc (ptr1, NULL, 200);
	__nih_realloc = realloc;

	TEST_EQ_P (ptr2, NULL);
	TEST_ALLOC_SIZE (ptr1, 10);

	nih_free (ptr1);
}
Exemple #13
0
void
test_cgroup_new (void)
{
	nih_local char *parent = NULL;
	CGroup         *cgroup;

	TEST_FUNCTION ("cgroup_new");

	parent = nih_strdup (NULL, "a parent object");
	TEST_NE_P (parent, NULL);

	TEST_FEATURE ("no parent, controller");
	TEST_ALLOC_FAIL {

		cgroup = cgroup_new (NULL, "cpuset");

		if (test_alloc_failed) {
			TEST_EQ_P (cgroup, NULL);
			continue;
		}

		TEST_NE_P (cgroup, NULL);

		TEST_ALLOC_SIZE (cgroup, sizeof (CGroup));

		TEST_ALLOC_PARENT (cgroup, NULL);

		TEST_EQ_STR (cgroup->controller, "cpuset");
		TEST_ALLOC_SIZE (cgroup->controller, 1+strlen ("cpuset"));
		TEST_ALLOC_PARENT (cgroup->controller, cgroup);

		TEST_LIST_EMPTY (&cgroup->names);

		nih_free (cgroup);
	}

	TEST_FEATURE ("parent, controller");
	TEST_ALLOC_FAIL {

		cgroup = cgroup_new (parent, "perf_event");

		if (test_alloc_failed) {
			TEST_EQ_P (cgroup, NULL);
			continue;
		}

		TEST_NE_P (cgroup, NULL);

		TEST_ALLOC_SIZE (cgroup, sizeof (CGroup));

		TEST_ALLOC_PARENT (cgroup, parent);

		TEST_EQ_STR (cgroup->controller, "perf_event");
		TEST_ALLOC_SIZE (cgroup->controller, 1+strlen ("perf_event"));
		TEST_ALLOC_PARENT (cgroup->controller, cgroup);

		TEST_LIST_EMPTY (&cgroup->names);

		nih_free (cgroup);
	}
}
Exemple #14
0
void
test_operator_environment (void)
{
	EventOperator  *root, *oper1, *oper2, *oper3, *oper4, *oper5, *oper6;
	Event          *event1, *event2, *event3;
	char          **env, **ptr;
	size_t          len;

	TEST_FUNCTION ("event_operator_environment");
	root = event_operator_new (NULL, EVENT_OR, NULL, NULL);
	oper1 = event_operator_new (root, EVENT_AND, NULL, NULL);
	oper2 = event_operator_new (root, EVENT_AND, NULL, NULL);
	oper3 = event_operator_new (root, EVENT_MATCH, "foo", NULL);
	oper4 = event_operator_new (root, EVENT_MATCH, "bar", NULL);
	oper5 = event_operator_new (root, EVENT_MATCH, "frodo", NULL);
	oper6 = event_operator_new (root, EVENT_MATCH, "bilbo", NULL);

	nih_tree_add (&root->node, &oper1->node, NIH_TREE_LEFT);
	nih_tree_add (&root->node, &oper2->node, NIH_TREE_RIGHT);
	nih_tree_add (&oper1->node, &oper3->node, NIH_TREE_LEFT);
	nih_tree_add (&oper1->node, &oper4->node, NIH_TREE_RIGHT);
	nih_tree_add (&oper2->node, &oper5->node, NIH_TREE_LEFT);
	nih_tree_add (&oper2->node, &oper6->node, NIH_TREE_RIGHT);

	root->value = TRUE;

	oper1->value = TRUE;

	oper3->value = TRUE;
	oper3->event = event1 = event_new (NULL, "foo", NULL);
	event_block (oper3->event);

	NIH_MUST (nih_str_array_add (&oper3->event->env, oper3->event,
				     NULL, "FOO=APPLE"));
	NIH_MUST (nih_str_array_add (&oper3->event->env, oper3->event,
				     NULL, "TEA=YES"));

	oper4->value = TRUE;
	oper4->event = event2 = event_new (NULL, "bar", NULL);
	event_block (oper4->event);

	NIH_MUST (nih_str_array_add (&oper4->event->env, oper4->event,
				     NULL, "BAR=ORANGE"));
	NIH_MUST (nih_str_array_add (&oper4->event->env, oper4->event,
				     NULL, "COFFEE=NO"));

	oper6->value = TRUE;
	oper6->event = event3 = event_new (NULL, "bilbo", NULL);
	event_block (oper6->event);

	NIH_MUST (nih_str_array_add (&oper6->event->env, oper6->event,
				     NULL, "FRODO=BAGGINS"));
	NIH_MUST (nih_str_array_add (&oper6->event->env, oper6->event,
				     NULL, "BILBO=WIBBLE"));


	/* Check that the environment from each of the events is appended
	 * to the passed array; except for the event that was matched but not
	 * in the true tree.
	 */
	TEST_FEATURE ("with environment table");
	TEST_ALLOC_FAIL {
		env = NULL;
		len = 0;

		ptr = event_operator_environment (root, &env, NULL, &len, NULL);

		if (test_alloc_failed) {
			TEST_EQ_P (ptr, NULL);

			if (env)
				nih_free (env);
			continue;
		}

		TEST_NE_P (env, NULL);
		TEST_ALLOC_SIZE (env, sizeof (char *) * 5);
		TEST_EQ (len, 4);

		TEST_ALLOC_PARENT (env[0], env);
		TEST_EQ_STR (env[0], "FOO=APPLE");
		TEST_ALLOC_PARENT (env[1], env);
		TEST_EQ_STR (env[1], "TEA=YES");
		TEST_ALLOC_PARENT (env[2], env);
		TEST_EQ_STR (env[2], "BAR=ORANGE");
		TEST_ALLOC_PARENT (env[3], env);
		TEST_EQ_STR (env[3], "COFFEE=NO");
		TEST_EQ_P (env[4], NULL);

		nih_free (env);
	}


	/* Check that if we also give the name of an environment variable,
	 * this will contain a space-separated list of the event names.
	 */
	TEST_FEATURE ("with environment variable for event list");
	TEST_ALLOC_FAIL {
		env = NULL;
		len = 0;

		ptr = event_operator_environment (root, &env, NULL, &len,
						  "UPSTART_EVENTS");

		if (test_alloc_failed) {
			TEST_EQ_P (ptr, NULL);

			if (env)
				nih_free (env);
			continue;
		}

		TEST_NE_P (env, NULL);
		TEST_ALLOC_SIZE (env, sizeof (char *) * 6);
		TEST_EQ (len, 5);

		TEST_ALLOC_PARENT (env[0], env);
		TEST_EQ_STR (env[0], "FOO=APPLE");
		TEST_ALLOC_PARENT (env[1], env);
		TEST_EQ_STR (env[1], "TEA=YES");
		TEST_ALLOC_PARENT (env[2], env);
		TEST_EQ_STR (env[2], "BAR=ORANGE");
		TEST_ALLOC_PARENT (env[3], env);
		TEST_EQ_STR (env[3], "COFFEE=NO");
		TEST_ALLOC_PARENT (env[4], env);
		TEST_EQ_STR (env[4], "UPSTART_EVENTS=foo bar");
		TEST_EQ_P (env[5], NULL);

		nih_free (env);
	}


	/* Check that if no events are matched the environment table only
	 * has an empty events list.
	 */
	TEST_FEATURE ("with no matches");
	TEST_ALLOC_FAIL {
		env = NULL;
		len = 0;

		ptr = event_operator_environment (oper5, &env, NULL, &len,
						  "UPSTART_EVENTS");

		if (test_alloc_failed) {
			TEST_EQ_P (ptr, NULL);
			if (env)
				nih_free (env);
			continue;
		}

		TEST_NE_P (env, NULL);
		TEST_ALLOC_SIZE (env, sizeof (char *) * 2);
		TEST_EQ (len, 1);

		TEST_ALLOC_PARENT (env[0], env);
		TEST_EQ_STR (env[0], "UPSTART_EVENTS=");
		TEST_EQ_P (env[1], NULL);

		nih_free (env);
	}


	nih_free (root);
	nih_free (event1);
	nih_free (event2);
	nih_free (event3);
}
Exemple #15
0
void
test_operator_new (void)
{
	EventOperator  *oper;
	char          **env;

	TEST_FUNCTION ("event_operator_new");


	/* Check that we can create a new EventOperator structure to match
	 * an event, and have the details filled in and returned.  It
	 * should not be placed into any tree structure.
	 */
	TEST_FEATURE ("with EVENT_MATCH");
	TEST_ALLOC_FAIL {
		oper = event_operator_new (NULL, EVENT_MATCH, "test", NULL);

		if (test_alloc_failed) {
			TEST_EQ_P (oper, NULL);
			continue;
		}

		TEST_ALLOC_SIZE (oper, sizeof (EventOperator));
		TEST_EQ_P (oper->node.parent, NULL);
		TEST_EQ_P (oper->node.left, NULL);
		TEST_EQ_P (oper->node.right, NULL);
		TEST_EQ (oper->value, FALSE);
		TEST_EQ_STR (oper->name, "test");
		TEST_ALLOC_PARENT (oper->name, oper);

		TEST_EQ_P (oper->env, NULL);
		TEST_EQ_P (oper->event, NULL);

		nih_free (oper);
	}


	/* Check that environment passed to event_operator_new is reparented
	 * to belong to the structure itself.
	 */
	TEST_FEATURE ("with EVENT_MATCH and environment");
	TEST_ALLOC_FAIL {
		TEST_ALLOC_SAFE {
			env = nih_str_array_new (NULL);
			NIH_MUST (nih_str_array_add (&env, NULL,
						     NULL, "foo"));
			NIH_MUST (nih_str_array_add (&env, NULL,
						     NULL, "BAR=frodo"));
		}

		oper = event_operator_new (NULL, EVENT_MATCH, "test", env);

		if (test_alloc_failed) {
			TEST_EQ_P (oper, NULL);
			TEST_ALLOC_PARENT (env, NULL);
			nih_free (env);
			continue;
		}

		nih_discard (env);

		TEST_ALLOC_SIZE (oper, sizeof (EventOperator));
		TEST_EQ_P (oper->node.parent, NULL);
		TEST_EQ_P (oper->node.left, NULL);
		TEST_EQ_P (oper->node.right, NULL);
		TEST_EQ (oper->value, FALSE);
		TEST_EQ_STR (oper->name, "test");
		TEST_ALLOC_PARENT (oper->name, oper);

		TEST_EQ_P (oper->env, env);
		TEST_ALLOC_PARENT (oper->env, oper);

		TEST_EQ_P (oper->event, NULL);

		nih_free (oper);
	}


	/* Check that an ordinary operator needs no name attached. */
	TEST_FEATURE ("with EVENT_OR");
	TEST_ALLOC_FAIL {
		oper = event_operator_new (NULL, EVENT_OR, NULL, NULL);

		if (test_alloc_failed) {
			TEST_EQ_P (oper, NULL);
			continue;
		}

		TEST_ALLOC_SIZE (oper, sizeof (EventOperator));
		TEST_EQ_P (oper->node.parent, NULL);
		TEST_EQ_P (oper->node.left, NULL);
		TEST_EQ_P (oper->node.right, NULL);
		TEST_EQ (oper->value, FALSE);
		TEST_EQ_P (oper->name, NULL);
		TEST_EQ_P (oper->env, NULL);
		TEST_EQ_P (oper->event, NULL);

		nih_free (oper);
	}
}
Exemple #16
0
void
test_cgroup_setting_new (void)
{
	CGroupSetting   *setting;
	nih_local char  *parent= NULL;

	parent = nih_strdup (NULL, "a parent object");
	TEST_NE_P (parent, NULL);

	TEST_FUNCTION ("cgroup_setting_new");

	TEST_FEATURE ("no parent, key, no value");
	TEST_ALLOC_FAIL {
		setting = cgroup_setting_new (NULL, "foo", NULL);

		if (test_alloc_failed) {
			TEST_EQ_P (setting, NULL);
			continue;
		}

		TEST_ALLOC_SIZE (setting, sizeof (CGroupSetting));
		TEST_ALLOC_PARENT (setting, NULL);
		TEST_EQ_STR (setting->key, "foo");
		TEST_ALLOC_SIZE (setting->key, 1+strlen ("foo"));
		TEST_ALLOC_PARENT (setting->key, setting);

		nih_free (setting);
	}

	TEST_FEATURE ("parent, key, no value");
	TEST_ALLOC_FAIL {
		setting = cgroup_setting_new (parent, "hello world", NULL);

		if (test_alloc_failed) {
			TEST_EQ_P (setting, NULL);
			continue;
		}

		TEST_ALLOC_SIZE (setting, sizeof (CGroupSetting));
		TEST_ALLOC_PARENT (setting, parent);
		TEST_EQ_STR (setting->key, "hello world");
		TEST_ALLOC_SIZE (setting->key, 1+strlen ("hello world"));
		TEST_ALLOC_PARENT (setting->key, setting);

		nih_free (setting);
	}

	TEST_FEATURE ("no parent, key, value");
	TEST_ALLOC_FAIL {
		setting = cgroup_setting_new (NULL, "hello world", "a value");

		if (test_alloc_failed) {
			TEST_EQ_P (setting, NULL);
			continue;
		}

		TEST_ALLOC_SIZE (setting, sizeof (CGroupSetting));
		TEST_ALLOC_PARENT (setting, NULL);

		TEST_EQ_STR (setting->key, "hello world");
		TEST_ALLOC_SIZE (setting->key, 1+strlen ("hello world"));
		TEST_ALLOC_PARENT (setting->key, setting);

		TEST_EQ_STR (setting->value, "a value");
		TEST_ALLOC_SIZE (setting->value, 1+strlen ("a value"));
		TEST_ALLOC_PARENT (setting->value, setting);

		nih_free (setting);
	}

	TEST_FEATURE ("parent, key, value");
	TEST_ALLOC_FAIL {
		setting = cgroup_setting_new (parent, "hello world", "a value");

		if (test_alloc_failed) {
			TEST_EQ_P (setting, NULL);
			continue;
		}

		TEST_ALLOC_SIZE (setting, sizeof (CGroupSetting));
		TEST_ALLOC_PARENT (setting, parent);

		TEST_EQ_STR (setting->key, "hello world");
		TEST_ALLOC_SIZE (setting->key, 1+strlen ("hello world"));
		TEST_ALLOC_PARENT (setting->key, setting);

		TEST_EQ_STR (setting->value, "a value");
		TEST_ALLOC_SIZE (setting->value, 1+strlen ("a value"));
		TEST_ALLOC_PARENT (setting->value, setting);

		nih_free (setting);
	}
}
Exemple #17
0
void
test_add (void)
{
	char   **env = NULL, **ret;
	size_t   len = 0;

	TEST_FUNCTION ("environ_add");

	/* Check that we can add a variable to a new environment table
	 * and that it is appended to the array.
	 */
	TEST_FEATURE ("with empty table");
	TEST_ALLOC_FAIL {
		TEST_ALLOC_SAFE {
			len = 0;
			env = nih_str_array_new (NULL);
		}

		ret = environ_add (&env, NULL, &len, TRUE, "FOO=BAR");

		if (test_alloc_failed) {
			TEST_EQ_P (ret, NULL);

			TEST_EQ (len, 0);
			TEST_EQ_P (env[0], NULL);

			nih_free (env);
			continue;
		}

		TEST_NE_P (ret, NULL);

		TEST_EQ (len, 1);
		TEST_ALLOC_PARENT (env[0], env);
		TEST_ALLOC_SIZE (env[0], 8);
		TEST_EQ_STR (env[0], "FOO=BAR");
		TEST_EQ_P (env[1], NULL);

		nih_free (env);
	}


	/* Check that we can add a variable to an environment table with
	 * existing different entries and that it is appended to the array.
	 */
	TEST_FEATURE ("with new variable");
	TEST_ALLOC_FAIL {
		TEST_ALLOC_SAFE {
			len = 0;
			env = nih_str_array_new (NULL);
			assert (nih_str_array_add (&env, NULL, &len,
						   "FOO=BAR"));
			assert (nih_str_array_add (&env, NULL, &len,
						   "BAR=BAZ"));
		}

		ret = environ_add (&env, NULL, &len, TRUE,
				   "FRODO=BAGGINS");

		if (test_alloc_failed) {
			TEST_EQ_P (ret, NULL);

			TEST_EQ (len, 2);
			TEST_EQ_STR (env[0], "FOO=BAR");
			TEST_EQ_STR (env[1], "BAR=BAZ");
			TEST_EQ_P (env[2], NULL);

			nih_free (env);
			continue;
		}

		TEST_NE_P (ret, NULL);

		TEST_EQ (len, 3);
		TEST_EQ_STR (env[0], "FOO=BAR");
		TEST_EQ_STR (env[1], "BAR=BAZ");
		TEST_ALLOC_PARENT (env[2], env);
		TEST_ALLOC_SIZE (env[2], 14);
		TEST_EQ_STR (env[2], "FRODO=BAGGINS");
		TEST_EQ_P (env[3], NULL);

		nih_free (env);
	}


	/* Check that we can add a variable from the environment to the table
	 * and that it is appended to the array.
	 */
	TEST_FEATURE ("with new variable from environment");
	putenv ("FRODO=BAGGINS");

	TEST_ALLOC_FAIL {
		TEST_ALLOC_SAFE {
			len = 0;
			env = nih_str_array_new (NULL);
			assert (nih_str_array_add (&env, NULL, &len,
						   "FOO=BAR"));
			assert (nih_str_array_add (&env, NULL, &len,
						   "BAR=BAZ"));
		}

		ret = environ_add (&env, NULL, &len, TRUE,
				   "FRODO");

		if (test_alloc_failed) {
			TEST_EQ_P (ret, NULL);

			TEST_EQ (len, 2);
			TEST_EQ_STR (env[0], "FOO=BAR");
			TEST_EQ_STR (env[1], "BAR=BAZ");
			TEST_EQ_P (env[2], NULL);

			nih_free (env);
			continue;
		}

		TEST_NE_P (ret, NULL);

		TEST_EQ (len, 3);
		TEST_EQ_STR (env[0], "FOO=BAR");
		TEST_EQ_STR (env[1], "BAR=BAZ");
		TEST_ALLOC_PARENT (env[2], env);
		TEST_ALLOC_SIZE (env[2], 14);
		TEST_EQ_STR (env[2], "FRODO=BAGGINS");
		TEST_EQ_P (env[3], NULL);

		nih_free (env);
	}

	unsetenv ("FRODO");


	/* Check that when we attempt to add a variable that's not in the
	 * environment, the table is not extended.
	 */
	TEST_FEATURE ("with new variable unset in environment");
	unsetenv ("FRODO");

	TEST_ALLOC_FAIL {
		TEST_ALLOC_SAFE {
			len = 0;
			env = nih_str_array_new (NULL);
			assert (nih_str_array_add (&env, NULL, &len,
						   "FOO=BAR"));
			assert (nih_str_array_add (&env, NULL, &len,
						   "BAR=BAZ"));
		}

		ret = environ_add (&env, NULL, &len, TRUE,
				   "FRODO");

		if (test_alloc_failed) {
			TEST_EQ_P (ret, NULL);

			TEST_EQ (len, 2);
			TEST_EQ_STR (env[0], "FOO=BAR");
			TEST_EQ_STR (env[1], "BAR=BAZ");
			TEST_EQ_P (env[2], NULL);

			nih_free (env);
			continue;
		}

		TEST_NE_P (ret, NULL);

		TEST_EQ (len, 2);
		TEST_EQ_STR (env[0], "FOO=BAR");
		TEST_EQ_STR (env[1], "BAR=BAZ");
		TEST_EQ_P (env[2], NULL);

		nih_free (env);
	}


	/* Check that we can replace a variable in the environment table
	 * when one already exists with the same or different value.
	 */
	TEST_FEATURE ("with replacement variable");
	TEST_ALLOC_FAIL {
		char *old_env;

		TEST_ALLOC_SAFE {
			len = 0;
			env = nih_str_array_new (NULL);
			assert (nih_str_array_add (&env, NULL, &len,
						   "FOO=BAR"));
			assert (nih_str_array_add (&env, NULL, &len,
						   "BAR=BAZ"));
			assert (nih_str_array_add (&env, NULL, &len,
						   "FRODO=BAGGINS"));
		}

		old_env = env[1];
		TEST_FREE_TAG (old_env);

		ret = environ_add (&env, NULL, &len, TRUE,
				   "BAR=WIBBLE");

		if (test_alloc_failed) {
			TEST_EQ_P (ret, NULL);
			TEST_NOT_FREE (old_env);

			TEST_EQ (len, 3);
			TEST_EQ_STR (env[0], "FOO=BAR");
			TEST_EQ_STR (env[1], "BAR=BAZ");
			TEST_EQ_STR (env[2], "FRODO=BAGGINS");
			TEST_EQ_P (env[3], NULL);

			nih_free (env);
			continue;
		}

		TEST_FREE (old_env);

		TEST_NE_P (ret, NULL);

		TEST_EQ (len, 3);
		TEST_EQ_STR (env[0], "FOO=BAR");
		TEST_ALLOC_PARENT (env[1], env);
		TEST_ALLOC_SIZE (env[1], 11);
		TEST_EQ_STR (env[1], "BAR=WIBBLE");
		TEST_EQ_STR (env[2], "FRODO=BAGGINS");
		TEST_EQ_P (env[3], NULL);

		nih_free (env);
	}


	/* Check that we can replace a variable from the environment in the
	 * environment table when one already exists with the same or
	 * different value.
	 */
	TEST_FEATURE ("with replacement variable from environment");
	putenv ("BAR=WIBBLE");

	TEST_ALLOC_FAIL {
		char *old_env;

		TEST_ALLOC_SAFE {
			len = 0;
			env = nih_str_array_new (NULL);
			assert (nih_str_array_add (&env, NULL, &len,
						   "FOO=BAR"));
			assert (nih_str_array_add (&env, NULL, &len,
						   "BAR=BAZ"));
			assert (nih_str_array_add (&env, NULL, &len,
						   "FRODO=BAGGINS"));
			assert (nih_str_array_add (&env, NULL, &len,
						   "BILBO=TOOK"));
		}

		old_env = env[1];
		TEST_FREE_TAG (old_env);

		ret = environ_add (&env, NULL, &len, TRUE, "BAR");

		if (test_alloc_failed) {
			TEST_EQ_P (ret, NULL);
			TEST_NOT_FREE (old_env);

			TEST_EQ (len, 4);
			TEST_EQ_STR (env[0], "FOO=BAR");
			TEST_EQ_STR (env[1], "BAR=BAZ");
			TEST_EQ_STR (env[2], "FRODO=BAGGINS");
			TEST_EQ_STR (env[3], "BILBO=TOOK");
			TEST_EQ_P (env[4], NULL);

			nih_free (env);
			continue;
		}

		TEST_FREE (old_env);

		TEST_NE_P (ret, NULL);

		TEST_EQ (len, 4);
		TEST_EQ_STR (env[0], "FOO=BAR");
		TEST_ALLOC_PARENT (env[1], env);
		TEST_ALLOC_SIZE (env[1], 11);
		TEST_EQ_STR (env[1], "BAR=WIBBLE");
		TEST_EQ_STR (env[2], "FRODO=BAGGINS");
		TEST_EQ_STR (env[3], "BILBO=TOOK");
		TEST_EQ_P (env[4], NULL);

		nih_free (env);
	}

	unsetenv ("BAR");


	/* Check that when we attempt to replace a variable that's unset
	 * in the environment, the existing variable is removed from the
	 * table.
	 */
	TEST_FEATURE ("with replacement variable unset in environment");
	unsetenv ("BAR");

	TEST_ALLOC_FAIL {
		char *old_env;

		TEST_ALLOC_SAFE {
			len = 0;
			env = nih_str_array_new (NULL);
			assert (nih_str_array_add (&env, NULL, &len,
						   "FOO=BAR"));
			assert (nih_str_array_add (&env, NULL, &len,
						   "BAR=BAZ"));
			assert (nih_str_array_add (&env, NULL, &len,
						   "FRODO=BAGGINS"));
			assert (nih_str_array_add (&env, NULL, &len,
						   "BILBO=TOOK"));
		}

		old_env = env[1];
		TEST_FREE_TAG (old_env);

		ret = environ_add (&env, NULL, &len, TRUE, "BAR");

		if (test_alloc_failed) {
			TEST_EQ_P (ret, NULL);
			TEST_NOT_FREE (old_env);

			TEST_EQ (len, 4);
			TEST_EQ_STR (env[0], "FOO=BAR");
			TEST_EQ_STR (env[1], "BAR=BAZ");
			TEST_EQ_STR (env[2], "FRODO=BAGGINS");
			TEST_EQ_STR (env[3], "BILBO=TOOK");
			TEST_EQ_P (env[4], NULL);

			nih_free (env);
			continue;
		}

		TEST_FREE (old_env);

		TEST_NE_P (ret, NULL);

		TEST_EQ (len, 3);
		TEST_EQ_STR (env[0], "FOO=BAR");
		TEST_EQ_STR (env[1], "FRODO=BAGGINS");
		TEST_EQ_STR (env[2], "BILBO=TOOK");
		TEST_EQ_P (env[3], NULL);

		nih_free (env);
	}

	unsetenv ("BAR");


	/* Check that we can add a variable to an environment table with
	 * existing different entries and that it is appended to the array,
	 * even if replace is FALSE.
	 */
	TEST_FEATURE ("with new variable but no replace");
	TEST_ALLOC_FAIL {
		TEST_ALLOC_SAFE {
			len = 0;
			env = nih_str_array_new (NULL);
			assert (nih_str_array_add (&env, NULL, &len,
						   "FOO=BAR"));
			assert (nih_str_array_add (&env, NULL, &len,
						   "BAR=BAZ"));
		}

		ret = environ_add (&env, NULL, &len, FALSE,
				   "FRODO=BAGGINS");

		if (test_alloc_failed) {
			TEST_EQ_P (ret, NULL);

			TEST_EQ (len, 2);
			TEST_EQ_STR (env[0], "FOO=BAR");
			TEST_EQ_STR (env[1], "BAR=BAZ");
			TEST_EQ_P (env[2], NULL);

			nih_free (env);
			continue;
		}

		TEST_NE_P (ret, NULL);

		TEST_EQ (len, 3);
		TEST_EQ_STR (env[0], "FOO=BAR");
		TEST_EQ_STR (env[1], "BAR=BAZ");
		TEST_ALLOC_PARENT (env[2], env);
		TEST_ALLOC_SIZE (env[2], 14);
		TEST_EQ_STR (env[2], "FRODO=BAGGINS");
		TEST_EQ_P (env[3], NULL);

		nih_free (env);
	}


	/* Check that when a variable already exists in the environment
	 * table, and we're not replacing, the original value is left
	 * untouched.
	 */
	TEST_FEATURE ("with existing variable");
	TEST_ALLOC_FAIL {
		TEST_ALLOC_SAFE {
			len = 0;
			env = nih_str_array_new (NULL);
			assert (nih_str_array_add (&env, NULL, &len,
						   "FOO=BAR"));
			assert (nih_str_array_add (&env, NULL, &len,
						   "BAR=BAZ"));
			assert (nih_str_array_add (&env, NULL, &len,
						   "FRODO=BAGGINS"));
		}

		ret = environ_add (&env, NULL, &len, FALSE,
				   "BAR=WIBBLE");

		if (test_alloc_failed) {
			TEST_EQ_P (ret, NULL);

			TEST_EQ (len, 3);
			TEST_EQ_STR (env[0], "FOO=BAR");
			TEST_EQ_STR (env[1], "BAR=BAZ");
			TEST_EQ_STR (env[2], "FRODO=BAGGINS");
			TEST_EQ_P (env[3], NULL);

			nih_free (env);
			continue;
		}

		TEST_NE_P (ret, NULL);

		TEST_EQ (len, 3);
		TEST_EQ_STR (env[0], "FOO=BAR");
		TEST_EQ_STR (env[1], "BAR=BAZ");
		TEST_EQ_STR (env[2], "FRODO=BAGGINS");
		TEST_EQ_P (env[3], NULL);

		nih_free (env);
	}


	/* Check that when a variable from the environment already exists in
	 * the environment table, and we're not replacing, the original value
	 * is left untouched.
	 */
	TEST_FEATURE ("with existing variable from environment");
	putenv ("BAR=WIBBLE");

	TEST_ALLOC_FAIL {
		TEST_ALLOC_SAFE {
			len = 0;
			env = nih_str_array_new (NULL);
			assert (nih_str_array_add (&env, NULL, &len,
						   "FOO=BAR"));
			assert (nih_str_array_add (&env, NULL, &len,
						   "BAR=BAZ"));
			assert (nih_str_array_add (&env, NULL, &len,
						   "FRODO=BAGGINS"));
			assert (nih_str_array_add (&env, NULL, &len,
						   "BILBO=TOOK"));
		}

		ret = environ_add (&env, NULL, &len, FALSE, "BAR");

		if (test_alloc_failed) {
			TEST_EQ_P (ret, NULL);

			TEST_EQ (len, 4);
			TEST_EQ_STR (env[0], "FOO=BAR");
			TEST_EQ_STR (env[1], "BAR=BAZ");
			TEST_EQ_STR (env[2], "FRODO=BAGGINS");
			TEST_EQ_STR (env[3], "BILBO=TOOK");
			TEST_EQ_P (env[4], NULL);

			nih_free (env);
			continue;
		}

		TEST_NE_P (ret, NULL);

		TEST_EQ (len, 4);
		TEST_EQ_STR (env[0], "FOO=BAR");
		TEST_EQ_STR (env[1], "BAR=BAZ");
		TEST_EQ_STR (env[2], "FRODO=BAGGINS");
		TEST_EQ_STR (env[3], "BILBO=TOOK");
		TEST_EQ_P (env[4], NULL);

		nih_free (env);
	}

	unsetenv ("BAR");


	/* Check that when a variable from the environment is unset it
	 * does not remove an existing variable in the environment table
	 * if we're not replacing.
	 */
	TEST_FEATURE ("with existing variable unset in environment");
	unsetenv ("BAR");

	TEST_ALLOC_FAIL {
		TEST_ALLOC_SAFE {
			len = 0;
			env = nih_str_array_new (NULL);
			assert (nih_str_array_add (&env, NULL, &len,
						   "FOO=BAR"));
			assert (nih_str_array_add (&env, NULL, &len,
						   "BAR=BAZ"));
			assert (nih_str_array_add (&env, NULL, &len,
						   "FRODO=BAGGINS"));
			assert (nih_str_array_add (&env, NULL, &len,
						   "BILBO=TOOK"));
		}

		ret = environ_add (&env, NULL, &len, FALSE, "BAR");

		if (test_alloc_failed) {
			TEST_EQ_P (ret, NULL);

			TEST_EQ (len, 4);
			TEST_EQ_STR (env[0], "FOO=BAR");
			TEST_EQ_STR (env[1], "BAR=BAZ");
			TEST_EQ_STR (env[2], "FRODO=BAGGINS");
			TEST_EQ_STR (env[3], "BILBO=TOOK");
			TEST_EQ_P (env[4], NULL);

			nih_free (env);
			continue;
		}

		TEST_NE_P (ret, NULL);

		TEST_EQ (len, 4);
		TEST_EQ_STR (env[0], "FOO=BAR");
		TEST_EQ_STR (env[1], "BAR=BAZ");
		TEST_EQ_STR (env[2], "FRODO=BAGGINS");
		TEST_EQ_STR (env[3], "BILBO=TOOK");
		TEST_EQ_P (env[4], NULL);

		nih_free (env);
	}

	unsetenv ("BAR");
}
Exemple #18
0
void
test_strndup (void)
{
    char *str1, *str2;

    TEST_FUNCTION ("nih_strndup");

    /* Check that we can create a duplicate of the first portion of
     * another string, allocated with nih_alloc and no parent.  The
     * new string should still include a NULL byte.
     */
    TEST_FEATURE ("with no parent");
    TEST_ALLOC_FAIL {
        str1 = nih_strndup (NULL, "this is a test", 7);

        if (test_alloc_failed) {
            TEST_EQ_P (str1, NULL);
            continue;
        }

        TEST_ALLOC_PARENT (str1, NULL);
        TEST_ALLOC_SIZE (str1, 8);
        TEST_EQ_STR (str1, "this is");

        nih_free (str1);
    }


    /* Check that it works with a parent. */
    TEST_FEATURE ("with a parent");
    str1 = nih_strndup (NULL, "this is a test", 7);

    TEST_ALLOC_FAIL {
        str2 = nih_strndup (str1, "another test string", 12);

        if (test_alloc_failed) {
            TEST_EQ_P (str2, NULL);
            continue;
        }

        TEST_ALLOC_PARENT (str2, str1);
        TEST_ALLOC_SIZE (str2, 13);
        TEST_EQ_STR (str2, "another test");

        nih_free (str2);
    }

    nih_free (str1);


    /* Check that the right thing happens if the length we give is
     * longer than the string, the returned size should be ample but
     * with the complete string copied in.
     */
    TEST_FEATURE ("with larger length than string");
    TEST_ALLOC_FAIL {
        str1 = nih_strndup (NULL, "small string", 20);

        if (test_alloc_failed) {
            TEST_EQ_P (str1, NULL);
            continue;
        }

        TEST_ALLOC_SIZE (str1, 21);
        TEST_EQ_STR (str1, "small string");

        nih_free (str1);
    }
}
Exemple #19
0
void
test_array_addp (void)
{
    char   **array, **ret;
    char    *ptr1 = NULL;
    char    *ptr2 = NULL;
    size_t   len;

    TEST_FUNCTION ("nih_str_array_addn");


    /* Check that we can call the function with a NULL array pointer,
     * and get one allocated automatically.
     */
    TEST_FEATURE ("with no array given");
    TEST_ALLOC_FAIL {
        TEST_ALLOC_SAFE {
            ptr1 = nih_alloc (NULL, 1024);
            memset (ptr1, ' ', 1024);
        }

        array = NULL;
        len = 0;

        ret = nih_str_array_addp (&array, NULL, &len, ptr1);

        if (test_alloc_failed) {
            TEST_EQ_P (ret, NULL);
            TEST_EQ_P (array, NULL);

            TEST_EQ (len, 0);

            nih_free (ptr1);
            continue;
        }

        TEST_NE_P (ret, NULL);

        TEST_EQ (len, 1);
        TEST_EQ_P (array[0], ptr1);
        TEST_ALLOC_PARENT (array[0], array);
        TEST_EQ_P (array[1], NULL);

        nih_free (array);
        nih_free (ptr1);
    }


    /* Check that we can append allocated blocks to a
     * NULL-terminated array, and that the blocks are automatically
     * reparented.
     */
    TEST_FEATURE ("with length given");
    TEST_ALLOC_FAIL {
        TEST_ALLOC_SAFE {
            array = nih_str_array_new (NULL);
            len = 0;

            ptr1 = nih_alloc (NULL, 1024);
            memset (ptr1, ' ', 1024);
        }

        ret = nih_str_array_addp (&array, NULL, &len, ptr1);

        if (test_alloc_failed) {
            TEST_EQ_P (ret, NULL);

            TEST_EQ (len, 0);
            TEST_EQ_P (array[0], NULL);

            nih_free (array);
            nih_free (ptr1);
            continue;
        }

        TEST_NE_P (ret, NULL);

        TEST_EQ (len, 1);
        TEST_EQ_P (array[0], ptr1);
        TEST_ALLOC_PARENT (array[0], array);
        TEST_EQ_P (array[1], NULL);

        nih_free (array);
        nih_free (ptr1);
    }


    /* Check that we can omit the length, and have it calculated. */
    TEST_FEATURE ("with no length given");
    TEST_ALLOC_FAIL {
        TEST_ALLOC_SAFE {
            array = nih_str_array_new (NULL);
            len = 0;

            ptr1 = nih_alloc (NULL, 1024);
            memset (ptr1, ' ', 1024);

            assert (nih_str_array_addp (&array, NULL, NULL, ptr1));

            ptr2 = nih_alloc (NULL, 512);
            memset (ptr2, ' ', 512);
        }

        ret = nih_str_array_addp (&array, NULL, NULL, ptr2);

        if (test_alloc_failed) {
            TEST_EQ_P (ret, NULL);

            TEST_EQ_P (array[0], ptr1);
            TEST_EQ_P (array[1], NULL);

            nih_free (array);
            nih_free (ptr1);
            nih_free (ptr2);
            continue;
        }

        TEST_NE_P (ret, NULL);

        TEST_EQ_P (array[0], ptr1);
        TEST_ALLOC_PARENT (array[0], array);
        TEST_EQ_P (array[1], ptr2);
        TEST_ALLOC_PARENT (array[0], array);
        TEST_EQ_P (array[2], NULL);

        nih_free (array);
        nih_free (ptr1);
        nih_free (ptr2);
    }
}
Exemple #20
0
void
test_array_append (void)
{
    char   **array, **args, **ret;
    size_t   len;

    TEST_FUNCTION ("nih_str_array_append");
    args = NIH_MUST (nih_str_array_new (NULL));
    NIH_MUST (nih_str_array_add (&args, NULL, NULL, "this"));
    NIH_MUST (nih_str_array_add (&args, NULL, NULL, "is"));
    NIH_MUST (nih_str_array_add (&args, NULL, NULL, "a"));
    NIH_MUST (nih_str_array_add (&args, NULL, NULL, "test"));


    /* Check that we can append one array onto the end of the other,
     * and that the array is extended and each new element copied
     * into the new array.
     */
    TEST_FEATURE ("with length given");
    TEST_ALLOC_FAIL {
        len = 0;

        TEST_ALLOC_SAFE {
            array = nih_str_array_new (NULL);
            NIH_MUST (nih_str_array_add (&array, NULL, &len,
            "foo"));
            NIH_MUST (nih_str_array_add (&array, NULL, &len,
            "bar"));
        }

        ret = nih_str_array_append (&array, NULL, &len, args);

        if (test_alloc_failed) {
            TEST_EQ_P (ret, NULL);

            TEST_EQ (len, 2);
            TEST_EQ_STR (array[0], "foo");
            TEST_EQ_STR (array[1], "bar");
            TEST_EQ_P (array[2], NULL);

            nih_free (array);
            continue;
        }

        TEST_NE_P (ret, NULL);

        TEST_EQ (len, 6);
        TEST_EQ_STR (array[0], "foo");
        TEST_ALLOC_PARENT (array[0], array);
        TEST_EQ_STR (array[1], "bar");
        TEST_ALLOC_PARENT (array[1], array);
        TEST_EQ_STR (array[2], "this");
        TEST_ALLOC_PARENT (array[2], array);
        TEST_EQ_STR (array[3], "is");
        TEST_ALLOC_PARENT (array[3], array);
        TEST_EQ_STR (array[4], "a");
        TEST_ALLOC_PARENT (array[4], array);
        TEST_EQ_STR (array[5], "test");
        TEST_ALLOC_PARENT (array[5], array);
        TEST_EQ_P (array[6], NULL);

        nih_free (array);
    }


    /* Check that we can omit the length, and have it calculated. */
    TEST_FEATURE ("with no length given");
    TEST_ALLOC_FAIL {
        TEST_ALLOC_SAFE {
            array = nih_str_array_new (NULL);
            NIH_MUST (nih_str_array_add (&array, NULL, NULL,
            "foo"));
            NIH_MUST (nih_str_array_add (&array, NULL, NULL,
            "bar"));
        }

        ret = nih_str_array_append (&array, NULL, NULL, args);

        if (test_alloc_failed) {
            TEST_EQ_P (ret, NULL);

            TEST_EQ_STR (array[0], "foo");
            TEST_EQ_STR (array[1], "bar");
            TEST_EQ_P (array[2], NULL);

            nih_free (array);
            continue;
        }

        TEST_NE_P (ret, NULL);

        TEST_EQ_STR (array[0], "foo");
        TEST_ALLOC_PARENT (array[0], array);
        TEST_EQ_STR (array[1], "bar");
        TEST_ALLOC_PARENT (array[1], array);
        TEST_EQ_STR (array[2], "this");
        TEST_ALLOC_PARENT (array[2], array);
        TEST_EQ_STR (array[3], "is");
        TEST_ALLOC_PARENT (array[3], array);
        TEST_EQ_STR (array[4], "a");
        TEST_ALLOC_PARENT (array[4], array);
        TEST_EQ_STR (array[5], "test");
        TEST_ALLOC_PARENT (array[5], array);
        TEST_EQ_P (array[6], NULL);

        nih_free (array);
    }


    /* Check that we can pass a NULL array to get a copy of it, with
     * the returned length containing the new length.
     */
    TEST_FEATURE ("with NULL array and length");
    TEST_ALLOC_FAIL {
        len = 0;
        array = NULL;
        ret = nih_str_array_append (&array, NULL, &len, args);

        if (test_alloc_failed) {
            TEST_EQ_P (ret, NULL);

            TEST_EQ (len, 0);
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_NE_P (ret, NULL);

        TEST_EQ (len, 4);
        TEST_EQ_STR (array[0], "this");
        TEST_ALLOC_PARENT (array[0], array);
        TEST_EQ_STR (array[1], "is");
        TEST_ALLOC_PARENT (array[1], array);
        TEST_EQ_STR (array[2], "a");
        TEST_ALLOC_PARENT (array[2], array);
        TEST_EQ_STR (array[3], "test");
        TEST_ALLOC_PARENT (array[3], array);
        TEST_EQ_P (array[4], NULL);

        nih_free (array);
    }


    /* Check that we can pass a NULL array to get a copy of it, without
     * passing the length in.
     */
    TEST_FEATURE ("with NULL array and no length");
    TEST_ALLOC_FAIL {
        array = NULL;
        ret = nih_str_array_append (&array, NULL, NULL, args);

        if (test_alloc_failed) {
            TEST_EQ_P (ret, NULL);

            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_NE_P (ret, NULL);

        TEST_EQ_STR (array[0], "this");
        TEST_ALLOC_PARENT (array[0], array);
        TEST_EQ_STR (array[1], "is");
        TEST_ALLOC_PARENT (array[1], array);
        TEST_EQ_STR (array[2], "a");
        TEST_ALLOC_PARENT (array[2], array);
        TEST_EQ_STR (array[3], "test");
        TEST_ALLOC_PARENT (array[3], array);
        TEST_EQ_P (array[4], NULL);

        nih_free (array);
    }


    nih_free (args);
}
Exemple #21
0
void
test_array_copy (void)
{
    char   **array, **args;
    size_t   len;

    TEST_FUNCTION ("nih_str_array_copy");
    args = NIH_MUST (nih_str_array_new (NULL));
    NIH_MUST (nih_str_array_add (&args, NULL, NULL, "this"));
    NIH_MUST (nih_str_array_add (&args, NULL, NULL, "is"));
    NIH_MUST (nih_str_array_add (&args, NULL, NULL, "a"));
    NIH_MUST (nih_str_array_add (&args, NULL, NULL, "test"));


    /* Check that we can make a copy of an array, with each element
     * a copy of the last.
     */
    TEST_FEATURE ("with length given");
    TEST_ALLOC_FAIL {
        len = 0;
        array = nih_str_array_copy (NULL, &len, args);

        if (test_alloc_failed) {
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_NE_P (array, NULL);

        TEST_EQ (len, 4);
        TEST_EQ_STR (array[0], "this");
        TEST_ALLOC_PARENT (array[0], array);
        TEST_EQ_STR (array[1], "is");
        TEST_ALLOC_PARENT (array[1], array);
        TEST_EQ_STR (array[2], "a");
        TEST_ALLOC_PARENT (array[2], array);
        TEST_EQ_STR (array[3], "test");
        TEST_ALLOC_PARENT (array[3], array);
        TEST_EQ_P (array[4], NULL);

        nih_free (array);
    }


    /* Check that we can omit the length, and have it calculated. */
    TEST_FEATURE ("with no length given");
    TEST_ALLOC_FAIL {
        array = nih_str_array_copy (NULL, NULL, args);

        if (test_alloc_failed) {
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_NE_P (array, NULL);

        TEST_EQ_STR (array[0], "this");
        TEST_ALLOC_PARENT (array[0], array);
        TEST_EQ_STR (array[1], "is");
        TEST_ALLOC_PARENT (array[1], array);
        TEST_EQ_STR (array[2], "a");
        TEST_ALLOC_PARENT (array[2], array);
        TEST_EQ_STR (array[3], "test");
        TEST_ALLOC_PARENT (array[3], array);
        TEST_EQ_P (array[4], NULL);

        nih_free (array);
    }

    nih_free (args);


    /* Check that we can make a copy of an array with a single NULL
     * element and have the same returned.
     */
    TEST_FEATURE ("with zero-length array");
    args = NIH_MUST (nih_str_array_new (NULL));

    TEST_ALLOC_FAIL {
        len = 0;
        array = nih_str_array_copy (NULL, &len, args);

        if (test_alloc_failed) {
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_NE_P (array, NULL);

        TEST_EQ (len, 0);
        TEST_EQ_P (array[0], NULL);

        nih_free (array);
    }

    nih_free (args);
}
Exemple #22
0
void
test_str_split (void)
{
    char **array;
    int    i;

    TEST_FUNCTION ("nih_str_split");

    /* Check that we can split a string into a NULL-terminated array
     * at each matching character.  The array should be allocated with
     * nih_alloc, and each element should also be with the array as
     * their parent.
     */
    TEST_FEATURE ("with no repeat");
    TEST_ALLOC_FAIL {
        array = nih_str_split (NULL, "this is  a\ttest", " \t", FALSE);

        if (test_alloc_failed) {
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_ALLOC_SIZE (array, sizeof (char *) * 6);
        for (i = 0; i < 5; i++)
            TEST_ALLOC_PARENT (array[i], array);

        TEST_EQ_STR (array[0], "this");
        TEST_EQ_STR (array[1], "is");
        TEST_EQ_STR (array[2], "");
        TEST_EQ_STR (array[3], "a");
        TEST_EQ_STR (array[4], "test");
        TEST_EQ_P (array[5], NULL);

        nih_free (array);
    }

    TEST_FEATURE ("with no repeat and multiple identical delimiter "
                  "characters at string start");
    TEST_ALLOC_FAIL {
        array = nih_str_split (NULL, "\t\tthis is a test", " \t", FALSE);

        if (test_alloc_failed) {
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_ALLOC_SIZE (array, sizeof (char *) * 7);
        for (i = 0; i < 6; i++)
            TEST_ALLOC_PARENT (array[i], array);

        TEST_EQ_STR (array[0], "");
        TEST_EQ_STR (array[1], "");
        TEST_EQ_STR (array[2], "this");
        TEST_EQ_STR (array[3], "is");
        TEST_EQ_STR (array[4], "a");
        TEST_EQ_STR (array[5], "test");
        TEST_EQ_P (array[6], NULL);

        nih_free (array);
    }

    TEST_FEATURE ("with no repeat and multiple different delimiter "
                  "characters at string start");
    TEST_ALLOC_FAIL {
        array = nih_str_split (NULL, " \tthis is a test", " \t", FALSE);

        if (test_alloc_failed) {
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_ALLOC_SIZE (array, sizeof (char *) * 7);
        for (i = 0; i < 6; i++)
            TEST_ALLOC_PARENT (array[i], array);

        TEST_EQ_STR (array[0], "");
        TEST_EQ_STR (array[1], "");
        TEST_EQ_STR (array[2], "this");
        TEST_EQ_STR (array[3], "is");
        TEST_EQ_STR (array[4], "a");
        TEST_EQ_STR (array[5], "test");
        TEST_EQ_P (array[6], NULL);

        nih_free (array);
    }

    TEST_FEATURE ("with no repeat and multiple identical delimiter "
                  "characters within string");
    TEST_ALLOC_FAIL {
        array = nih_str_split (NULL, "this is   a\t\ttest", " \t", FALSE);

        if (test_alloc_failed) {
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_ALLOC_SIZE (array, sizeof (char *) * 8);
        for (i = 0; i < 7; i++)
            TEST_ALLOC_PARENT (array[i], array);

        TEST_EQ_STR (array[0], "this");
        TEST_EQ_STR (array[1], "is");
        TEST_EQ_STR (array[2], "");
        TEST_EQ_STR (array[3], "");
        TEST_EQ_STR (array[4], "a");
        TEST_EQ_STR (array[5], "");
        TEST_EQ_STR (array[6], "test");
        TEST_EQ_P (array[7], NULL);

        nih_free (array);
    }

    TEST_FEATURE ("with no repeat and multiple different delimiter "
                  "characters within string");
    TEST_ALLOC_FAIL {
        array = nih_str_split (NULL, "this is \n\ta\ttest", " \t\n", FALSE);

        if (test_alloc_failed) {
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_ALLOC_SIZE (array, sizeof (char *) * 7);
        for (i = 0; i < 6; i++)
            TEST_ALLOC_PARENT (array[i], array);

        TEST_EQ_STR (array[0], "this");
        TEST_EQ_STR (array[1], "is");
        TEST_EQ_STR (array[2], "");
        TEST_EQ_STR (array[3], "");
        TEST_EQ_STR (array[4], "a");
        TEST_EQ_STR (array[5], "test");
        TEST_EQ_P (array[6], NULL);

        nih_free (array);
    }

    TEST_FEATURE ("with no repeat and multiple identical delimiter "
                  "characters at string end");
    TEST_ALLOC_FAIL {
        array = nih_str_split (NULL, "this is a test  ", " \t", FALSE);

        if (test_alloc_failed) {
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_ALLOC_SIZE (array, sizeof (char *) * 6);
        for (i = 0; i < 5; i++)
            TEST_ALLOC_PARENT (array[i], array);

        TEST_EQ_STR (array[0], "this");
        TEST_EQ_STR (array[1], "is");
        TEST_EQ_STR (array[2], "a");
        TEST_EQ_STR (array[3], "test");
        TEST_EQ_STR (array[4], "");
        TEST_EQ_P (array[5], NULL);

        nih_free (array);
    }

    TEST_FEATURE ("with no repeat and multiple different delimiter "
                  "characters at string end");
    TEST_ALLOC_FAIL {
        array = nih_str_split (NULL, "this is a test \t", " \t", FALSE);

        if (test_alloc_failed) {
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_ALLOC_SIZE (array, sizeof (char *) * 6);
        for (i = 0; i < 5; i++)
            TEST_ALLOC_PARENT (array[i], array);

        TEST_EQ_STR (array[0], "this");
        TEST_EQ_STR (array[1], "is");
        TEST_EQ_STR (array[2], "a");
        TEST_EQ_STR (array[3], "test");
        TEST_EQ_STR (array[4], "");
        TEST_EQ_P (array[5], NULL);

        nih_free (array);
    }

    TEST_FEATURE ("with no repeat and multiple identical delimiter "
                  "characters at beginning, middle and end of string");
    TEST_ALLOC_FAIL {
        array = nih_str_split (NULL, "   this is\n\n\na test\t\t\t", " \t\n", FALSE);

        if (test_alloc_failed) {
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_ALLOC_SIZE (array, sizeof (char *) * 12);
        for (i = 0; i < 11; i++)
            TEST_ALLOC_PARENT (array[i], array);

        TEST_EQ_STR (array[0], "");
        TEST_EQ_STR (array[1], "");
        TEST_EQ_STR (array[2], "");
        TEST_EQ_STR (array[3], "this");
        TEST_EQ_STR (array[4], "is");
        TEST_EQ_STR (array[5], "");
        TEST_EQ_STR (array[6], "");
        TEST_EQ_STR (array[7], "a");
        TEST_EQ_STR (array[8], "test");
        TEST_EQ_STR (array[9], "");
        TEST_EQ_STR (array[10], "");
        TEST_EQ_P (array[11], NULL);

        nih_free (array);
    }

    TEST_FEATURE ("with no repeat and multiple different delimiter "
                  "characters at beginning, middle and end of string");
    TEST_ALLOC_FAIL {
        array = nih_str_split (NULL, ": \nthis is\t \n:a test:\n ", "\n :\t", FALSE);

        if (test_alloc_failed) {
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_ALLOC_SIZE (array, sizeof (char *) * 13);
        for (i = 0; i < 12; i++)
            TEST_ALLOC_PARENT (array[i], array);

        TEST_EQ_STR (array[0], "");
        TEST_EQ_STR (array[1], "");
        TEST_EQ_STR (array[2], "");
        TEST_EQ_STR (array[3], "this");
        TEST_EQ_STR (array[4], "is");
        TEST_EQ_STR (array[5], "");
        TEST_EQ_STR (array[6], "");
        TEST_EQ_STR (array[7], "");
        TEST_EQ_STR (array[8], "a");
        TEST_EQ_STR (array[9], "test");
        TEST_EQ_STR (array[10], "");
        TEST_EQ_STR (array[11], "");
        TEST_EQ_P (array[12], NULL);

        nih_free (array);
    }

    /* Check that we can split a string treating multiple consecutive
     * matching characters as a single separator to be skipped.
     */
    TEST_FEATURE ("with repeat");
    TEST_ALLOC_FAIL {
        array = nih_str_split (NULL, "this is  a\ttest", " \t", TRUE);

        if (test_alloc_failed) {
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_ALLOC_SIZE (array, sizeof (char *) * 5);
        for (i = 0; i < 4; i++)
            TEST_ALLOC_PARENT (array[i], array);

        TEST_EQ_STR (array[0], "this");
        TEST_EQ_STR (array[1], "is");
        TEST_EQ_STR (array[2], "a");
        TEST_EQ_STR (array[3], "test");
        TEST_EQ_P (array[4], NULL);

        nih_free (array);
    }

    /* Check that we can split a string containing multiple
     * occurences of one of the delimiter characters at the
     * beginning of the string.
     */
    TEST_FEATURE ("with repeat and multiple identical adjacent delimiter characters at string start");
    TEST_ALLOC_FAIL {
        array = nih_str_split (NULL, "\n\nhello", " \t\r\n", TRUE);

        if (test_alloc_failed) {
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_ALLOC_SIZE (array, sizeof (char *) * 2);
        for (i = 0; i < 1; i++)
            TEST_ALLOC_PARENT (array[i], array);

        TEST_EQ_STR (array[0], "hello");
        TEST_EQ_P (array[1], NULL);

        nih_free (array);
    }

    TEST_FEATURE ("with repeat and multiple different adjacent delimiter characters at string start");
    TEST_ALLOC_FAIL {
        array = nih_str_split (NULL, "\n\r hello", " \t\r\n", TRUE);

        if (test_alloc_failed) {
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_ALLOC_SIZE (array, sizeof (char *) * 2);
        for (i = 0; i < 1; i++)
            TEST_ALLOC_PARENT (array[i], array);

        TEST_EQ_STR (array[0], "hello");
        TEST_EQ_P (array[1], NULL);

        nih_free (array);
    }

    TEST_FEATURE ("with repeat and multiple identical adjacent delimiter "
                  "characters within string");
    TEST_ALLOC_FAIL {
        array = nih_str_split (NULL, "hello\n\rworld", " \t\n\r", TRUE);

        if (test_alloc_failed) {
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_ALLOC_SIZE (array, sizeof (char *) * 3);
        for (i = 0; i < 2; i++)
            TEST_ALLOC_PARENT (array[i], array);

        TEST_EQ_STR (array[0], "hello");
        TEST_EQ_STR (array[1], "world");
        TEST_EQ_P (array[2], NULL);

        nih_free (array);
    }

    TEST_FEATURE ("with repeat and multiple different adjacent delimiter "
                  "characters within string");
    TEST_ALLOC_FAIL {
        array = nih_str_split (NULL, "hello\n\r\tworld", " \t\n\r", TRUE);

        if (test_alloc_failed) {
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_ALLOC_SIZE (array, sizeof (char *) * 3);
        for (i = 0; i < 2; i++)
            TEST_ALLOC_PARENT (array[i], array);

        TEST_EQ_STR (array[0], "hello");
        TEST_EQ_STR (array[1], "world");
        TEST_EQ_P (array[2], NULL);

        nih_free (array);
    }

    TEST_FEATURE ("with repeat and multiple identical adjacent delimiter "
                  "characters at string end");
    TEST_ALLOC_FAIL {
        array = nih_str_split (NULL, "hello\n\n\n\n\n\n\n", " \t\r\n", TRUE);

        if (test_alloc_failed) {
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_ALLOC_SIZE (array, sizeof (char *) * 2);
        for (i = 0; i < 1; i++)
            TEST_ALLOC_PARENT (array[i], array);

        TEST_EQ_STR (array[0], "hello");
        TEST_EQ_P (array[1], NULL);

        nih_free (array);
    }

    TEST_FEATURE ("with repeat and multiple different adjacent delimiter "
                  "characters at string end");
    TEST_ALLOC_FAIL {
        array = nih_str_split (NULL, "hello \r\t\r\t\n ", " \t\r\n", TRUE);

        if (test_alloc_failed) {
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_ALLOC_SIZE (array, sizeof (char *) * 2);
        for (i = 0; i < 1; i++)
            TEST_ALLOC_PARENT (array[i], array);

        TEST_EQ_STR (array[0], "hello");
        TEST_EQ_P (array[1], NULL);

        nih_free (array);
    }

    TEST_FEATURE ("with repeat and multiple identical adjacent delimiter "
                  "characters at beginning, middle and end of string");
    TEST_ALLOC_FAIL {
        array = nih_str_split (NULL,
        "        hello\n\n\n,  world\n\n\n",
        "\r\t\n ", TRUE);

        if (test_alloc_failed) {
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_ALLOC_SIZE (array, sizeof (char *) * 4);
        for (i = 0; i < 3; i++)
            TEST_ALLOC_PARENT (array[i], array);

        TEST_EQ_STR (array[0], "hello");
        TEST_EQ_STR (array[1], ",");
        TEST_EQ_STR (array[2], "world");
        TEST_EQ_P (array[3], NULL);

        nih_free (array);
    }

    TEST_FEATURE ("with repeat and multiple different adjacent delimiter "
                  "characters at beginning, middle and end of string");
    TEST_ALLOC_FAIL {
        array = nih_str_split (NULL,
        "\n    \r\thello\n\n\r , \n\t\rworld\t \r\n \n",
        " \t\n\r", TRUE);

        if (test_alloc_failed) {
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_ALLOC_SIZE (array, sizeof (char *) * 4);
        for (i = 0; i < 3; i++)
            TEST_ALLOC_PARENT (array[i], array);

        TEST_EQ_STR (array[0], "hello");
        TEST_EQ_STR (array[1], ",");
        TEST_EQ_STR (array[2], "world");
        TEST_EQ_P (array[3], NULL);

        nih_free (array);
    }

    /* Check that we can give an empty string, and end up with a
     * one-element array that only contains a NULL pointer.
     */
    TEST_FEATURE ("with empty string");
    TEST_ALLOC_FAIL {
        array = nih_str_split (NULL, "", " ", FALSE);

        if (test_alloc_failed) {
            TEST_EQ_P (array, NULL);
            continue;
        }

        TEST_ALLOC_SIZE (array, sizeof (char *));
        TEST_EQ_P (array[0], NULL);

        nih_free (array);
    }
}
Exemple #23
0
void
test_operator_copy (void)
{
	EventOperator *oper = NULL, *copy;
	EventOperator *oper1 = NULL, *oper2 = NULL, *copy1, *copy2;

	TEST_FUNCTION ("event_operator_copy");
	event_init ();

	/* Check that we can copy a plain event operator, the value should
	 * be copied as well, and the other fields left as NULL.
	 */
	TEST_FEATURE ("with EVENT_OR");
	TEST_ALLOC_FAIL {
		TEST_ALLOC_SAFE {
			oper = event_operator_new (NULL, EVENT_OR, NULL, NULL);
			oper->value = TRUE;
		}

		copy = event_operator_copy (NULL, oper);

		if (test_alloc_failed) {
			TEST_EQ_P (copy, NULL);
			nih_free (oper);
			continue;
		}

		TEST_ALLOC_SIZE (copy, sizeof (EventOperator));
		TEST_EQ_P (copy->node.parent, NULL);
		TEST_EQ_P (copy->node.left, NULL);
		TEST_EQ_P (copy->node.right, NULL);
		TEST_EQ (copy->type, EVENT_OR);
		TEST_EQ (copy->value, TRUE);
		TEST_EQ_P (copy->name, NULL);
		TEST_EQ_P (copy->env, NULL);
		TEST_EQ_P (copy->event, NULL);

		nih_free (copy);
		nih_free (oper);
	}


	/* Check that we can copy and EVENT_MATCH operator which does not
	 * have any environment or matched event.
	 */
	TEST_FEATURE ("with EVENT_MATCH and no environment or event");
	TEST_ALLOC_FAIL {
		TEST_ALLOC_SAFE {
			oper = event_operator_new (NULL, EVENT_MATCH,
						   "test", NULL);
			oper->value = TRUE;
		}

		copy = event_operator_copy (NULL, oper);

		if (test_alloc_failed) {
			TEST_EQ_P (copy, NULL);
			nih_free (oper);
			continue;
		}

		TEST_ALLOC_SIZE (copy, sizeof (EventOperator));
		TEST_EQ_P (copy->node.parent, NULL);
		TEST_EQ_P (copy->node.left, NULL);
		TEST_EQ_P (copy->node.right, NULL);
		TEST_EQ (copy->type, EVENT_MATCH);
		TEST_EQ (copy->value, TRUE);
		TEST_EQ_STR (copy->name, "test");
		TEST_ALLOC_PARENT (copy->name, copy);
		TEST_EQ_P (copy->env, NULL);
		TEST_EQ_P (copy->event, NULL);

		nih_free (copy);
		nih_free (oper);
	}


	/* Check that environment to an EVENT_MATCH operator are also copied,
	 * and each entry within the array copied too.
	 */
	TEST_FEATURE ("with EVENT_MATCH and environment");
	TEST_ALLOC_FAIL {
		TEST_ALLOC_SAFE {
			oper = event_operator_new (NULL, EVENT_MATCH,
						   "test", NULL);
			oper->value = TRUE;

			NIH_MUST (nih_str_array_add (&oper->env, oper,
						     NULL, "FOO=foo"));
			NIH_MUST (nih_str_array_add (&oper->env, oper,
						     NULL, "BAR=bar"));
		}

		copy = event_operator_copy (NULL, oper);

		if (test_alloc_failed) {
			TEST_EQ_P (copy, NULL);
			nih_free (oper);
			continue;
		}

		TEST_ALLOC_SIZE (copy, sizeof (EventOperator));
		TEST_EQ_P (copy->node.parent, NULL);
		TEST_EQ_P (copy->node.left, NULL);
		TEST_EQ_P (copy->node.right, NULL);
		TEST_EQ (copy->type, EVENT_MATCH);
		TEST_EQ (copy->value, TRUE);
		TEST_EQ_STR (copy->name, "test");
		TEST_ALLOC_PARENT (copy->name, copy);

		TEST_ALLOC_PARENT (copy->env, copy);
		TEST_ALLOC_SIZE (copy->env, sizeof (char *) * 3);
		TEST_ALLOC_PARENT (copy->env[0], copy->env);
		TEST_ALLOC_PARENT (copy->env[1], copy->env);
		TEST_EQ_STR (copy->env[0], "FOO=foo");
		TEST_EQ_STR (copy->env[1], "BAR=bar");
		TEST_EQ_P (copy->env[2], NULL);

		TEST_EQ_P (copy->event, NULL);

		nih_free (copy);
		nih_free (oper);
	}


	/* Check that if the EVENT_MATCH operator has a referenced event,
	 * the event is copied and referenced a second time.
	 */
	TEST_FEATURE ("with EVENT_MATCH and referenced event");
	TEST_ALLOC_FAIL {
		TEST_ALLOC_SAFE {
			oper = event_operator_new (NULL, EVENT_MATCH,
						   "test", NULL);
			oper->value = TRUE;

			oper->event = event_new (oper, "test", NULL);
			event_block (oper->event);
		}

		copy = event_operator_copy (NULL, oper);

		if (test_alloc_failed) {
			TEST_EQ_P (copy, NULL);
			nih_free (oper);
			continue;
		}

		TEST_ALLOC_SIZE (copy, sizeof (EventOperator));
		TEST_EQ_P (copy->node.parent, NULL);
		TEST_EQ_P (copy->node.left, NULL);
		TEST_EQ_P (copy->node.right, NULL);
		TEST_EQ (copy->type, EVENT_MATCH);
		TEST_EQ (copy->value, TRUE);
		TEST_EQ_STR (copy->name, "test");
		TEST_ALLOC_PARENT (copy->name, copy);
		TEST_EQ_P (copy->env, NULL);

		TEST_EQ_P (copy->event, oper->event);
		TEST_EQ (copy->event->blockers, 2);

		nih_free (copy);
		nih_free (oper);
	}


	/* Check that if the operator has children, these are copied as
	 * well, including their state.
	 */
	TEST_FEATURE ("with children");
	TEST_ALLOC_FAIL {
		TEST_ALLOC_SAFE {
			oper = event_operator_new (NULL, EVENT_OR, NULL, NULL);
			oper->value = TRUE;

			oper1 = event_operator_new (NULL, EVENT_MATCH,
						    "foo", NULL);
			oper1->value = TRUE;
			oper1->event = event_new (oper1, "foo", NULL);
			event_block (oper1->event);
			nih_tree_add (&oper->node, &oper1->node,
				      NIH_TREE_LEFT);

			oper2 = event_operator_new (NULL, EVENT_MATCH,
						    "bar", NULL);
			oper2->value = TRUE;
			oper2->event = event_new (oper2, "foo", NULL);
			event_block (oper2->event);
			nih_tree_add (&oper->node, &oper2->node,
				      NIH_TREE_RIGHT);
		}

		copy = event_operator_copy (NULL, oper);

		if (test_alloc_failed) {
			TEST_EQ_P (copy, NULL);
			nih_free (oper);
			TEST_EQ (oper1->event->blockers, 1);
			nih_free (oper1);
			TEST_EQ (oper2->event->blockers, 1);
			nih_free (oper2);
			continue;
		}

		TEST_ALLOC_SIZE (copy, sizeof (EventOperator));
		TEST_EQ_P (copy->node.parent, NULL);
		TEST_NE_P (copy->node.left, NULL);
		TEST_NE_P (copy->node.right, NULL);
		TEST_EQ (copy->type, EVENT_OR);
		TEST_EQ (copy->value, TRUE);
		TEST_EQ_P (copy->name, NULL);
		TEST_EQ_P (copy->env, NULL);

		copy1 = (EventOperator *)copy->node.left;
		TEST_ALLOC_SIZE (copy1, sizeof (EventOperator));
		TEST_ALLOC_PARENT (copy1, copy);
		TEST_EQ_P (copy1->node.parent, &copy->node);
		TEST_EQ_P (copy1->node.left, NULL);
		TEST_EQ_P (copy1->node.right, NULL);
		TEST_EQ (copy1->type, EVENT_MATCH);
		TEST_EQ (copy1->value, TRUE);
		TEST_EQ_STR (copy1->name, "foo");
		TEST_ALLOC_PARENT (copy1->name, copy1);
		TEST_EQ_P (copy1->env, NULL);

		TEST_EQ_P (copy1->event, oper1->event);
		TEST_EQ (copy1->event->blockers, 2);

		nih_free (copy1);

		copy2 = (EventOperator *)copy->node.right;
		TEST_ALLOC_SIZE (copy2, sizeof (EventOperator));
		TEST_ALLOC_PARENT (copy2, copy);
		TEST_EQ_P (copy2->node.parent, &copy->node);
		TEST_EQ_P (copy2->node.left, NULL);
		TEST_EQ_P (copy2->node.right, NULL);
		TEST_EQ (copy2->type, EVENT_MATCH);
		TEST_EQ (copy2->value, TRUE);
		TEST_EQ_STR (copy2->name, "bar");
		TEST_ALLOC_PARENT (copy2->name, copy2);
		TEST_EQ_P (copy2->env, NULL);

		TEST_EQ_P (copy2->event, oper2->event);
		TEST_EQ (copy2->event->blockers, 2);

		nih_free (copy2);
		nih_free (copy);
		nih_free (oper1);
		nih_free (oper2);
		nih_free (oper);
	}

	event_poll ();
}