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 #include "gtest/gtest.h"
16 #include <memory>
17 #include "mock_constant.h"
18 #define private public
19 #define protected public
20 #include "domain_json_util.h"
21 #undef private
22 #undef protected
23 namespace OHOS::AppDomainVerify {
24 using namespace testing;
25 using namespace testing::ext;
26 
27 class JsonUtilTest : public testing::Test {
28 public:
29     static void SetUpTestCase(void);
30     static void TearDownTestCase(void);
31     void SetUp();
32     void TearDown();
33 };
34 
SetUpTestCase(void)35 void JsonUtilTest::SetUpTestCase(void)
36 {
37 }
38 
TearDownTestCase(void)39 void JsonUtilTest::TearDownTestCase(void)
40 {
41 }
42 
SetUp(void)43 void JsonUtilTest::SetUp(void)
44 {
45 }
46 
TearDown(void)47 void JsonUtilTest::TearDown(void)
48 {
49 }
50 /**
51  * @tc.name: JsonUtilTest001
52  * @tc.desc: Parse test
53  * @tc.type: FUNC
54  */
55 HWTEST_F(JsonUtilTest, JsonUtilTest001, TestSize.Level0)
56 {
57     // empty
58     AssetJsonObj assetJsonObj;
59     ASSERT_FALSE(JsonUtil::Parse("", assetJsonObj));
60 
61     // parse exception
62     ASSERT_FALSE(JsonUtil::Parse("123", assetJsonObj));
63 
64     // obj null
65     std::string assetJsonsStr = R"({})";
66     ASSERT_FALSE(JsonUtil::Parse(assetJsonsStr, assetJsonObj));
67 
68     // obj not obj
69     assetJsonsStr = R"({"applinking":1})";
70     ASSERT_FALSE(JsonUtil::Parse(assetJsonsStr, assetJsonObj));
71 
72     // applinking null
73     assetJsonsStr = R"({"applinkings":1})";
74     ASSERT_FALSE(JsonUtil::Parse(assetJsonsStr, assetJsonObj));
75 
76     // applinking not obj
77     assetJsonsStr = R"({"applinking":1})";
78     ASSERT_FALSE(JsonUtil::Parse(assetJsonsStr, assetJsonObj));
79 
80     // apps null
81     assetJsonsStr = R"({"applinking":{})";
82     ASSERT_FALSE(JsonUtil::Parse(assetJsonsStr, assetJsonObj));
83 
84     // apps not array
85     assetJsonsStr = R"({"applinking":{"apps":123})";
86     ASSERT_FALSE(JsonUtil::Parse(assetJsonsStr, assetJsonObj));
87 
88     // appIdentifier null
89     assetJsonsStr = R"({"applinking":{"apps":[{"bundleName":1,"fingerprint":1}]}})";
90     ASSERT_TRUE(JsonUtil::Parse(assetJsonsStr, assetJsonObj));
91 
92     // bundleName null
93     assetJsonsStr = R"({"applinking":{"apps":[{"appIdentifier":1,"fingerprint":1}]}})";
94     ASSERT_TRUE(JsonUtil::Parse(assetJsonsStr, assetJsonObj));
95 
96     // fingerprint null
97     assetJsonsStr = R"({"applinking":{"apps":[{"appIdentifier":1,"bundleName":1,}]}})";
98     ASSERT_FALSE(JsonUtil::Parse(assetJsonsStr, assetJsonObj));
99 
100     // appIdentifier null
101     assetJsonsStr = R"({"applinking":{"apps":[{"bundleName":"bundleName","fingerprint":"fingerprint"}]}})";
102     ASSERT_TRUE(JsonUtil::Parse(assetJsonsStr, assetJsonObj));
103 
104     // bundleName null
105     assetJsonsStr = R"({"applinking":{"apps":[{"appIdentifier":"appIdentifier","fingerprint":"fingerprint"}]}})";
106     ASSERT_TRUE(JsonUtil::Parse(assetJsonsStr, assetJsonObj));
107 
108     // fingerprint null
109     assetJsonsStr = R"({"applinking":{"apps":[{"appIdentifier":"appIdentifier","bundleName":"bundleName"}]}})";
110     ASSERT_TRUE(JsonUtil::Parse(assetJsonsStr, assetJsonObj));
111 
112     // all
113     assetJsonsStr = R"({"applinking":{"apps":[{"appIdentifier":1,"bundleName":1,"fingerprint":1}]}})";
114     ASSERT_TRUE(JsonUtil::Parse(assetJsonsStr, assetJsonObj));
115 
116     // all
117     assetJsonsStr =
118         R"({"applinking":{"apps":[{"appIdentifier":"appIdentifier","bundleName":"bundleName","fingerprint":"fingerprint"}]}})";
119     ASSERT_TRUE(JsonUtil::Parse(assetJsonsStr, assetJsonObj));
120 }
121 }