Release 0.1.66
[platform/core/security/key-manager.git] / unit-tests / test_exception.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 <exception.h>
17
18 #include <boost_macros_wrapper.h>
19 #include <string>
20
21 using namespace CKM;
22
23 namespace {
24
25 template <int Error, bool IsDebug, typename Before, typename After>
26 void verifyCatched(const Exc::DefineException<Error, IsDebug, Before, After>& e,
27                    const std::string errmsg)
28 {
29         BOOST_REQUIRE_MESSAGE(e.error() == Error,
30                                                   "ec(" << Error << ") not matched(" << e.error() << ")");
31
32         if (IsDebug) {
33                 BOOST_REQUIRE_MESSAGE(std::string() == e.what(),
34                                                           "expected empty e.what(), got(" << e.what() << ")");
35         } else {
36                 BOOST_REQUIRE_MESSAGE(errmsg == e.what(),
37                                                           "msg(" << errmsg << ") isn't matched(" << e.what() << ")");
38
39                 BOOST_REQUIRE_MESSAGE(e.message().find(errmsg) != std::string::npos,
40                                                           "msg(" << errmsg << ") isn't contained from message("
41                                                           << e.message() << ")");
42         }
43 }
44
45 template <typename Exc>
46 void checkException()
47 {
48         const std::string errmsg = "test exception string";
49         try {
50                 ThrowErr(Exc, errmsg);
51                 BOOST_FAIL("No exception thrown");
52         } catch (const Exc &e) {
53                 verifyCatched(e ,errmsg);
54         }
55 }
56
57 } // namespace anonymous
58
59 BOOST_AUTO_TEST_SUITE(EXCEPTION_TEST)
60
61 POSITIVE_TEST_CASE(check_exceptions)
62 {
63         checkException<Exc::InternalError>();
64         checkException<Exc::DatabaseLocked>();
65         checkException<Exc::DatabaseFailed>();
66         checkException<Exc::FileSystemFailed>();
67         checkException<Exc::InputParam>();
68         checkException<Exc::AuthenticationFailed>();
69         checkException<Exc::VerificationFailed>();
70         checkException<Exc::InvalidFormat>();
71         checkException<Exc::BadResponse>();
72         checkException<Exc::TransactionFailed>();
73 }
74
75 BOOST_AUTO_TEST_SUITE_END()