• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  */
4 /*
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <stdio.h>
19 #include "gtest/gtest.h"
20 #include "hota_updater.h"
21 #include "ohos_types.h"
22 
23 const int USE_HOS_PKG = 1;
24 const int NOT_HOS_PKG = 0;
25 const int ERR_PAK_FALG = 2;
26 const int DATA_LENGTH = 190;
27 const int DATA_OFFSET = 0;
28 
29 using namespace std;
30 using namespace testing::ext;
31 
32 class UpdateTest : public testing::Test {
33 protected:
SetUpTestCase(void)34     static void SetUpTestCase(void) {}
TearDownTestCase(void)35     static void TearDownTestCase(void) {}
SetUp()36     virtual void SetUp() {}
TearDown()37     virtual void TearDown() {}
38 };
39 
40 /**
41  * @tc.number    : SUB_Upgrade_API_0100
42  * @tc.name      : Initializes the OTA module
43  * @tc.desc      : [C- SOFTWARE -0200]
44  */
45 HWTEST_F(UpdateTest, subUpgradeAPI0100, Function | MediumTest | Level1)
46 {
47     unsigned int ret;
48     ret = HotaInit(NULL, NULL);
49     printf("HotaInit return = %d\r\n", ret);
50     EXPECT_EQ(OHOS_SUCCESS, ret);
51 };
52 
53 /**
54  * @tc.number    : SUB_Upgrade_API_0200
55  * @tc.name      : Sets the switch for using the default upgrade package format
56  * @tc.desc      : [C- SOFTWARE -0200]
57  */
58 HWTEST_F(UpdateTest, subUpgradeAPI0200, Function | MediumTest | Level1)
59 {
60     unsigned int ret;
61     ret = HotaSetPackageType(USE_HOS_PKG);
62     printf("use package flag = %d\r\n, return = %d\r\n", USE_HOS_PKG, ret);
63     EXPECT_EQ(OHOS_SUCCESS, ret);
64 };
65 
66 /**
67  * @tc.number    : SUB_Upgrade_API_0300
68  * @tc.name      : Sets the switch for using the non default upgrade package format
69  * @tc.desc      : [C- SOFTWARE -0200]
70  */
71 HWTEST_F(UpdateTest, subUpgradeAPI0300, Function | MediumTest | Level1)
72 {
73     unsigned int ret;
74     ret = HotaSetPackageType(NOT_HOS_PKG);
75     printf("use package flag = %d\r\n, return = %d\r\n", NOT_HOS_PKG, ret);
76     EXPECT_EQ(OHOS_SUCCESS, ret);
77 };
78 
79 /**
80  * @tc.number    : SUB_Upgrade_API_0400
81  * @tc.name      : Sets the switch for using the default upgrade package format fail
82  * @tc.desc      : [C- SOFTWARE -0200]
83  */
84 HWTEST_F(UpdateTest, subUpgradeAPI0400, Function | MediumTest | Level1)
85 {
86     unsigned int ret;
87     ret = HotaSetPackageType(ERR_PAK_FALG);
88     printf("use package flag = %d, return = %d\r\n", ERR_PAK_FALG, ret);
89     EXPECT_EQ(OHOS_FAILURE, ret);
90 };
91 
92 /**
93  * @tc.number    : SUB_Upgrade_API_0500
94  * @tc.name      : Obtains the index of the A or B partition to be upgraded
95  * @tc.desc      : [C- SOFTWARE -0200]
96  */
97 HWTEST_F(UpdateTest, subUpgradeAPI0500, Function | MediumTest | Level1)
98 {
99     unsigned int ret;
100     unsigned int index = 0;
101     ret = HotaGetUpdateIndex(&index);
102     printf("index = %d\r\n, get index return = %d\r\n", index, ret);
103     EXPECT_EQ(OHOS_SUCCESS, ret);
104 };
105 
106 /**
107  * @tc.number    : SUB_Upgrade_API_0600
108  * @tc.name      : Set upgrade status
109  * @tc.desc      : [C- SOFTWARE -0200]
110  */
111 HWTEST_F(UpdateTest, subUpgradeAPI0600, Function | MediumTest | Level1)
112 {
113     unsigned int ret;
114     ret = HotaSetBootSettings();
115     printf("HotaSetBootSettings return = %d\r\n", ret);
116     EXPECT_EQ(OHOS_SUCCESS, ret);
117 };
118 
119 
120 /**
121  * @tc.number    : SUB_Upgrade_API_1000
122  * @tc.name      : Writes specified data into flash memory fail
123  * @tc.desc      : [C- SOFTWARE -0200]
124  */
125 HWTEST_F(UpdateTest, subUpgradeAPI1000, Function | MediumTest | Level1)
126 {
127     unsigned int ret;
128     unsigned int index = 0;
129     ret = HotaGetUpdateIndex(&index);
130     printf("index = %d, get index return = %d\r\n", index, ret);
131     ret = HotaSetPackageType(USE_HOS_PKG);
132     printf("use package flag = %d\r\n, return = %d\r\n", USE_HOS_PKG, ret);
133     unsigned char buffer[DATA_LENGTH] = {0};
134     ret = HotaWrite(buffer, DATA_OFFSET, DATA_LENGTH);
135     printf("Hota write return =%d\r\n", ret);
136     EXPECT_EQ(OHOS_FAILURE, ret);
137     HotaCancel();
138 };
139 
140 /**
141  * @tc.number    : SUB_Upgrade_API_1100
142  * @tc.name      : Reads the data that has been written into flash memory fail
143  * @tc.desc      : [C- SOFTWARE -0200]
144  */
145 HWTEST_F(UpdateTest, subUpgradeAPI1100, Function | MediumTest | Level1)
146 {
147     unsigned int ret;
148     unsigned int index = 0;
149     unsigned char buffer[DATA_LENGTH] = {0};
150     unsigned char bufferread[DATA_LENGTH] = {0};
151     HotaGetUpdateIndex(&index);
152     HotaSetPackageType(USE_HOS_PKG);
153     HotaWrite(buffer, DATA_OFFSET, DATA_LENGTH);
154     ret = HotaRead(DATA_OFFSET, DATA_LENGTH, bufferread);
155     printf("Hota read return =%d\r\n", ret);
156     EXPECT_EQ(OHOS_FAILURE, ret);
157     HotaCancel();
158 };
159 
160 /**
161  * @tc.number    : SUB_Upgrade_API_1200
162  * @tc.name      : Cancels an upgrade fail
163  * @tc.desc      : [C- SOFTWARE -0200]
164  */
165 HWTEST_F(UpdateTest, subUpgradeAPI1200, Function | MediumTest | Level1)
166 {
167     unsigned int ret;
168     ret = HotaCancel();
169     printf("Hota Cancel return = %d\r\n", ret);
170     EXPECT_EQ(OHOS_SUCCESS, ret);
171 };
172