9201f8fcca3f8358919f60a04b4f7c2b669edd67
[platform/core/security/vasum.git] / tests / unit_tests / log / ut-logger.cpp
1 /*
2  *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Pawel Broda <p.broda@partner.samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18
19
20 /**
21  * @file
22  * @author  Pawel Broda (p.broda@partner.samsung.com)
23  * @brief   Unit tests of the log utility
24  */
25
26 #include "config.hpp"
27 #include "ut.hpp"
28 #include "logger/logger.hpp"
29 #include "logger/logger-scope.hpp"
30 #include "logger/formatter.hpp"
31 #include "logger/backend.hpp"
32 #include "logger/backend-stderr.hpp"
33
34 #include <stdexcept>
35
36 BOOST_AUTO_TEST_SUITE(LoggerSuite)
37
38 using namespace logger;
39
40 namespace {
41
42 class StubbedBackend : public LogBackend {
43 public:
44     StubbedBackend(std::ostringstream& s) : mLogStream(s) {}
45
46     // stubbed function
47     void log(LogLevel logLevel,
48              const std::string& file,
49              const unsigned int& line,
50              const std::string& func,
51              const std::string& message) override
52     {
53         mLogStream << '[' + toString(logLevel) + ']' + ' '
54                    << file + ':' + std::to_string(line) + ' ' + func + ':'
55                    << message << std::endl;
56     }
57
58 private:
59     std::ostringstream& mLogStream;
60 };
61
62 class TestLog {
63 public:
64     TestLog(LogLevel level)
65     {
66         Logger::setLogLevel(level);
67         Logger::setLogBackend(new StubbedBackend(mLogStream));
68     }
69
70     ~TestLog()
71     {
72         Logger::setLogLevel(LogLevel::TRACE);
73         Logger::setLogBackend(new StderrBackend());
74     }
75
76     // helpers
77     bool logContains(const std::string& expression) const
78     {
79         std::string s = mLogStream.str();
80         if (s.find(expression) != std::string::npos) {
81             return true;
82         }
83         return false;
84     }
85 private:
86     std::ostringstream mLogStream;
87 };
88
89 void exampleTestLogs(void)
90 {
91     LOGE("test log error " << "1");
92     LOGW("test log warn "  << "2");
93     LOGI("test log info "  << "3");
94     LOGD("test log debug " << "4");
95     LOGT("test log trace " << "5");
96 }
97
98 } // namespace
99
100 BOOST_AUTO_TEST_CASE(LogLevelSetAndGet)
101 {
102     Logger::setLogLevel(LogLevel::TRACE);
103     BOOST_CHECK(LogLevel::TRACE == Logger::getLogLevel());
104
105     Logger::setLogLevel(LogLevel::DEBUG);
106     BOOST_CHECK(LogLevel::DEBUG == Logger::getLogLevel());
107
108     Logger::setLogLevel(LogLevel::INFO);
109     BOOST_CHECK(LogLevel::INFO == Logger::getLogLevel());
110
111     Logger::setLogLevel(LogLevel::WARN);
112     BOOST_CHECK(LogLevel::WARN == Logger::getLogLevel());
113
114     Logger::setLogLevel(LogLevel::ERROR);
115     BOOST_CHECK(LogLevel::ERROR == Logger::getLogLevel());
116 }
117
118 BOOST_AUTO_TEST_CASE(StringLogLevelSetAndGet)
119 {
120     Logger::setLogLevel("TRACE");
121     BOOST_CHECK(LogLevel::TRACE == Logger::getLogLevel());
122
123     Logger::setLogLevel("traCE");
124     BOOST_CHECK(LogLevel::TRACE == Logger::getLogLevel());
125
126     Logger::setLogLevel("DEBUG");
127     BOOST_CHECK(LogLevel::DEBUG == Logger::getLogLevel());
128
129     Logger::setLogLevel("INFO");
130     BOOST_CHECK(LogLevel::INFO == Logger::getLogLevel());
131
132     Logger::setLogLevel("WARN");
133     BOOST_CHECK(LogLevel::WARN == Logger::getLogLevel());
134
135     Logger::setLogLevel("ERROR");
136     BOOST_CHECK(LogLevel::ERROR == Logger::getLogLevel());
137
138     BOOST_REQUIRE_EXCEPTION(Logger::setLogLevel("UNKNOWN"),
139                             std::runtime_error,
140                             WhatEquals("Invalid LogLevel to parse")); //TODO change message
141 }
142
143 BOOST_AUTO_TEST_CASE(LogsLevelError)
144 {
145     TestLog tf(LogLevel::ERROR);
146     exampleTestLogs();
147
148     BOOST_CHECK(tf.logContains("[ERROR]") == true);
149     BOOST_CHECK(tf.logContains("[WARN]")  == false);
150     BOOST_CHECK(tf.logContains("[INFO]")  == false);
151     BOOST_CHECK(tf.logContains("[DEBUG]") == false);
152     BOOST_CHECK(tf.logContains("[TRACE]") == false);
153 }
154
155 BOOST_AUTO_TEST_CASE(LogsLevelWarn)
156 {
157     TestLog tf(LogLevel::WARN);
158     exampleTestLogs();
159
160     BOOST_CHECK(tf.logContains("[ERROR]") == true);
161     BOOST_CHECK(tf.logContains("[WARN]")  == true);
162     BOOST_CHECK(tf.logContains("[INFO]")  == false);
163     BOOST_CHECK(tf.logContains("[DEBUG]") == false);
164     BOOST_CHECK(tf.logContains("[TRACE]") == false);
165 }
166
167 BOOST_AUTO_TEST_CASE(LogsLevelInfo)
168 {
169     TestLog tf(LogLevel::INFO);
170     exampleTestLogs();
171
172     BOOST_CHECK(tf.logContains("[ERROR]") == true);
173     BOOST_CHECK(tf.logContains("[WARN]")  == true);
174     BOOST_CHECK(tf.logContains("[INFO]")  == true);
175     BOOST_CHECK(tf.logContains("[DEBUG]") == false);
176     BOOST_CHECK(tf.logContains("[TRACE]") == false);
177 }
178
179 #if !defined(NDEBUG)
180 BOOST_AUTO_TEST_CASE(LogsLevelDebug)
181 {
182     TestLog tf(LogLevel::DEBUG);
183     exampleTestLogs();
184
185     BOOST_CHECK(tf.logContains("[ERROR]") == true);
186     BOOST_CHECK(tf.logContains("[WARN]")  == true);
187     BOOST_CHECK(tf.logContains("[INFO]")  == true);
188     BOOST_CHECK(tf.logContains("[DEBUG]") == true);
189     BOOST_CHECK(tf.logContains("[TRACE]") == false);
190 }
191
192 BOOST_AUTO_TEST_CASE(LogsLevelTrace)
193 {
194     TestLog tf(LogLevel::TRACE);
195     exampleTestLogs();
196
197     BOOST_CHECK(tf.logContains("[ERROR]") == true);
198     BOOST_CHECK(tf.logContains("[WARN]")  == true);
199     BOOST_CHECK(tf.logContains("[INFO]")  == true);
200     BOOST_CHECK(tf.logContains("[DEBUG]") == true);
201     BOOST_CHECK(tf.logContains("[TRACE]") == true);
202 }
203 #endif
204
205 BOOST_AUTO_TEST_CASE(LoggerScope)
206 {
207     LOGS("Main function scope");
208
209     {
210         LOGS("Scope inside function");
211         LOGD("Some additional information in-between scoped logs");
212         {
213             LOGS("Additional scope with " << "stringstream" << ' ' << "test" << 3 << ' ' << 3.42);
214             LOGD("More additional information in-between scoped logs");
215         }
216     }
217 }
218
219 BOOST_AUTO_TEST_SUITE_END()
220