OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/environment.h" | 5 #include "base/environment.h" |
6 #include "base/logging.h" | 6 #include "base/logging.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 #include "sandbox/linux/suid/common/sandbox.h" | 11 #include "sandbox/linux/suid/common/sandbox.h" |
12 #include "setuid_sandbox_client.h" | 12 #include "setuid_sandbox_client.h" |
13 | 13 |
14 namespace sandbox { | 14 namespace sandbox { |
15 | 15 |
16 TEST(SetuidSandboxClient, SetupLaunchEnvironment) { | 16 TEST(SetuidSandboxClient, SetupLaunchEnvironment) { |
17 const char kTestValue[] = "This is a test"; | 17 const char kTestValue[] = "This is a test"; |
18 scoped_ptr<base::Environment> env(base::Environment::Create()); | 18 scoped_ptr<base::Environment> env(base::Environment::Create()); |
19 EXPECT_TRUE(env != NULL); | 19 EXPECT_TRUE(env != NULL); |
20 | 20 |
| 21 std::string saved_ld_preload; |
| 22 bool environment_had_ld_preload; |
| 23 // First, back-up the real LD_PRELOAD if any. |
| 24 environment_had_ld_preload = env->GetVar("LD_PRELOAD", &saved_ld_preload); |
21 // Setup environment variables to save or not save. | 25 // Setup environment variables to save or not save. |
22 EXPECT_TRUE(env->SetVar("LD_PRELOAD", kTestValue)); | 26 EXPECT_TRUE(env->SetVar("LD_PRELOAD", kTestValue)); |
23 EXPECT_TRUE(env->UnSetVar("LD_ORIGIN_PATH")); | 27 EXPECT_TRUE(env->UnSetVar("LD_ORIGIN_PATH")); |
24 | 28 |
25 scoped_ptr<SetuidSandboxClient> | 29 scoped_ptr<SetuidSandboxClient> |
26 sandbox_client(SetuidSandboxClient::Create()); | 30 sandbox_client(SetuidSandboxClient::Create()); |
27 EXPECT_TRUE(sandbox_client != NULL); | 31 EXPECT_TRUE(sandbox_client != NULL); |
28 | 32 |
29 // Make sure the environment is clean. | 33 // Make sure the environment is clean. |
30 EXPECT_TRUE(env->UnSetVar(kSandboxEnvironmentApiRequest)); | 34 EXPECT_TRUE(env->UnSetVar(kSandboxEnvironmentApiRequest)); |
31 EXPECT_TRUE(env->UnSetVar(kSandboxEnvironmentApiProvides)); | 35 EXPECT_TRUE(env->UnSetVar(kSandboxEnvironmentApiProvides)); |
32 | 36 |
33 sandbox_client->SetupLaunchEnvironment(); | 37 sandbox_client->SetupLaunchEnvironment(); |
34 | 38 |
35 // Check if the requested API environment was set. | 39 // Check if the requested API environment was set. |
36 std::string api_request; | 40 std::string api_request; |
37 EXPECT_TRUE(env->GetVar(kSandboxEnvironmentApiRequest, &api_request)); | 41 EXPECT_TRUE(env->GetVar(kSandboxEnvironmentApiRequest, &api_request)); |
38 int api_request_num; | 42 int api_request_num; |
39 EXPECT_TRUE(base::StringToInt(api_request, &api_request_num)); | 43 EXPECT_TRUE(base::StringToInt(api_request, &api_request_num)); |
40 EXPECT_EQ(api_request_num, kSUIDSandboxApiNumber); | 44 EXPECT_EQ(api_request_num, kSUIDSandboxApiNumber); |
41 | 45 |
42 // Now check if LD_PRELOAD was saved to SANDBOX_LD_PRELOAD. | 46 // Now check if LD_PRELOAD was saved to SANDBOX_LD_PRELOAD. |
43 std::string sandbox_ld_preload; | 47 std::string sandbox_ld_preload; |
44 EXPECT_TRUE(env->GetVar("SANDBOX_LD_PRELOAD", &sandbox_ld_preload)); | 48 EXPECT_TRUE(env->GetVar("SANDBOX_LD_PRELOAD", &sandbox_ld_preload)); |
45 EXPECT_EQ(sandbox_ld_preload, kTestValue); | 49 EXPECT_EQ(sandbox_ld_preload, kTestValue); |
46 | 50 |
47 // Check that LD_ORIGIN_PATH was not saved. | 51 // Check that LD_ORIGIN_PATH was not saved. |
48 EXPECT_FALSE(env->HasVar("SANDBOX_LD_ORIGIN_PATH")); | 52 EXPECT_FALSE(env->HasVar("SANDBOX_LD_ORIGIN_PATH")); |
| 53 |
| 54 // We should not forget to restore LD_PRELOAD at the end, or this environment |
| 55 // variable will affect the next running tests! |
| 56 if (environment_had_ld_preload) { |
| 57 EXPECT_TRUE(env->SetVar("LD_PRELOAD", saved_ld_preload)); |
| 58 } else { |
| 59 EXPECT_TRUE(env->UnSetVar("LD_PRELOAD")); |
| 60 } |
49 } | 61 } |
50 | 62 |
51 TEST(SetuidSandboxClient, SandboxedClientAPI) { | 63 TEST(SetuidSandboxClient, SandboxedClientAPI) { |
52 scoped_ptr<base::Environment> env(base::Environment::Create()); | 64 scoped_ptr<base::Environment> env(base::Environment::Create()); |
53 EXPECT_TRUE(env != NULL); | 65 EXPECT_TRUE(env != NULL); |
54 | 66 |
55 scoped_ptr<SetuidSandboxClient> | 67 scoped_ptr<SetuidSandboxClient> |
56 sandbox_client(SetuidSandboxClient::Create()); | 68 sandbox_client(SetuidSandboxClient::Create()); |
57 EXPECT_TRUE(sandbox_client != NULL); | 69 EXPECT_TRUE(sandbox_client != NULL); |
58 | 70 |
(...skipping 13 matching lines...) Expand all Loading... |
72 // Forge an incorrect API version and check. | 84 // Forge an incorrect API version and check. |
73 EXPECT_TRUE(env->SetVar(kSandboxEnvironmentApiProvides, | 85 EXPECT_TRUE(env->SetVar(kSandboxEnvironmentApiProvides, |
74 base::IntToString(kSUIDSandboxApiNumber + 1))); | 86 base::IntToString(kSUIDSandboxApiNumber + 1))); |
75 EXPECT_FALSE(sandbox_client->IsSuidSandboxUpToDate()); | 87 EXPECT_FALSE(sandbox_client->IsSuidSandboxUpToDate()); |
76 // We didn't go through the actual sandboxing mechanism as it is | 88 // We didn't go through the actual sandboxing mechanism as it is |
77 // very hard in a unit test. | 89 // very hard in a unit test. |
78 EXPECT_FALSE(sandbox_client->IsSandboxed()); | 90 EXPECT_FALSE(sandbox_client->IsSandboxed()); |
79 } | 91 } |
80 | 92 |
81 } // namespace sandbox | 93 } // namespace sandbox |
OLD | NEW |