• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 using namespace testing::ext;
22 using namespace UpdaterUt;
23 using namespace Updater;
24 using namespace std;
25 
26 namespace UpdaterUt {
SetUpTestCase(void)27 void LogUnitTest::SetUpTestCase(void)
28 {
29     cout << "SetUpTestCase" << endl;
30 }
31 
TearDownTestCase(void)32 void LogUnitTest::TearDownTestCase(void)
33 {
34     cout << "TearDownTestCase" << endl;
35 }
36 
37 HWTEST_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     char ch[100];
62     f.getline(ch, 100);
63     string result = ch;
64     auto ret = result.find("this is ut");
65     if (ret != string::npos) {
66         f.close();
67         unlink("/data/updater/m_log.txt");
68         unlink("/data/updater/m_stage.txt");
69         EXPECT_NE(ret, string::npos);
70         SUCCEED();
71     } else {
72         f.close();
73         unlink("/data/updater/m_log.txt");
74         unlink("/data/updater/m_stage.txt");
75         FAIL();
76     }
77 }
78 } // namespace updater_ut
79