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 16 #include <array> 17 #include <iostream> 18 #include <string> 19 20 #include <gtest/gtest.h> 21 #include <properties.h> 22 #include <hilog/log.h> 23 24 #include "log_utils.h" 25 26 using namespace testing::ext; 27 using namespace std::chrono_literals; 28 using namespace OHOS::HiviewDFX; 29 30 namespace { 31 constexpr uint32_t QUERY_INTERVAL = 100000; // sleep 0.1s 32 class PropertiesTest : public testing::Test { 33 public: SetUpTestCase()34 static void SetUpTestCase() {} TearDownTestCase()35 static void TearDownTestCase() {} SetUp()36 void SetUp() {} TearDown()37 void TearDown() {} 38 }; 39 40 HWTEST_F(PropertiesTest, SwitchTest, TestSize.Level1) 41 { 42 SetPrivateSwitchOn(true); 43 SetProcessSwitchOn(true); 44 SetDomainSwitchOn(true); 45 SetOnceDebugOn(true); 46 SetKmsgSwitchOn(true); 47 SetPersistDebugOn(true); 48 49 usleep(QUERY_INTERVAL); 50 EXPECT_TRUE(IsDebugOn()); 51 EXPECT_TRUE(IsOnceDebugOn()); 52 EXPECT_TRUE(IsPersistDebugOn()); 53 EXPECT_TRUE(IsPrivateSwitchOn()); 54 EXPECT_TRUE(IsProcessSwitchOn()); 55 EXPECT_TRUE(IsDomainSwitchOn()); 56 EXPECT_TRUE(IsKmsgSwitchOn()); 57 58 SetPrivateSwitchOn(false); 59 SetProcessSwitchOn(false); 60 SetDomainSwitchOn(false); 61 SetOnceDebugOn(false); 62 SetKmsgSwitchOn(false); 63 SetPersistDebugOn(false); 64 65 usleep(QUERY_INTERVAL); 66 EXPECT_FALSE(IsDebugOn()); 67 EXPECT_FALSE(IsOnceDebugOn()); 68 EXPECT_FALSE(IsPersistDebugOn()); 69 EXPECT_FALSE(IsPrivateSwitchOn()); 70 EXPECT_FALSE(IsProcessSwitchOn()); 71 EXPECT_FALSE(IsDomainSwitchOn()); 72 EXPECT_FALSE(IsKmsgSwitchOn()); 73 74 SetOnceDebugOn(true); 75 SetPersistDebugOn(false); 76 usleep(QUERY_INTERVAL); 77 EXPECT_TRUE(IsDebugOn()); 78 79 SetOnceDebugOn(false); 80 SetPersistDebugOn(true); 81 usleep(QUERY_INTERVAL); 82 EXPECT_TRUE(IsDebugOn()); 83 } 84 85 HWTEST_F(PropertiesTest, LevelTest, TestSize.Level1) 86 { 87 static const std::array<const char*, 10> charLevels = {"d", "D", "f", "F", "e", "E", "w", "W", "i", "I"}; 88 static const std::array<uint16_t, 10> expected = { 89 LOG_DEBUG, LOG_DEBUG, 90 LOG_FATAL, LOG_FATAL, 91 LOG_ERROR, LOG_ERROR, 92 LOG_WARN, LOG_WARN, 93 LOG_INFO, LOG_INFO, 94 }; 95 96 for (size_t i = 0; i < charLevels.size(); ++i) { 97 SetGlobalLevel(ShortStr2LogLevel(charLevels[i])); 98 usleep(QUERY_INTERVAL); 99 EXPECT_EQ(GetGlobalLevel(), expected[i]); 100 } 101 102 uint32_t domain = 12345; 103 for (size_t i = 0; i < charLevels.size(); ++i) { 104 SetDomainLevel(domain, ShortStr2LogLevel(charLevels[i])); 105 usleep(QUERY_INTERVAL); 106 EXPECT_EQ(GetDomainLevel(domain), expected[i]); 107 } 108 109 std::string tag = "test_tag"; 110 for (size_t i = 0; i < charLevels.size(); ++i) { 111 SetTagLevel(tag, ShortStr2LogLevel(charLevels[i])); 112 usleep(QUERY_INTERVAL); 113 EXPECT_EQ(GetTagLevel(tag), expected[i]); 114 } 115 } 116 117 HWTEST_F(PropertiesTest, BufferTest, TestSize.Level1) 118 { 119 static const std::array<uint16_t, 7> logType = {-1, 0, 1, 3, 4, 5, 100}; 120 static const size_t size = 512 * 1024; 121 static const std::array<size_t, 7> expectedSize = {0, size, size, size, size, size, 0}; 122 123 for (size_t i = 0; i < logType.size(); ++i) { 124 SetBufferSize(logType[i], false, size); 125 EXPECT_EQ(GetBufferSize(logType[i], false), expectedSize[i]); 126 } 127 } 128 } // namespace 129