diff --git a/src/util/meson.build b/src/util/meson.build index f9f9fee9c71..705de9c1093 100644 --- a/src/util/meson.build +++ b/src/util/meson.build @@ -243,6 +243,10 @@ if with_tests cpp_args: ['-Wno-write-strings'] ), suite : ['util'], + env: ['HOME=' + join_paths(meson.current_source_dir(), + 'tests', 'drirc_home'), + 'DRIRC_CONFIGDIR=' + join_paths(meson.current_source_dir(), + 'tests', 'drirc_configdir')] ) endif diff --git a/src/util/tests/drirc_configdir/00-test.conf b/src/util/tests/drirc_configdir/00-test.conf new file mode 100644 index 00000000000..fcc7208772d --- /dev/null +++ b/src/util/tests/drirc_configdir/00-test.conf @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + +]> + + + + + + + + + + + + + + + + + + + + + diff --git a/src/util/tests/drirc_configdir/01-unused b/src/util/tests/drirc_configdir/01-unused new file mode 100644 index 00000000000..dfdc4eb75ec --- /dev/null +++ b/src/util/tests/drirc_configdir/01-unused @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + +]> + + + + + + + + + diff --git a/src/util/tests/drirc_home/.drirc b/src/util/tests/drirc_home/.drirc new file mode 100644 index 00000000000..695fe0e5757 --- /dev/null +++ b/src/util/tests/drirc_home/.drirc @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + +]> + + + + + + + diff --git a/src/util/tests/xmlconfig.cpp b/src/util/tests/xmlconfig.cpp index ad9af36d6c1..42bb10149c2 100644 --- a/src/util/tests/xmlconfig.cpp +++ b/src/util/tests/xmlconfig.cpp @@ -30,6 +30,11 @@ protected: xmlconfig_test(); ~xmlconfig_test(); + driOptionCache drirc_init(const char *driver, const char *drm, + const char *exec_name, + const char *app, int appver, + const char *engine, int enginever); + driOptionCache options; }; @@ -153,3 +158,93 @@ TEST_F(xmlconfig_test, copy_cache) driDestroyOptionCache(&cache); } + +driOptionCache +xmlconfig_test::drirc_init(const char *driver, const char *drm, + const char *exec_name, + const char *app, int appver, + const char *engine, int enginever) +{ + /* Make the parser look in the directory of config files for the test, + * passed in by meson.build. + */ + driInjectDataDir(getenv("DRIRC_CONFIGDIR")); + + driInjectExecName(exec_name); + + driOptionDescription driconf[] = { + DRI_CONF_SECTION_MISCELLANEOUS + DRI_CONF_OPT_I(mesa_drirc_option, 0, 0, 200, "description") + }; + driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf)); + + driOptionCache cache; + + /* This should parse the "user" drirc files under ./tests/drirc_test/, + * based on the setting of $HOME by meson.build. + */ + driParseConfigFiles(&cache, &options, + 0, driver, drm, + app, appver, + engine, enginever); + + return cache; +} + +TEST_F(xmlconfig_test, drirc_app) +{ + driOptionCache cache = drirc_init("driver", "drm", + "app1", + NULL, 0, + NULL, 0); +#if WITH_XMLCONFIG + EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 1); +#else + EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 0); +#endif +} + +TEST_F(xmlconfig_test, drirc_user_app) +{ + driOptionCache cache = drirc_init("driver", "drm", + "app3", + NULL, 0, + NULL, 0); +#if WITH_XMLCONFIG + EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 10); +#else + EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 0); +#endif +} + +TEST_F(xmlconfig_test, drirc_env_override) +{ + setenv("mesa_drirc_option", "7", 1); + driOptionCache cache = drirc_init("driver", "drm", + "app1", + NULL, 0, + NULL, 0); + /* env var takes precedence over config files */ + EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 7); + unsetenv("mesa_drirc_option"); +} + +#if WITH_XMLCONFIG +TEST_F(xmlconfig_test, drirc_app_versioned) +{ + driOptionCache cache = drirc_init("driver", "drm", + NULL, + "Versioned App Name", 1, + NULL, 0); + EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 3); +} + +TEST_F(xmlconfig_test, drirc_engine_versioned) +{ + driOptionCache cache = drirc_init("driver", "drm", + NULL, + "unknownapp", 0, + "Versioned Engine Name", 1); + EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 5); +} +#endif diff --git a/src/util/xmlconfig.c b/src/util/xmlconfig.c index 0245f8f78dc..170e1b5da6c 100644 --- a/src/util/xmlconfig.c +++ b/src/util/xmlconfig.c @@ -27,12 +27,7 @@ * \author Felix Kuehling */ -#if defined(ANDROID) || defined(_WIN32) -#define WITH_XMLCONFIG 0 -#else -#define WITH_XMLCONFIG 1 -#endif - +#include "xmlconfig.h" #include #include #include @@ -52,7 +47,6 @@ #include #include #include "strndup.h" -#include "xmlconfig.h" #include "u_process.h" #include "os_file.h" @@ -1036,6 +1030,21 @@ initOptionCache(driOptionCache *cache, const driOptionCache *info) #define DATADIR "/usr/share" #endif +static const char *datadir = DATADIR "/drirc.d"; +static const char *execname; + +void +driInjectDataDir(const char *dir) +{ + datadir = dir; +} + +void +driInjectExecName(const char *exec) +{ + execname = exec; +} + void driParseConfigFiles(driOptionCache *cache, const driOptionCache *info, int screenNum, const char *driverName, @@ -1057,9 +1066,9 @@ driParseConfigFiles(driOptionCache *cache, const driOptionCache *info, userData.applicationVersion = applicationVersion; userData.engineName = engineName ? engineName : ""; userData.engineVersion = engineVersion; - userData.execName = util_get_process_name(); + userData.execName = execname ?: util_get_process_name(); - parseConfigDir(&userData, DATADIR "/drirc.d"); + parseConfigDir(&userData, datadir); parseOneConfigFile(&userData, SYSCONFDIR "/drirc"); if ((home = getenv("HOME"))) { diff --git a/src/util/xmlconfig.h b/src/util/xmlconfig.h index 130de090188..d0bb0b32221 100644 --- a/src/util/xmlconfig.h +++ b/src/util/xmlconfig.h @@ -39,6 +39,12 @@ extern "C" { #endif +#ifdef ANDROID +#define WITH_XMLCONFIG 0 +#else +#define WITH_XMLCONFIG 1 +#endif + #define STRING_CONF_MAXLEN 1024 /** \brief Option data types */ @@ -157,6 +163,10 @@ float driQueryOptionf(const driOptionCache *cache, const char *name); /** \brief Query a string option value */ char *driQueryOptionstr(const driOptionCache *cache, const char *name); +/* Overrides for the unit tests to control drirc parsing. */ +void driInjectDataDir(const char *dir); +void driInjectExecName(const char *exec); + /** * Returns a hash of the options for this application. */