1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci* Copyright (c) 2021 Huawei Device Co., Ltd.
3fb299fa2Sopenharmony_ci* Licensed under the Apache License, Version 2.0 (the "License");
4fb299fa2Sopenharmony_ci* you may not use this file except in compliance with the License.
5fb299fa2Sopenharmony_ci* You may obtain a copy of the License at
6fb299fa2Sopenharmony_ci*
7fb299fa2Sopenharmony_ci*     http://www.apache.org/licenses/LICENSE-2.0
8fb299fa2Sopenharmony_ci*
9fb299fa2Sopenharmony_ci* Unless required by applicable law or agreed to in writing, software
10fb299fa2Sopenharmony_ci* distributed under the License is distributed on an "AS IS" BASIS,
11fb299fa2Sopenharmony_ci* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb299fa2Sopenharmony_ci* See the License for the specific language governing permissions and
13fb299fa2Sopenharmony_ci* limitations under the License.
14fb299fa2Sopenharmony_ci*/
15fb299fa2Sopenharmony_ci#include "log_unittest.h"
16fb299fa2Sopenharmony_ci#include <fstream>
17fb299fa2Sopenharmony_ci#include <iostream>
18fb299fa2Sopenharmony_ci#include <string>
19fb299fa2Sopenharmony_ci#include "log/log.h"
20fb299fa2Sopenharmony_ci
21fb299fa2Sopenharmony_ciusing namespace testing::ext;
22fb299fa2Sopenharmony_ciusing namespace UpdaterUt;
23fb299fa2Sopenharmony_ciusing namespace Updater;
24fb299fa2Sopenharmony_ciusing namespace std;
25fb299fa2Sopenharmony_ci
26fb299fa2Sopenharmony_cinamespace UpdaterUt {
27fb299fa2Sopenharmony_civoid LogUnitTest::SetUpTestCase(void)
28fb299fa2Sopenharmony_ci{
29fb299fa2Sopenharmony_ci    cout << "SetUpTestCase" << endl;
30fb299fa2Sopenharmony_ci}
31fb299fa2Sopenharmony_ci
32fb299fa2Sopenharmony_civoid LogUnitTest::TearDownTestCase(void)
33fb299fa2Sopenharmony_ci{
34fb299fa2Sopenharmony_ci    cout << "TearDownTestCase" << endl;
35fb299fa2Sopenharmony_ci}
36fb299fa2Sopenharmony_ci
37fb299fa2Sopenharmony_ciHWTEST_F(LogUnitTest, log_test_001, TestSize.Level1)
38fb299fa2Sopenharmony_ci{
39fb299fa2Sopenharmony_ci    InitUpdaterLogger("UPDATER_UT", "", "", "");
40fb299fa2Sopenharmony_ci    SetLogLevel(ERROR);
41fb299fa2Sopenharmony_ci    LOG(ERROR) << "this is ut";
42fb299fa2Sopenharmony_ci    STAGE(UPDATE_STAGE_BEGIN) << "this is ut";
43fb299fa2Sopenharmony_ci    ERROR_CODE(CODE_VERIFY_FAIL);
44fb299fa2Sopenharmony_ci    SUCCEED();
45fb299fa2Sopenharmony_ci
46fb299fa2Sopenharmony_ci    SetLogLevel(INFO);
47fb299fa2Sopenharmony_ci    LOG(ERROR) << "this is ut";
48fb299fa2Sopenharmony_ci    STAGE(UPDATE_STAGE_BEGIN) << "this is ut";
49fb299fa2Sopenharmony_ci    SUCCEED();
50fb299fa2Sopenharmony_ci
51fb299fa2Sopenharmony_ci    SetLogLevel(ERROR);
52fb299fa2Sopenharmony_ci    LOG(ERROR) << "this is ut";
53fb299fa2Sopenharmony_ci    STAGE(UPDATE_STAGE_BEGIN) << "this is ut";
54fb299fa2Sopenharmony_ci    InitUpdaterLogger("UPDATER_UT", "/data/updater/m_log.txt", "/data/updater/m_stage.txt", "/data/updater/m_code.txt");
55fb299fa2Sopenharmony_ci
56fb299fa2Sopenharmony_ci    LOG(ERROR) << "this is ut";
57fb299fa2Sopenharmony_ci    STAGE(UPDATE_STAGE_BEGIN) << "this is ut";
58fb299fa2Sopenharmony_ci    ERROR_CODE(CODE_VERIFY_FAIL);
59fb299fa2Sopenharmony_ci    fstream f;
60fb299fa2Sopenharmony_ci    f.open("/data/updater/m_log.txt", ios::in);
61fb299fa2Sopenharmony_ci    if (!f) {
62fb299fa2Sopenharmony_ci        SUCCEED();
63fb299fa2Sopenharmony_ci    };
64fb299fa2Sopenharmony_ci    char ch[100];
65fb299fa2Sopenharmony_ci    f.getline(ch, 100);
66fb299fa2Sopenharmony_ci    string result = ch;
67fb299fa2Sopenharmony_ci    auto ret = result.find("this is ut");
68fb299fa2Sopenharmony_ci    if (ret != string::npos) {
69fb299fa2Sopenharmony_ci        f.close();
70fb299fa2Sopenharmony_ci        unlink("/data/updater/m_log.txt");
71fb299fa2Sopenharmony_ci        unlink("/data/updater/m_stage.txt");
72fb299fa2Sopenharmony_ci        EXPECT_NE(ret, string::npos);
73fb299fa2Sopenharmony_ci        SUCCEED();
74fb299fa2Sopenharmony_ci    } else {
75fb299fa2Sopenharmony_ci        f.close();
76fb299fa2Sopenharmony_ci        unlink("/data/updater/m_log.txt");
77fb299fa2Sopenharmony_ci        unlink("/data/updater/m_stage.txt");
78fb299fa2Sopenharmony_ci        FAIL();
79fb299fa2Sopenharmony_ci    }
80fb299fa2Sopenharmony_ci}
81fb299fa2Sopenharmony_ci} // namespace updater_ut
82