1/*
2* Copyright (c) 2021 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 "log_unittest.h"
16#include <fstream>
17#include <iostream>
18#include <string>
19#include "log/log.h"
20
21using namespace testing::ext;
22using namespace UpdaterUt;
23using namespace Updater;
24using namespace std;
25
26namespace UpdaterUt {
27void LogUnitTest::SetUpTestCase(void)
28{
29    cout << "SetUpTestCase" << endl;
30}
31
32void LogUnitTest::TearDownTestCase(void)
33{
34    cout << "TearDownTestCase" << endl;
35}
36
37HWTEST_F(LogUnitTest, log_test_001, TestSize.Level1)
38{
39    InitUpdaterLogger("UPDATER_UT", "", "", "");
40    SetLogLevel(ERROR);
41    LOG(ERROR) << "this is ut";
42    STAGE(UPDATE_STAGE_BEGIN) << "this is ut";
43    ERROR_CODE(CODE_VERIFY_FAIL);
44    SUCCEED();
45
46    SetLogLevel(INFO);
47    LOG(ERROR) << "this is ut";
48    STAGE(UPDATE_STAGE_BEGIN) << "this is ut";
49    SUCCEED();
50
51    SetLogLevel(ERROR);
52    LOG(ERROR) << "this is ut";
53    STAGE(UPDATE_STAGE_BEGIN) << "this is ut";
54    InitUpdaterLogger("UPDATER_UT", "/data/updater/m_log.txt", "/data/updater/m_stage.txt", "/data/updater/m_code.txt");
55
56    LOG(ERROR) << "this is ut";
57    STAGE(UPDATE_STAGE_BEGIN) << "this is ut";
58    ERROR_CODE(CODE_VERIFY_FAIL);
59    fstream f;
60    f.open("/data/updater/m_log.txt", ios::in);
61    if (!f) {
62        SUCCEED();
63    };
64    char ch[100];
65    f.getline(ch, 100);
66    string result = ch;
67    auto ret = result.find("this is ut");
68    if (ret != string::npos) {
69        f.close();
70        unlink("/data/updater/m_log.txt");
71        unlink("/data/updater/m_stage.txt");
72        EXPECT_NE(ret, string::npos);
73        SUCCEED();
74    } else {
75        f.close();
76        unlink("/data/updater/m_log.txt");
77        unlink("/data/updater/m_stage.txt");
78        FAIL();
79    }
80}
81} // namespace updater_ut
82