1 // Copyright 2015 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.
5 #include "sandbox/linux/suid/client/setuid_sandbox_host.h"
9 #include "base/environment.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "sandbox/linux/suid/common/sandbox.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 TEST(SetuidSandboxHost
, SetupLaunchEnvironment
) {
19 const char kTestValue
[] = "This is a test";
20 scoped_ptr
<base::Environment
> env(base::Environment::Create());
21 EXPECT_TRUE(env
!= NULL
);
23 std::string saved_ld_preload
;
24 bool environment_had_ld_preload
;
25 // First, back-up the real LD_PRELOAD if any.
26 environment_had_ld_preload
= env
->GetVar("LD_PRELOAD", &saved_ld_preload
);
27 // Setup environment variables to save or not save.
28 EXPECT_TRUE(env
->SetVar("LD_PRELOAD", kTestValue
));
29 EXPECT_TRUE(env
->UnSetVar("LD_ORIGIN_PATH"));
31 scoped_ptr
<SetuidSandboxHost
> sandbox_host(SetuidSandboxHost::Create());
32 EXPECT_TRUE(sandbox_host
!= NULL
);
34 // Make sure the environment is clean.
35 EXPECT_TRUE(env
->UnSetVar(kSandboxEnvironmentApiRequest
));
36 EXPECT_TRUE(env
->UnSetVar(kSandboxEnvironmentApiProvides
));
38 sandbox_host
->SetupLaunchEnvironment();
40 // Check if the requested API environment was set.
41 std::string api_request
;
42 EXPECT_TRUE(env
->GetVar(kSandboxEnvironmentApiRequest
, &api_request
));
44 EXPECT_TRUE(base::StringToInt(api_request
, &api_request_num
));
45 EXPECT_EQ(api_request_num
, kSUIDSandboxApiNumber
);
47 // Now check if LD_PRELOAD was saved to SANDBOX_LD_PRELOAD.
48 std::string sandbox_ld_preload
;
49 EXPECT_TRUE(env
->GetVar("SANDBOX_LD_PRELOAD", &sandbox_ld_preload
));
50 EXPECT_EQ(sandbox_ld_preload
, kTestValue
);
52 // Check that LD_ORIGIN_PATH was not saved.
53 EXPECT_FALSE(env
->HasVar("SANDBOX_LD_ORIGIN_PATH"));
55 // We should not forget to restore LD_PRELOAD at the end, or this environment
56 // variable will affect the next running tests!
57 if (environment_had_ld_preload
) {
58 EXPECT_TRUE(env
->SetVar("LD_PRELOAD", saved_ld_preload
));
60 EXPECT_TRUE(env
->UnSetVar("LD_PRELOAD"));
64 // This test doesn't accomplish much, but will make sure that analysis tools
65 // will run this codepath.
66 TEST(SetuidSandboxHost
, GetSandboxBinaryPath
) {
67 scoped_ptr
<SetuidSandboxHost
> setuid_sandbox_host(
68 SetuidSandboxHost::Create());
69 ignore_result(setuid_sandbox_host
->GetSandboxBinaryPath());
72 } // namespace sandbox