halcc: Introduce halcc library
[platform/hal/api/common.git] / tests / unittest / test-hal-compatibility-checker.cc
1 /*
2  * Copyright (c) 2024 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 <string.h>
18 #include <gtest/gtest.h>
19
20 #include <halcc/hal-compatibility-checker.h>
21 #include "../../halcc/src/halcc-object.h"
22 #include "../../halcc/src/halcc-parser.h"
23
24 using namespace std;
25
26 static char g_manifest_xml[] =
27 "<manifest version=\"1.0\" type=\"platform\" level=\"7\">"
28 "       <hal>"
29 "               <name>hal.device.display</name>"
30 "               <version>1.5</version>"
31 "               <transport>passthrough</transport>"
32 "               <interface>"
33 "                       <name>hal_backend_device_display_data</name>"
34 "               </interface>"
35 "               <dependency>"
36 "                       <hal>"
37 "                               <name>hal.device.board</name>"
38 "                               <version>1.0</version>"
39 "                               <transport>passthrough</transport>"
40 "                               <interface>"
41 "                                       <name>hal_backend_device_board_data</name>"
42 "                               </interface>"
43 "                       </hal>"
44 "                       <hal>"
45 "                               <name>hal.tdm</name>"
46 "                               <version>1.0</version>"
47 "                               <transport>passthrough</transport>"
48 "                               <interface>"
49 "                                       <name>hal_backend_tdm_data</name>"
50 "                               </interface>"
51 "                       </hal>"
52 "               </dependency>"
53 "       </hal>"
54 "       <hal>"
55 "               <name>hal.something</name>"
56 "               <version>2.34</version>"
57 "               <transport>passthrough</transport>"
58 "               <interface>"
59 "                       <name>hal_backend_something_data</name>"
60 "               </interface>"
61 "       </hal>"
62 "       <hal>"
63 "               <name>hal.test</name>"
64 "               <version>4.8</version>"
65 "               <transport>passthrough</transport>"
66 "               <interface>"
67 "                       <name>hal_backend_test_data</name>"
68 "               </interface>"
69 "       </hal>"
70 "</manifest>";
71
72 class HalccObjectTest : public ::testing::Test
73 {
74         protected:
75                 static void SetUpTestSuite() {
76                         int ret;
77
78                         ret = halcc_manifest_new(&g_manifest);
79                         ASSERT_EQ(ret, 0);
80
81                         ret = halcc_parse_string(g_manifest_xml, sizeof(g_manifest_xml), g_manifest);
82                         ASSERT_EQ(ret, 0);
83
84                         ret = halcc_manifest_find_hal(g_manifest, "hal.test", 4, 8, &g_hal);
85                         ASSERT_EQ(ret, 0);
86                 }
87
88                 static void TearDownTestSuite() {
89                         halcc_manifest_free(g_manifest);
90                         g_manifest = NULL;
91
92                 }
93
94                 void SetUp() override {
95
96                 }
97
98                 void TearDown() override {
99
100                 }
101
102                 static halcc_manifest *g_manifest;
103                 static halcc_hal *g_hal;
104                 static halcc_interface *g_interface;
105 };
106
107 halcc_manifest* HalccObjectTest::g_manifest = NULL;
108 halcc_hal* HalccObjectTest::g_hal = NULL;
109 halcc_interface* HalccObjectTest::g_interface = NULL;
110
111 TEST_F(HalccObjectTest, manifest_get_type)
112 {
113         halcc_manifest_type_e type;
114         int ret;
115
116         ret = halcc_manifest_get_type(g_manifest, &type);
117         ASSERT_EQ(ret, 0);
118         ASSERT_EQ(type, HALCC_MANIFEST_TYPE_HAL_API);
119 }
120
121 TEST_F(HalccObjectTest, manifest_get_version)
122 {
123         int major, minor;
124         int ret;
125
126         ret = halcc_manifest_get_version(g_manifest, &major, &minor);
127         ASSERT_EQ(ret, 0);
128         ASSERT_EQ(major, 1);
129         ASSERT_EQ(minor, 0);
130 }
131
132 TEST_F(HalccObjectTest, manifest_get_level)
133 {
134         int level;
135         int ret;
136
137         ret = halcc_manifest_get_level(g_manifest, &level);
138         ASSERT_EQ(ret, 0);
139         ASSERT_EQ(level, 7);
140 }
141
142 TEST_F(HalccObjectTest, manifest_find_hal_success)
143 {
144         halcc_hal *hal = NULL;
145         int ret;
146
147         ret = halcc_manifest_find_hal(g_manifest, "hal.device.display", 1, 5, &hal);
148         ASSERT_EQ(ret, 0);
149 }
150
151 TEST_F(HalccObjectTest, manifest_find_hal_fail)
152 {
153         halcc_hal *hal = NULL;
154         int ret;
155
156         ret = halcc_manifest_find_hal(g_manifest, "hal.device.display", 1, 4, &hal);
157         ASSERT_EQ(ret, -ENOTSUP);
158
159         ret = halcc_manifest_find_hal(g_manifest, "hal.device.display", 1, 6, &hal);
160         ASSERT_EQ(ret, -ENOTSUP);
161
162         ret = halcc_manifest_find_hal(g_manifest, "hal.device.display", 2, 4, &hal);
163         ASSERT_EQ(ret, -ENOTSUP);
164 }
165
166 TEST_F(HalccObjectTest, manifest_find_hal_backward_compatible_success)
167 {
168         halcc_hal *hal = NULL;
169         int ret;
170
171         ret = halcc_manifest_find_hal_backward_compatible(g_manifest, "hal.device.display", 1, 5, &hal);
172         ASSERT_EQ(ret, 0);
173
174         ret = halcc_manifest_find_hal_backward_compatible(g_manifest, "hal.device.display", 1, 6, &hal);
175         ASSERT_EQ(ret, 0);
176
177         ret = halcc_manifest_find_hal_backward_compatible(g_manifest, "hal.device.display", 1, 100, &hal);
178         ASSERT_EQ(ret, 0);
179 }
180
181 TEST_F(HalccObjectTest, manifest_find_hal_backward_compatible_fail)
182 {
183         halcc_hal *hal = NULL;
184         int ret;
185
186         ret = halcc_manifest_find_hal_backward_compatible(g_manifest, "hal.device.display", 1, 4, &hal);
187         ASSERT_EQ(ret, -ENOTSUP);
188
189         ret = halcc_manifest_find_hal_backward_compatible(g_manifest, "hal.device.display", 2, 4, &hal);
190         ASSERT_EQ(ret, -ENOTSUP);
191
192         ret = halcc_manifest_find_hal_backward_compatible(g_manifest, "hal.device.display", 2, 5, &hal);
193         ASSERT_EQ(ret, -ENOTSUP);
194 }
195
196 TEST_F(HalccObjectTest, manifest_find_hal_steal_success)
197 {
198         halcc_hal *hal = NULL;
199         const char *hal_name;
200         int major, minor;
201         int ret;
202
203         ret = halcc_manifest_steal_hal(g_manifest, "hal.something", 2, 34, &hal);
204         ASSERT_EQ(ret, 0);
205
206         ret = halcc_hal_get_name(hal, &hal_name);
207         ASSERT_EQ(ret, 0);
208         ASSERT_STREQ(hal_name, "hal.something");
209
210         ret = halcc_hal_get_version(hal, &major, &minor, NULL);
211         ASSERT_EQ(ret, 0);
212         ASSERT_EQ(major, 2);
213         ASSERT_EQ(minor, 34);
214
215         // Shouldn't be there
216         ret = halcc_manifest_find_hal(g_manifest, "hal.something", 2, 34, &hal);
217         ASSERT_EQ(ret, -ENOTSUP);
218
219         ret = halcc_manifest_add_hal(g_manifest, hal);
220         ASSERT_EQ(ret, 0);
221
222         ret = halcc_manifest_find_hal(g_manifest, "hal.something", 2, 34, &hal);
223         ASSERT_EQ(ret, 0);
224
225         ret = halcc_hal_get_name(hal, &hal_name);
226         ASSERT_EQ(ret, 0);
227         ASSERT_STREQ(hal_name, "hal.something");
228
229         ret = halcc_hal_get_version(hal, &major, &minor, NULL);
230         ASSERT_EQ(ret, 0);
231         ASSERT_EQ(major, 2);
232         ASSERT_EQ(minor, 34);
233 }