12498b56bSopenharmony_ci/*
22498b56bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
32498b56bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
42498b56bSopenharmony_ci * you may not use this file except in compliance with the License.
52498b56bSopenharmony_ci * You may obtain a copy of the License at
62498b56bSopenharmony_ci *
72498b56bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
82498b56bSopenharmony_ci *
92498b56bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
102498b56bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
112498b56bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
122498b56bSopenharmony_ci * See the License for the specific language governing permissions and
132498b56bSopenharmony_ci * limitations under the License.
142498b56bSopenharmony_ci */
152498b56bSopenharmony_ci
162498b56bSopenharmony_ci#include <array>
172498b56bSopenharmony_ci#include <iostream>
182498b56bSopenharmony_ci#include <string>
192498b56bSopenharmony_ci
202498b56bSopenharmony_ci#include <gtest/gtest.h>
212498b56bSopenharmony_ci#include <properties.h>
222498b56bSopenharmony_ci#include <hilog/log.h>
232498b56bSopenharmony_ci
242498b56bSopenharmony_ci#include "log_utils.h"
252498b56bSopenharmony_ci
262498b56bSopenharmony_ciusing namespace testing::ext;
272498b56bSopenharmony_ciusing namespace std::chrono_literals;
282498b56bSopenharmony_ciusing namespace OHOS::HiviewDFX;
292498b56bSopenharmony_ci
302498b56bSopenharmony_cinamespace {
312498b56bSopenharmony_ciconstexpr uint32_t QUERY_INTERVAL = 100000; // sleep 0.1s
322498b56bSopenharmony_ciclass PropertiesTest : public testing::Test {
332498b56bSopenharmony_cipublic:
342498b56bSopenharmony_ci    static void SetUpTestCase() {}
352498b56bSopenharmony_ci    static void TearDownTestCase() {}
362498b56bSopenharmony_ci    void SetUp() {}
372498b56bSopenharmony_ci    void TearDown() {}
382498b56bSopenharmony_ci};
392498b56bSopenharmony_ci
402498b56bSopenharmony_ciHWTEST_F(PropertiesTest, SwitchTest, TestSize.Level1)
412498b56bSopenharmony_ci{
422498b56bSopenharmony_ci    SetPrivateSwitchOn(true);
432498b56bSopenharmony_ci    SetProcessSwitchOn(true);
442498b56bSopenharmony_ci    SetDomainSwitchOn(true);
452498b56bSopenharmony_ci    SetOnceDebugOn(true);
462498b56bSopenharmony_ci    SetKmsgSwitchOn(true);
472498b56bSopenharmony_ci    SetPersistDebugOn(true);
482498b56bSopenharmony_ci
492498b56bSopenharmony_ci    usleep(QUERY_INTERVAL);
502498b56bSopenharmony_ci    EXPECT_TRUE(IsDebugOn());
512498b56bSopenharmony_ci    EXPECT_TRUE(IsOnceDebugOn());
522498b56bSopenharmony_ci    EXPECT_TRUE(IsPersistDebugOn());
532498b56bSopenharmony_ci    EXPECT_TRUE(IsPrivateSwitchOn());
542498b56bSopenharmony_ci    EXPECT_TRUE(IsProcessSwitchOn());
552498b56bSopenharmony_ci    EXPECT_TRUE(IsDomainSwitchOn());
562498b56bSopenharmony_ci    EXPECT_TRUE(IsKmsgSwitchOn());
572498b56bSopenharmony_ci
582498b56bSopenharmony_ci    SetPrivateSwitchOn(false);
592498b56bSopenharmony_ci    SetProcessSwitchOn(false);
602498b56bSopenharmony_ci    SetDomainSwitchOn(false);
612498b56bSopenharmony_ci    SetOnceDebugOn(false);
622498b56bSopenharmony_ci    SetKmsgSwitchOn(false);
632498b56bSopenharmony_ci    SetPersistDebugOn(false);
642498b56bSopenharmony_ci
652498b56bSopenharmony_ci    usleep(QUERY_INTERVAL);
662498b56bSopenharmony_ci    EXPECT_FALSE(IsDebugOn());
672498b56bSopenharmony_ci    EXPECT_FALSE(IsOnceDebugOn());
682498b56bSopenharmony_ci    EXPECT_FALSE(IsPersistDebugOn());
692498b56bSopenharmony_ci    EXPECT_FALSE(IsPrivateSwitchOn());
702498b56bSopenharmony_ci    EXPECT_FALSE(IsProcessSwitchOn());
712498b56bSopenharmony_ci    EXPECT_FALSE(IsDomainSwitchOn());
722498b56bSopenharmony_ci    EXPECT_FALSE(IsKmsgSwitchOn());
732498b56bSopenharmony_ci
742498b56bSopenharmony_ci    SetOnceDebugOn(true);
752498b56bSopenharmony_ci    SetPersistDebugOn(false);
762498b56bSopenharmony_ci    usleep(QUERY_INTERVAL);
772498b56bSopenharmony_ci    EXPECT_TRUE(IsDebugOn());
782498b56bSopenharmony_ci
792498b56bSopenharmony_ci    SetOnceDebugOn(false);
802498b56bSopenharmony_ci    SetPersistDebugOn(true);
812498b56bSopenharmony_ci    usleep(QUERY_INTERVAL);
822498b56bSopenharmony_ci    EXPECT_TRUE(IsDebugOn());
832498b56bSopenharmony_ci}
842498b56bSopenharmony_ci
852498b56bSopenharmony_ciHWTEST_F(PropertiesTest, LevelTest, TestSize.Level1)
862498b56bSopenharmony_ci{
872498b56bSopenharmony_ci    static const std::array<const char*, 10> charLevels = {"d", "D", "f", "F", "e", "E", "w", "W", "i", "I"};
882498b56bSopenharmony_ci    static const std::array<uint16_t, 10> expected = {
892498b56bSopenharmony_ci        LOG_DEBUG, LOG_DEBUG,
902498b56bSopenharmony_ci        LOG_FATAL, LOG_FATAL,
912498b56bSopenharmony_ci        LOG_ERROR, LOG_ERROR,
922498b56bSopenharmony_ci        LOG_WARN, LOG_WARN,
932498b56bSopenharmony_ci        LOG_INFO, LOG_INFO,
942498b56bSopenharmony_ci    };
952498b56bSopenharmony_ci
962498b56bSopenharmony_ci    for (size_t i = 0; i < charLevels.size(); ++i) {
972498b56bSopenharmony_ci        SetGlobalLevel(ShortStr2LogLevel(charLevels[i]));
982498b56bSopenharmony_ci        usleep(QUERY_INTERVAL);
992498b56bSopenharmony_ci        EXPECT_EQ(GetGlobalLevel(), expected[i]);
1002498b56bSopenharmony_ci    }
1012498b56bSopenharmony_ci
1022498b56bSopenharmony_ci    uint32_t domain = 12345;
1032498b56bSopenharmony_ci    for (size_t i = 0; i < charLevels.size(); ++i) {
1042498b56bSopenharmony_ci        SetDomainLevel(domain, ShortStr2LogLevel(charLevels[i]));
1052498b56bSopenharmony_ci        usleep(QUERY_INTERVAL);
1062498b56bSopenharmony_ci        EXPECT_EQ(GetDomainLevel(domain), expected[i]);
1072498b56bSopenharmony_ci    }
1082498b56bSopenharmony_ci
1092498b56bSopenharmony_ci    std::string tag = "test_tag";
1102498b56bSopenharmony_ci    for (size_t i = 0; i < charLevels.size(); ++i) {
1112498b56bSopenharmony_ci        SetTagLevel(tag, ShortStr2LogLevel(charLevels[i]));
1122498b56bSopenharmony_ci        usleep(QUERY_INTERVAL);
1132498b56bSopenharmony_ci        EXPECT_EQ(GetTagLevel(tag), expected[i]);
1142498b56bSopenharmony_ci    }
1152498b56bSopenharmony_ci}
1162498b56bSopenharmony_ci
1172498b56bSopenharmony_ciHWTEST_F(PropertiesTest, BufferTest, TestSize.Level1)
1182498b56bSopenharmony_ci{
1192498b56bSopenharmony_ci    static const std::array<uint16_t, 7> logType = {-1, 0, 1, 3, 4, 5, 100};
1202498b56bSopenharmony_ci    static const size_t size = 512 * 1024;
1212498b56bSopenharmony_ci    static const std::array<size_t, 7> expectedSize = {0, size, size, size, size, size, 0};
1222498b56bSopenharmony_ci
1232498b56bSopenharmony_ci    for (size_t i = 0; i < logType.size(); ++i) {
1242498b56bSopenharmony_ci        SetBufferSize(logType[i], false, size);
1252498b56bSopenharmony_ci        EXPECT_EQ(GetBufferSize(logType[i], false), expectedSize[i]);
1262498b56bSopenharmony_ci    }
1272498b56bSopenharmony_ci}
1282498b56bSopenharmony_ci} // namespace
129