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