1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
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
16 #include <gtest/gtest.h>
17 #include "parcel.h"
18 #include "parcel_utils.h"
19 #include "policy_info.h"
20 #include "policy_info_parcel.h"
21 #include "policy_info_vector_parcel.h"
22 #include <string>
23 #include <vector>
24
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace AccessControl {
29 namespace SandboxManager {
30 class SandboxManagerParcelTest : public testing::Test {
31 public:
32 static void SetUpTestCase(void);
33 static void TearDownTestCase(void);
34 void SetUp();
35 void TearDown();
36 };
37
38 PolicyInfo g_info1 = {
39 .path = "path1",
40 .mode = 0b01,
41 };
42
43 PolicyInfo g_info2 = {
44 .path = "path2",
45 .mode = 0b10,
46 };
47
48 PolicyInfo g_info3 = {
49 .path = "path3",
50 .mode = 0b11,
51 };
52
SetUpTestCase(void)53 void SandboxManagerParcelTest::SetUpTestCase(void)
54 {}
55
TearDownTestCase(void)56 void SandboxManagerParcelTest::TearDownTestCase(void)
57 {}
58
SetUp(void)59 void SandboxManagerParcelTest::SetUp(void)
60 {}
61
TearDown(void)62 void SandboxManagerParcelTest::TearDown(void)
63 {}
64
65
66 /**
67 * @tc.name: PolicyInfoParcel001
68 * @tc.desc: Test PolicyInfo Marshalling/Unmarshalling.
69 * @tc.type: FUNC
70 * @tc.require:
71 */
72 HWTEST_F(SandboxManagerParcelTest, PolicyInfoParcel001, TestSize.Level1)
73 {
74 PolicyInfoParcel policyInfoParcel;
75 policyInfoParcel.policyInfo = g_info1;
76
77 Parcel parcel;
78 EXPECT_EQ(true, policyInfoParcel.Marshalling(parcel));
79
80 std::shared_ptr<PolicyInfoParcel> readedData(PolicyInfoParcel::Unmarshalling(parcel));
81 ASSERT_NE(nullptr, readedData);
82
83 EXPECT_EQ(g_info1.path, readedData->policyInfo.path);
84 EXPECT_EQ(g_info1.mode, readedData->policyInfo.mode);
85 }
86
87 /**
88 * @tc.name: PolicyInfoParcel002
89 * @tc.desc: Test PolicyInfoVector Marshalling/Unmarshalling.
90 * @tc.type: FUNC
91 * @tc.require:
92 */
93 HWTEST_F(SandboxManagerParcelTest, PolicyInfoParcel002, TestSize.Level1)
94 {
95 PolicyInfoVectorParcel policyInfoVectorParcel;
96 std::vector<PolicyInfo> policyVector;
97 policyVector.emplace_back(g_info1);
98 policyVector.emplace_back(g_info2);
99 policyVector.emplace_back(g_info3);
100
101 policyInfoVectorParcel.policyVector = policyVector;
102
103 Parcel parcel;
104 EXPECT_EQ(true, policyInfoVectorParcel.Marshalling(parcel));
105
106 std::shared_ptr<PolicyInfoVectorParcel> readedData(PolicyInfoVectorParcel::Unmarshalling(parcel));
107 ASSERT_NE(nullptr, readedData);
108
109 EXPECT_EQ(g_info1.path, readedData->policyVector[0].path);
110 EXPECT_EQ(g_info1.mode, readedData->policyVector[0].mode);
111 EXPECT_EQ(g_info2.path, readedData->policyVector[1].path);
112 EXPECT_EQ(g_info2.mode, readedData->policyVector[1].mode);
113 EXPECT_EQ(g_info3.path, readedData->policyVector[2].path);
114 EXPECT_EQ(g_info3.mode, readedData->policyVector[2].mode);
115 }
116
117 /**
118 * @tc.name: PolicyInfoParcel003
119 * @tc.desc: Test PolicyInfoVector Marshalling/Unmarshalling.
120 * @tc.type: FUNC
121 * @tc.require:
122 */
123 HWTEST_F(SandboxManagerParcelTest, PolicyInfoParcel003, TestSize.Level1)
124 {
125 PolicyInfoVectorParcel policyInfoVectorParcel;
126 std::vector<PolicyInfo> policyVector;
127 for (int i = 0; i < 501; i++) {
128 policyVector.emplace_back(g_info1);
129 }
130 policyInfoVectorParcel.policyVector = policyVector;
131 Parcel parcel;
132 EXPECT_EQ(false, policyInfoVectorParcel.Marshalling(parcel));
133
134 parcel.WriteUint32(501);
135 std::shared_ptr<PolicyInfoParcel> readedData(PolicyInfoParcel::Unmarshalling(parcel));
136 EXPECT_EQ(nullptr, readedData);
137 }
138
139 /**
140 * @tc.name: PolicyInfoParcel004
141 * @tc.desc: Test PolicyInfoVector Marshalling/Unmarshalling, larger than max size
142 * @tc.type: FUNC
143 * @tc.require:
144 */
145 HWTEST_F(SandboxManagerParcelTest, PolicyInfoParcel004, TestSize.Level1)
146 {
147 Parcel parcel;
148 uint32_t maxSize = 500; // 500 is max
149 EXPECT_EQ(true, parcel.WriteUint32(maxSize + 1));
150
151 std::shared_ptr<PolicyInfoVectorParcel> readedData(PolicyInfoVectorParcel::Unmarshalling(parcel));
152 ASSERT_EQ(nullptr, readedData);
153 }
154 } // SandboxManager
155 } // AccessControl
156 } // OHOS