@@ -344,19 +344,33 @@ TEST(AzureCliCredential, UnsafeChars)
344344 }
345345}
346346
347- TEST (AzureCliCredential, SpaceNotAllowed)
347+ class ParameterizedTestForDisallowedChars : public ::testing::TestWithParam<std::string> {
348+ protected:
349+ std::string value;
350+ };
351+
352+ TEST_P (ParameterizedTestForDisallowedChars, DisallowedCharsForScopeAndTenantId)
348353{
349- std::string const invalid = " space character " ;
354+ std::string const InvalidValue = GetParam () ;
350355
351356 {
352357 AzureCliCredentialOptions options;
353358 options.TenantId = " 01234567-89AB-CDEF-0123-456789ABCDEF" ;
354- options.TenantId += invalid ;
359+ options.TenantId += InvalidValue ;
355360 AzureCliCredential azCliCred (options);
356361
357362 TokenRequestContext trc;
358363 trc.Scopes .push_back (std::string (" https://storage.azure.com/.default" ));
359364 EXPECT_THROW (static_cast <void >(azCliCred.GetToken (trc, {})), AuthenticationException);
365+
366+ try
367+ {
368+ auto const token = azCliCred.GetToken (trc, {});
369+ }
370+ catch (AuthenticationException const & e)
371+ {
372+ EXPECT_TRUE (std::string (e.what ()).find (" Unsafe" ) != std::string::npos) << e.what ();
373+ }
360374 }
361375
362376 {
@@ -365,12 +379,134 @@ TEST(AzureCliCredential, SpaceNotAllowed)
365379 AzureCliCredential azCliCred (options);
366380
367381 TokenRequestContext trc;
368- trc.Scopes .push_back (std::string (" https://storage.azure.com/.default" ) + invalid);
382+ trc.Scopes .push_back (std::string (" https://storage.azure.com/.default" ) + InvalidValue);
383+ EXPECT_THROW (static_cast <void >(azCliCred.GetToken (trc, {})), AuthenticationException);
384+
385+ try
386+ {
387+ auto const token = azCliCred.GetToken (trc, {});
388+ }
389+ catch (AuthenticationException const & e)
390+ {
391+ EXPECT_TRUE (std::string (e.what ()).find (" Unsafe" ) != std::string::npos) << e.what ();
392+ }
393+ }
394+ }
395+
396+ INSTANTIATE_TEST_SUITE_P (
397+ AzureCliCredential,
398+ ParameterizedTestForDisallowedChars,
399+ ::testing::Values (" " , " |" , " `" , " \" " , " '" , " ;" , " &" ));
400+
401+ class ParameterizedTestForCharDifferences : public ::testing::TestWithParam<std::string> {
402+ protected:
403+ std::string value;
404+ };
405+
406+ TEST_P (ParameterizedTestForCharDifferences, ValidCharsForScopeButNotTenantId)
407+ {
408+ std::string const ValidScopeButNotTenantId = GetParam ();
409+
410+ {
411+ AzureCliCredentialOptions options;
412+ options.TenantId = " 01234567-89AB-CDEF-0123-456789ABCDEF" ;
413+ options.TenantId += ValidScopeButNotTenantId;
414+ AzureCliCredential azCliCred (options);
369415
416+ TokenRequestContext trc;
417+ trc.Scopes .push_back (std::string (" https://storage.azure.com/.default" ));
370418 EXPECT_THROW (static_cast <void >(azCliCred.GetToken (trc, {})), AuthenticationException);
419+
420+ try
421+ {
422+ auto const token = azCliCred.GetToken (trc, {});
423+ }
424+ catch (AuthenticationException const & e)
425+ {
426+ EXPECT_TRUE (std::string (e.what ()).find (" Unsafe" ) != std::string::npos) << e.what ();
427+ }
428+ }
429+
430+ {
431+ AzureCliCredentialOptions options;
432+ options.CliProcessTimeout = std::chrono::hours (24 );
433+ AzureCliCredential azCliCred (options);
434+
435+ TokenRequestContext trc;
436+ trc.Scopes .push_back (
437+ std::string (" https://storage.azure.com/.default" ) + ValidScopeButNotTenantId);
438+
439+ // We expect the GetToken to fail, but not because of the unsafe chars.
440+ try
441+ {
442+ auto const token = azCliCred.GetToken (trc, {});
443+ }
444+ catch (AuthenticationException const & e)
445+ {
446+ EXPECT_TRUE (std::string (e.what ()).find (" Unsafe" ) == std::string::npos) << e.what ();
447+ }
371448 }
372449}
373450
451+ INSTANTIATE_TEST_SUITE_P (
452+ AzureCliCredential,
453+ ParameterizedTestForCharDifferences,
454+ ::testing::Values (" :" , " /" , " _" ));
455+
456+ class ParameterizedTestForAllowedChars : public ::testing::TestWithParam<std::string> {
457+ protected:
458+ std::string value;
459+ };
460+
461+ TEST_P (ParameterizedTestForAllowedChars, ValidCharsForScopeAndTenantId)
462+ {
463+ std::string const ValidChars = GetParam ();
464+
465+ {
466+ AzureCliCredentialOptions options;
467+ options.TenantId = " 01234567-89AB-CDEF-0123-456789ABCDEF" ;
468+ options.TenantId += ValidChars;
469+ AzureCliCredential azCliCred (options);
470+
471+ TokenRequestContext trc;
472+ trc.Scopes .push_back (std::string (" https://storage.azure.com/.default" ));
473+
474+ // We expect the GetToken to fail, but not because of the unsafe chars.
475+ try
476+ {
477+ auto const token = azCliCred.GetToken (trc, {});
478+ }
479+ catch (AuthenticationException const & e)
480+ {
481+ EXPECT_TRUE (std::string (e.what ()).find (" Unsafe" ) == std::string::npos) << e.what ();
482+ }
483+ }
484+
485+ {
486+ AzureCliCredentialOptions options;
487+ options.CliProcessTimeout = std::chrono::hours (24 );
488+ AzureCliCredential azCliCred (options);
489+
490+ TokenRequestContext trc;
491+ trc.Scopes .push_back (std::string (" https://storage.azure.com/.default" ) + ValidChars);
492+
493+ // We expect the GetToken to fail, but not because of the unsafe chars.
494+ try
495+ {
496+ auto const token = azCliCred.GetToken (trc, {});
497+ }
498+ catch (AuthenticationException const & e)
499+ {
500+ EXPECT_TRUE (std::string (e.what ()).find (" Unsafe" ) == std::string::npos) << e.what ();
501+ }
502+ }
503+ }
504+
505+ INSTANTIATE_TEST_SUITE_P (
506+ AzureCliCredential,
507+ ParameterizedTestForAllowedChars,
508+ ::testing::Values (" ." , " -" , " A" , " 9" ));
509+
374510TEST (AzureCliCredential, StrictIso8601TimeFormat)
375511{
376512 constexpr auto Token = " {\" accessToken\" :\" ABCDEFGHIJKLMNOPQRSTUVWXYZ\" ,"
0 commit comments