Home | History | Annotate | Line # | Download | only in utils
      1 // Copyright 2010 Google Inc.
      2 // All rights reserved.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions are
      6 // met:
      7 //
      8 // * Redistributions of source code must retain the above copyright
      9 //   notice, this list of conditions and the following disclaimer.
     10 // * Redistributions in binary form must reproduce the above copyright
     11 //   notice, this list of conditions and the following disclaimer in the
     12 //   documentation and/or other materials provided with the distribution.
     13 // * Neither the name of Google Inc. nor the names of its contributors
     14 //   may be used to endorse or promote products derived from this software
     15 //   without specific prior written permission.
     16 //
     17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 
     29 #include "utils/env.hpp"
     30 
     31 #include <atf-c++.hpp>
     32 
     33 #include "utils/optional.ipp"
     34 
     35 using utils::optional;
     36 
     37 
     38 ATF_TEST_CASE_WITHOUT_HEAD(getallenv);
     39 ATF_TEST_CASE_BODY(getallenv)
     40 {
     41     utils::unsetenv("test-missing");
     42     utils::setenv("test-empty", "");
     43     utils::setenv("test-text", "some-value");
     44 
     45     const std::map< std::string, std::string > allenv = utils::getallenv();
     46 
     47     {
     48         const std::map< std::string, std::string >::const_iterator iter =
     49             allenv.find("test-missing");
     50         ATF_REQUIRE(iter == allenv.end());
     51     }
     52 
     53     {
     54         const std::map< std::string, std::string >::const_iterator iter =
     55             allenv.find("test-empty");
     56         ATF_REQUIRE(iter != allenv.end());
     57         ATF_REQUIRE((*iter).second.empty());
     58     }
     59 
     60     {
     61         const std::map< std::string, std::string >::const_iterator iter =
     62             allenv.find("test-text");
     63         ATF_REQUIRE(iter != allenv.end());
     64         ATF_REQUIRE_EQ("some-value", (*iter).second);
     65     }
     66 
     67     if (utils::getenv("PATH")) {
     68         const std::map< std::string, std::string >::const_iterator iter =
     69             allenv.find("PATH");
     70         ATF_REQUIRE(iter != allenv.end());
     71         ATF_REQUIRE_EQ(utils::getenv("PATH").get(), (*iter).second);
     72     }
     73 }
     74 
     75 
     76 ATF_TEST_CASE_WITHOUT_HEAD(getenv);
     77 ATF_TEST_CASE_BODY(getenv)
     78 {
     79     const optional< std::string > path = utils::getenv("PATH");
     80     ATF_REQUIRE(path);
     81     ATF_REQUIRE(!path.get().empty());
     82 
     83     ATF_REQUIRE(!utils::getenv("__UNDEFINED_VARIABLE__"));
     84 }
     85 
     86 
     87 ATF_TEST_CASE_WITHOUT_HEAD(getenv_with_default);
     88 ATF_TEST_CASE_BODY(getenv_with_default)
     89 {
     90     ATF_REQUIRE("don't use" !=
     91                 utils::getenv_with_default("PATH", "don't use"));
     92 
     93     ATF_REQUIRE_EQ("foo",
     94                    utils::getenv_with_default("__UNDEFINED_VARIABLE__", "foo"));
     95 }
     96 
     97 
     98 ATF_TEST_CASE_WITHOUT_HEAD(setenv);
     99 ATF_TEST_CASE_BODY(setenv)
    100 {
    101     ATF_REQUIRE(utils::getenv("PATH"));
    102     const std::string oldval = utils::getenv("PATH").get();
    103     utils::setenv("PATH", "foo-bar");
    104     ATF_REQUIRE(utils::getenv("PATH").get() != oldval);
    105     ATF_REQUIRE_EQ("foo-bar", utils::getenv("PATH").get());
    106 
    107     ATF_REQUIRE(!utils::getenv("__UNDEFINED_VARIABLE__"));
    108     utils::setenv("__UNDEFINED_VARIABLE__", "foo2-bar2");
    109     ATF_REQUIRE_EQ("foo2-bar2", utils::getenv("__UNDEFINED_VARIABLE__").get());
    110 }
    111 
    112 
    113 ATF_TEST_CASE_WITHOUT_HEAD(unsetenv);
    114 ATF_TEST_CASE_BODY(unsetenv)
    115 {
    116     ATF_REQUIRE(utils::getenv("PATH"));
    117     utils::unsetenv("PATH");
    118     ATF_REQUIRE(!utils::getenv("PATH"));
    119 }
    120 
    121 
    122 ATF_INIT_TEST_CASES(tcs)
    123 {
    124     ATF_ADD_TEST_CASE(tcs, getallenv);
    125     ATF_ADD_TEST_CASE(tcs, getenv);
    126     ATF_ADD_TEST_CASE(tcs, getenv_with_default);
    127     ATF_ADD_TEST_CASE(tcs, setenv);
    128     ATF_ADD_TEST_CASE(tcs, unsetenv);
    129 }
    130