1/*
2 * Automated Testing Framework (atf)
3 *
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <stdlib.h>
31#include <string.h>
32
33#include <atf-c.h>
34
35#include "env.h"
36#include "test_helpers.h"
37#include "text.h"
38
39/* ---------------------------------------------------------------------
40 * Test cases for the free functions.
41 * --------------------------------------------------------------------- */
42
43ATF_TC(has);
44ATF_TC_HEAD(has, tc)
45{
46    atf_tc_set_md_var(tc, "descr", "Tests the atf_env_has function");
47}
48ATF_TC_BODY(has, tc)
49{
50    ATF_REQUIRE(atf_env_has("PATH"));
51    ATF_REQUIRE(!atf_env_has("_UNDEFINED_VARIABLE_"));
52}
53
54ATF_TC(get);
55ATF_TC_HEAD(get, tc)
56{
57    atf_tc_set_md_var(tc, "descr", "Tests the atf_env_get function");
58}
59ATF_TC_BODY(get, tc)
60{
61    const char *val;
62
63    ATF_REQUIRE(atf_env_has("PATH"));
64
65    val = atf_env_get("PATH");
66    ATF_REQUIRE(strlen(val) > 0);
67    ATF_REQUIRE(strchr(val, ':') != NULL);
68}
69
70ATF_TC(set);
71ATF_TC_HEAD(set, tc)
72{
73    atf_tc_set_md_var(tc, "descr", "Tests the atf_env_set function");
74}
75ATF_TC_BODY(set, tc)
76{
77    char *oldval;
78
79    ATF_REQUIRE(atf_env_has("PATH"));
80    RE(atf_text_format(&oldval, "%s", atf_env_get("PATH")));
81    RE(atf_env_set("PATH", "foo-bar"));
82    ATF_REQUIRE(strcmp(atf_env_get("PATH"), oldval) != 0);
83    ATF_REQUIRE(strcmp(atf_env_get("PATH"), "foo-bar") == 0);
84    free(oldval);
85
86    ATF_REQUIRE(!atf_env_has("_UNDEFINED_VARIABLE_"));
87    RE(atf_env_set("_UNDEFINED_VARIABLE_", "foo2-bar2"));
88    ATF_REQUIRE(strcmp(atf_env_get("_UNDEFINED_VARIABLE_"),
89                     "foo2-bar2") == 0);
90}
91
92ATF_TC(unset);
93ATF_TC_HEAD(unset, tc)
94{
95    atf_tc_set_md_var(tc, "descr", "Tests the atf_env_unset function");
96}
97ATF_TC_BODY(unset, tc)
98{
99    ATF_REQUIRE(atf_env_has("PATH"));
100    RE(atf_env_unset("PATH"));
101    ATF_REQUIRE(!atf_env_has("PATH"));
102}
103
104/* ---------------------------------------------------------------------
105 * Main.
106 * --------------------------------------------------------------------- */
107
108ATF_TP_ADD_TCS(tp)
109{
110    ATF_TP_ADD_TC(tp, has);
111    ATF_TP_ADD_TC(tp, get);
112    ATF_TP_ADD_TC(tp, set);
113    ATF_TP_ADD_TC(tp, unset);
114
115    return atf_no_error();
116}
117