env_test.c revision 240116
1254721Semaste/*
2254721Semaste * Automated Testing Framework (atf)
3254721Semaste *
4254721Semaste * Copyright (c) 2007 The NetBSD Foundation, Inc.
5254721Semaste * All rights reserved.
6254721Semaste *
7254721Semaste * Redistribution and use in source and binary forms, with or without
8254721Semaste * modification, are permitted provided that the following conditions
9254721Semaste * are met:
10254721Semaste * 1. Redistributions of source code must retain the above copyright
11254721Semaste *    notice, this list of conditions and the following disclaimer.
12254721Semaste * 2. Redistributions in binary form must reproduce the above copyright
13254721Semaste *    notice, this list of conditions and the following disclaimer in the
14254721Semaste *    documentation and/or other materials provided with the distribution.
15254721Semaste *
16254721Semaste * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17254721Semaste * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18254721Semaste * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19254721Semaste * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20254721Semaste * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21254721Semaste * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22254721Semaste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23254721Semaste * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24254721Semaste * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25254721Semaste * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26254721Semaste * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27254721Semaste * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28254721Semaste */
29254721Semaste
30254721Semaste#include <stdlib.h>
31254721Semaste#include <string.h>
32254721Semaste
33254721Semaste#include <atf-c.h>
34254721Semaste
35254721Semaste#include "env.h"
36254721Semaste#include "test_helpers.h"
37254721Semaste#include "text.h"
38254721Semaste
39254721Semaste/* ---------------------------------------------------------------------
40254721Semaste * Test cases for the free functions.
41254721Semaste * --------------------------------------------------------------------- */
42254721Semaste
43254721SemasteATF_TC(has);
44254721SemasteATF_TC_HEAD(has, tc)
45254721Semaste{
46254721Semaste    atf_tc_set_md_var(tc, "descr", "Tests the atf_env_has function");
47254721Semaste}
48254721SemasteATF_TC_BODY(has, tc)
49254721Semaste{
50254721Semaste    ATF_REQUIRE(atf_env_has("PATH"));
51254721Semaste    ATF_REQUIRE(!atf_env_has("_UNDEFINED_VARIABLE_"));
52254721Semaste}
53254721Semaste
54254721SemasteATF_TC(get);
55254721SemasteATF_TC_HEAD(get, tc)
56254721Semaste{
57254721Semaste    atf_tc_set_md_var(tc, "descr", "Tests the atf_env_get function");
58254721Semaste}
59254721SemasteATF_TC_BODY(get, tc)
60254721Semaste{
61254721Semaste    const char *val;
62254721Semaste
63254721Semaste    ATF_REQUIRE(atf_env_has("PATH"));
64254721Semaste
65254721Semaste    val = atf_env_get("PATH");
66254721Semaste    ATF_REQUIRE(strlen(val) > 0);
67254721Semaste    ATF_REQUIRE(strchr(val, ':') != NULL);
68254721Semaste}
69254721Semaste
70254721SemasteATF_TC(set);
71254721SemasteATF_TC_HEAD(set, tc)
72254721Semaste{
73254721Semaste    atf_tc_set_md_var(tc, "descr", "Tests the atf_env_set function");
74254721Semaste}
75254721SemasteATF_TC_BODY(set, tc)
76254721Semaste{
77254721Semaste    char *oldval;
78254721Semaste
79254721Semaste    ATF_REQUIRE(atf_env_has("PATH"));
80254721Semaste    RE(atf_text_format(&oldval, "%s", atf_env_get("PATH")));
81254721Semaste    RE(atf_env_set("PATH", "foo-bar"));
82254721Semaste    ATF_REQUIRE(strcmp(atf_env_get("PATH"), oldval) != 0);
83254721Semaste    ATF_REQUIRE(strcmp(atf_env_get("PATH"), "foo-bar") == 0);
84254721Semaste    free(oldval);
85254721Semaste
86254721Semaste    ATF_REQUIRE(!atf_env_has("_UNDEFINED_VARIABLE_"));
87254721Semaste    RE(atf_env_set("_UNDEFINED_VARIABLE_", "foo2-bar2"));
88254721Semaste    ATF_REQUIRE(strcmp(atf_env_get("_UNDEFINED_VARIABLE_"),
89254721Semaste                     "foo2-bar2") == 0);
90254721Semaste}
91254721Semaste
92254721SemasteATF_TC(unset);
93254721SemasteATF_TC_HEAD(unset, tc)
94254721Semaste{
95254721Semaste    atf_tc_set_md_var(tc, "descr", "Tests the atf_env_unset function");
96254721Semaste}
97254721SemasteATF_TC_BODY(unset, tc)
98254721Semaste{
99254721Semaste    ATF_REQUIRE(atf_env_has("PATH"));
100254721Semaste    RE(atf_env_unset("PATH"));
101254721Semaste    ATF_REQUIRE(!atf_env_has("PATH"));
102254721Semaste}
103254721Semaste
104254721Semaste/* ---------------------------------------------------------------------
105254721Semaste * Main.
106254721Semaste * --------------------------------------------------------------------- */
107254721Semaste
108254721SemasteATF_TP_ADD_TCS(tp)
109254721Semaste{
110254721Semaste    ATF_TP_ADD_TC(tp, has);
111254721Semaste    ATF_TP_ADD_TC(tp, get);
112254721Semaste    ATF_TP_ADD_TC(tp, set);
113254721Semaste    ATF_TP_ADD_TC(tp, unset);
114254721Semaste
115254721Semaste    return atf_no_error();
116254721Semaste}
117254721Semaste