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 "log/log.h"
17 #include "securec.h"
18 #include "updater/updater_const.h"
19 #include "utils.h"
20 #include "updater_main.h"
21 #include <thread>
22 
23 using namespace Updater;
24 using namespace std;
25 using namespace testing::ext;
26 
27 namespace {
28 class RecordInstallTimeUnittest : public testing::Test {
29 public:
RecordInstallTimeUnittest()30     RecordInstallTimeUnittest()
31     {
32         InitUpdaterLogger("UPDATER", TMP_LOG, TMP_STAGE_LOG, TMP_ERROR_CODE_PATH);
33     }
~RecordInstallTimeUnittest()34     ~RecordInstallTimeUnittest() {}
35 
SetUpTestCase(void)36     static void SetUpTestCase(void) {}
TearDownTestCase(void)37     static void TearDownTestCase(void) {}
SetUp()38     void SetUp() {}
TearDown()39     void TearDown() {}
TestBody()40     void TestBody() {}
41 };
42 
43 HWTEST_F(RecordInstallTimeUnittest, UpdaterRecordInstallTime, TestSize.Level1)
44 {
45     UpdaterParams params;
46     params.installTime.resize(3);  // 3:number of upgrade packages
47     for (params.pkgLocation = 0; params.pkgLocation < params.installTime.size(); params.pkgLocation++) {
48         auto startTime = std::chrono::system_clock::now();
49         std::this_thread::sleep_for(std::chrono::seconds(params.pkgLocation));
50         auto endTime = std::chrono::system_clock::now();
51         params.installTime[params.pkgLocation] = endTime - startTime;
52         WriteInstallTime(params);
53     }
54     UpdaterParams newParams;
55     newParams.installTime.resize(params.installTime.size());
56     newParams.pkgLocation = params.pkgLocation;
57     ReadInstallTime(newParams);
58     for (int i = 0; i < params.installTime.size(); i++) {
59         EXPECT_EQ(
60             Utils::DurationToString(params.installTime, i, 2), // 2:precision
61             Utils::DurationToString(params.installTime, i, 2)); // 2:precision
62     }
63 }
64 
65 HWTEST_F(RecordInstallTimeUnittest, UpdaterTestIsDouble, TestSize.Level1)
66 {
67     std::string installTime = "12.11";
68     std::string exceptionData = "12.11.11";
69     EXPECT_EQ(IsDouble(installTime), true);
70     EXPECT_EQ(IsDouble(exceptionData), false);
71 }
72 }  // namespace