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