예제 #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);
	}
}
예제 #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);
}
예제 #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);
}
예제 #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);
}
예제 #5
0
void
test_new (void)
{
	Process *process;

	/* Check that we can create a new Process structure; the structure
	 * should be allocated with nih_alloc and have sensible defaults.
	 */
	TEST_FUNCTION ("process_new");
	TEST_ALLOC_FAIL {
		process = process_new (NULL);

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

		TEST_ALLOC_SIZE (process, sizeof (Process));

		TEST_EQ (process->script, FALSE);
		TEST_EQ_P (process->command, NULL);

		nih_free (process);
	}
}
예제 #6
0
파일: test_main.c 프로젝트: Gladiel/libnih
void
test_main_loop_add_func (void)
{
	NihMainLoopFunc *func;

	/* Check that we can add a callback function to the main loop,
	 * and that the structure returned is correctly populated and
	 * placed in a list.
	 */
	TEST_FUNCTION ("nih_main_loop_add_func");
	TEST_ALLOC_FAIL {
		func = nih_main_loop_add_func (NULL, my_callback, &func);

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

		TEST_ALLOC_SIZE (func, sizeof (NihMainLoopFunc));
		TEST_LIST_NOT_EMPTY (&func->entry);
		TEST_EQ_P (func->callback, my_callback);
		TEST_EQ_P (func->data, &func);

		nih_free (func);
	}
}
예제 #7
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);
}
예제 #8
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);
}
예제 #9
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);
}
예제 #10
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);
	}
}
예제 #11
0
void
test_array_new (void)
{
    char **array;

    /* Check that we can allocate a NULL-terminated array of strings using
     * nih_alloc().
     */
    TEST_FUNCTION ("nih_str_array_new");
    TEST_ALLOC_FAIL {
        array = nih_str_array_new (NULL);

        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);
    }
}
예제 #12
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);
}
예제 #13
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);
    }
}
예제 #14
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);
}
예제 #15
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);
    }
}
예제 #16
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);
	}
}
예제 #17
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);
	}
}
예제 #18
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);
	}
}
예제 #19
0
void
test_strcat (void)
{
    char *str, *ret;

    TEST_FUNCTION ("nih_strcat");

    /* Check that we can extend a string with another, resulting in the
     * original string being modified and the new pointer stored in the
     * argument and returned.
     */
    TEST_FEATURE ("with string");
    TEST_ALLOC_FAIL {
        char *tmp;

        TEST_ALLOC_SAFE {
            str = nih_strdup (NULL, "this is a test");
        }

        tmp = str;
        ret = nih_strcat (&str, NULL, " of strdup");

        if (test_alloc_failed) {
            TEST_EQ_P (ret, NULL);
            TEST_EQ_P (str, tmp);
            TEST_EQ_STR (str, "this is a test");

            nih_free (str);
            continue;
        }

        TEST_NE (ret, NULL);
        TEST_EQ_P (ret, str);

        TEST_ALLOC_SIZE (str, 25);
        TEST_EQ_STR (str, "this is a test of strdup");

        nih_free (str);
    }


    /* Check that when no string is passed, this behaves as strdup.
     */
    TEST_FEATURE ("with NULL");
    TEST_ALLOC_FAIL {
        str = NULL;
        ret = nih_strcat (&str, NULL, "test of strdup");

        if (test_alloc_failed) {
            TEST_EQ_P (ret, NULL);
            TEST_EQ_P (str, NULL);
            continue;
        }

        TEST_NE (ret, NULL);
        TEST_EQ_P (ret, str);

        TEST_ALLOC_SIZE (str, 15);
        TEST_EQ_STR (str, "test of strdup");

        nih_free (str);
    }
}
예제 #20
0
void
test_strncat (void)
{
    char *str, *ret;

    TEST_FUNCTION ("nih_strncat");

    /* Check that we can extend a string with the first number of bytes
     * from another, resulting in the original string being modified and
     * the new pointer stored in the argument and returned.
     */
    TEST_FEATURE ("with larger string than length");
    TEST_ALLOC_FAIL {
        char *tmp;

        TEST_ALLOC_SAFE {
            str = nih_strdup (NULL, "this is a test");
        }

        tmp = str;
        ret = nih_strncat (&str, NULL, " of strndup", 3);

        if (test_alloc_failed) {
            TEST_EQ_P (ret, NULL);
            TEST_EQ_P (str, tmp);
            TEST_EQ_STR (str, "this is a test");

            nih_free (str);
            continue;
        }

        TEST_NE (ret, NULL);
        TEST_EQ_P (ret, str);

        TEST_ALLOC_SIZE (str, 18);
        TEST_EQ_STR (str, "this is a test of");

        nih_free (str);
    }


    /* Check that if a longer length than the string is given, enough
     * space is reserved but the string copy stops at the NULL.
     */
    TEST_FEATURE ("with larger length than string");
    TEST_ALLOC_FAIL {
        char *tmp;

        TEST_ALLOC_SAFE {
            str = nih_strdup (NULL, "this is a test");
        }

        tmp = str;
        ret = nih_strncat (&str, NULL, " of strndup", 21);

        if (test_alloc_failed) {
            TEST_EQ_P (ret, NULL);
            TEST_EQ_P (str, tmp);
            TEST_EQ_STR (str, "this is a test");

            nih_free (str);
            continue;
        }

        TEST_NE (ret, NULL);
        TEST_EQ_P (ret, str);

        TEST_ALLOC_SIZE (str, 36);
        TEST_EQ_STR (str, "this is a test of strndup");

        nih_free (str);
    }


    /* Check that when no string is passed, this behaves as strndup.
     */
    TEST_FEATURE ("with NULL");
    TEST_ALLOC_FAIL {
        str = NULL;
        ret = nih_strncat (&str, NULL, "test of strndup", 12);

        if (test_alloc_failed) {
            TEST_EQ_P (ret, NULL);
            TEST_EQ_P (str, NULL);
            continue;
        }

        TEST_NE (ret, NULL);
        TEST_EQ_P (ret, str);

        TEST_ALLOC_SIZE (str, 13);
        TEST_EQ_STR (str, "test of strn");

        nih_free (str);
    }
}
예제 #21
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 ();
}
예제 #22
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");
}
예제 #23
0
void
test_strcat_vsprintf (void)
{
    char *str, *ret;

    TEST_FUNCTION ("test_strcat_vsprintf");

    /* Check that we can extend a string with a formatted string,
     * resulting in the original string being modified and the new
     * pointer stored in the argument and returned.
     */
    TEST_FEATURE ("with original string");
    TEST_ALLOC_FAIL {
        char *tmp;

        TEST_ALLOC_SAFE {
            str = nih_strdup (NULL, "this");
        }

        tmp = str;
        ret = my_strcat_vsprintf (&str, NULL,
        " %s a test %d", "is", 54321);

        if (test_alloc_failed) {
            TEST_EQ_P (ret, NULL);
            TEST_EQ_P (str, tmp);
            TEST_EQ_STR (str, "this");

            nih_free (str);
            continue;
        }

        TEST_NE (ret, NULL);
        TEST_EQ_P (ret, str);

        TEST_ALLOC_SIZE (str, 21);
        TEST_EQ_STR (str, "this is a test 54321");

        nih_free (str);
    }


    /* Check that when no string is passed, this behaves as sprintf.
     */
    TEST_FEATURE ("with NULL");
    TEST_ALLOC_FAIL {
        str = NULL;
        ret = my_strcat_vsprintf (&str, NULL,
        "%s a test %d", "is", 54321);

        if (test_alloc_failed) {
            TEST_EQ_P (ret, NULL);
            TEST_EQ_P (str, NULL);
            continue;
        }

        TEST_NE (ret, NULL);
        TEST_EQ_P (ret, str);

        TEST_ALLOC_SIZE (str, 15);
        TEST_EQ_STR (str, "is a test 54321");

        nih_free (str);
    }
}
예제 #24
0
void
test_operator_events (void)
{
	EventOperator  *root, *oper1, *oper2, *oper3, *oper4, *oper5, *oper6;
	Event          *event1, *event2, *event3;
	NihList        *list = NULL;
	Blocked        *blocked;

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

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

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


	/* Check that the events are appended in tree order to the list
	 * and each event is referenced and blocked; the event that was
	 * matched, but not in the operator tree, should not be added.
	 */
	TEST_FEATURE ("with matches in tree");
	TEST_ALLOC_FAIL {
		TEST_ALLOC_SAFE {
			list = nih_list_new (NULL);
		}

		event_operator_events (root, NULL, list);

		TEST_LIST_NOT_EMPTY (list);

		blocked = (Blocked *)list->next;
		TEST_ALLOC_SIZE (blocked, sizeof (Blocked));
		TEST_EQ (blocked->type, BLOCKED_EVENT);
		TEST_EQ_P (blocked->event, oper3->event);
		TEST_EQ (blocked->event->blockers, 2);
		event_unblock (blocked->event);
		nih_free (blocked);

		blocked = (Blocked *)list->next;
		TEST_ALLOC_SIZE (blocked, sizeof (Blocked));
		TEST_EQ (blocked->type, BLOCKED_EVENT);
		TEST_EQ_P (blocked->event, oper4->event);
		TEST_EQ (blocked->event->blockers, 2);
		event_unblock (blocked->event);
		nih_free (blocked);

		TEST_LIST_EMPTY (list);

		TEST_EQ (oper6->event->blockers, 1);

		nih_free (list);
	}


	/* Check that if no events are matched, the list remains empty.
	 */
	TEST_FEATURE ("with no matches");
	TEST_ALLOC_FAIL {
		TEST_ALLOC_SAFE {
			list = nih_list_new (NULL);
		}

		event_operator_events (oper5, NULL, list);

		TEST_LIST_EMPTY (list);

		TEST_EQ (oper3->event->blockers, 1);
		TEST_EQ (oper4->event->blockers, 1);
		TEST_EQ (oper6->event->blockers, 1);

		nih_free (list);
	}


	nih_free (root);
	nih_free (event1);
	nih_free (event2);
	nih_free (event3);
}