1 /*
2 * Copyright © 2020 Google LLC
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <gtest/gtest.h>
25 #include <driconf.h>
26 #include <xmlconfig.h>
27
28 class xmlconfig_test : public ::testing::Test {
29 protected:
30 xmlconfig_test();
31 ~xmlconfig_test();
32
33 driOptionCache drirc_init(const char *driver, const char *drm,
34 const char *exec_name,
35 const char *app, int appver,
36 const char *engine, int enginever);
37
38 driOptionCache options;
39 };
40
xmlconfig_test()41 xmlconfig_test::xmlconfig_test()
42 {
43 /* Unset variables from the envrionment to prevent user settings from
44 * impacting the tests.
45 */
46 unsetenv("glsl_zero_init");
47 unsetenv("always_have_depth_buffer");
48 unsetenv("opt");
49 unsetenv("vblank_mode");
50 unsetenv("not_present");
51 unsetenv("mesa_b_option");
52 unsetenv("mesa_s_option");
53 unsetenv("mest_test_unknown_option");
54 unsetenv("mest_drirc_option");
55
56 options = {};
57 }
58
~xmlconfig_test()59 xmlconfig_test::~xmlconfig_test()
60 {
61 driDestroyOptionInfo(&options);
62 }
63
64 /* wraps a DRI_CONF_OPT_* in the required xml bits */
65 #define DRI_CONF_TEST_OPT(x) x
66
TEST_F(xmlconfig_test,bools)67 TEST_F(xmlconfig_test, bools)
68 {
69 driOptionDescription driconf[] = {
70 DRI_CONF_SECTION_MISCELLANEOUS
71 DRI_CONF_GLSL_ZERO_INIT(false)
72 DRI_CONF_ALWAYS_HAVE_DEPTH_BUFFER(true)
73 };
74 driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
75
76 EXPECT_EQ(driQueryOptionb(&options, "glsl_zero_init"), false);
77 EXPECT_EQ(driQueryOptionb(&options, "always_have_depth_buffer"), true);
78 }
79
TEST_F(xmlconfig_test,ints)80 TEST_F(xmlconfig_test, ints)
81 {
82 driOptionDescription driconf[] = {
83 DRI_CONF_SECTION_MISCELLANEOUS
84 DRI_CONF_OPT_I(opt, 2, 0, 999, "option")
85 };
86 driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
87
88 EXPECT_EQ(driQueryOptioni(&options, "opt"), 2);
89 }
90
TEST_F(xmlconfig_test,floats)91 TEST_F(xmlconfig_test, floats)
92 {
93 driOptionDescription driconf[] = {
94 DRI_CONF_SECTION_MISCELLANEOUS
95 DRI_CONF_OPT_F(opt, 2.0, 1.0, 2.0, "option")
96 };
97 driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
98
99 EXPECT_EQ(driQueryOptionf(&options, "opt"), 2.0);
100 }
101
TEST_F(xmlconfig_test,enums)102 TEST_F(xmlconfig_test, enums)
103 {
104 driOptionDescription driconf[] = {
105 DRI_CONF_SECTION_MISCELLANEOUS
106 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1)
107 };
108 driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
109
110 EXPECT_EQ(driQueryOptioni(&options, "vblank_mode"), DRI_CONF_VBLANK_DEF_INTERVAL_1);
111 }
112
TEST_F(xmlconfig_test,enums_from_env)113 TEST_F(xmlconfig_test, enums_from_env)
114 {
115 driOptionDescription driconf[] = {
116 DRI_CONF_SECTION_MISCELLANEOUS
117 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1)
118 };
119
120 setenv("vblank_mode", "0", 1);
121 driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
122
123 EXPECT_EQ(0, driQueryOptioni(&options, "vblank_mode"));
124 }
125
TEST_F(xmlconfig_test,string)126 TEST_F(xmlconfig_test, string)
127 {
128 driOptionDescription driconf[] = {
129 DRI_CONF_SECTION_MISCELLANEOUS
130 DRI_CONF_OPT_S(opt, value, "option")
131 };
132 driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
133
134 EXPECT_STREQ(driQueryOptionstr(&options, "opt"), "value");
135 }
136
TEST_F(xmlconfig_test,check_option)137 TEST_F(xmlconfig_test, check_option)
138 {
139 driOptionDescription driconf[] = {
140 DRI_CONF_SECTION_MISCELLANEOUS
141 DRI_CONF_GLSL_ZERO_INIT(true)
142 DRI_CONF_ALWAYS_HAVE_DEPTH_BUFFER(true)
143 };
144 driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
145
146 EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_BOOL), true);
147
148 EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_ENUM), false);
149 EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_INT), false);
150 EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_FLOAT), false);
151 EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_STRING), false);
152
153 EXPECT_EQ(driCheckOption(&options, "not_present", DRI_BOOL), false);
154 }
155
TEST_F(xmlconfig_test,copy_cache)156 TEST_F(xmlconfig_test, copy_cache)
157 {
158 driOptionDescription driconf[] = {
159 DRI_CONF_SECTION_MISCELLANEOUS
160 DRI_CONF_OPT_B(mesa_b_option, true, "description")
161 DRI_CONF_OPT_S(mesa_s_option, value, "description")
162 };
163 driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
164
165 driOptionCache cache;
166
167 /* This tries to parse user config files. We've called our option
168 * "mesa_test_option" so the test shouldn't end up with something from the
169 * user's homedir/environment that would override us.
170 */
171 driParseConfigFiles(&cache, &options,
172 0, "driver", "drm", NULL,
173 NULL, 0,
174 NULL, 0);
175
176 /* Can we inspect the cache? */
177 EXPECT_EQ(driCheckOption(&cache, "mesa_b_option", DRI_BOOL), true);
178 EXPECT_EQ(driCheckOption(&cache, "mesa_s_option", DRI_STRING), true);
179 EXPECT_EQ(driCheckOption(&cache, "mesa_test_unknown_option", DRI_BOOL), false);
180
181 /* Did the value get copied? */
182 EXPECT_EQ(driQueryOptionb(&cache, "mesa_b_option"), true);
183 EXPECT_STREQ(driQueryOptionstr(&cache, "mesa_s_option"), "value");
184
185 driDestroyOptionCache(&cache);
186 }
187
188 driOptionCache
drirc_init(const char * driver,const char * drm,const char * exec_name,const char * app,int appver,const char * engine,int enginever)189 xmlconfig_test::drirc_init(const char *driver, const char *drm,
190 const char *exec_name,
191 const char *app, int appver,
192 const char *engine, int enginever)
193 {
194 driInjectExecName(exec_name);
195
196 driOptionDescription driconf[] = {
197 DRI_CONF_SECTION_MISCELLANEOUS
198 DRI_CONF_OPT_I(mesa_drirc_option, 0, 0, 200, "description")
199 };
200 driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
201
202 driOptionCache cache;
203
204 /* This should parse the "user" drirc files under ./tests/drirc_test/,
205 * based on the setting of $HOME by meson.build.
206 */
207 driParseConfigFiles(&cache, &options,
208 0, driver, drm, NULL,
209 app, appver,
210 engine, enginever);
211
212 return cache;
213 }
214
TEST_F(xmlconfig_test,drirc_app)215 TEST_F(xmlconfig_test, drirc_app)
216 {
217 driOptionCache cache = drirc_init("driver", "drm",
218 "app1",
219 NULL, 0,
220 NULL, 0);
221 #if WITH_XMLCONFIG
222 EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 1);
223 #else
224 EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 0);
225 #endif
226 driDestroyOptionCache(&cache);
227 }
228
TEST_F(xmlconfig_test,drirc_user_app)229 TEST_F(xmlconfig_test, drirc_user_app)
230 {
231 driOptionCache cache = drirc_init("driver", "drm",
232 "app3",
233 NULL, 0,
234 NULL, 0);
235 #if WITH_XMLCONFIG
236 EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 10);
237 #else
238 EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 0);
239 #endif
240 driDestroyOptionCache(&cache);
241 }
242
TEST_F(xmlconfig_test,drirc_env_override)243 TEST_F(xmlconfig_test, drirc_env_override)
244 {
245 setenv("mesa_drirc_option", "7", 1);
246 driOptionCache cache = drirc_init("driver", "drm",
247 "app1",
248 NULL, 0,
249 NULL, 0);
250 /* env var takes precedence over config files */
251 EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 7);
252 unsetenv("mesa_drirc_option");
253 driDestroyOptionCache(&cache);
254 }
255
256 #if WITH_XMLCONFIG
TEST_F(xmlconfig_test,drirc_app_versioned)257 TEST_F(xmlconfig_test, drirc_app_versioned)
258 {
259 driOptionCache cache = drirc_init("driver", "drm",
260 NULL,
261 "Versioned App Name", 1,
262 NULL, 0);
263 EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 3);
264 driDestroyOptionCache(&cache);
265 }
266
TEST_F(xmlconfig_test,drirc_engine_versioned)267 TEST_F(xmlconfig_test, drirc_engine_versioned)
268 {
269 driOptionCache cache = drirc_init("driver", "drm",
270 NULL,
271 "unknownapp", 0,
272 "Versioned Engine Name", 1);
273 EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 5);
274 driDestroyOptionCache(&cache);
275 }
276
TEST_F(xmlconfig_test,drirc_exec_regexp)277 TEST_F(xmlconfig_test, drirc_exec_regexp)
278 {
279 driOptionCache cache = drirc_init("driver", "drm",
280 "app2v4",
281 NULL, 0,
282 NULL, 0);
283 EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 7);
284 driDestroyOptionCache(&cache);
285 }
286
TEST_F(xmlconfig_test,drirc_exec_override)287 TEST_F(xmlconfig_test, drirc_exec_override)
288 {
289 putenv("MESA_DRICONF_EXECUTABLE_OVERRIDE=app1");
290 driOptionCache cache = drirc_init("driver", "drm",
291 NULL,
292 NULL, 0,
293 NULL, 0);
294 EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 1);
295 driDestroyOptionCache(&cache);
296 }
297 #endif
298