Path: blob/main/contrib/atf/atf-c/detail/env_test.c
39507 views
/* Copyright (c) 2007 The NetBSD Foundation, Inc.1* All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND13* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,14* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF15* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16* IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY17* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE19* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS20* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER21* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR22* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN23* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */2425#include "atf-c/detail/env.h"2627#include <stdlib.h>28#include <string.h>2930#include <atf-c.h>3132#include "atf-c/detail/test_helpers.h"33#include "atf-c/detail/text.h"3435/* ---------------------------------------------------------------------36* Test cases for the free functions.37* --------------------------------------------------------------------- */3839ATF_TC(has);40ATF_TC_HEAD(has, tc)41{42atf_tc_set_md_var(tc, "descr", "Tests the atf_env_has function");43}44ATF_TC_BODY(has, tc)45{46ATF_REQUIRE(atf_env_has("PATH"));47ATF_REQUIRE(!atf_env_has("_UNDEFINED_VARIABLE_"));48}4950ATF_TC(get);51ATF_TC_HEAD(get, tc)52{53atf_tc_set_md_var(tc, "descr", "Tests the atf_env_get function");54}55ATF_TC_BODY(get, tc)56{57const char *val;5859ATF_REQUIRE(atf_env_has("PATH"));6061val = atf_env_get("PATH");62ATF_REQUIRE(strlen(val) > 0);63ATF_REQUIRE(strchr(val, ':') != NULL);64}6566ATF_TC(get_with_default);67ATF_TC_HEAD(get_with_default, tc)68{69atf_tc_set_md_var(tc, "descr", "Tests the atf_env_get_with_default "70"function");71}72ATF_TC_BODY(get_with_default, tc)73{74const char *val;7576ATF_REQUIRE(atf_env_has("PATH"));7778val = atf_env_get_with_default("PATH", "unknown");79ATF_REQUIRE(strcmp(val, "unknown") != 0);8081val = atf_env_get_with_default("_UNKNOWN_VARIABLE_", "foo bar");82ATF_REQUIRE(strcmp(val, "foo bar") == 0);83}8485ATF_TC(set);86ATF_TC_HEAD(set, tc)87{88atf_tc_set_md_var(tc, "descr", "Tests the atf_env_set function");89}90ATF_TC_BODY(set, tc)91{92char *oldval;9394ATF_REQUIRE(atf_env_has("PATH"));95RE(atf_text_format(&oldval, "%s", atf_env_get("PATH")));96RE(atf_env_set("PATH", "foo-bar"));97ATF_REQUIRE(strcmp(atf_env_get("PATH"), oldval) != 0);98ATF_REQUIRE(strcmp(atf_env_get("PATH"), "foo-bar") == 0);99free(oldval);100101ATF_REQUIRE(!atf_env_has("_UNDEFINED_VARIABLE_"));102RE(atf_env_set("_UNDEFINED_VARIABLE_", "foo2-bar2"));103ATF_REQUIRE(strcmp(atf_env_get("_UNDEFINED_VARIABLE_"),104"foo2-bar2") == 0);105}106107ATF_TC(unset);108ATF_TC_HEAD(unset, tc)109{110atf_tc_set_md_var(tc, "descr", "Tests the atf_env_unset function");111}112ATF_TC_BODY(unset, tc)113{114ATF_REQUIRE(atf_env_has("PATH"));115RE(atf_env_unset("PATH"));116ATF_REQUIRE(!atf_env_has("PATH"));117}118119/* ---------------------------------------------------------------------120* Main.121* --------------------------------------------------------------------- */122123ATF_TP_ADD_TCS(tp)124{125ATF_TP_ADD_TC(tp, has);126ATF_TP_ADD_TC(tp, get);127ATF_TP_ADD_TC(tp, get_with_default);128ATF_TP_ADD_TC(tp, set);129ATF_TP_ADD_TC(tp, unset);130131return atf_no_error();132}133134135