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 third_policy_ =
38         "open: 1\n"
39         "close: 1\n";
40 
41     const std::string full_policy_ = base_policy_ + std::string("\n") + additional_policy_;
42     const std::string triple_policy_ = base_policy_ +
43                                        std::string("\n") + additional_policy_ +
44                                        std::string("\n") + third_policy_;
45 };
46 
TEST_F(WritePolicyTest,OneFile)47 TEST_F(WritePolicyTest, OneFile)
48 {
49     std::string final_string;
50     // vector with an empty pathname
51     android::base::unique_fd fd(android::WritePolicyToPipe(base_policy_, {std::string()}));
52     EXPECT_LE(0, fd.get());
53     bool success = android::base::ReadFdToString(fd.get(), &final_string);
54     EXPECT_TRUE(success);
55     EXPECT_EQ(final_string, base_policy_);
56 }
57 
TEST_F(WritePolicyTest,OneFileAlternate)58 TEST_F(WritePolicyTest, OneFileAlternate)
59 {
60     std::string final_string;
61     // empty vector
62     android::base::unique_fd fd(android::WritePolicyToPipe(base_policy_, {}));
63     EXPECT_LE(0, fd.get());
64     bool success = android::base::ReadFdToString(fd.get(), &final_string);
65     EXPECT_TRUE(success);
66     EXPECT_EQ(final_string, base_policy_);
67 }
68 
TEST_F(WritePolicyTest,TwoFiles)69 TEST_F(WritePolicyTest, TwoFiles)
70 {
71     std::string final_string;
72     android::base::unique_fd fd(android::WritePolicyToPipe(base_policy_, {additional_policy_}));
73     EXPECT_LE(0, fd.get());
74     bool success = android::base::ReadFdToString(fd.get(), &final_string);
75     EXPECT_TRUE(success);
76     EXPECT_EQ(final_string, full_policy_);
77 }
78 
TEST_F(WritePolicyTest,ThreeFiles)79 TEST_F(WritePolicyTest, ThreeFiles)
80 {
81     std::string final_string;
82     android::base::unique_fd fd(android::WritePolicyToPipe(base_policy_, {additional_policy_, third_policy_}));
83     EXPECT_LE(0, fd.get());
84     bool success = android::base::ReadFdToString(fd.get(), &final_string);
85     EXPECT_TRUE(success);
86     EXPECT_EQ(final_string, triple_policy_);
87 }
88