1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <string>
18 
19 #include "TestHelpers.h"
20 #include "gtest/gtest.h"
21 #include "idmap2/Policies.h"
22 
23 using android::idmap2::PolicyBitmask;
24 using android::idmap2::PolicyFlags;
25 
26 namespace android::idmap2 {
27 
TEST(PoliciesTests,PoliciesToBitmasks)28 TEST(PoliciesTests, PoliciesToBitmasks) {
29   const auto bitmask1 = PoliciesToBitmask({"system"});
30   ASSERT_TRUE(bitmask1);
31   ASSERT_EQ(*bitmask1, PolicyFlags::POLICY_SYSTEM_PARTITION);
32 
33   const auto bitmask2 = PoliciesToBitmask({"system", "vendor"});
34   ASSERT_TRUE(bitmask2);
35   ASSERT_EQ(*bitmask2, PolicyFlags::POLICY_SYSTEM_PARTITION | PolicyFlags::POLICY_VENDOR_PARTITION);
36 
37   const auto bitmask3 = PoliciesToBitmask({"vendor", "system"});
38   ASSERT_TRUE(bitmask3);
39   ASSERT_EQ(*bitmask3, PolicyFlags::POLICY_SYSTEM_PARTITION | PolicyFlags::POLICY_VENDOR_PARTITION);
40 
41   const auto bitmask4 = PoliciesToBitmask({"odm", "oem", "public", "product", "system", "vendor"});
42   ASSERT_TRUE(bitmask4);
43   ASSERT_EQ(*bitmask4, PolicyFlags::POLICY_ODM_PARTITION | PolicyFlags::POLICY_OEM_PARTITION |
44                            PolicyFlags::POLICY_PUBLIC | PolicyFlags::POLICY_PRODUCT_PARTITION |
45                            PolicyFlags::POLICY_SYSTEM_PARTITION |
46                            PolicyFlags::POLICY_VENDOR_PARTITION);
47 
48   const auto bitmask5 = PoliciesToBitmask({"system", "system", "system"});
49   ASSERT_TRUE(bitmask5);
50   ASSERT_EQ(*bitmask5, PolicyFlags::POLICY_SYSTEM_PARTITION);
51 
52   const auto bitmask6 = PoliciesToBitmask({""});
53   ASSERT_FALSE(bitmask6);
54 
55   const auto bitmask7 = PoliciesToBitmask({"foo"});
56   ASSERT_FALSE(bitmask7);
57 
58   const auto bitmask8 = PoliciesToBitmask({"system", "foo"});
59   ASSERT_FALSE(bitmask8);
60 
61   const auto bitmask9 = PoliciesToBitmask({"system", ""});
62   ASSERT_FALSE(bitmask9);
63 
64   const auto bitmask10 = PoliciesToBitmask({"system "});
65   ASSERT_FALSE(bitmask10);
66 }
67 
TEST(PoliciesTests,BitmaskToPolicies)68 TEST(PoliciesTests, BitmaskToPolicies) {
69   const auto policies1 = BitmaskToPolicies(PolicyFlags::POLICY_PUBLIC);
70   ASSERT_EQ(1, policies1.size());
71   ASSERT_EQ(policies1[0], "public");
72 
73   const auto policies2 = BitmaskToPolicies(PolicyFlags::POLICY_SYSTEM_PARTITION |
74                                            PolicyFlags::POLICY_VENDOR_PARTITION);
75   ASSERT_EQ(2, policies2.size());
76   ASSERT_EQ(policies2[0], "system");
77   ASSERT_EQ(policies2[1], "vendor");
78 
79   const auto policies3 = BitmaskToPolicies(
80       PolicyFlags::POLICY_ODM_PARTITION | PolicyFlags::POLICY_OEM_PARTITION |
81       PolicyFlags::POLICY_PUBLIC | PolicyFlags::POLICY_PRODUCT_PARTITION |
82       PolicyFlags::POLICY_SYSTEM_PARTITION | PolicyFlags::POLICY_VENDOR_PARTITION);
83   ASSERT_EQ(2, policies2.size());
84   ASSERT_EQ(policies3[0], "odm");
85   ASSERT_EQ(policies3[1], "oem");
86   ASSERT_EQ(policies3[2], "public");
87   ASSERT_EQ(policies3[3], "product");
88   ASSERT_EQ(policies3[4], "system");
89   ASSERT_EQ(policies3[5], "vendor");
90 }
91 
92 }  // namespace android::idmap2
93