| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | WSLCCLILabelParserUnitTests.cpp |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains unit tests for WSLC CLI label parsing and validation. |
| 12 | --*/ |
| 13 | |
| 14 | #include "precomp.h" |
| 15 | #include "windows/Common.h" |
| 16 | #include "WSLCCLITestHelpers.h" |
| 17 | #include "ArgumentValidation.h" |
| 18 | |
| 19 | using namespace wsl::windows::wslc; |
| 20 | |
| 21 | namespace WSLCCLILabelParserUnitTests { |
| 22 | |
| 23 | class WSLCCLILabelParserUnitTests |
| 24 | { |
| 25 | WSLC_TEST_CLASS(WSLCCLILabelParserUnitTests) |
| 26 | |
| 27 | TEST_METHOD(WSLCCLILabelParser_ValidLabels) |
| 28 | { |
| 29 | std::vector<std::tuple<std::wstring, std::string, std::string>> validLabels = { |
| 30 | {L"foo=bar", "foo", "bar"}, |
| 31 | {L"foo=", "foo", ""}, |
| 32 | {L"foo", "foo", ""}, |
| 33 | {L"foo=a=b=c", "foo", "a=b=c"}, |
| 34 | }; |
| 35 | |
| 36 | for (const auto& [input, expectedKey, expectedValue] : validLabels) |
| 37 | { |
| 38 | auto result = validation::ParseLabel(input); |
| 39 | VERIFY_ARE_EQUAL(expectedKey, result.first); |
| 40 | VERIFY_ARE_EQUAL(expectedValue, result.second); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | TEST_METHOD(WSLCCLILabelParser_InvalidLabels) |
| 45 | { |
| 46 | std::vector<std::wstring> invalidLabels = { |
| 47 | L"", |
| 48 | L"=", |
| 49 | L"=value", |
| 50 | }; |
| 51 | |
| 52 | for (const auto& input : invalidLabels) |
| 53 | { |
| 54 | try |
| 55 | { |
| 56 | (void)validation::ParseLabel(input); |
| 57 | VERIFY_FAIL(L"Expected exception"); |
| 58 | } |
| 59 | catch (const wil::ResultException& ex) |
| 60 | { |
| 61 | VERIFY_ARE_EQUAL(E_INVALIDARG, ex.GetErrorCode()); |
| 62 | |
| 63 | const auto raw = ex.GetFailureInfo().pszMessage; |
| 64 | std::wstring message = raw ? raw : L""; |
| 65 | VERIFY_ARE_EQUAL(L"Label key cannot be empty", message); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | } // namespace WSLCCLILabelParserUnitTests |