sysevent_unittest.cpp 4.0 KB
Newer Older
C
cheng_jinsong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
/*
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <cinttypes>
#include <ctime>
#include <sys/time.h>

#include "bootevent.h"
#include "init_utils.h"
#include "list.h"
#include "param_stub.h"
#include "securec.h"
#include "sys_event.h"

using namespace std;
using namespace testing::ext;
extern "C" {
extern void ReportBootEventComplete(ListNode *events);
}

static void AddBootEvent(ListNode *events, const char *name, int32_t type)
{
    BOOT_EVENT_PARAM_ITEM *item = (BOOT_EVENT_PARAM_ITEM *)calloc(1, sizeof(BOOT_EVENT_PARAM_ITEM));
    if (item == NULL) {
        return;
    }
    OH_ListInit(&item->node);
    item->paramName = strdup(name);
    if (item->paramName == NULL) {
        free(item);
        return;
    }
    (void)clock_gettime(CLOCK_MONOTONIC, &(item->timestamp[BOOTEVENT_FORK]));
    (void)clock_gettime(CLOCK_MONOTONIC, &(item->timestamp[BOOTEVENT_READY]));
    item->flags = type;
    OH_ListAddTail(events, (ListNode *)&item->node);
}

static void BootEventDestroyProc(ListNode *node)
{
    if (node == NULL) {
        return;
    }
    BOOT_EVENT_PARAM_ITEM *item = (BOOT_EVENT_PARAM_ITEM *)node;
    OH_ListRemove(node);
    OH_ListInit(node);
    free(item->paramName);
    free(item);
}

namespace init_ut {
class SysEventUnitTest : public testing::Test {
public:
    static void SetUpTestCase(void) {};
    static void TearDownTestCase(void) {};
    void SetUp() {};
    void TearDown() {};
};

HWTEST_F(SysEventUnitTest, SysEventTest_001, TestSize.Level1)
{
    ReportBootEventComplete(nullptr);
}

HWTEST_F(SysEventUnitTest, SysEventTest_002, TestSize.Level1)
{
    ListNode events = { &events, &events };
    // empty event
    ReportBootEventComplete(&events);
}

HWTEST_F(SysEventUnitTest, SysEventTest_003, TestSize.Level1)
{
    ListNode events = { &events, &events };
    // create event and report
    AddBootEvent(&events, "bootevent.11111.xxxx", BOOTEVENT_TYPE_JOB);
    AddBootEvent(&events, "bootevent.22222222.xxxx", BOOTEVENT_TYPE_SERVICE);
    AddBootEvent(&events, "bootevent.33333333333.xxxx", BOOTEVENT_TYPE_SERVICE);
    AddBootEvent(&events, "bootevent.44444444444444", BOOTEVENT_TYPE_SERVICE);
    AddBootEvent(&events, "bootevent.44444444444444.6666666666.777777", BOOTEVENT_TYPE_SERVICE);
    ReportBootEventComplete(&events);
    OH_ListRemoveAll(&events, BootEventDestroyProc);
}

HWTEST_F(SysEventUnitTest, SysEventTest_004, TestSize.Level1)
{
C
cheng_jinsong 已提交
98
    struct timespec curr = {0};
C
cheng_jinsong 已提交
99 100 101 102 103 104
    if (clock_gettime(CLOCK_MONOTONIC, &curr) != 0) {
        return;
    }
    StartupTimeEvent startupTime = {};
    startupTime.event.type = STARTUP_TIME;
    startupTime.totalTime = curr.tv_sec;
C
cheng_jinsong 已提交
105 106
    startupTime.totalTime = startupTime.totalTime * MSECTONSEC;
    startupTime.totalTime += curr.tv_nsec / USTONSEC;
C
cheng_jinsong 已提交
107 108 109 110 111 112 113 114
    startupTime.detailTime = const_cast<char *>("buffer");
    startupTime.reason = const_cast<char *>("");
    startupTime.firstStart = 1;
    ReportSysEvent(&startupTime.event);
}

HWTEST_F(SysEventUnitTest, SysEventTest_005, TestSize.Level1)
{
C
cheng_jinsong 已提交
115
    struct timespec curr = {0};
C
cheng_jinsong 已提交
116 117 118 119 120 121
    if (clock_gettime(CLOCK_MONOTONIC, &curr) != 0) {
        return;
    }
    StartupTimeEvent startupTime = {};
    startupTime.event.type = STARTUP_EVENT_MAX;
    startupTime.totalTime = curr.tv_sec;
C
cheng_jinsong 已提交
122 123
    startupTime.totalTime = startupTime.totalTime * MSECTONSEC;
    startupTime.totalTime += curr.tv_nsec / USTONSEC;
C
cheng_jinsong 已提交
124 125 126 127 128 129
    startupTime.detailTime = const_cast<char *>("buffer");
    startupTime.reason = const_cast<char *>("");
    startupTime.firstStart = 1;
    ReportSysEvent(&startupTime.event);
}
} // namespace init_ut