Skip to content

Commit 338036f

Browse files
committed
Validate CommitButtonText and SettingsIdentifier
1 parent 71d0016 commit 338036f

File tree

6 files changed

+231
-16
lines changed

6 files changed

+231
-16
lines changed

dev/Interop/StoragePickers/FileOpenPicker.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
3838
}
3939
void FileOpenPicker::SettingsIdentifier(hstring const& value)
4040
{
41+
PickerCommon::ValidateStringNoEmbeddedNulls(value);
4142
m_settingsIdentifier = value;
4243
}
4344
winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId FileOpenPicker::SuggestedStartLocation()
@@ -55,6 +56,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
5556
}
5657
void FileOpenPicker::CommitButtonText(winrt::hstring const& value)
5758
{
59+
PickerCommon::ValidateStringNoEmbeddedNulls(value);
5860
m_commitButtonText = value;
5961
}
6062
winrt::Windows::Foundation::Collections::IVector<hstring> FileOpenPicker::FileTypeFilter()

dev/Interop/StoragePickers/FileSavePicker.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
3434
}
3535
void FileSavePicker::SettingsIdentifier(hstring const& value)
3636
{
37+
PickerCommon::ValidateStringNoEmbeddedNulls(value);
3738
m_settingsIdentifier = value;
3839
}
3940
winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId FileSavePicker::SuggestedStartLocation()
@@ -51,6 +52,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
5152
}
5253
void FileSavePicker::CommitButtonText(hstring const& value)
5354
{
55+
PickerCommon::ValidateStringNoEmbeddedNulls(value);
5456
m_commitButtonText = value;
5557
}
5658
winrt::Windows::Foundation::Collections::IMap<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>> FileSavePicker::FileTypeChoices()
@@ -88,13 +90,13 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
8890
{
8991
return m_suggestedFileName;
9092
}
93+
9194
void FileSavePicker::SuggestedFileName(hstring const& value)
9295
{
9396
PickerCommon::ValidateSuggestedFileName(value);
9497
m_suggestedFileName = value;
9598
}
9699

97-
98100
void FileSavePicker::CaptureParameters(PickerCommon::PickerParameters& parameters)
99101
{
100102
parameters.HWnd = winrt::Microsoft::UI::GetWindowFromWindowId(m_windowId);

dev/Interop/StoragePickers/FolderPicker.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
3535
}
3636
void FolderPicker::SettingsIdentifier(hstring const& value)
3737
{
38+
PickerCommon::ValidateStringNoEmbeddedNulls(value);
3839
m_settingsIdentifier = value;
3940
}
4041
winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId FolderPicker::SuggestedStartLocation()
@@ -52,6 +53,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
5253
}
5354
void FolderPicker::CommitButtonText(hstring const& value)
5455
{
56+
PickerCommon::ValidateStringNoEmbeddedNulls(value);
5557
m_commitButtonText = value;
5658
}
5759

dev/Interop/StoragePickers/PickerCommon.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -179,24 +179,34 @@ namespace PickerCommon {
179179
}
180180
}
181181

182-
void ValidateSingleFileTypeFilterElement(winrt::hstring const& filter)
182+
void ValidateStringNoEmbeddedNulls(winrt::hstring const& value)
183183
{
184-
if (filter.empty() || (filter[0] != L'.' && filter != L"*"))
184+
if (value.empty())
185185
{
186-
throw winrt::hresult_invalid_argument(
187-
PickerLocalization::GetStoragePickersLocalizationText(L"IDS_APIERROR_IMPROPERFILEEXTENSION"));
186+
return;
188187
}
189188

190-
for (const auto& ch : filter)
189+
for (int i = 0; i < value.size(); i++)
191190
{
192-
if (ch == L'\0')
191+
if (value[i] == L'\0')
193192
{
194193
throw winrt::hresult_invalid_argument(
195194
PickerLocalization::GetStoragePickersLocalizationText(L"IDS_APIERROR_STRINGSNOEMBEDDEDNULLS"));
196195
}
197196
}
198197
}
199198

199+
void ValidateSingleFileTypeFilterElement(winrt::hstring const& filter)
200+
{
201+
if (filter.empty() || (filter[0] != L'.' && filter != L"*"))
202+
{
203+
throw winrt::hresult_invalid_argument(
204+
PickerLocalization::GetStoragePickersLocalizationText(L"IDS_APIERROR_IMPROPERFILEEXTENSION"));
205+
}
206+
207+
ValidateStringNoEmbeddedNulls(filter);
208+
}
209+
200210
void ValidateSuggestedFileName(winrt::hstring const& suggestedFileName)
201211
{
202212
if (suggestedFileName.size() > MAX_PATH)
@@ -205,14 +215,7 @@ namespace PickerCommon {
205215
PickerLocalization::GetStoragePickersLocalizationText(L"IDS_APIERROR_MAXSAVEFILELENGTHEXCEEDED"));
206216
}
207217

