Spring cleaning
[platform/core/test/security-tests.git] / src / cynara-tests / common / cynara_test_env.cpp
1 /*
2  * Copyright (c) 2014-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
17 #include <cynara_test_commons.h>
18 #include <cynara_test_cynara_mask.h>
19 #include <cynara_test_file_operations.h>
20 #include <tests_common.h>
21
22 #include <cynara_test_env.h>
23
24 #include <sys/mount.h>
25
26 using namespace FileOperations;
27
28 CynaraTestEnv::CynaraTestEnv()
29     : m_dbPresent(false)
30 {
31 }
32
33 CynaraTestEnv::~CynaraTestEnv()
34 {
35 }
36
37 void CynaraTestEnv::init(const std::string &testName)
38 {
39     m_saveDir = "/tmp/" + testName;
40     m_dbSaveDir = m_saveDir + "/db";
41     m_defaultDir = "/etc/security-tests/db_patterns/default";
42     m_pluginsToInstallDir = "/opt/plugins_to_install";
43
44     CynaraMask mask;
45
46     removeDirFiles(m_dbSaveDir);
47     removeDirIfExists(m_dbSaveDir);
48     removeDirIfExists(m_saveDir);
49
50     makeDir(m_saveDir);
51     makeDir(m_pluginsToInstallDir);
52
53     int res = mount(m_pluginsToInstallDir.c_str(), CynaraTestConsts::SERVICE_PLUGINS_DIR.c_str(), NULL, MS_BIND, NULL);
54     RUNNER_ASSERT_ERRNO_MSG(res == 0, "Bind mount failed");
55
56     m_dbPresent = dirExists(CynaraTestConsts::DB_DIR);
57     if (m_dbPresent) {
58         makeDir(m_dbSaveDir);
59         copyCynaraFiles(CynaraTestConsts::DB_DIR, m_dbSaveDir);
60     }
61     unmaskedLoadDefaultDatabase();
62 }
63
64 void CynaraTestEnv::finish()
65 {
66     CynaraMask mask;
67
68     removeDirFiles(CynaraTestConsts::DB_DIR);
69     if (m_dbPresent)
70         copyCynaraFiles(m_dbSaveDir, CynaraTestConsts::DB_DIR);
71     else
72         removeDirIfExists(CynaraTestConsts::DB_DIR);
73
74     int res = umount(CynaraTestConsts::SERVICE_PLUGINS_DIR.c_str());
75     RUNNER_ASSERT_ERRNO_MSG(res == 0, "Unmounting failed");
76
77     removeDirFiles(m_dbSaveDir);
78     removeDirIfExists(m_dbSaveDir);
79     removeDirIfExists(m_saveDir);
80     removeDirFiles(m_pluginsToInstallDir);
81     removeDirIfExists(m_pluginsToInstallDir);
82 }
83
84 void CynaraTestEnv::unmaskedLoadDefaultDatabase()
85 {
86     if (m_dbPresent) {
87         removeDirFiles(CynaraTestConsts::DB_DIR);
88         copyCynaraFiles(m_defaultDir, CynaraTestConsts::DB_DIR);
89     }
90 }
91
92 void CynaraTestEnv::loadDefaultDatabase()
93 {
94     CynaraMask mask;
95     unmaskedLoadDefaultDatabase();
96 }