1// Copyright 2010 The Kyua Authors. 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/fs/path.hpp" 34#include "utils/optional.ipp" 35 36namespace fs = utils::fs; 37 38using utils::optional; 39 40 41ATF_TEST_CASE_WITHOUT_HEAD(getallenv); 42ATF_TEST_CASE_BODY(getallenv) 43{ 44 utils::unsetenv("test-missing"); 45 utils::setenv("test-empty", ""); 46 utils::setenv("test-text", "some-value"); 47 48 const std::map< std::string, std::string > allenv = utils::getallenv(); 49 50 { 51 const std::map< std::string, std::string >::const_iterator iter = 52 allenv.find("test-missing"); 53 ATF_REQUIRE(iter == allenv.end()); 54 } 55 56 { 57 const std::map< std::string, std::string >::const_iterator iter = 58 allenv.find("test-empty"); 59 ATF_REQUIRE(iter != allenv.end()); 60 ATF_REQUIRE((*iter).second.empty()); 61 } 62 63 { 64 const std::map< std::string, std::string >::const_iterator iter = 65 allenv.find("test-text"); 66 ATF_REQUIRE(iter != allenv.end()); 67 ATF_REQUIRE_EQ("some-value", (*iter).second); 68 } 69 70 if (utils::getenv("PATH")) { 71 const std::map< std::string, std::string >::const_iterator iter = 72 allenv.find("PATH"); 73 ATF_REQUIRE(iter != allenv.end()); 74 ATF_REQUIRE_EQ(utils::getenv("PATH").get(), (*iter).second); 75 } 76} 77 78 79ATF_TEST_CASE_WITHOUT_HEAD(getenv); 80ATF_TEST_CASE_BODY(getenv) 81{ 82 const optional< std::string > path = utils::getenv("PATH"); 83 ATF_REQUIRE(path); 84 ATF_REQUIRE(!path.get().empty()); 85 86 ATF_REQUIRE(!utils::getenv("__UNDEFINED_VARIABLE__")); 87} 88 89 90ATF_TEST_CASE_WITHOUT_HEAD(getenv_with_default); 91ATF_TEST_CASE_BODY(getenv_with_default) 92{ 93 ATF_REQUIRE("don't use" != 94 utils::getenv_with_default("PATH", "don't use")); 95 96 ATF_REQUIRE_EQ("foo", 97 utils::getenv_with_default("__UNDEFINED_VARIABLE__", "foo")); 98} 99 100 101ATF_TEST_CASE_WITHOUT_HEAD(get_home__ok); 102ATF_TEST_CASE_BODY(get_home__ok) 103{ 104 const fs::path home("/foo/bar"); 105 utils::setenv("HOME", home.str()); 106 const optional< fs::path > computed = utils::get_home(); 107 ATF_REQUIRE(computed); 108 ATF_REQUIRE_EQ(home, computed.get()); 109} 110 111 112ATF_TEST_CASE_WITHOUT_HEAD(get_home__missing); 113ATF_TEST_CASE_BODY(get_home__missing) 114{ 115 utils::unsetenv("HOME"); 116 ATF_REQUIRE(!utils::get_home()); 117} 118 119 120ATF_TEST_CASE_WITHOUT_HEAD(get_home__invalid); 121ATF_TEST_CASE_BODY(get_home__invalid) 122{ 123 utils::setenv("HOME", ""); 124 ATF_REQUIRE(!utils::get_home()); 125} 126 127 128ATF_TEST_CASE_WITHOUT_HEAD(setenv); 129ATF_TEST_CASE_BODY(setenv) 130{ 131 ATF_REQUIRE(utils::getenv("PATH")); 132 const std::string oldval = utils::getenv("PATH").get(); 133 utils::setenv("PATH", "foo-bar"); 134 ATF_REQUIRE(utils::getenv("PATH").get() != oldval); 135 ATF_REQUIRE_EQ("foo-bar", utils::getenv("PATH").get()); 136 137 ATF_REQUIRE(!utils::getenv("__UNDEFINED_VARIABLE__")); 138 utils::setenv("__UNDEFINED_VARIABLE__", "foo2-bar2"); 139 ATF_REQUIRE_EQ("foo2-bar2", utils::getenv("__UNDEFINED_VARIABLE__").get()); 140} 141 142 143ATF_TEST_CASE_WITHOUT_HEAD(unsetenv); 144ATF_TEST_CASE_BODY(unsetenv) 145{ 146 ATF_REQUIRE(utils::getenv("PATH")); 147 utils::unsetenv("PATH"); 148 ATF_REQUIRE(!utils::getenv("PATH")); 149} 150 151 152ATF_INIT_TEST_CASES(tcs) 153{ 154 ATF_ADD_TEST_CASE(tcs, getallenv); 155 156 ATF_ADD_TEST_CASE(tcs, getenv); 157 158 ATF_ADD_TEST_CASE(tcs, getenv_with_default); 159 160 ATF_ADD_TEST_CASE(tcs, get_home__ok); 161 ATF_ADD_TEST_CASE(tcs, get_home__missing); 162 ATF_ADD_TEST_CASE(tcs, get_home__invalid); 163 164 ATF_ADD_TEST_CASE(tcs, setenv); 165 166 ATF_ADD_TEST_CASE(tcs, unsetenv); 167} 168