@@ -4210,6 +4210,59 @@ TEST_F(ValidationTest, ValidateWithHash) {
4210
4210
VERIFY_ARE_EQUAL (memcmp (Result, pHeader->Hash .Digest , sizeof (Result)), 0 );
4211
4211
}
4212
4212
4213
+ std::wstring string_to_wstring (const std::string &str) {
4214
+ if (str.empty ())
4215
+ return L" " ;
4216
+
4217
+ int size_needed =
4218
+ MultiByteToWideChar (CP_UTF8, 0 , str.c_str (), (int )str.size (), nullptr , 0 );
4219
+ std::wstring wstrTo (size_needed, 0 );
4220
+ MultiByteToWideChar (CP_UTF8, 0 , str.c_str (), (int )str.size (), &wstrTo[0 ],
4221
+ size_needed);
4222
+ return wstrTo;
4223
+ }
4224
+
4225
+ std::string wstring_to_utf8 (const std::wstring &wstr) {
4226
+ if (wstr.empty ())
4227
+ return " " ;
4228
+
4229
+ int size = WideCharToMultiByte (CP_UTF8, 0 , wstr.data (), -1 , nullptr , 0 ,
4230
+ nullptr , nullptr );
4231
+ std::string result (size - 1 , 0 ); // -1 to exclude null terminator
4232
+ WideCharToMultiByte (CP_UTF8, 0 , wstr.data (), -1 , &result[0 ], size, nullptr ,
4233
+ nullptr );
4234
+ return result;
4235
+ }
4236
+
4237
+ std::string GetEnvVarA (const std::string &varName) {
4238
+ #ifdef _WIN32
4239
+ DWORD size = GetEnvironmentVariableA (varName.c_str (), nullptr , 0 );
4240
+ if (size == 0 ) {
4241
+ return " " ; // Not found or empty
4242
+ }
4243
+
4244
+ std::string buffer (size - 1 , ' \0 ' ); // size includes null terminator
4245
+ GetEnvironmentVariableA (varName.c_str (), &buffer[0 ], size);
4246
+ return buffer;
4247
+ #else
4248
+ const char *result = std::getenv (varName.c_str ());
4249
+ return result ? std::string (result) : std::string ();
4250
+ #endif
4251
+ }
4252
+
4253
+ void SetEnvVarW (const std::wstring &varName, const std::wstring &varValue) {
4254
+ #ifdef _WIN32
4255
+ VERIFY_IS_TRUE (SetEnvironmentVariableW (varName.c_str (), varValue.c_str ()));
4256
+ // also update the CRT environment
4257
+ _putenv_s (wstring_to_utf8 (varName).c_str (),
4258
+ wstring_to_utf8 (varValue).c_str ());
4259
+ #else
4260
+ std::string name_utf8 = wstring_to_utf8 (varName);
4261
+ std::string value_utf8 = wstring_to_utf8 (varValue);
4262
+ setenv (name_utf8.c_str (), value_utf8.c_str (), 1 );
4263
+ #endif
4264
+ }
4265
+
4213
4266
// For now, 3 things are tested:
4214
4267
// 1. The environment variable is not set. GetDxilDllPath() is empty and
4215
4268
// DxilDllFailedToLoad() returns false
@@ -4225,20 +4278,23 @@ TEST_F(ValidationTest, UnitTestExtValidationSupport) {
4225
4278
DxcDllExtValidationSupport m_dllExtSupport1;
4226
4279
DxcDllExtValidationSupport m_dllExtSupport2;
4227
4280
4281
+ // capture any existing value in the environment variable,
4282
+ // so that it can be restored after the test
4283
+ std::string oldEnvVal = GetEnvVarA (" DXC_DXIL_DLL_PATH" );
4284
+
4228
4285
// 1. with no env var set, test GetDxilDllPath() and DxilDllFailedToLoad()
4229
- m_dllExtSupport1.Initialize ();
4286
+
4287
+ // make sure the variable is cleared, in case other tests may have set it
4288
+ SetEnvVarW (L" DXC_DXIL_DLL_PATH" , L" " );
4289
+
4290
+ // empty initialization should succeed
4291
+ VERIFY_SUCCEEDED (m_dllExtSupport1.Initialize ());
4292
+
4230
4293
VERIFY_IS_FALSE (m_dllExtSupport1.DxilDllFailedToLoad ());
4231
4294
VERIFY_ARE_EQUAL (m_dllExtSupport1.GetDxilDllPath (), " " );
4232
4295
4233
4296
// 2. Test with a bogus path in the environment variable
4234
- #ifdef _WIN32
4235
- SetEnvironmentVariableW (L" DXC_DXIL_DLL_PATH" , L" bogus" );
4236
- // also update the CRT environment
4237
- _putenv_s (" DXC_DXIL_DLL_PATH" , " bogus" );
4238
-
4239
- #else
4240
- setenv (" DXC_DXIL_DLL_PATH" , " bogus" , 1 );
4241
- #endif
4297
+ SetEnvVarW (L" DXC_DXIL_DLL_PATH" , L" bogus" );
4242
4298
4243
4299
if (!m_dllExtSupport2.IsEnabled ()) {
4244
4300
VERIFY_SUCCEEDED (m_dllExtSupport2.Initialize ());
@@ -4276,6 +4332,11 @@ TEST_F(ValidationTest, UnitTestExtValidationSupport) {
4276
4332
VERIFY_SUCCEEDED (m_dllExtSupport2.CreateInstance2 (pMalloc, CLSID_DxcValidator,
4277
4333
__uuidof (IDxcValidator),
4278
4334
(IUnknown **)&pValidator));
4335
+
4336
+ // reset the environment variable to its previous value, if it had one.
4337
+ if (!oldEnvVal.empty ()) {
4338
+ SetEnvVarW (L" DXC_DXIL_DLL_PATH" , string_to_wstring (oldEnvVal));
4339
+ }
4279
4340
}
4280
4341
4281
4342
TEST_F (ValidationTest, ValidatePreviewBypassHash) {
0 commit comments