1 /*
2 * Copyright (c) 2022 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
18 #include "net_quota_policy.h"
19
20 namespace OHOS {
21 namespace NetManagerStandard {
22 namespace {
23 static constexpr const char *ICCID_1 = "sim_abcdefg_1";
24 using namespace testing::ext;
GetQuota()25 NetQuotaPolicy GetQuota()
26 {
27 NetQuotaPolicy policy;
28 policy.networkmatchrule.simId = "testIccid";
29 policy.networkmatchrule.ident = "testIdent";
30 policy.quotapolicy.title = "testTitle";
31 return policy;
32 }
33 } // namespace
34 class UtNetQuotaPolicy : public testing::Test {
35 public:
36 static void SetUpTestCase();
37 static void TearDownTestCase();
38 void SetUp();
39 void TearDown();
40 };
41
SetUpTestCase()42 void UtNetQuotaPolicy::SetUpTestCase() {}
43
TearDownTestCase()44 void UtNetQuotaPolicy::TearDownTestCase() {}
45
SetUp()46 void UtNetQuotaPolicy::SetUp() {}
47
TearDown()48 void UtNetQuotaPolicy::TearDown() {}
49
50 /**
51 * @tc.name: GetPeriodStart001
52 * @tc.desc: Test NetPolicyQuota GetPeriodStart.
53 * @tc.type: FUNC
54 */
55 HWTEST_F(UtNetQuotaPolicy, GetPeriodStart001, TestSize.Level1)
56 {
57 NetQuotaPolicy netQuotaPolicy1;
58 netQuotaPolicy1.networkmatchrule.simId = ICCID_1;
59 netQuotaPolicy1.quotapolicy.periodDuration = "M1";
60 auto result = netQuotaPolicy1.GetPeriodStart();
61 std::cout << "result1:" << result << std::endl;
62
63 NetQuotaPolicy netQuotaPolicy2;
64 netQuotaPolicy2.networkmatchrule.simId = ICCID_1;
65 netQuotaPolicy2.quotapolicy.periodDuration = "Y1";
66 auto result2 = netQuotaPolicy2.GetPeriodStart();
67 std::cout << "result2:" << result2 << std::endl;
68
69 NetQuotaPolicy netQuotaPolicy3;
70 netQuotaPolicy3.networkmatchrule.simId = ICCID_1;
71 netQuotaPolicy3.quotapolicy.periodDuration = "D1";
72 auto result3 = netQuotaPolicy3.GetPeriodStart();
73 std::cout << "result3:" << result3 << std::endl;
74
75 ASSERT_TRUE(result > 0);
76 ASSERT_TRUE(result2 > 0);
77 ASSERT_TRUE(result3 > 0);
78 }
79
80 /**
81 * @tc.name: Marshalling001
82 * @tc.desc: Test NetPolicyQuota Marshalling.
83 * @tc.type: FUNC
84 */
85 HWTEST_F(UtNetQuotaPolicy, Marshalling001, TestSize.Level1)
86 {
87 Parcel parcel;
88 NetQuotaPolicy netQuotaPolicy = GetQuota();
89 bool ret = netQuotaPolicy.Marshalling(parcel);
90 ASSERT_TRUE(ret);
91 NetQuotaPolicy recv1;
92 ret = NetQuotaPolicy::Unmarshalling(parcel, recv1);
93 ASSERT_TRUE(ret);
94 EXPECT_EQ(recv1.networkmatchrule.simId, netQuotaPolicy.networkmatchrule.simId);
95 EXPECT_EQ(recv1.networkmatchrule.ident, netQuotaPolicy.networkmatchrule.ident);
96 }
97
98 /**
99 * @tc.name: Marshalling002
100 * @tc.desc: Test NetPolicyQuota Marshalling.
101 * @tc.type: FUNC
102 */
103 HWTEST_F(UtNetQuotaPolicy, Marshalling002, TestSize.Level1)
104
105 {
106 Parcel parcel;
107 NetQuotaPolicy data = GetQuota();
108 bool ret = NetQuotaPolicy::Marshalling(parcel, data);
109 ASSERT_TRUE(ret);
110 NetQuotaPolicy recv;
111 ret = NetQuotaPolicy::Unmarshalling(parcel, recv);
112 ASSERT_TRUE(ret);
113 EXPECT_EQ(recv.networkmatchrule.simId, data.networkmatchrule.simId);
114 EXPECT_EQ(recv.networkmatchrule.ident, data.networkmatchrule.ident);
115 }
116
117 /**
118 * @tc.name: Marshalling003
119 * @tc.desc: Test NetPolicyQuota Marshalling.
120 * @tc.type: FUNC
121 */
122 HWTEST_F(UtNetQuotaPolicy, Marshalling003, TestSize.Level1)
123 {
124 Parcel parcel;
125 std::vector<NetQuotaPolicy> data;
126 const uint32_t dataSize = 12;
127 for (uint32_t i = 0; i < dataSize; i++) {
128 data.push_back(GetQuota());
129 }
130 bool ret = NetQuotaPolicy::Marshalling(parcel, data);
131 ASSERT_TRUE(ret);
132 std::vector<NetQuotaPolicy> recv;
133 ret = NetQuotaPolicy::Unmarshalling(parcel, recv);
134 ASSERT_TRUE(ret);
__anond094590c0202(const auto &cv) 135 std::for_each(recv.begin(), recv.end(), [this](const auto &cv) {
136 EXPECT_EQ(cv.networkmatchrule.simId, GetQuota().networkmatchrule.simId);
137 EXPECT_EQ(cv.networkmatchrule.ident, GetQuota().networkmatchrule.ident);
138 });
139 }
140 } // namespace NetManagerStandard
141 } // namespace OHOS