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 "androidfw/ResourceTypes.h"
21 #include "gtest/gtest.h"
22 #include "idmap2/PolicyUtils.h"
23 
24 using android::idmap2::utils::BitmaskToPolicies;
25 using android::idmap2::utils::PoliciesToBitmaskResult;
26 
27 using PolicyBitmask = android::ResTable_overlayable_policy_header::PolicyBitmask;
28 using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
29 
30 namespace android::idmap2 {
31 
TEST(PoliciesTests,PoliciesToBitmaskResults)32 TEST(PoliciesTests, PoliciesToBitmaskResults) {
33   const auto bitmask1 = PoliciesToBitmaskResult({"system"});
34   ASSERT_TRUE(bitmask1);
35   ASSERT_EQ(*bitmask1, PolicyFlags::SYSTEM_PARTITION);
36 
37   const auto bitmask2 = PoliciesToBitmaskResult({"system", "vendor"});
38   ASSERT_TRUE(bitmask2);
39   ASSERT_EQ(*bitmask2, PolicyFlags::SYSTEM_PARTITION | PolicyFlags::VENDOR_PARTITION);
40 
41   const auto bitmask3 = PoliciesToBitmaskResult({"vendor", "system"});
42   ASSERT_TRUE(bitmask3);
43   ASSERT_EQ(*bitmask3, PolicyFlags::SYSTEM_PARTITION | PolicyFlags::VENDOR_PARTITION);
44 
45   const auto bitmask4 =
46       PoliciesToBitmaskResult({"odm", "oem", "public", "product", "system", "vendor"});
47   ASSERT_TRUE(bitmask4);
48   ASSERT_EQ(*bitmask4, PolicyFlags::ODM_PARTITION | PolicyFlags::OEM_PARTITION |
49                            PolicyFlags::PUBLIC | PolicyFlags::PRODUCT_PARTITION |
50                            PolicyFlags::SYSTEM_PARTITION | PolicyFlags::VENDOR_PARTITION);
51 
52   const auto bitmask5 = PoliciesToBitmaskResult({"system", "system", "system"});
53   ASSERT_TRUE(bitmask5);
54   ASSERT_EQ(*bitmask5, PolicyFlags::SYSTEM_PARTITION);
55 
56   const auto bitmask6 = PoliciesToBitmaskResult({""});
57   ASSERT_FALSE(bitmask6);
58 
59   const auto bitmask7 = PoliciesToBitmaskResult({"foo"});
60   ASSERT_FALSE(bitmask7);
61 
62   const auto bitmask8 = PoliciesToBitmaskResult({"system", "foo"});
63   ASSERT_FALSE(bitmask8);
64 
65   const auto bitmask9 = PoliciesToBitmaskResult({"system", ""});
66   ASSERT_FALSE(bitmask9);
67 
68   const auto bitmask10 = PoliciesToBitmaskResult({"system "});
69   ASSERT_FALSE(bitmask10);
70 
71   const auto bitmask11 = PoliciesToBitmaskResult({"signature"});
72   ASSERT_TRUE(bitmask11);
73   ASSERT_EQ(*bitmask11, PolicyFlags::SIGNATURE);
74 
75   const auto bitmask12 = PoliciesToBitmaskResult({"actor"});
76   ASSERT_TRUE(bitmask12);
77   ASSERT_EQ(*bitmask12, PolicyFlags::ACTOR_SIGNATURE);
78 }
79 
TEST(PoliciesTests,BitmaskToPolicies)80 TEST(PoliciesTests, BitmaskToPolicies) {
81   const auto policies1 = BitmaskToPolicies(PolicyFlags::PUBLIC);
82   ASSERT_EQ(1, policies1.size());
83   ASSERT_EQ(policies1[0], "public");
84 
85   const auto policies2 =
86       BitmaskToPolicies(PolicyFlags::SYSTEM_PARTITION | PolicyFlags::VENDOR_PARTITION);
87   ASSERT_EQ(2, policies2.size());
88   ASSERT_EQ(policies2[0], "system");
89   ASSERT_EQ(policies2[1], "vendor");
90 
91   const auto policies3 =
92       BitmaskToPolicies(PolicyFlags::ODM_PARTITION | PolicyFlags::OEM_PARTITION |
93                         PolicyFlags::PUBLIC | PolicyFlags::PRODUCT_PARTITION |
94                         PolicyFlags::SYSTEM_PARTITION | PolicyFlags::VENDOR_PARTITION);
95   ASSERT_EQ(2, policies2.size());
96   ASSERT_EQ(policies3[0], "odm");
97   ASSERT_EQ(policies3[1], "oem");
98   ASSERT_EQ(policies3[2], "product");
99   ASSERT_EQ(policies3[3], "public");
100   ASSERT_EQ(policies3[4], "system");
101   ASSERT_EQ(policies3[5], "vendor");
102 
103   const auto policies4 = BitmaskToPolicies(PolicyFlags::SIGNATURE);
104   ASSERT_EQ(1, policies4.size());
105   ASSERT_EQ(policies4[0], "signature");
106 
107   const auto policies5 = BitmaskToPolicies(PolicyFlags::ACTOR_SIGNATURE);
108   ASSERT_EQ(1, policies5.size());
109   ASSERT_EQ(policies5[0], "actor");
110 }
111 
112 }  // namespace android::idmap2
113