• 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 
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 using namespace testing::ext;
25 using namespace std::chrono_literals;
26 
27 namespace {
28 class PropertiesTest : public testing::Test {
29 public:
SetUpTestCase()30     static void SetUpTestCase() {}
TearDownTestCase()31     static void TearDownTestCase() {}
SetUp()32     void SetUp() {}
TearDown()33     void TearDown() {}
34 };
35 
36 
37 HWTEST_F(PropertiesTest, PropertiesTest1, TestSize.Level1)
38 {
39     std::string key = "my_property";
40     std::string value = "1234";
41     PropertySet(key, value.data());
42 
43     std::array<char, HILOG_PROP_VALUE_MAX - 1> prop = {0};
44     PropertyGet(key, prop.data(), prop.size());
45 
46     EXPECT_EQ(std::string(prop.data()), value);
47 }
48 
49 HWTEST_F(PropertiesTest, PropertiesTest2, TestSize.Level1)
50 {
51     std::string key = "my_property";
52     for (int i = 0; i < 10; ++i) {
53         std::string value = std::to_string(i);
54         PropertySet(key, value.data());
55 
56         std::array<char, HILOG_PROP_VALUE_MAX - 1> prop = {0};
57         PropertyGet(key, prop.data(), prop.size());
58 
59         EXPECT_EQ(std::string(prop.data()), value);
60     }
61 }
62 
63 HWTEST_F(PropertiesTest, PropertiesInvalidParamsTest, TestSize.Level1)
64 {
65     std::string key = "my_property";
66     std::string value = "5678";
67     PropertySet(key, value.data());
68 
69     PropertyGet(key, nullptr, 0);
70     PropertySet(key, nullptr);
71 
72     std::array<char, HILOG_PROP_VALUE_MAX - 1> prop = {0};
73     PropertyGet(key, prop.data(), prop.size());
74 
75     EXPECT_EQ(std::string(prop.data()), value);
76 }
77 
78 HWTEST_F(PropertiesTest, PropertiesTooLongBufferTest, TestSize.Level1)
79 {
80     std::string key = "my_property";
81     std::string value = "5678";
82     PropertySet(key, value.data());
83 
84     std::array<char, HILOG_PROP_VALUE_MAX> prop1 = {0};
85     PropertyGet(key, prop1.data(), prop1.size());
86 
87     std::string tooLongText = "0123456789";
88     while (tooLongText.length() < HILOG_PROP_VALUE_MAX) {
89         tooLongText += tooLongText;
90     }
91     PropertySet(key, tooLongText.data());
92 
93     std::array<char, HILOG_PROP_VALUE_MAX-1> prop = {0};
94     PropertyGet(key, prop.data(), prop.size());
95     std::string currentValue = prop.data();
96     EXPECT_EQ(currentValue, value);
97     EXPECT_NE(tooLongText, value);
98 }
99 
100 HWTEST_F(PropertiesTest, PropertiesNoKeyTest, TestSize.Level1)
101 {
102     std::string key = "PropertiesNoKeyTest";
103 
104     std::array<char, HILOG_PROP_VALUE_MAX - 1> prop = {0};
105     PropertyGet(key, prop.data(), prop.size());
106 
107     EXPECT_TRUE(std::string(prop.data()).empty());
108 }
109 
110 HWTEST_F(PropertiesTest, SwitchTest, TestSize.Level1)
111 {
112     PropertySet(GetPropertyName(PROP_PRIVATE), "true");
113     PropertySet(GetPropertyName(PROP_PROCESS_FLOWCTRL), "true");
114     PropertySet(GetPropertyName(PROP_DOMAIN_FLOWCTRL), "true");
115     PropertySet(GetPropertyName(PROP_SINGLE_DEBUG), "true");
116     PropertySet(GetPropertyName(PROP_KMSG), "true");
117     PropertySet(GetPropertyName(PROP_PERSIST_DEBUG), "true");
118 
119     EXPECT_TRUE(IsDebugOn());
120     EXPECT_TRUE(IsSingleDebugOn());
121     EXPECT_TRUE(IsPersistDebugOn());
122     EXPECT_TRUE(IsPrivateSwitchOn());
123     EXPECT_TRUE(IsProcessSwitchOn());
124     EXPECT_TRUE(IsDomainSwitchOn());
125     EXPECT_TRUE(IsKmsgSwitchOn());
126 
127     PropertySet(GetPropertyName(PROP_PRIVATE), "false");
128     PropertySet(GetPropertyName(PROP_PROCESS_FLOWCTRL), "false");
129     PropertySet(GetPropertyName(PROP_DOMAIN_FLOWCTRL), "false");
130     PropertySet(GetPropertyName(PROP_SINGLE_DEBUG), "false");
131     PropertySet(GetPropertyName(PROP_KMSG), "false");
132     PropertySet(GetPropertyName(PROP_PERSIST_DEBUG), "false");
133 
134     EXPECT_FALSE(IsDebugOn());
135     EXPECT_FALSE(IsSingleDebugOn());
136     EXPECT_FALSE(IsPersistDebugOn());
137     EXPECT_FALSE(IsPrivateSwitchOn());
138     EXPECT_FALSE(IsProcessSwitchOn());
139     EXPECT_FALSE(IsDomainSwitchOn());
140     EXPECT_FALSE(IsKmsgSwitchOn());
141 
142     PropertySet(GetPropertyName(PROP_SINGLE_DEBUG), "true");
143     PropertySet(GetPropertyName(PROP_PERSIST_DEBUG), "false");
144     EXPECT_TRUE(IsDebugOn());
145 
146     PropertySet(GetPropertyName(PROP_SINGLE_DEBUG), "false");
147     PropertySet(GetPropertyName(PROP_PERSIST_DEBUG), "true");
148     EXPECT_TRUE(IsDebugOn());
149 }
150 
151 HWTEST_F(PropertiesTest, LevelTest, TestSize.Level1)
152 {
153     static const std::array<const char*, 10> charLevels = {"d", "D", "f", "F", "e", "E", "w", "W", "i", "I"};
154     static const std::array<uint16_t, 10> expected = {
155         LOG_DEBUG, LOG_DEBUG,
156         LOG_FATAL, LOG_FATAL,
157         LOG_ERROR, LOG_ERROR,
158         LOG_WARN, LOG_WARN,
159         LOG_INFO, LOG_INFO,
160     };
161 
162     for (size_t i = 0; i < charLevels.size(); ++i) {
163         PropertySet(GetPropertyName(PROP_GLOBAL_LOG_LEVEL), charLevels[i]);
164         EXPECT_EQ(GetGlobalLevel(), expected[i]);
165     }
166     PropertySet(GetPropertyName(PROP_GLOBAL_LOG_LEVEL), "z");
167     EXPECT_EQ(GetGlobalLevel(), LOG_LEVEL_MIN);
168 
169     uint32_t domain = 12345;
170     for (size_t i = 0; i < charLevels.size(); ++i) {
171         PropertySet(GetPropertyName(PROP_DOMAIN_LOG_LEVEL) + std::to_string(domain), charLevels[i]);
172         EXPECT_EQ(GetDomainLevel(domain), expected[i]);
173     }
174     PropertySet(GetPropertyName(PROP_DOMAIN_LOG_LEVEL) + std::to_string(domain), "z");
175     EXPECT_EQ(GetDomainLevel(domain), LOG_LEVEL_MIN);
176 
177     std::string tag = "test_tag";
178     for (size_t i = 0; i < charLevels.size(); ++i) {
179         PropertySet(GetPropertyName(PROP_TAG_LOG_LEVEL) + tag, charLevels[i]);
180         EXPECT_EQ(GetTagLevel(tag), expected[i]);
181     }
182     PropertySet(GetPropertyName(PROP_TAG_LOG_LEVEL) + tag, "z");
183     EXPECT_EQ(GetTagLevel(tag), LOG_LEVEL_MIN);
184 }
185 } // namespace
186