APIs and description changed for ACR
[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_SUCCESS(csr_get_current_engine(id, &m_engine));
38         }
39
40         ~Engine()
41         {
42                 ASSERT_SUCCESS(csr_engine_destroy(m_engine));
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         Test::ScopedCstr vendor;
66         ASSERT_SUCCESS(csr_engine_get_vendor(e.get(), &vendor.ptr));
67         ASSERT_IF(vendor.ptr, std::string("TEST_VENDOR"));
68
69         Test::ScopedCstr name;
70         ASSERT_SUCCESS(csr_engine_get_name(e.get(), &name.ptr));
71         ASSERT_IF(name.ptr, std::string("TEST_LOCAL_TCS_ENGINE"));
72
73         Test::ScopedCstr version;
74         ASSERT_SUCCESS(csr_engine_get_version(e.get(), &version.ptr));
75         ASSERT_IF(version.ptr, std::string("0.0.1"));
76
77         Test::ScopedCstr dataVersion;
78         ASSERT_SUCCESS(csr_engine_get_version(e.get(), &dataVersion.ptr));
79         ASSERT_IF(dataVersion.ptr, std::string("0.0.1"));
80
81         csr_activated_e activated;
82         ASSERT_SUCCESS(csr_engine_get_activated(e.get(), &activated));
83         ASSERT_IF(activated, CSR_ACTIVATED);
84
85         csr_state_e state = CSR_STATE_ENABLE;
86         ASSERT_SUCCESS(csr_engine_set_state(e.get(), state));
87
88         ASSERT_SUCCESS(csr_engine_get_state(e.get(), &state));
89         ASSERT_IF(state, CSR_STATE_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_STATE_ENABLE;
101
102         // enable
103         ASSERT_SUCCESS(csr_engine_set_state(e.get(), CSR_STATE_ENABLE));
104         ASSERT_SUCCESS(csr_engine_get_state(e.get(), &state));
105         ASSERT_IF(state, CSR_STATE_ENABLE);
106
107         // prepare data
108         auto c = Test::Context<csr_cs_context_h>();
109         auto context = c.get();
110         csr_cs_malware_h detected;
111         unsigned char data[100] = {0, };
112
113         // check if engine is working well
114         memcpy(data, MALWARE_HIGH_SIGNATURE, strlen(MALWARE_HIGH_SIGNATURE));
115         ASSERT_SUCCESS(csr_cs_scan_data(context, data, sizeof(data), &detected));
116
117         // disable
118         ASSERT_SUCCESS(csr_engine_set_state(e.get(), CSR_STATE_DISABLE));
119         ASSERT_SUCCESS(csr_engine_get_state(e.get(), &state));
120         ASSERT_IF(state, CSR_STATE_DISABLE);
121
122         // test operation
123         ASSERT_IF(csr_cs_scan_data(context, data, sizeof(data), &detected),
124                           CSR_ERROR_ENGINE_DISABLED);
125         ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_NORMAL, &detected),
126                           CSR_ERROR_ENGINE_DISABLED);
127
128         // enable
129         ASSERT_SUCCESS(csr_engine_set_state(e.get(), CSR_STATE_ENABLE));
130         ASSERT_SUCCESS(csr_engine_get_state(e.get(), &state));
131         ASSERT_IF(state, CSR_STATE_ENABLE);
132
133         // test operation
134         ASSERT_SUCCESS(csr_cs_scan_data(context, data, sizeof(data), &detected));
135         ASSERT_SUCCESS(csr_cs_scan_file(context, TEST_FILE_NORMAL, &detected));
136
137         EXCEPTION_GUARD_END
138 }
139
140 BOOST_AUTO_TEST_SUITE_END() // CS
141
142 BOOST_AUTO_TEST_SUITE(WP)
143
144 BOOST_AUTO_TEST_CASE(fields_getters)
145 {
146         EXCEPTION_GUARD_START
147
148         Engine e(CSR_ENGINE_WP);
149
150         Test::ScopedCstr vendor;
151         ASSERT_SUCCESS(csr_engine_get_vendor(e.get(), &vendor.ptr));
152         ASSERT_IF(vendor.ptr, std::string("TEST_VENDOR"));
153
154         Test::ScopedCstr name;
155         ASSERT_SUCCESS(csr_engine_get_name(e.get(), &name.ptr));
156         ASSERT_IF(name.ptr, std::string("TEST_LOCAL_TWP_ENGINE"));
157
158         Test::ScopedCstr version;
159         ASSERT_SUCCESS(csr_engine_get_version(e.get(), &version.ptr));
160         ASSERT_IF(version.ptr, std::string("0.0.1"));
161
162         Test::ScopedCstr dataVersion;
163         ASSERT_SUCCESS(csr_engine_get_version(e.get(), &dataVersion.ptr));
164         ASSERT_IF(dataVersion.ptr, std::string("0.0.1"));
165
166         csr_activated_e activated;
167         ASSERT_SUCCESS(csr_engine_get_activated(e.get(), &activated));
168         ASSERT_IF(activated, CSR_ACTIVATED);
169
170         csr_state_e state = CSR_STATE_ENABLE;
171         ASSERT_SUCCESS(csr_engine_set_state(e.get(), state));
172
173         ASSERT_SUCCESS(csr_engine_get_state(e.get(), &state));
174         ASSERT_IF(state, CSR_STATE_ENABLE);
175
176         EXCEPTION_GUARD_END
177 }
178
179 BOOST_AUTO_TEST_CASE(set_state)
180 {
181         EXCEPTION_GUARD_START
182
183         Engine e(CSR_ENGINE_WP);
184
185         csr_state_e state = CSR_STATE_ENABLE;
186
187         // enable
188         ASSERT_SUCCESS(csr_engine_set_state(e.get(), CSR_STATE_ENABLE));
189         ASSERT_SUCCESS(csr_engine_get_state(e.get(), &state));
190         ASSERT_IF(state, CSR_STATE_ENABLE);
191
192         // prepare data
193         auto c = Test::Context<csr_wp_context_h>();
194         auto context = c.get();
195         csr_wp_check_result_h result;
196
197         // check if engine is working well
198         ASSERT_SUCCESS(csr_wp_check_url(context, RISK_HIGH_URL, &result));
199
200         // disable
201         ASSERT_SUCCESS(csr_engine_set_state(e.get(), CSR_STATE_DISABLE));
202         ASSERT_SUCCESS(csr_engine_get_state(e.get(), &state));
203         ASSERT_IF(state, CSR_STATE_DISABLE);
204
205         // test operation
206         ASSERT_IF(csr_wp_check_url(context, RISK_HIGH_URL, &result),
207                           CSR_ERROR_ENGINE_DISABLED);
208
209         // enable
210         ASSERT_SUCCESS(csr_engine_set_state(e.get(), CSR_STATE_ENABLE));
211         ASSERT_SUCCESS(csr_engine_get_state(e.get(), &state));
212         ASSERT_IF(state, CSR_STATE_ENABLE);
213
214         // test operation
215         ASSERT_SUCCESS(csr_wp_check_url(context, RISK_HIGH_URL, &result));
216
217         EXCEPTION_GUARD_END
218 }
219
220
221 BOOST_AUTO_TEST_SUITE_END() // WP
222 BOOST_AUTO_TEST_SUITE_END() // API_ENGINE_MANAGER