change API header file names
[platform/upstream/csr-framework.git] / test / test-api-engine-manager.cpp
1 /*
2  *  Copyright (c) 2016 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  * @file        test-api-engine-manager.cpp
18  * @author      Kyungwook Tak (k.tak@samsung.com)
19  * @version     1.0
20  * @brief       CSR Engine manager API test
21  */
22 #include <csr-engine-manager.h>
23
24 #include <string>
25 #include <boost/test/unit_test.hpp>
26
27 #include "test-common.h"
28 #include "test-helper.h"
29 #include "test-resource.h"
30
31 namespace {
32
33 class Engine {
34 public:
35         Engine(csr_engine_id_e id)
36         {
37                 ASSERT_IF(csr_get_current_engine(id, &m_engine), CSR_ERROR_NONE);
38         }
39
40         ~Engine()
41         {
42                 ASSERT_IF(csr_engine_destroy(m_engine), CSR_ERROR_NONE);
43         }
44
45         csr_engine_h &get(void)
46         {
47                 return m_engine;
48         }
49
50 private:
51         csr_engine_h m_engine;
52 };
53
54 } // namespace anonymous
55
56 BOOST_AUTO_TEST_SUITE(API_ENGINE_MANAGER)
57 BOOST_AUTO_TEST_SUITE(CS)
58
59 BOOST_AUTO_TEST_CASE(fields_getters)
60 {
61         EXCEPTION_GUARD_START
62
63         Engine e(CSR_ENGINE_CS);
64
65         const char *vendor = nullptr;
66         ASSERT_IF(csr_engine_get_vendor(e.get(), &vendor), CSR_ERROR_NONE);
67         ASSERT_IF(std::string("TEST_VENDOR"), vendor);
68
69         const char *name = nullptr;
70         ASSERT_IF(csr_engine_get_name(e.get(), &name), CSR_ERROR_NONE);
71         ASSERT_IF(std::string("TEST_LOCAL_TCS_ENGINE"), name);
72
73         const char *version = nullptr;
74         ASSERT_IF(csr_engine_get_version(e.get(), &version), CSR_ERROR_NONE);
75         ASSERT_IF(std::string("0.0.1"), version);
76
77         const char *dataVersion = nullptr;
78         ASSERT_IF(csr_engine_get_version(e.get(), &dataVersion), CSR_ERROR_NONE);
79         ASSERT_IF(std::string("0.0.1"), dataVersion);
80
81         csr_activated_e activated;
82         ASSERT_IF(csr_engine_get_activated(e.get(), &activated), CSR_ERROR_NONE);
83         ASSERT_IF(activated, CSR_ACTIVATED);
84
85         csr_state_e state = CSR_ENABLE;
86         ASSERT_IF(csr_engine_set_state(e.get(), state), CSR_ERROR_NONE);
87
88         ASSERT_IF(csr_engine_get_state(e.get(), &state), CSR_ERROR_NONE);
89         ASSERT_IF(state, CSR_ENABLE);
90
91         EXCEPTION_GUARD_END
92 }
93
94 BOOST_AUTO_TEST_CASE(set_state)
95 {
96         EXCEPTION_GUARD_START
97
98         Engine e(CSR_ENGINE_CS);
99
100         csr_state_e state = CSR_ENABLE;
101
102         // enable
103         ASSERT_IF(csr_engine_set_state(e.get(), CSR_ENABLE), CSR_ERROR_NONE);
104         ASSERT_IF(csr_engine_get_state(e.get(), &state), CSR_ERROR_NONE);
105         ASSERT_IF(state, CSR_ENABLE);
106
107         // prepare data
108         auto c = Test::Context<csr_cs_context_h>();
109         auto context = c.get();
110         csr_cs_detected_h detected;
111         unsigned char data[100] = {0, };
112         //const char *files[1] = { TEST_FILE_NORMAL };
113         //const char *dirs[1] = { TEST_DIR };
114
115         // check if engine is working well
116         memcpy(data, MALWARE_HIGH_SIGNATURE, strlen(MALWARE_HIGH_SIGNATURE));
117         ASSERT_IF(csr_cs_scan_data(context, data, sizeof(data), &detected), CSR_ERROR_NONE);
118
119         // disable
120         ASSERT_IF(csr_engine_set_state(e.get(), CSR_DISABLE), CSR_ERROR_NONE);
121         ASSERT_IF(csr_engine_get_state(e.get(), &state), CSR_ERROR_NONE);
122         ASSERT_IF(state, CSR_DISABLE);
123
124         // test operation
125         ASSERT_IF(csr_cs_scan_data(context, data, sizeof(data), &detected), CSR_ERROR_ENGINE_DISABLED);
126         ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_NORMAL, &detected), CSR_ERROR_ENGINE_DISABLED);
127         //ASSERT_IF(csr_cs_scan_files_async(context, files, sizeof(files) / sizeof(const char *), nullptr),
128         //      CSR_ERROR_ENGINE_DISABLED);
129         //ASSERT_IF(csr_cs_scan_dir_async(context, TEST_DIR, nullptr), CSR_ERROR_ENGINE_DISABLED);
130         //ASSERT_IF(csr_cs_scan_dirs_async(context, dirs, sizeof(dirs) / sizeof(const char *), nullptr),
131         //      CSR_ERROR_ENGINE_DISABLED);
132
133         // enable
134         ASSERT_IF(csr_engine_set_state(e.get(), CSR_ENABLE), CSR_ERROR_NONE);
135         ASSERT_IF(csr_engine_get_state(e.get(), &state), CSR_ERROR_NONE);
136         ASSERT_IF(state, CSR_ENABLE);
137
138         // test operation
139         ASSERT_IF(csr_cs_scan_data(context, data, sizeof(data), &detected), CSR_ERROR_NONE);
140         ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_NORMAL, &detected), CSR_ERROR_NONE);
141         //ASSERT_IF(csr_cs_scan_files_async(context, files, sizeof(files) / sizeof(const char *), nullptr),
142         //      CSR_ERROR_NONE);
143         //ASSERT_IF(csr_cs_scan_dir_async(context, TEST_DIR, nullptr), CSR_ERROR_NONE);
144         //ASSERT_IF(csr_cs_scan_dirs_async(context, dirs, sizeof(dirs) / sizeof(const char *), nullptr),
145         //      CSR_ERROR_NONE);
146
147         EXCEPTION_GUARD_END
148 }
149
150 BOOST_AUTO_TEST_SUITE_END() // CS
151
152 BOOST_AUTO_TEST_SUITE(WP)
153
154 BOOST_AUTO_TEST_CASE(fields_getters)
155 {
156         EXCEPTION_GUARD_START
157
158         Engine e(CSR_ENGINE_WP);
159
160         const char *vendor = nullptr;
161         ASSERT_IF(csr_engine_get_vendor(e.get(), &vendor), CSR_ERROR_NONE);
162         ASSERT_IF(std::string("TEST_VENDOR"), vendor);
163
164         const char *name = nullptr;
165         ASSERT_IF(csr_engine_get_name(e.get(), &name), CSR_ERROR_NONE);
166         ASSERT_IF(std::string("TEST_LOCAL_TWP_ENGINE"), name);
167
168         const char *version = nullptr;
169         ASSERT_IF(csr_engine_get_version(e.get(), &version), CSR_ERROR_NONE);
170         ASSERT_IF(std::string("0.0.1"), version);
171
172         const char *dataVersion = nullptr;
173         ASSERT_IF(csr_engine_get_version(e.get(), &dataVersion), CSR_ERROR_NONE);
174         ASSERT_IF(std::string("0.0.1"), dataVersion);
175
176         csr_activated_e activated;
177         ASSERT_IF(csr_engine_get_activated(e.get(), &activated), CSR_ERROR_NONE);
178         ASSERT_IF(activated, CSR_ACTIVATED);
179
180         csr_state_e state = CSR_ENABLE;
181         ASSERT_IF(csr_engine_set_state(e.get(), state), CSR_ERROR_NONE);
182
183         ASSERT_IF(csr_engine_get_state(e.get(), &state), CSR_ERROR_NONE);
184         ASSERT_IF(state, CSR_ENABLE);
185
186         EXCEPTION_GUARD_END
187 }
188
189 BOOST_AUTO_TEST_CASE(set_state)
190 {
191         EXCEPTION_GUARD_START
192
193         Engine e(CSR_ENGINE_WP);
194
195         csr_state_e state = CSR_ENABLE;
196
197         // enable
198         ASSERT_IF(csr_engine_set_state(e.get(), CSR_ENABLE), CSR_ERROR_NONE);
199         ASSERT_IF(csr_engine_get_state(e.get(), &state), CSR_ERROR_NONE);
200         ASSERT_IF(state, CSR_ENABLE);
201
202         // prepare data
203         auto c = Test::Context<csr_wp_context_h>();
204         auto context = c.get();
205         csr_wp_check_result_h result;
206
207         // check if engine is working well
208         ASSERT_IF(csr_wp_check_url(context, RISK_HIGH_URL, &result), CSR_ERROR_NONE);
209
210         // disable
211         ASSERT_IF(csr_engine_set_state(e.get(), CSR_DISABLE), CSR_ERROR_NONE);
212         ASSERT_IF(csr_engine_get_state(e.get(), &state), CSR_ERROR_NONE);
213         ASSERT_IF(state, CSR_DISABLE);
214
215         // test operation
216         ASSERT_IF(csr_wp_check_url(context, RISK_HIGH_URL, &result), CSR_ERROR_ENGINE_DISABLED);
217
218         // enable
219         ASSERT_IF(csr_engine_set_state(e.get(), CSR_ENABLE), CSR_ERROR_NONE);
220         ASSERT_IF(csr_engine_get_state(e.get(), &state), CSR_ERROR_NONE);
221         ASSERT_IF(state, CSR_ENABLE);
222
223         // test operation
224         ASSERT_IF(csr_wp_check_url(context, RISK_HIGH_URL, &result), CSR_ERROR_NONE);
225
226         EXCEPTION_GUARD_END
227 }
228
229
230 BOOST_AUTO_TEST_SUITE_END() // WP
231 BOOST_AUTO_TEST_SUITE_END() // API_ENGINE_MANAGER