Release 0.1.66
[platform/core/security/key-manager.git] / unit-tests / test_log-provider.cpp
1 /*
2  *  Copyright (c) 2017 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16 #include <cstdlib>
17
18 #include <boost_macros_wrapper.h>
19
20 #include <dpl/log/old_style_log_provider.h>
21 #include <dpl/log/journal_log_provider.h>
22 #include <dpl/log/dlog_log_provider.h>
23 #include <dpl/log/log.h>
24
25 using namespace CKM;
26 using namespace CKM::Log;
27
28 namespace {
29
30 std::vector<AbstractLogProvider::LogLevel> levels({
31         AbstractLogProvider::LogLevel::Error,
32         AbstractLogProvider::LogLevel::Warning,
33         AbstractLogProvider::LogLevel::Info,
34         AbstractLogProvider::LogLevel::Debug,
35         AbstractLogProvider::LogLevel::Pedantic,
36         static_cast<AbstractLogProvider::LogLevel>(999)
37 });
38
39 void testProvider(AbstractLogProvider &provider)
40 {
41         for (auto level : levels)
42                 BOOST_REQUIRE_NO_THROW(provider.Log(
43                         level, "message", __FILE__, __LINE__, __func__));
44
45         BOOST_REQUIRE_NO_THROW(provider.SetTag("tag"));
46 }
47
48 constexpr AbstractLogProvider::LogLevel trimLogLevel(AbstractLogProvider::LogLevel level)
49 {
50 #ifndef BUILD_TYPE_DEBUG
51         if (level > AbstractLogProvider::LogLevel::Error)
52                 level = AbstractLogProvider::LogLevel::Error;
53 #endif
54         return level;
55 }
56
57 constexpr AbstractLogProvider::LogLevel defaultLogLevel()
58 {
59 #ifdef BUILD_TYPE_DEBUG
60         return AbstractLogProvider::LogLevel::Debug;
61 #else
62         return  AbstractLogProvider::LogLevel::Error;
63 #endif
64 }
65
66
67 class Env
68 {
69 public:
70         explicit Env(const std::string& _name) : name(_name)
71         {
72                 char* val = getenv(name.c_str());
73                 if (val)
74                         backup = val;
75         }
76
77         void Set(const std::string& value)
78         {
79                 BOOST_REQUIRE(0 == setenv(name.c_str(), value.c_str(), 1));
80         }
81
82         void Unset()
83         {
84                 BOOST_REQUIRE(0 == unsetenv(name.c_str()));
85         }
86
87         ~Env()
88         {
89                 if (backup.empty())
90                         BOOST_CHECK(0 == unsetenv(name.c_str()));
91                 else
92                         BOOST_CHECK(0 == setenv(name.c_str(), backup.c_str(), 1));
93         }
94 private:
95         std::string name;
96         std::string backup;
97 };
98
99 } // namespace anonymous
100
101 BOOST_AUTO_TEST_SUITE(LOG_PROVIDER_TEST)
102
103 POSITIVE_TEST_CASE(oldstyle_backend)
104 {
105         OldStyleLogProvider provider;
106
107         testProvider(provider);
108 }
109
110 POSITIVE_TEST_CASE(journal_backend)
111 {
112         JournalLogProvider provider;
113
114         testProvider(provider);
115 }
116
117 POSITIVE_TEST_CASE(dlog_backend)
118 {
119         DLOGLogProvider provider;
120
121         testProvider(provider);
122 }
123
124 POSITIVE_TEST_CASE(log_system)
125 {
126         LogSystem system;
127
128         system.AddProvider(new OldStyleLogProvider);
129         system.AddProvider(new JournalLogProvider);
130         system.AddProvider(new DLOGLogProvider);
131
132         for (auto level : levels)
133                 BOOST_REQUIRE_NO_THROW(system.Log(
134                         level, "message", __FILE__, __LINE__, __func__));
135
136         BOOST_REQUIRE_NO_THROW(system.SetTag("Test"));
137
138         BOOST_REQUIRE_NO_THROW(system.SetLogLevel("5"));
139         BOOST_REQUIRE(system.GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::Pedantic));
140         BOOST_REQUIRE_NO_THROW(system.SetLogLevel("4"));
141         BOOST_REQUIRE(system.GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::Debug));
142         BOOST_REQUIRE_NO_THROW(system.SetLogLevel("3"));
143         BOOST_REQUIRE(system.GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::Info));
144         BOOST_REQUIRE_NO_THROW(system.SetLogLevel("2"));
145         BOOST_REQUIRE(system.GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::Warning));
146         BOOST_REQUIRE_NO_THROW(system.SetLogLevel("1"));
147         BOOST_REQUIRE(system.GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::Error));
148         BOOST_REQUIRE_NO_THROW(system.SetLogLevel("0"));
149         BOOST_REQUIRE(system.GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::None));
150
151         BOOST_REQUIRE_NO_THROW(system.SelectProvider("DLOG"));
152         BOOST_REQUIRE_NO_THROW(system.SelectProvider("JOURNALD"));
153 }
154
155 NEGATIVE_TEST_CASE(log_system)
156 {
157         LogSystem system;
158
159         BOOST_REQUIRE_NO_THROW(system.SetLogLevel(""));
160         BOOST_REQUIRE(system.GetLogLevel() == defaultLogLevel());
161         BOOST_REQUIRE_NO_THROW(system.SetLogLevel("whatever"));
162         BOOST_REQUIRE(system.GetLogLevel() == defaultLogLevel());
163
164         BOOST_REQUIRE_THROW(system.SelectProvider("WRONG_PROVIDER"), std::out_of_range);
165         BOOST_REQUIRE_THROW(system.SelectProvider(""), std::out_of_range);
166 }
167
168 POSITIVE_TEST_CASE(env_log_provider)
169 {
170         Env env("CKM_LOG_PROVIDER");
171
172         env.Set("CONSOLE");
173         BOOST_REQUIRE_NO_THROW(LogSystem());
174         env.Set("DLOG");
175         BOOST_REQUIRE_NO_THROW(LogSystem());
176         env.Set("JOURNALD");
177         BOOST_REQUIRE_NO_THROW(LogSystem());
178 }
179
180 NEGATIVE_TEST_CASE(env_log_provider)
181 {
182         Env env("CKM_LOG_PROVIDER");
183
184         env.Unset();
185         BOOST_REQUIRE_NO_THROW(LogSystem());
186         env.Set("");
187         BOOST_REQUIRE_NO_THROW(LogSystem());
188         env.Set("WRONG_PROVIDER");
189         BOOST_REQUIRE_NO_THROW(LogSystem());
190 }
191
192 POSITIVE_TEST_CASE(env_log_level)
193 {
194         Env env("CKM_LOG_LEVEL");
195
196         typedef std::underlying_type<AbstractLogProvider::LogLevel>::type underlying;
197         for (underlying i = 0; i < 6; i++) {
198                 std::stringstream ss;
199                 ss << i;
200                 env.Set(ss.str().c_str());
201
202                 auto level = trimLogLevel(static_cast<AbstractLogProvider::LogLevel>(i));
203                 BOOST_REQUIRE(LogSystem().GetLogLevel() == level);
204         }
205 }
206
207 NEGATIVE_TEST_CASE(env_log_level)
208 {
209         Env env("CKM_LOG_LEVEL");
210
211         env.Unset();
212         BOOST_REQUIRE(LogSystem().GetLogLevel() == defaultLogLevel());
213
214         env.Set("");
215         BOOST_REQUIRE(LogSystem().GetLogLevel() == defaultLogLevel());
216
217         env.Set("-1");
218         BOOST_REQUIRE(LogSystem().GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::None));
219
220         env.Set("6");
221         BOOST_REQUIRE(LogSystem().GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::Pedantic));
222
223         env.Set("WRONG_LEVEL");
224         BOOST_REQUIRE(LogSystem().GetLogLevel() == defaultLogLevel());
225 }
226
227
228 BOOST_AUTO_TEST_SUITE_END()