ba3350a3e35096c7be84436693fd9d0c1778d125
[platform/core/test/security-tests.git] / src / cynara-tests / common / cynara_test_env.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_cynara_mask.h>
19 #include <cynara_test_file_operations.h>
20 #include <tests_common.h>
21
22 #include <cynara_test_env.h>
23
24 using namespace FileOperations;
25
26 CynaraTestEnv::CynaraTestEnv(const char *dirName)
27     : m_dbPresent(false)
28 {
29     m_saveDir = TMP_DIR + "/" + dirName;
30     m_dbSaveDir = m_saveDir + "/db";
31     m_pluginsSaveDir = m_saveDir + "/plugins";
32     m_defaultDir = "/etc/security-tests/db_patterns/default";
33 }
34
35 CynaraTestEnv::~CynaraTestEnv()
36 {
37 }
38
39 void CynaraTestEnv::save()
40 {
41     CynaraMask mask;
42
43     removeDirFiles(m_dbSaveDir);
44     removeDirIfExists(m_dbSaveDir);
45     removeDirFiles(m_pluginsSaveDir);
46     removeDirIfExists(m_pluginsSaveDir);
47     removeDirIfExists(m_saveDir);
48
49     makeDir(m_saveDir);
50     m_dbPresent = dirExists(CynaraTestConsts::DB_DIR);
51     if (m_dbPresent) {
52         makeDir(m_dbSaveDir);
53         copyCynaraFiles(CynaraTestConsts::DB_DIR, m_dbSaveDir);
54     }
55     makeDir(m_pluginsSaveDir);
56     copyCynaraFiles(CynaraTestConsts::SERVICE_PLUGINS_DIR, m_pluginsSaveDir);
57 }
58
59 void CynaraTestEnv::restore()
60 {
61     CynaraMask mask;
62
63     removeDirFiles(CynaraTestConsts::DB_DIR);
64     if (m_dbPresent)
65         copyCynaraFiles(m_dbSaveDir, CynaraTestConsts::DB_DIR);
66     else
67         removeDirIfExists(CynaraTestConsts::DB_DIR);
68
69     removeDirFiles(CynaraTestConsts::SERVICE_PLUGINS_DIR);
70     copyCynaraFiles(m_pluginsSaveDir, CynaraTestConsts::SERVICE_PLUGINS_DIR);
71
72     removeDirFiles(m_dbSaveDir);
73     removeDirIfExists(m_dbSaveDir);
74     removeDirFiles(m_pluginsSaveDir);
75     removeDirIfExists(m_pluginsSaveDir);
76     removeDirIfExists(m_saveDir);
77 }
78
79 void CynaraTestEnv::loadDefaultDatabase()
80 {
81     CynaraMask mask;
82
83     if (m_dbPresent) {
84         removeDirFiles(CynaraTestConsts::DB_DIR);
85         copyCynaraFiles(m_defaultDir, CynaraTestConsts::DB_DIR);
86     }
87 }