17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2020 Google LLC
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ec681f3Smrg * IN THE SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg#include <gtest/gtest.h>
257ec681f3Smrg#include <driconf.h>
267ec681f3Smrg#include <xmlconfig.h>
277ec681f3Smrg
287ec681f3Smrgclass xmlconfig_test : public ::testing::Test {
297ec681f3Smrgprotected:
307ec681f3Smrg   xmlconfig_test();
317ec681f3Smrg   ~xmlconfig_test();
327ec681f3Smrg
337ec681f3Smrg   driOptionCache drirc_init(const char *driver, const char *drm,
347ec681f3Smrg                             const char *exec_name,
357ec681f3Smrg                             const char *app, int appver,
367ec681f3Smrg                             const char *engine, int enginever);
377ec681f3Smrg
387ec681f3Smrg   driOptionCache options;
397ec681f3Smrg};
407ec681f3Smrg
417ec681f3Smrgxmlconfig_test::xmlconfig_test()
427ec681f3Smrg{
437ec681f3Smrg   /* Unset variables from the envrionment to prevent user settings from
447ec681f3Smrg    * impacting the tests.
457ec681f3Smrg    */
467ec681f3Smrg   unsetenv("glsl_zero_init");
477ec681f3Smrg   unsetenv("always_have_depth_buffer");
487ec681f3Smrg   unsetenv("opt");
497ec681f3Smrg   unsetenv("vblank_mode");
507ec681f3Smrg   unsetenv("not_present");
517ec681f3Smrg   unsetenv("mesa_b_option");
527ec681f3Smrg   unsetenv("mesa_s_option");
537ec681f3Smrg   unsetenv("mest_test_unknown_option");
547ec681f3Smrg   unsetenv("mest_drirc_option");
557ec681f3Smrg
567ec681f3Smrg   options = {};
577ec681f3Smrg}
587ec681f3Smrg
597ec681f3Smrgxmlconfig_test::~xmlconfig_test()
607ec681f3Smrg{
617ec681f3Smrg   driDestroyOptionInfo(&options);
627ec681f3Smrg}
637ec681f3Smrg
647ec681f3Smrg/* wraps a DRI_CONF_OPT_* in the required xml bits */
657ec681f3Smrg#define DRI_CONF_TEST_OPT(x) x
667ec681f3Smrg
677ec681f3SmrgTEST_F(xmlconfig_test, bools)
687ec681f3Smrg{
697ec681f3Smrg   driOptionDescription driconf[] = {
707ec681f3Smrg      DRI_CONF_SECTION_MISCELLANEOUS
717ec681f3Smrg      DRI_CONF_GLSL_ZERO_INIT(false)
727ec681f3Smrg      DRI_CONF_ALWAYS_HAVE_DEPTH_BUFFER(true)
737ec681f3Smrg   };
747ec681f3Smrg   driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
757ec681f3Smrg
767ec681f3Smrg   EXPECT_EQ(driQueryOptionb(&options, "glsl_zero_init"), false);
777ec681f3Smrg   EXPECT_EQ(driQueryOptionb(&options, "always_have_depth_buffer"), true);
787ec681f3Smrg}
797ec681f3Smrg
807ec681f3SmrgTEST_F(xmlconfig_test, ints)
817ec681f3Smrg{
827ec681f3Smrg   driOptionDescription driconf[] = {
837ec681f3Smrg      DRI_CONF_SECTION_MISCELLANEOUS
847ec681f3Smrg      DRI_CONF_OPT_I(opt, 2, 0, 999, "option")
857ec681f3Smrg   };
867ec681f3Smrg   driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
877ec681f3Smrg
887ec681f3Smrg   EXPECT_EQ(driQueryOptioni(&options, "opt"), 2);
897ec681f3Smrg}
907ec681f3Smrg
917ec681f3SmrgTEST_F(xmlconfig_test, floats)
927ec681f3Smrg{
937ec681f3Smrg   driOptionDescription driconf[] = {
947ec681f3Smrg      DRI_CONF_SECTION_MISCELLANEOUS
957ec681f3Smrg      DRI_CONF_OPT_F(opt, 2.0, 1.0, 2.0, "option")
967ec681f3Smrg   };
977ec681f3Smrg   driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
987ec681f3Smrg
997ec681f3Smrg   EXPECT_EQ(driQueryOptionf(&options, "opt"), 2.0);
1007ec681f3Smrg}
1017ec681f3Smrg
1027ec681f3SmrgTEST_F(xmlconfig_test, enums)
1037ec681f3Smrg{
1047ec681f3Smrg   driOptionDescription driconf[] = {
1057ec681f3Smrg      DRI_CONF_SECTION_MISCELLANEOUS
1067ec681f3Smrg      DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1)
1077ec681f3Smrg   };
1087ec681f3Smrg   driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
1097ec681f3Smrg
1107ec681f3Smrg   EXPECT_EQ(driQueryOptioni(&options, "vblank_mode"), DRI_CONF_VBLANK_DEF_INTERVAL_1);
1117ec681f3Smrg}
1127ec681f3Smrg
1137ec681f3SmrgTEST_F(xmlconfig_test, enums_from_env)
1147ec681f3Smrg{
1157ec681f3Smrg   driOptionDescription driconf[] = {
1167ec681f3Smrg      DRI_CONF_SECTION_MISCELLANEOUS
1177ec681f3Smrg      DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1)
1187ec681f3Smrg   };
1197ec681f3Smrg
1207ec681f3Smrg   setenv("vblank_mode", "0", 1);
1217ec681f3Smrg   driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
1227ec681f3Smrg
1237ec681f3Smrg   EXPECT_EQ(0, driQueryOptioni(&options, "vblank_mode"));
1247ec681f3Smrg}
1257ec681f3Smrg
1267ec681f3SmrgTEST_F(xmlconfig_test, string)
1277ec681f3Smrg{
1287ec681f3Smrg   driOptionDescription driconf[] = {
1297ec681f3Smrg      DRI_CONF_SECTION_MISCELLANEOUS
1307ec681f3Smrg      DRI_CONF_OPT_S(opt, value, "option")
1317ec681f3Smrg   };
1327ec681f3Smrg   driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
1337ec681f3Smrg
1347ec681f3Smrg   EXPECT_STREQ(driQueryOptionstr(&options, "opt"), "value");
1357ec681f3Smrg}
1367ec681f3Smrg
1377ec681f3SmrgTEST_F(xmlconfig_test, check_option)
1387ec681f3Smrg{
1397ec681f3Smrg   driOptionDescription driconf[] = {
1407ec681f3Smrg      DRI_CONF_SECTION_MISCELLANEOUS
1417ec681f3Smrg      DRI_CONF_GLSL_ZERO_INIT(true)
1427ec681f3Smrg      DRI_CONF_ALWAYS_HAVE_DEPTH_BUFFER(true)
1437ec681f3Smrg   };
1447ec681f3Smrg   driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
1457ec681f3Smrg
1467ec681f3Smrg   EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_BOOL), true);
1477ec681f3Smrg
1487ec681f3Smrg   EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_ENUM), false);
1497ec681f3Smrg   EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_INT), false);
1507ec681f3Smrg   EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_FLOAT), false);
1517ec681f3Smrg   EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_STRING), false);
1527ec681f3Smrg
1537ec681f3Smrg   EXPECT_EQ(driCheckOption(&options, "not_present", DRI_BOOL), false);
1547ec681f3Smrg}
1557ec681f3Smrg
1567ec681f3SmrgTEST_F(xmlconfig_test, copy_cache)
1577ec681f3Smrg{
1587ec681f3Smrg   driOptionDescription driconf[] = {
1597ec681f3Smrg      DRI_CONF_SECTION_MISCELLANEOUS
1607ec681f3Smrg      DRI_CONF_OPT_B(mesa_b_option, true, "description")
1617ec681f3Smrg      DRI_CONF_OPT_S(mesa_s_option, value, "description")
1627ec681f3Smrg   };
1637ec681f3Smrg   driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
1647ec681f3Smrg
1657ec681f3Smrg   driOptionCache cache;
1667ec681f3Smrg
1677ec681f3Smrg   /* This tries to parse user config files.  We've called our option
1687ec681f3Smrg    * "mesa_test_option" so the test shouldn't end up with something from the
1697ec681f3Smrg    * user's homedir/environment that would override us.
1707ec681f3Smrg    */
1717ec681f3Smrg   driParseConfigFiles(&cache, &options,
1727ec681f3Smrg                       0, "driver", "drm", NULL,
1737ec681f3Smrg                       NULL, 0,
1747ec681f3Smrg                       NULL, 0);
1757ec681f3Smrg
1767ec681f3Smrg   /* Can we inspect the cache? */
1777ec681f3Smrg   EXPECT_EQ(driCheckOption(&cache, "mesa_b_option", DRI_BOOL), true);
1787ec681f3Smrg   EXPECT_EQ(driCheckOption(&cache, "mesa_s_option", DRI_STRING), true);
1797ec681f3Smrg   EXPECT_EQ(driCheckOption(&cache, "mesa_test_unknown_option", DRI_BOOL), false);
1807ec681f3Smrg
1817ec681f3Smrg   /* Did the value get copied? */
1827ec681f3Smrg   EXPECT_EQ(driQueryOptionb(&cache, "mesa_b_option"), true);
1837ec681f3Smrg   EXPECT_STREQ(driQueryOptionstr(&cache, "mesa_s_option"), "value");
1847ec681f3Smrg
1857ec681f3Smrg   driDestroyOptionCache(&cache);
1867ec681f3Smrg}
1877ec681f3Smrg
1887ec681f3SmrgdriOptionCache
1897ec681f3Smrgxmlconfig_test::drirc_init(const char *driver, const char *drm,
1907ec681f3Smrg                           const char *exec_name,
1917ec681f3Smrg                           const char *app, int appver,
1927ec681f3Smrg                           const char *engine, int enginever)
1937ec681f3Smrg{
1947ec681f3Smrg   /* Make the parser look in the directory of config files for the test,
1957ec681f3Smrg    * passed in by meson.build.
1967ec681f3Smrg    */
1977ec681f3Smrg   driInjectDataDir(getenv("DRIRC_CONFIGDIR"));
1987ec681f3Smrg
1997ec681f3Smrg   driInjectExecName(exec_name);
2007ec681f3Smrg
2017ec681f3Smrg   driOptionDescription driconf[] = {
2027ec681f3Smrg      DRI_CONF_SECTION_MISCELLANEOUS
2037ec681f3Smrg      DRI_CONF_OPT_I(mesa_drirc_option, 0, 0, 200, "description")
2047ec681f3Smrg   };
2057ec681f3Smrg   driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
2067ec681f3Smrg
2077ec681f3Smrg   driOptionCache cache;
2087ec681f3Smrg
2097ec681f3Smrg   /* This should parse the "user" drirc files under ./tests/drirc_test/,
2107ec681f3Smrg    * based on the setting of $HOME by meson.build.
2117ec681f3Smrg    */
2127ec681f3Smrg   driParseConfigFiles(&cache, &options,
2137ec681f3Smrg                       0, driver, drm, NULL,
2147ec681f3Smrg                       app, appver,
2157ec681f3Smrg                       engine, enginever);
2167ec681f3Smrg
2177ec681f3Smrg   return cache;
2187ec681f3Smrg}
2197ec681f3Smrg
2207ec681f3SmrgTEST_F(xmlconfig_test, drirc_app)
2217ec681f3Smrg{
2227ec681f3Smrg   driOptionCache cache = drirc_init("driver", "drm",
2237ec681f3Smrg                                     "app1",
2247ec681f3Smrg                                     NULL, 0,
2257ec681f3Smrg                                     NULL, 0);
2267ec681f3Smrg#if WITH_XMLCONFIG
2277ec681f3Smrg   EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 1);
2287ec681f3Smrg#else
2297ec681f3Smrg   EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 0);
2307ec681f3Smrg#endif
2317ec681f3Smrg   driDestroyOptionCache(&cache);
2327ec681f3Smrg}
2337ec681f3Smrg
2347ec681f3SmrgTEST_F(xmlconfig_test, drirc_user_app)
2357ec681f3Smrg{
2367ec681f3Smrg   driOptionCache cache = drirc_init("driver", "drm",
2377ec681f3Smrg                                     "app3",
2387ec681f3Smrg                                     NULL, 0,
2397ec681f3Smrg                                     NULL, 0);
2407ec681f3Smrg#if WITH_XMLCONFIG
2417ec681f3Smrg   EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 10);
2427ec681f3Smrg#else
2437ec681f3Smrg   EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 0);
2447ec681f3Smrg#endif
2457ec681f3Smrg   driDestroyOptionCache(&cache);
2467ec681f3Smrg}
2477ec681f3Smrg
2487ec681f3SmrgTEST_F(xmlconfig_test, drirc_env_override)
2497ec681f3Smrg{
2507ec681f3Smrg   setenv("mesa_drirc_option", "7", 1);
2517ec681f3Smrg   driOptionCache cache = drirc_init("driver", "drm",
2527ec681f3Smrg                                     "app1",
2537ec681f3Smrg                                     NULL, 0,
2547ec681f3Smrg                                     NULL, 0);
2557ec681f3Smrg   /* env var takes precedence over config files */
2567ec681f3Smrg   EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 7);
2577ec681f3Smrg   unsetenv("mesa_drirc_option");
2587ec681f3Smrg   driDestroyOptionCache(&cache);
2597ec681f3Smrg}
2607ec681f3Smrg
2617ec681f3Smrg#if WITH_XMLCONFIG
2627ec681f3SmrgTEST_F(xmlconfig_test, drirc_app_versioned)
2637ec681f3Smrg{
2647ec681f3Smrg   driOptionCache cache = drirc_init("driver", "drm",
2657ec681f3Smrg                                     NULL,
2667ec681f3Smrg                                     "Versioned App Name", 1,
2677ec681f3Smrg                                     NULL, 0);
2687ec681f3Smrg   EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 3);
2697ec681f3Smrg   driDestroyOptionCache(&cache);
2707ec681f3Smrg}
2717ec681f3Smrg
2727ec681f3SmrgTEST_F(xmlconfig_test, drirc_engine_versioned)
2737ec681f3Smrg{
2747ec681f3Smrg   driOptionCache cache = drirc_init("driver", "drm",
2757ec681f3Smrg                                     NULL,
2767ec681f3Smrg                                     "unknownapp", 0,
2777ec681f3Smrg                                     "Versioned Engine Name", 1);
2787ec681f3Smrg   EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 5);
2797ec681f3Smrg   driDestroyOptionCache(&cache);
2807ec681f3Smrg}
2817ec681f3Smrg#endif
282