Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "base/environment.h"
      6 #include "base/memory/scoped_ptr.h"
      7 #include "build/build_config.h"
      8 #include "testing/gtest/include/gtest/gtest.h"
      9 #include "testing/platform_test.h"
     10 
     11 typedef PlatformTest EnvironmentTest;
     12 
     13 namespace base {
     14 
     15 TEST_F(EnvironmentTest, GetVar) {
     16   // Every setup should have non-empty PATH...
     17   scoped_ptr<Environment> env(Environment::Create());
     18   std::string env_value;
     19   EXPECT_TRUE(env->GetVar("PATH", &env_value));
     20   EXPECT_NE(env_value, "");
     21 }
     22 
     23 TEST_F(EnvironmentTest, GetVarReverse) {
     24   scoped_ptr<Environment> env(Environment::Create());
     25   const char kFooUpper[] = "FOO";
     26   const char kFooLower[] = "foo";
     27 
     28   // Set a variable in UPPER case.
     29   EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
     30 
     31   // And then try to get this variable passing the lower case.
     32   std::string env_value;
     33   EXPECT_TRUE(env->GetVar(kFooLower, &env_value));
     34 
     35   EXPECT_STREQ(env_value.c_str(), kFooLower);
     36 
     37   EXPECT_TRUE(env->UnSetVar(kFooUpper));
     38 
     39   const char kBar[] = "bar";
     40   // Now do the opposite, set the variable in the lower case.
     41   EXPECT_TRUE(env->SetVar(kFooLower, kBar));
     42 
     43   // And then try to get this variable passing the UPPER case.
     44   EXPECT_TRUE(env->GetVar(kFooUpper, &env_value));
     45 
     46   EXPECT_STREQ(env_value.c_str(), kBar);
     47 
     48   EXPECT_TRUE(env->UnSetVar(kFooLower));
     49 }
     50 
     51 TEST_F(EnvironmentTest, HasVar) {
     52   // Every setup should have PATH...
     53   scoped_ptr<Environment> env(Environment::Create());
     54   EXPECT_TRUE(env->HasVar("PATH"));
     55 }
     56 
     57 TEST_F(EnvironmentTest, SetVar) {
     58   scoped_ptr<Environment> env(Environment::Create());
     59 
     60   const char kFooUpper[] = "FOO";
     61   const char kFooLower[] = "foo";
     62   EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
     63 
     64   // Now verify that the environment has the new variable.
     65   EXPECT_TRUE(env->HasVar(kFooUpper));
     66 
     67   std::string var_value;
     68   EXPECT_TRUE(env->GetVar(kFooUpper, &var_value));
     69   EXPECT_EQ(var_value, kFooLower);
     70 }
     71 
     72 TEST_F(EnvironmentTest, UnSetVar) {
     73   scoped_ptr<Environment> env(Environment::Create());
     74 
     75   const char kFooUpper[] = "FOO";
     76   const char kFooLower[] = "foo";
     77   // First set some environment variable.
     78   EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
     79 
     80   // Now verify that the environment has the new variable.
     81   EXPECT_TRUE(env->HasVar(kFooUpper));
     82 
     83   // Finally verify that the environment variable was erased.
     84   EXPECT_TRUE(env->UnSetVar(kFooUpper));
     85 
     86   // And check that the variable has been unset.
     87   EXPECT_FALSE(env->HasVar(kFooUpper));
     88 }
     89 
     90 #if defined(OS_WIN)
     91 
     92 TEST_F(EnvironmentTest, AlterEnvironment) {
     93   const wchar_t empty[] = L"\0";
     94   const wchar_t a2[] = L"A=2\0";
     95   EnvironmentMap changes;
     96   string16 e;
     97 
     98   e = AlterEnvironment(empty, changes);
     99   EXPECT_EQ(0, e[0]);
    100 
    101   changes[L"A"] = L"1";
    102   e = AlterEnvironment(empty, changes);
    103   EXPECT_EQ(string16(L"A=1\0\0", 5), e);
    104 
    105   changes.clear();
    106   changes[L"A"] = string16();
    107   e = AlterEnvironment(empty, changes);
    108   EXPECT_EQ(string16(L"\0\0", 2), e);
    109 
    110   changes.clear();
    111   e = AlterEnvironment(a2, changes);
    112   EXPECT_EQ(string16(L"A=2\0\0", 5), e);
    113 
    114   changes.clear();
    115   changes[L"A"] = L"1";
    116   e = AlterEnvironment(a2, changes);
    117   EXPECT_EQ(string16(L"A=1\0\0", 5), e);
    118 
    119   changes.clear();
    120   changes[L"A"] = string16();
    121   e = AlterEnvironment(a2, changes);
    122   EXPECT_EQ(string16(L"\0\0", 2), e);
    123 }
    124 
    125 #else
    126 
    127 TEST_F(EnvironmentTest, AlterEnvironment) {
    128   const char* const empty[] = { NULL };
    129   const char* const a2[] = { "A=2", NULL };
    130   EnvironmentMap changes;
    131   scoped_ptr<char*[]> e;
    132 
    133   e = AlterEnvironment(empty, changes);
    134   EXPECT_TRUE(e[0] == NULL);
    135 
    136   changes["A"] = "1";
    137   e = AlterEnvironment(empty, changes);
    138   EXPECT_EQ(std::string("A=1"), e[0]);
    139   EXPECT_TRUE(e[1] == NULL);
    140 
    141   changes.clear();
    142   changes["A"] = std::string();
    143   e = AlterEnvironment(empty, changes);
    144   EXPECT_TRUE(e[0] == NULL);
    145 
    146   changes.clear();
    147   e = AlterEnvironment(a2, changes);
    148   EXPECT_EQ(std::string("A=2"), e[0]);
    149   EXPECT_TRUE(e[1] == NULL);
    150 
    151   changes.clear();
    152   changes["A"] = "1";
    153   e = AlterEnvironment(a2, changes);
    154   EXPECT_EQ(std::string("A=1"), e[0]);
    155   EXPECT_TRUE(e[1] == NULL);
    156 
    157   changes.clear();
    158   changes["A"] = std::string();
    159   e = AlterEnvironment(a2, changes);
    160   EXPECT_TRUE(e[0] == NULL);
    161 }
    162 
    163 #endif
    164 
    165 }  // namespace base
    166