208-
for (const auto& ch : suggestedFileName)
209-
{
210-
if (ch == L'\0')
211-
{
212-
throw winrt::hresult_invalid_argument(
213-
PickerLocalization::GetStoragePickersLocalizationText(L"IDS_APIERROR_STRINGSNOEMBEDDEDNULLS"));
214-
}
215-
}
218+
ValidateStringNoEmbeddedNulls(suggestedFileName);
216219
}
217220

218221
winrt::hstring PickerParameters::FormatExtensionWithWildcard(winrt::hstring extension)

dev/Interop/StoragePickers/PickerCommon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace PickerCommon {
1717

1818
std::pair<winrt::com_ptr<IShellItem>, std::wstring> ParseFolderItemAndFileName(winrt::hstring const& filePath);
1919

20+
void ValidateStringNoEmbeddedNulls(winrt::hstring const& value);
2021
void ValidateViewMode(winrt::Microsoft::Windows::Storage::Pickers::PickerViewMode const& value);
2122
void ValidateSuggestedStartLocation(winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId const& value);
2223
void ValidateSingleFileTypeFilterElement(winrt::hstring const& filter);

test/StoragePickersTests/PickerCommonTests.cpp

Lines changed: 206 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ namespace Test::PickerCommonTests
387387
}
388388
}
389389

390-
TEST_METHOD(VerifySuggestedFileName)
390+
TEST_METHOD(VerifyValidateSuggestedFileName)
391391
{
392392
// Arrange.
393393
auto test_cases = std::vector<std::tuple<winrt::hstring, bool>>{
@@ -431,6 +431,211 @@ namespace Test::PickerCommonTests
431431
}
432432
}
433433

