env_test.c revision 240116
1240116Smarcel/*
2240116Smarcel * Automated Testing Framework (atf)
3240116Smarcel *
4240116Smarcel * Copyright (c) 2007 The NetBSD Foundation, Inc.
5240116Smarcel * All rights reserved.
6240116Smarcel *
7240116Smarcel * Redistribution and use in source and binary forms, with or without
8240116Smarcel * modification, are permitted provided that the following conditions
9240116Smarcel * are met:
10240116Smarcel * 1. Redistributions of source code must retain the above copyright
11240116Smarcel *    notice, this list of conditions and the following disclaimer.
12240116Smarcel * 2. Redistributions in binary form must reproduce the above copyright
13240116Smarcel *    notice, this list of conditions and the following disclaimer in the
14240116Smarcel *    documentation and/or other materials provided with the distribution.
15240116Smarcel *
16240116Smarcel * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17240116Smarcel * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18240116Smarcel * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19240116Smarcel * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20240116Smarcel * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21240116Smarcel * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22240116Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23240116Smarcel * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24240116Smarcel * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25240116Smarcel * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26240116Smarcel * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27240116Smarcel * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28240116Smarcel */
29240116Smarcel
30240116Smarcel#include <stdlib.h>
31240116Smarcel#include <string.h>
32240116Smarcel
33240116Smarcel#include <atf-c.h>
34240116Smarcel
35240116Smarcel#include "env.h"
36240116Smarcel#include "test_helpers.h"
37240116Smarcel#include "text.h"
38240116Smarcel
39240116Smarcel/* ---------------------------------------------------------------------
40240116Smarcel * Test cases for the free functions.
41240116Smarcel * --------------------------------------------------------------------- */
42240116Smarcel
43240116SmarcelATF_TC(has);
44240116SmarcelATF_TC_HEAD(has, tc)
45240116Smarcel{
46240116Smarcel    atf_tc_set_md_var(tc, "descr", "Tests the atf_env_has function");
47240116Smarcel}
48240116SmarcelATF_TC_BODY(has, tc)
49240116Smarcel{
50240116Smarcel    ATF_REQUIRE(atf_env_has("PATH"));
51240116Smarcel    ATF_REQUIRE(!atf_env_has("_UNDEFINED_VARIABLE_"));
52240116Smarcel}
53240116Smarcel
54240116SmarcelATF_TC(get);
55240116SmarcelATF_TC_HEAD(get, tc)
56240116Smarcel{
57240116Smarcel    atf_tc_set_md_var(tc, "descr", "Tests the atf_env_get function");
58240116Smarcel}
59240116SmarcelATF_TC_BODY(get, tc)
60240116Smarcel{
61240116Smarcel    const char *val;
62240116Smarcel
63240116Smarcel    ATF_REQUIRE(atf_env_has("PATH"));
64240116Smarcel
65240116Smarcel    val = atf_env_get("PATH");
66240116Smarcel    ATF_REQUIRE(strlen(val) > 0);
67240116Smarcel    ATF_REQUIRE(strchr(val, ':') != NULL);
68240116Smarcel}
69240116Smarcel
70240116SmarcelATF_TC(set);
71240116SmarcelATF_TC_HEAD(set, tc)
72240116Smarcel{
73240116Smarcel    atf_tc_set_md_var(tc, "descr", "Tests the atf_env_set function");
74240116Smarcel}
75240116SmarcelATF_TC_BODY(set, tc)
76240116Smarcel{
77240116Smarcel    char *oldval;
78240116Smarcel
79240116Smarcel    ATF_REQUIRE(atf_env_has("PATH"));
80240116Smarcel    RE(atf_text_format(&oldval, "%s", atf_env_get("PATH")));
81240116Smarcel    RE(atf_env_set("PATH", "foo-bar"));
82240116Smarcel    ATF_REQUIRE(strcmp(atf_env_get("PATH"), oldval) != 0);
83240116Smarcel    ATF_REQUIRE(strcmp(atf_env_get("PATH"), "foo-bar") == 0);
84240116Smarcel    free(oldval);
85240116Smarcel
86240116Smarcel    ATF_REQUIRE(!atf_env_has("_UNDEFINED_VARIABLE_"));
87240116Smarcel    RE(atf_env_set("_UNDEFINED_VARIABLE_", "foo2-bar2"));
88240116Smarcel    ATF_REQUIRE(strcmp(atf_env_get("_UNDEFINED_VARIABLE_"),
89240116Smarcel                     "foo2-bar2") == 0);
90240116Smarcel}
91240116Smarcel
92240116SmarcelATF_TC(unset);
93240116SmarcelATF_TC_HEAD(unset, tc)
94240116Smarcel{
95240116Smarcel    atf_tc_set_md_var(tc, "descr", "Tests the atf_env_unset function");
96240116Smarcel}
97240116SmarcelATF_TC_BODY(unset, tc)
98240116Smarcel{
99240116Smarcel    ATF_REQUIRE(atf_env_has("PATH"));
100240116Smarcel    RE(atf_env_unset("PATH"));
101240116Smarcel    ATF_REQUIRE(!atf_env_has("PATH"));
102240116Smarcel}
103240116Smarcel
104240116Smarcel/* ---------------------------------------------------------------------
105240116Smarcel * Main.
106240116Smarcel * --------------------------------------------------------------------- */
107240116Smarcel
108240116SmarcelATF_TP_ADD_TCS(tp)
109240116Smarcel{
110240116Smarcel    ATF_TP_ADD_TC(tp, has);
111240116Smarcel    ATF_TP_ADD_TC(tp, get);
112240116Smarcel    ATF_TP_ADD_TC(tp, set);
113240116Smarcel    ATF_TP_ADD_TC(tp, unset);
114240116Smarcel
115240116Smarcel    return atf_no_error();
116240116Smarcel}
117