Fix build break with boost 1.71.0
[platform/core/security/key-manager.git] / tests / test_exception.cpp
1 /*
2  *  Copyright (c) 2000 - 2017 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 <exception.h>
17
18 #include <boost/test/unit_test.hpp>
19 #include <string>
20
21 using namespace CKM;
22
23 #define CHECK_EXCEPTION(exception, ec, isDebug) \
24 do { \
25         const std::string errmsg = "test exception string"; \
26         BOOST_REQUIRE_THROW(ThrowErr(exception, errmsg), exception); \
27         try { \
28                 ThrowErr(exception, errmsg); \
29         } catch (const exception &e) { \
30                 checkExceptionInternal(e, ec, isDebug ? std::string() : errmsg); \
31         } \
32 } while (false)
33
34 namespace {
35
36 void checkExceptionInternal(const Exc::Exception &e, int ec, const std::string &msg)
37 {
38         BOOST_REQUIRE_MESSAGE(e.error() == ec,
39                 "ec(" << ec << ") not matched(" << e.error() << ")");
40
41         BOOST_REQUIRE_MESSAGE(msg == e.what(),
42                 "msg(" << msg << ") isn't matched(" << e.what() << ")");
43
44         BOOST_REQUIRE_MESSAGE(e.message().find(msg) != std::string::npos,
45                 "msg(" << msg << ") isn't contained from message("
46                 << e.message() << ")");
47 }
48
49 } // namespace anonymous
50
51 BOOST_AUTO_TEST_SUITE(EXCEPTION_TEST)
52
53 BOOST_AUTO_TEST_CASE(internal_error)
54 {
55         CHECK_EXCEPTION(Exc::InternalError, CKM_API_ERROR_SERVER_ERROR, false);
56 }
57
58 BOOST_AUTO_TEST_CASE(database_locked)
59 {
60         CHECK_EXCEPTION(Exc::DatabaseLocked, CKM_API_ERROR_DB_LOCKED, false);
61 }
62
63 BOOST_AUTO_TEST_CASE(database_failed)
64 {
65         CHECK_EXCEPTION(Exc::DatabaseFailed, CKM_API_ERROR_DB_ERROR, false);
66 }
67
68 BOOST_AUTO_TEST_CASE(filesystem_failed)
69 {
70         CHECK_EXCEPTION(Exc::FileSystemFailed, CKM_API_ERROR_FILE_SYSTEM, false);
71 }
72
73 BOOST_AUTO_TEST_CASE(inputparam)
74 {
75         CHECK_EXCEPTION(Exc::InputParam, CKM_API_ERROR_INPUT_PARAM, true);
76 }
77
78 BOOST_AUTO_TEST_CASE(authentication_failed)
79 {
80         CHECK_EXCEPTION(Exc::AuthenticationFailed, CKM_API_ERROR_AUTHENTICATION_FAILED, true);
81 }
82
83 BOOST_AUTO_TEST_CASE(transaction_failed)
84 {
85         CHECK_EXCEPTION(Exc::TransactionFailed, CKM_API_ERROR_DB_ERROR, false);
86 }
87
88 BOOST_AUTO_TEST_SUITE_END()