434+
std::vector<std::tuple<winrt::hstring, bool>> cbsi_text_test_cases{
435+
{L"Valid", true}, // Valid commit button text
436+
{L"", true}, // Allow empty string
437+
{L"Valid Text", true}, // Allow spaces
438+
{L"Invalid\0Text", false}, // Embedded null is invalid
439+
};
440+
441+
TEST_METHOD(VerifyFolderPicker_ValidateCommitButtonText)
442+
{
443+
// Arrange.
444+
winrt::Microsoft::UI::WindowId windowId{};
445+
winrt::Microsoft::Windows::Storage::Pickers::FolderPicker picker(windowId);
446+
447+
// Act & Assert
448+
for (const auto& test_case : cbsi_text_test_cases)
449+
{
450+
winrt::hstring commitButtonText = std::get<0>(test_case);
451+
bool expectValid = std::get<1>(test_case);
452+
453+
if (expectValid)
454+
{
455+
picker.CommitButtonText(commitButtonText);
456+
VERIFY_ARE_EQUAL(picker.CommitButtonText(), commitButtonText);
457+
}
458+
else
459+
{
460+
try
461+
{
462+
picker.CommitButtonText(commitButtonText);
463+
std::wstring errorMessage = L"Expected exception for invalid commit button text: " + std::wstring(commitButtonText);
464+
VERIFY_FAIL(errorMessage.c_str());
465+
}
466+
catch (...)
467+
{
468+
// Expected exception for invalid commit button text
469+
}
470+
}
471+
}
472+
}
473+
474+
TEST_METHOD(VerifyFileOpenPicker_ValidateCommitButtonText)
475+
{
476+
// Arrange.
477+
winrt::Microsoft::UI::WindowId windowId{};
478+
winrt::Microsoft::Windows::Storage::Pickers::FileOpenPicker picker(windowId);
479+
480+
// Act & Assert
481+
for (const auto& test_case : cbsi_text_test_cases)
482+
{
483+
winrt::hstring commitButtonText = std::get<0>(test_case);
484+
bool expectValid = std::get<1>(test_case);
485+
486+
if (expectValid)
487+
{
488+
picker.CommitButtonText(commitButtonText);
489+
VERIFY_ARE_EQUAL(picker.CommitButtonText(), commitButtonText);
490+
}
491+
else
492+
{
493+
try
494+
{
495+
picker.CommitButtonText(commitButtonText);
496+
std::wstring errorMessage = L"Expected exception for invalid commit button text: " + std::wstring(commitButtonText);
497+
VERIFY_FAIL(errorMessage.c_str());
498+
}
499+
catch (...)
500+
{
501+
// Expected exception for invalid commit button text
502+
}
503+
}
504+
}
505+
}
506+
507+
TEST_METHOD(VerifyFileSavePicker_ValidateCommitButtonText)
508+
{
509+
// Arrange.
510+
winrt::Microsoft::UI::WindowId windowId{};
511+
winrt::Microsoft::Windows::Storage::Pickers::FileSavePicker picker(windowId);
512+
513+
// Act & Assert
514+
for (const auto& test_case : cbsi_text_test_cases)
515+
{
516+
winrt::hstring commitButtonText = std::get<0>(test_case);
517+
bool expectValid = std::get<1>(test_case);
518+
519+
if (expectValid)
520+
{
521+
picker.CommitButtonText(commitButtonText);
522+
VERIFY_ARE_EQUAL(picker.CommitButtonText(), commitButtonText);
523+
}
524+
else
525+
{
526+
try
527+
{
528+
picker.CommitButtonText(commitButtonText);
529+
std::wstring errorMessage = L"Expected exception for invalid commit button text: " + std::wstring(commitButtonText);
530+
VERIFY_FAIL(errorMessage.c_str());
531+
}
532+
catch (...)
533+
{
534+
// Expected exception for invalid commit button text
535+
}
536+
}
537+
}
538+
}
539+
540+
TEST_METHOD(VerifyFolderPicker_ValidateSettingsIdentifer)
541+
{
542+
// Arrange.
543+
winrt::Microsoft::UI::WindowId windowId{};
544+
winrt::Microsoft::Windows::Storage::Pickers::FolderPicker picker(windowId);
545+
546+
// Act & Assert
547+
for (const auto& test_case : cbsi_text_test_cases)
548+
{
549+
winrt::hstring settingsIdentifier = std::get<0>(test_case);
550+
bool expectValid = std::get<1>(test_case);
551+
552+
if (expectValid)
553+
{
554+
picker.SettingsIdentifier(settingsIdentifier);
555+
VERIFY_ARE_EQUAL(picker.SettingsIdentifier(), settingsIdentifier);
556+
}
557+
else
558+
{
559+
try
560+
{
561+
picker.SettingsIdentifier(settingsIdentifier);
562+
std::wstring errorMessage = L"Expected exception for invalid commit button text: " + std::wstring(settingsIdentifier);
563+
VERIFY_FAIL(errorMessage.c_str());
564+
}
565+
catch (...)
566+
{
567+
// Expected exception for invalid commit button text
568+
}
569+
}
570+
}
571+
}
572+
573+
TEST_METHOD(VerifyFileOpenPicker_ValidateSettingsIdentifier)
574+
{
575+
// Arrange.
576+
winrt::Microsoft::UI::WindowId windowId{};
577+
winrt::Microsoft::Windows::Storage::Pickers::FileOpenPicker picker(windowId);
578+
579+
// Act & Assert
580+
for (const auto& test_case : cbsi_text_test_cases)
581+
{
582+
winrt::hstring settingsIdentifier = std::get<0>(test_case);
583+
bool expectValid = std::get<1>(test_case);
584+
585+
if (expectValid)
586+
{
587+
picker.SettingsIdentifier(settingsIdentifier);
588+
VERIFY_ARE_EQUAL(picker.SettingsIdentifier(), settingsIdentifier);
589+
}
590+
else
591+
{
592+
try
593+
{
594+
picker.SettingsIdentifier(settingsIdentifier);
595+
std::wstring errorMessage = L"Expected exception for invalid commit button text: " + std::wstring(settingsIdentifier);
596+
VERIFY_FAIL(errorMessage.c_str());
597+
}
598+
catch (...)
599+
{
600+
// Expected exception for invalid commit button text
601+
}
602+
}
603+
}
604+
}
605+
606+
TEST_METHOD(VerifyFileSavePicker_ValidateSettingsIdentifier)
607+
{
608+
// Arrange.
609+
winrt::Microsoft::UI::WindowId windowId{};
610+
winrt::Microsoft::Windows::Storage::Pickers::FileSavePicker picker(windowId);
611+
612+
// Act & Assert
613+
for (const auto& test_case : cbsi_text_test_cases)
614+
{
615+
winrt::hstring settingsIdentifier = std::get<0>(test_case);
616+
bool expectValid = std::get<1>(test_case);
617+
618+
if (expectValid)
619+
{
620+
picker.SettingsIdentifier(settingsIdentifier);
621+
VERIFY_ARE_EQUAL(picker.SettingsIdentifier(), settingsIdentifier);
622+
}
623+
else
624+
{
625+
try
626+
{
627+
picker.SettingsIdentifier(settingsIdentifier);
628+
std::wstring errorMessage = L"Expected exception for invalid commit button text: " + std::wstring(settingsIdentifier);
629+
VERIFY_FAIL(errorMessage.c_str());
630+
}
631+
catch (...)
632+
{
633+
// Expected exception for invalid commit button text
634+
}
635+
}
636+
}
637+
}
638+
434639
std::vector<std::tuple<winrt::hstring, bool>> file_extension_validation_test_cases{
435640
{L".txt", true},
436641
{L"*", true}, // One asterisk is valid

0 commit comments

Comments
 (0)