• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include <gtest/gtest.h>
16 
17 #include "managed_browser_policy_serializer_test.h"
18 #include "utils.h"
19 
20 using namespace testing::ext;
21 
22 namespace OHOS {
23 namespace EDM {
24 namespace TEST {
25 const std::string TEST_POLICY_DATA = "{\"test_bundle_name\":{\"version\":1,\"policyNames\":[\"test_policy_name\"]}}";
26 const std::string TEST_BUNDLE_NAME = "test_bundle_name";
27 const std::string TEST_POLICY_NAME = "test_policy_name";
28 const std::string TEST_POLICY_VALUE = "\"test_policy_value\"";
29 
SetUpTestSuite(void)30 void ManagedBrowserPolicySerializerTest::SetUpTestSuite(void)
31 {
32     Utils::SetEdmInitialEnv();
33 }
34 
TearDownTestSuite(void)35 void ManagedBrowserPolicySerializerTest::TearDownTestSuite(void)
36 {
37     Utils::ResetTokenTypeAndUid();
38     ASSERT_TRUE(Utils::IsOriginalUTEnv());
39     std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
40 }
41 
42 /**
43  * @tc.name: TestSerializeSuc
44  * @tc.desc: Test ManagedBrowserPolicySerializer::Serialize success
45  * @tc.type: FUNC
46  */
47 HWTEST_F(ManagedBrowserPolicySerializerTest, TestSerializeSuc, TestSize.Level1)
48 {
49     auto serializer = ManagedBrowserPolicySerializer::GetInstance();
50     std::map<std::string, ManagedBrowserPolicyType> dataObj;
51     dataObj[TEST_BUNDLE_NAME].version = 1;
52     dataObj[TEST_BUNDLE_NAME].policyNames.push_back(TEST_POLICY_NAME);
53     std::string policyData;
54     bool ret = serializer->Serialize(dataObj, policyData);
55     ASSERT_TRUE(ret);
56     ASSERT_FALSE(policyData.empty());
57 }
58 
59 /**
60  * @tc.name: TestDeserializeEmpty
61  * @tc.desc: Test ManagedBrowserPolicySerializer::Deserialize when policy is empty
62  * @tc.type: FUNC
63  */
64 HWTEST_F(ManagedBrowserPolicySerializerTest, TestDeserializeEmpty, TestSize.Level1)
65 {
66     auto serializer = ManagedBrowserPolicySerializer::GetInstance();
67     std::string emptyPolicy;
68     std::map<std::string, ManagedBrowserPolicyType> dataObj;
69     bool ret = serializer->Deserialize(emptyPolicy, dataObj);
70     ASSERT_TRUE(ret);
71     ASSERT_TRUE(dataObj.empty());
72 }
73 
74 /**
75  * @tc.name: TestDeserializeSuc
76  * @tc.desc: Test ManagedBrowserPolicySerializer::Deserialize success
77  * @tc.type: FUNC
78  */
79 HWTEST_F(ManagedBrowserPolicySerializerTest, TestDeserializeSuc, TestSize.Level1)
80 {
81     auto serializer = ManagedBrowserPolicySerializer::GetInstance();
82     std::map<std::string, ManagedBrowserPolicyType> dataObj;
83     bool ret = serializer->Deserialize(TEST_POLICY_DATA, dataObj);
84     ASSERT_TRUE(ret);
85     ASSERT_TRUE(dataObj[TEST_BUNDLE_NAME].version == 1);
86     ASSERT_TRUE(dataObj[TEST_BUNDLE_NAME].policyNames.size() == 1);
87 }
88 
89 /**
90  * @tc.name: TestDeserializeFail
91  * @tc.desc: Test ManagedBrowserPolicySerializer::Deserialize failed
92  * @tc.type: FUNC
93  */
94 HWTEST_F(ManagedBrowserPolicySerializerTest, TestDeserializeFail, TestSize.Level1)
95 {
96     auto serializer = ManagedBrowserPolicySerializer::GetInstance();
97     std::map<std::string, ManagedBrowserPolicyType> dataObj;
98     const std::string policy = "invalid";
99     bool ret = serializer->Deserialize(policy, dataObj);
100     ASSERT_FALSE(ret);
101     ASSERT_TRUE(dataObj.empty());
102 }
103 
104 } // namespace TEST
105 } // namespace EDM
106 } // namespace OHOS