38c17dd9d0386633e7ba1991e6cdd015edf995d2
[platform/core/test/security-tests.git] / src / cynara-tests / common / cynara_test_commons.cpp
1 /*
2  * Copyright (c) 2014-2015 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
17 #include <cynara_test_commons.h>
18 #include <cynara_test_env.h>
19 #include <cynara_test_file_operations.h>
20 #include <cynara_test_cynara_mask.h>
21
22 #include <dpl/exception.h>
23 #include <dpl/test/test_exception.h>
24
25 #include <exception>
26
27 namespace CynaraTestConsts
28 {
29
30 const std::string DB_DIR(CYNARA_DB_DIR);
31 const std::string USER("cynara");
32 const std::string LABEL("System");
33 const std::string SERVICE("cynara.service");
34 const std::string SOCKET_CLIENT("cynara.socket");
35 const std::string SOCKET_ADMIN("cynara-admin.socket");
36 const std::string SOCKET_AGENT("cynara-agent.socket");
37
38 const std::string SERVICE_PLUGINS_DIR("/usr/lib/cynara/plugin/service/");
39
40 }
41
42 void environmentWrap(const char *testName, const std::function<void(void)> &func)
43 {
44     CynaraTestEnv env(testName);
45     env.save();
46     env.loadDefaultDatabase();
47
48     try {
49         func();
50     } catch (const DPL::Test::TestException &e) {
51         env.restore();
52         throw;
53     } catch (const DPL::Exception &e) {
54         env.restore();
55         throw;
56     } catch (const std::exception &e) {
57         env.restore();
58         throw;
59     } catch (...) {
60         env.restore();
61         throw std::runtime_error("Unknown exception");
62     }
63     env.restore();
64 }
65
66 void loadServicePlugins(const DirectoryPaths &pluginDirectories)
67 {
68     CynaraMask mask;
69
70     FileOperations::removeDirFiles(CynaraTestConsts::SERVICE_PLUGINS_DIR);
71     for (const auto &dir : pluginDirectories)
72         FileOperations::copyCynaraFiles(dir.c_str(), CynaraTestConsts::SERVICE_PLUGINS_DIR);
73 }
74
75 void restartCynaraService()
76 {
77     ServiceManager service(CynaraTestConsts::SERVICE);
78     service.restartService();
79 }
80
81 void restartCynaraServiceAndSockets()
82 {
83     ServiceManager service(CynaraTestConsts::SERVICE, { CynaraTestConsts::SOCKET_CLIENT,
84                                                         CynaraTestConsts::SOCKET_ADMIN,
85                                                         CynaraTestConsts::SOCKET_AGENT });
86
87     service.restartService(true);
88 }