• 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     SetLogLevel(ERROR);
40     LOG(ERROR) << "this is ut";
41     STAGE(UPDATE_STAGE_BEGIN) << "this is ut";
42     InitUpdaterLogger("UPDATER_UT", "/data/updater/m_log.txt", "/data/updater/m_stage.txt", "/data/updater/m_code.txt");
43 
44     LOG(ERROR) << "this is ut";
45     STAGE(UPDATE_STAGE_BEGIN) << "this is ut";
46     ERROR_CODE(CODE_VERIFY_FAIL);
47     fstream f;
48     f.open("/data/updater/m_log.txt", ios::in);
49     char ch[100];
50     f.getline(ch, 100);
51     string result = ch;
52     if (result.find("this is ut") != string::npos) {
53         f.close();
54         unlink("/data/updater/m_log.txt");
55         unlink("/data/updater/m_stage.txt");
56         SUCCEED();
57     } else {
58         f.close();
59         unlink("/data/updater/m_log.txt");
60         unlink("/data/updater/m_stage.txt");
61         FAIL();
62     }
63 }
64 
65 HWTEST_F(LogUnitTest, log_test_002, TestSize.Level0)
66 {
67     InitUpdaterLogger("UPDATER_UT", "", "", "");
68     SetLogLevel(ERROR);
69     LOG(ERROR) << "this is ut";
70     STAGE(UPDATE_STAGE_BEGIN) << "this is ut";
71     ERROR_CODE(CODE_VERIFY_FAIL);
72     SUCCEED();
73 
74     SetLogLevel(INFO);
75     LOG(ERROR) << "this is ut";
76     STAGE(UPDATE_STAGE_BEGIN) << "this is ut";
77     SUCCEED();
78 }
79 } // namespace updater_ut
80