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 READ_LENGTH = 0; 28 const int ERR_LENGTH = 9; 29 const int DATA_OFFSET = 0; 30 31 using namespace std; 32 using namespace testing::ext; 33 34 class UpdateTest : public testing::Test { 35 protected: SetUpTestCase(void)36 static void SetUpTestCase(void) {} TearDownTestCase(void)37 static void TearDownTestCase(void) {} SetUp()38 virtual void SetUp() {} TearDown()39 virtual void TearDown() {} 40 }; 41 42 /** 43 * @tc.number : SUB_Upgrade_API_0100 44 * @tc.name : Initializes the OTA module 45 * @tc.desc : [C- SOFTWARE -0200] 46 */ 47 HWTEST_F(UpdateTest, subUpgradeAPI0100, Function | MediumTest | Level1) 48 { 49 unsigned int ret; 50 ret = HotaInit(NULL, NULL); 51 printf("HotaInit return = %d\r\n", ret); 52 EXPECT_EQ(OHOS_SUCCESS, ret); 53 }; 54 55 /** 56 * @tc.number : SUB_Upgrade_API_0200 57 * @tc.name : Sets the switch for using the default upgrade package format 58 * @tc.desc : [C- SOFTWARE -0200] 59 */ 60 HWTEST_F(UpdateTest, subUpgradeAPI0200, Function | MediumTest | Level1) 61 { 62 unsigned int ret; 63 ret = HotaSetPackageType(USE_HOS_PKG); 64 printf("use package flag = %d\r\n, return = %d\r\n", USE_HOS_PKG, ret); 65 EXPECT_EQ(OHOS_SUCCESS, ret); 66 }; 67 68 /** 69 * @tc.number : SUB_Upgrade_API_0300 70 * @tc.name : Sets the switch for using the non default upgrade package format 71 * @tc.desc : [C- SOFTWARE -0200] 72 */ 73 HWTEST_F(UpdateTest, subUpgradeAPI0300, Function | MediumTest | Level1) 74 { 75 unsigned int ret; 76 ret = HotaSetPackageType(NOT_HOS_PKG); 77 printf("use package flag = %d\r\n, return = %d\r\n", NOT_HOS_PKG, ret); 78 EXPECT_EQ(OHOS_SUCCESS, ret); 79 }; 80 81 /** 82 * @tc.number : SUB_Upgrade_API_0400 83 * @tc.name : Sets the switch for using the default upgrade package format fail 84 * @tc.desc : [C- SOFTWARE -0200] 85 */ 86 HWTEST_F(UpdateTest, subUpgradeAPI0400, Function | MediumTest | Level1) 87 { 88 unsigned int ret; 89 ret = HotaSetPackageType(ERR_PAK_FALG); 90 printf("use package flag = %d, return = %d\r\n", ERR_PAK_FALG, ret); 91 EXPECT_EQ(OHOS_FAILURE, ret); 92 }; 93 94 /** 95 * @tc.number : SUB_Upgrade_API_0500 96 * @tc.name : Obtains the index of the A or B partition to be upgraded 97 * @tc.desc : [C- SOFTWARE -0200] 98 */ 99 HWTEST_F(UpdateTest, subUpgradeAPI0500, Function | MediumTest | Level1) 100 { 101 unsigned int ret; 102 unsigned int index = 0; 103 ret = HotaGetUpdateIndex(&index); 104 printf("index = %d\r\n, get index return = %d\r\n", index, ret); 105 EXPECT_EQ(OHOS_SUCCESS, ret); 106 }; 107 108 /** 109 * @tc.number : SUB_Upgrade_API_0600 110 * @tc.name : Set upgrade status 111 * @tc.desc : [C- SOFTWARE -0200] 112 */ 113 HWTEST_F(UpdateTest, subUpgradeAPI0600, Function | MediumTest | Level1) 114 { 115 unsigned int ret; 116 ret = HotaSetBootSettings(); 117 printf("HotaSetBootSettings return = %d\r\n", ret); 118 EXPECT_EQ(OHOS_SUCCESS, ret); 119 }; 120 121 122 /** 123 * @tc.number : SUB_Upgrade_API_1000 124 * @tc.name : Writes specified data into flash memory fail 125 * @tc.desc : [C- SOFTWARE -0200] 126 */ 127 HWTEST_F(UpdateTest, subUpgradeAPI1000, Function | MediumTest | Level1) 128 { 129 unsigned int ret; 130 unsigned int index = 0; 131 ret = HotaGetUpdateIndex(&index); 132 printf("index = %d, get index return = %d\r\n", index, ret); 133 ret = HotaSetPackageType(USE_HOS_PKG); 134 printf("use package flag = %d\r\n, return = %d\r\n", USE_HOS_PKG, ret); 135 unsigned char buffer[DATA_LENGTH] = {0}; 136 ret = HotaWrite(buffer, DATA_OFFSET, DATA_LENGTH); 137 printf("Hota write return =%d\r\n", ret); 138 EXPECT_EQ(OHOS_FAILURE, ret); 139 HotaCancel(); 140 }; 141 142 /** 143 * @tc.number : SUB_Upgrade_API_1100 144 * @tc.name : Reads the data that has been written into flash memory fail 145 * @tc.desc : [C- SOFTWARE -0200] 146 */ 147 HWTEST_F(UpdateTest, subUpgradeAPI1100, Function | MediumTest | Level1) 148 { 149 unsigned int ret; 150 unsigned int index = 0; 151 unsigned char buffer[DATA_LENGTH] = {0}; 152 unsigned char bufferread[DATA_LENGTH] = {0}; 153 HotaGetUpdateIndex(&index); 154 HotaSetPackageType(USE_HOS_PKG); 155 HotaWrite(buffer, DATA_OFFSET, DATA_LENGTH); 156 ret = HotaRead(DATA_OFFSET, DATA_LENGTH, bufferread); 157 printf("Hota read return =%d\r\n", ret); 158 EXPECT_EQ(OHOS_FAILURE, ret); 159 HotaCancel(); 160 }; 161 162 /** 163 * @tc.number : SUB_Upgrade_API_1200 164 * @tc.name : Cancels an upgrade fail 165 * @tc.desc : [C- SOFTWARE -0200] 166 */ 167 HWTEST_F(UpdateTest, subUpgradeAPI1200, Function | MediumTest | Level1) 168 { 169 unsigned int ret; 170 ret = HotaCancel(); 171 printf("Hota Cancel return = %d\r\n", ret); 172 EXPECT_EQ(OHOS_SUCCESS, ret); 173 }; 174