@@ -4211,46 +4211,31 @@ TEST_F(ValidationTest, ValidateWithHash) {
4211
4211
VERIFY_ARE_EQUAL (memcmp (Result, pHeader->Hash .Digest , sizeof (Result)), 0 );
4212
4212
}
4213
4213
4214
- std::wstring GetEnvVarW (const std::wstring &varName ) {
4214
+ std::wstring GetEnvVarW (const std::wstring &VarName ) {
4215
4215
#ifdef _WIN32
4216
- DWORD size = GetEnvironmentVariableW (varName.c_str (), nullptr , 0 );
4217
- if (size == 0 ) {
4218
- return L" " ; // Not found or empty
4219
- }
4220
-
4221
- std::wstring buffer (size - 1 , ' \0 ' ); // size includes null terminator
4222
- GetEnvironmentVariableW (varName.c_str (), &buffer[0 ], size);
4223
- return buffer;
4224
- #else
4225
- const char *result = std::getenv (varName.c_str ());
4226
- return result ? std::string (result) : std::string ();
4227
- #endif
4228
- }
4229
-
4230
- void SetEnvVarW (const std::wstring &varName, const std::wstring &varValue) {
4231
- #ifdef _WIN32
4232
- VERIFY_IS_TRUE (SetEnvironmentVariableW (varName.c_str (), varValue.c_str ()));
4233
- // also update the CRT environment
4234
- std::string varNameStr;
4235
- std::string varValueStr;
4236
- Unicode::WideToUTF8String (varName.c_str (), &varNameStr);
4237
- Unicode::WideToUTF8String (varValue.c_str (), &varValueStr);
4238
- _putenv_s (varNameStr.c_str (), varValueStr.c_str ());
4216
+ if (const wchar_t *Result = _wgetenv (VarName.c_str ()))
4217
+ return std::wstring (Result);
4239
4218
#else
4240
- std::string name_utf8 = wstring_to_utf8 (varName);
4241
- std::string value_utf8 = wstring_to_utf8 (varValue);
4242
- setenv (name_utf8.c_str (), value_utf8.c_str (), 1 );
4219
+ std::string NameUtf8;
4220
+ WideToUTF8String (Name.c_str , &NameUtf8);
4221
+ if (const char *Result = std::getenv (NameUtf8.c_str ())) {
4222
+ std::wstring ResultWide;
4223
+ Unicode::UTF8ToWideString (Result.c_str (), &ResultWide);
4224
+ return std::wstring (ResultWide);
4225
+ }
4243
4226
#endif
4227
+ return std::wstring ();
4244
4228
}
4245
4229
4246
- void ClearEnvVarW (const std::wstring &varName) {
4247
- std::string varNameStr;
4248
- Unicode::WideToUTF8String (varName.c_str (), &varNameStr);
4230
+ void SetEnvVarW (const std::wstring &VarName, const std::wstring &VarValue) {
4249
4231
#ifdef _WIN32
4250
- SetEnvironmentVariableW (varName.c_str (), nullptr );
4251
- _putenv_s (varNameStr.c_str (), " " );
4232
+ _wputenv_s (VarName.c_str (), VarValue.c_str ());
4252
4233
#else
4253
- unsetenv (varNameStr.c_str ());
4234
+ std::string VarNameUtf8;
4235
+ std::string VarValueUtf8;
4236
+ Unicode::WideToUTF8String (VarName.c_str (), &VarNameUtf8);
4237
+ Unicode::WideToUTF8String (VarValue.c_str (), &VarValueUtf8);
4238
+ setenv (VarNameUtf8.c_str (), VarValueUtf8.c_str (), 1 );
4254
4239
#endif
4255
4240
}
4256
4241
@@ -4266,70 +4251,66 @@ void ClearEnvVarW(const std::wstring &varName) {
4266
4251
// works as intended.
4267
4252
4268
4253
TEST_F (ValidationTest, UnitTestExtValidationSupport) {
4269
- DxcDllExtValidationSupport m_dllExtSupport1 ;
4270
- DxcDllExtValidationSupport m_dllExtSupport2 ;
4254
+ DxcDllExtValidationSupport ExtSupportEmpty ;
4255
+ DxcDllExtValidationSupport ExtSupportBogus ;
4271
4256
4272
4257
// capture any existing value in the environment variable,
4273
4258
// so that it can be restored after the test
4274
- std::wstring oldEnvVal = GetEnvVarW (L" DXC_DXIL_DLL_PATH" );
4259
+ std::wstring OldEnvVal = GetEnvVarW (L" DXC_DXIL_DLL_PATH" );
4275
4260
4276
4261
// 1. with no env var set, test GetDxilDllPath() and DxilDllFailedToLoad()
4277
4262
4278
4263
// make sure the variable is cleared, in case other tests may have set it
4279
4264
SetEnvVarW (L" DXC_DXIL_DLL_PATH" , L" " );
4280
4265
4281
4266
// empty initialization should succeed
4282
- VERIFY_SUCCEEDED (m_dllExtSupport1 .Initialize ());
4267
+ VERIFY_SUCCEEDED (ExtSupportEmpty .Initialize ());
4283
4268
4284
- VERIFY_IS_FALSE (m_dllExtSupport1 .DxilDllFailedToLoad ());
4285
- VERIFY_ARE_EQUAL (m_dllExtSupport1 .GetDxilDllPath (), " " );
4269
+ VERIFY_IS_FALSE (ExtSupportEmpty .DxilDllFailedToLoad ());
4270
+ VERIFY_ARE_EQUAL_WSTR (ExtSupportEmpty .GetDxilDllPath (). c_str (), L "" );
4286
4271
4287
4272
// 2. Test with a bogus path in the environment variable
4288
4273
SetEnvVarW (L" DXC_DXIL_DLL_PATH" , L" bogus" );
4289
4274
4290
- if (!m_dllExtSupport2 .IsEnabled ()) {
4291
- VERIFY_SUCCEEDED (m_dllExtSupport2 .Initialize ());
4275
+ if (!ExtSupportBogus .IsEnabled ()) {
4276
+ VERIFY_SUCCEEDED (ExtSupportBogus .Initialize ());
4292
4277
}
4293
4278
4294
4279
// validate that m_dllExtSupport2 was able to capture the environment
4295
4280
// variable's value, and that loading the bogus path was unsuccessful
4296
- std::string extPath (" bogus" );
4297
- VERIFY_ARE_EQUAL (m_dllExtSupport2.GetDxilDllPath (), extPath);
4298
- VERIFY_IS_TRUE (m_dllExtSupport2.DxilDllFailedToLoad ());
4281
+ VERIFY_ARE_EQUAL_WSTR (ExtSupportBogus.GetDxilDllPath ().c_str (), L" bogus" );
4282
+ VERIFY_IS_TRUE (ExtSupportBogus.DxilDllFailedToLoad ());
4299
4283
4300
4284
// 3. Test production of class IDs CLSID_DxcCompiler, CLSID_DxcLinker,
4301
4285
// and CLSID_DxcValidator through DxcDllExtValidationSupport.
4302
- CComPtr<IDxcCompiler> pCompiler;
4303
- CComPtr<IDxcLinker> pLinker;
4304
- CComPtr<IDxcValidator> pValidator;
4305
- CComPtr<IDxcOperationResult> pResult;
4306
-
4307
- VERIFY_SUCCEEDED (m_dllExtSupport2.CreateInstance (
4308
- CLSID_DxcCompiler, __uuidof (IDxcCompiler), (IUnknown **)&pCompiler));
4309
- VERIFY_SUCCEEDED (m_dllExtSupport2.CreateInstance (
4310
- CLSID_DxcLinker, __uuidof (IDxcLinker), (IUnknown **)&pLinker));
4311
- VERIFY_SUCCEEDED (m_dllExtSupport2.CreateInstance (
4312
- CLSID_DxcValidator, __uuidof (IDxcValidator), (IUnknown **)&pValidator));
4313
- CComPtr<IMalloc> pMalloc;
4314
- CComPtr<IDxcCompiler2> pCompiler2;
4315
- pLinker.Release ();
4316
- pValidator.Release ();
4317
- VERIFY_SUCCEEDED (DxcCoGetMalloc (1 , &pMalloc));
4318
- VERIFY_SUCCEEDED (m_dllExtSupport2.CreateInstance2 (pMalloc, CLSID_DxcCompiler,
4319
- __uuidof (IDxcCompiler),
4320
- (IUnknown **)&pCompiler2));
4321
- VERIFY_SUCCEEDED (m_dllExtSupport2.CreateInstance2 (
4322
- pMalloc, CLSID_DxcLinker, __uuidof (IDxcLinker), (IUnknown **)&pLinker));
4323
- VERIFY_SUCCEEDED (m_dllExtSupport2.CreateInstance2 (pMalloc, CLSID_DxcValidator,
4324
- __uuidof (IDxcValidator),
4325
- (IUnknown **)&pValidator));
4326
-
4327
- // reset the environment variable to its previous value, if it had one.
4328
- if (!oldEnvVal.empty ()) {
4329
- SetEnvVarW (L" DXC_DXIL_DLL_PATH" , oldEnvVal);
4330
- } else {
4331
- ClearEnvVarW (L" DXC_DXIL_DLL_PATH" );
4332
- }
4286
+ CComPtr<IDxcCompiler> Compiler;
4287
+ CComPtr<IDxcLinker> Linker;
4288
+ CComPtr<IDxcValidator> Validator;
4289
+
4290
+ VERIFY_SUCCEEDED (ExtSupportBogus.CreateInstance (
4291
+ CLSID_DxcCompiler, __uuidof (IDxcCompiler), (IUnknown **)&Compiler));
4292
+ VERIFY_SUCCEEDED (ExtSupportBogus.CreateInstance (
4293
+ CLSID_DxcLinker, __uuidof (IDxcLinker), (IUnknown **)&Linker));
4294
+ VERIFY_SUCCEEDED (ExtSupportBogus.CreateInstance (
4295
+ CLSID_DxcValidator, __uuidof (IDxcValidator), (IUnknown **)&Validator));
4296
+
4297
+ CComPtr<IMalloc> Malloc;
4298
+ CComPtr<IDxcCompiler2> Compiler2;
4299
+ Linker.Release ();
4300
+ Validator.Release ();
4301
+ VERIFY_SUCCEEDED (DxcCoGetMalloc (1 , &Malloc));
4302
+ VERIFY_SUCCEEDED (ExtSupportBogus.CreateInstance2 (Malloc, CLSID_DxcCompiler,
4303
+ __uuidof (IDxcCompiler),
4304
+ (IUnknown **)&Compiler2));
4305
+ VERIFY_SUCCEEDED (ExtSupportBogus.CreateInstance2 (
4306
+ Malloc, CLSID_DxcLinker, __uuidof (IDxcLinker), (IUnknown **)&Linker));
4307
+ VERIFY_SUCCEEDED (ExtSupportBogus.CreateInstance2 (Malloc, CLSID_DxcValidator,
4308
+ __uuidof (IDxcValidator),
4309
+ (IUnknown **)&Validator));
4310
+
4311
+ // reset the environment variable to its previous value,
4312
+ // or the empty string if there was no previous value
4313
+ SetEnvVarW (L" DXC_DXIL_DLL_PATH" , OldEnvVal);
4333
4314
}
4334
4315
4335
4316
TEST_F (ValidationTest, ValidatePreviewBypassHash) {
0 commit comments