aboutsummaryrefslogblamecommitdiffstats
path: root/libexec_putenv_noclobber.c
blob: 1c21a559a9ced1b1daf433b11972fd95f5123a29 (plain) (tree)
1
2
3
4
5
6




                                                         
                                                                                   














                                                                 
                                   








































































                                                                 
                                   










































































                                                                 
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


extern inline int libexec_putenv_noclobber(struct libexec_command *, const char *);


#else


int
main(void)
{
	struct libexec_command cmd, ref;
	char **env;

	errno = 0;
	ASSERT_EQ_INT(libexec_putenv_noclobber(NULL, "X=Y"), -1);
	ASSERT_EQ_INT(errno, EINVAL);

	cmd = LIBEXEC_COMMAND_INIT;
	memcpy(&ref, &cmd, sizeof(cmd));

	errno = 0;
	ASSERT_EQ_INT(libexec_putenv_noclobber(&cmd, NULL), -1);
	ASSERT_EQ_INT(errno, EINVAL);
	ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd)));

	errno = 0;
	ASSERT_EQ_INT(libexec_putenv_noclobber(&cmd, "X"), -1);
	ASSERT_EQ_INT(errno, EINVAL);
	ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd)));

	ASSERT_ZERO(libexec_clear_environ(&cmd));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_IS_NULL(cmd.environ[0]);

	ASSERT_ZERO(libexec_putenv_noclobber(&cmd, "A=B=C"));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "A=B=C");
	ASSERT_IS_NULL(cmd.environ[1]);

	ASSERT_ZERO(libexec_putenv_noclobber(&cmd, "A=B=C"));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "A=B=C");
	ASSERT_IS_NULL(cmd.environ[1]);

	errno = 0;
	ASSERT_EQ_INT(libexec_putenv_noclobber(&cmd, "A=D"), -1);
	ASSERT_EQ_INT(errno, 0);
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "A=B=C");
	ASSERT_IS_NULL(cmd.environ[1]);

	ASSERT_ZERO(libexec_putenv_noclobber(&cmd, "AA=E"));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "A=B=C");
	ASSERT_NOT_NULL(cmd.environ[1]);
	ASSERT_EQ_STR(cmd.environ[1], "AA=E");
	ASSERT_IS_NULL(cmd.environ[2]);

	ASSERT_ZERO(libexec_putenv_noclobber(&cmd, "BB=F"));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "A=B=C");
	ASSERT_NOT_NULL(cmd.environ[1]);
	ASSERT_EQ_STR(cmd.environ[1], "AA=E");
	ASSERT_NOT_NULL(cmd.environ[2]);
	ASSERT_EQ_STR(cmd.environ[2], "BB=F");
	ASSERT_IS_NULL(cmd.environ[3]);

	ASSERT_ZERO(libexec_putenv_noclobber(&cmd, "B=F"));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "A=B=C");
	ASSERT_NOT_NULL(cmd.environ[1]);
	ASSERT_EQ_STR(cmd.environ[1], "AA=E");
	ASSERT_NOT_NULL(cmd.environ[2]);
	ASSERT_EQ_STR(cmd.environ[2], "BB=F");
	ASSERT_NOT_NULL(cmd.environ[3]);
	ASSERT_EQ_STR(cmd.environ[3], "B=F");
	ASSERT_IS_NULL(cmd.environ[4]);

	env = cmd.environ;
	cmd.environ = NULL;
	ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd)));
	cmd.environ = env;

	libexec_destroy_command(&cmd);

	cmd = LIBEXEC_COMMAND_INIT;
	env = environ;
	environ = calloc(2, sizeof(*environ));
	ASSERT_NOT_NULL(environ);
	environ[0] = strdup("X=0");
	ASSERT_NOT_NULL(environ[0]);

	ASSERT_ZERO(libexec_putenv_noclobber(&cmd, "A=B=C"));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "X=0");
	ASSERT_NOT_NULL(cmd.environ[1]);
	ASSERT_EQ_STR(cmd.environ[1], "A=B=C");
	ASSERT_IS_NULL(cmd.environ[2]);

	ASSERT_ZERO(libexec_putenv_noclobber(&cmd, "A=B=C"));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "X=0");
	ASSERT_NOT_NULL(cmd.environ[1]);
	ASSERT_EQ_STR(cmd.environ[1], "A=B=C");
	ASSERT_IS_NULL(cmd.environ[2]);

	errno = 0;
	ASSERT_EQ_INT(libexec_putenv_noclobber(&cmd, "A=D"), -1);
	ASSERT_EQ_INT(errno, 0);
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "X=0");
	ASSERT_NOT_NULL(cmd.environ[1]);
	ASSERT_EQ_STR(cmd.environ[1], "A=B=C");
	ASSERT_IS_NULL(cmd.environ[2]);

	ASSERT_ZERO(libexec_putenv_noclobber(&cmd, "AA=E"));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "X=0");
	ASSERT_NOT_NULL(cmd.environ[1]);
	ASSERT_EQ_STR(cmd.environ[1], "A=B=C");
	ASSERT_NOT_NULL(cmd.environ[2]);
	ASSERT_EQ_STR(cmd.environ[2], "AA=E");
	ASSERT_IS_NULL(cmd.environ[3]);

	ASSERT_ZERO(libexec_putenv_noclobber(&cmd, "BB=F"));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "X=0");
	ASSERT_NOT_NULL(cmd.environ[1]);
	ASSERT_EQ_STR(cmd.environ[1], "A=B=C");
	ASSERT_NOT_NULL(cmd.environ[2]);
	ASSERT_EQ_STR(cmd.environ[2], "AA=E");
	ASSERT_NOT_NULL(cmd.environ[3]);
	ASSERT_EQ_STR(cmd.environ[3], "BB=F");
	ASSERT_IS_NULL(cmd.environ[4]);

	ASSERT_ZERO(libexec_putenv_noclobber(&cmd, "B=F"));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "X=0");
	ASSERT_NOT_NULL(cmd.environ[1]);
	ASSERT_EQ_STR(cmd.environ[1], "A=B=C");
	ASSERT_NOT_NULL(cmd.environ[2]);
	ASSERT_EQ_STR(cmd.environ[2], "AA=E");
	ASSERT_NOT_NULL(cmd.environ[3]);
	ASSERT_EQ_STR(cmd.environ[3], "BB=F");
	ASSERT_NOT_NULL(cmd.environ[4]);
	ASSERT_EQ_STR(cmd.environ[4], "B=F");
	ASSERT_IS_NULL(cmd.environ[5]);

	libexec_destroy_command(&cmd);
	environ = env;
	return 0;
}


#endif