1 // Copyright (C) 2017 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <string>
16 
17 #include <android-base/file.h>
18 #include <android-base/unique_fd.h>
19 
20 #include <gtest/gtest.h>
21 
22 #include "minijail.h"
23 
24 class WritePolicyTest : public ::testing::Test
25 {
26   protected:
27     const std::string base_policy_ =
28         "read: 1\n"
29         "write: 1\n"
30         "rt_sigreturn: 1\n"
31         "exit: 1\n";
32 
33     const std::string additional_policy_ =
34         "mmap: 1\n"
35         "munmap: 1\n";
36 
37     const std::string full_policy_ = base_policy_ + std::string("\n") + additional_policy_;
38 };
39 
TEST_F(WritePolicyTest,OneFile)40 TEST_F(WritePolicyTest, OneFile)
41 {
42     std::string final_string;
43     android::base::unique_fd fd(android::WritePolicyToPipe(base_policy_, std::string()));
44     EXPECT_LE(0, fd.get());
45     bool success = android::base::ReadFdToString(fd.get(), &final_string);
46     EXPECT_TRUE(success);
47     EXPECT_EQ(final_string, base_policy_);
48 }
49 
TEST_F(WritePolicyTest,TwoFiles)50 TEST_F(WritePolicyTest, TwoFiles)
51 {
52     std::string final_string;
53     android::base::unique_fd fd(android::WritePolicyToPipe(base_policy_, additional_policy_));
54     EXPECT_LE(0, fd.get());
55     bool success = android::base::ReadFdToString(fd.get(), &final_string);
56     EXPECT_TRUE(success);
57     EXPECT_EQ(final_string, full_policy_);
58 }
59