master
cpp 163 lines 4.9 KB
Raw
1 /*++
2
3 Copyright (c) Microsoft. All rights reserved.
4
5 Module Name:
6
7 WSLCCLICredStorageUnitTests.cpp
8
9 Abstract:
10
11 Unit tests for the FileCredStorage and WinCredStorage credential
12 storage backends. Tests Store, Get, List, and Erase operations.
13
14 --*/
15
16 #include "precomp.h"
17 #include "windows/Common.h"
18 #include "FileCredStorage.h"
19 #include "WinCredStorage.h"
20
21 using namespace wsl::windows::wslc::services;
22 using namespace WEX::Logging;
23 using namespace WEX::Common;
24 using namespace WEX::TestExecution;
25
26 namespace WSLCCredStorageUnitTests {
27
28 class WSLCCLICredStorageUnitTests
29 {
30 WSLC_TEST_CLASS(WSLCCLICredStorageUnitTests)
31
32 FileCredStorage m_fileStorage;
33 WinCredStorage m_winCredStorage;
34
35 static void TestStoreAndGetRoundTrips(ICredentialStorage& storage)
36 {
37 auto cleanup = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [&]() { storage.Erase("wslc-test-server1"); });
38 storage.Store("wslc-test-server1", "test-user", "credential-data-1");
39
40 auto [username, secret] = storage.Get("wslc-test-server1");
41 VERIFY_ARE_EQUAL(std::string("test-user"), username);
42 VERIFY_ARE_EQUAL(std::string("credential-data-1"), secret);
43 }
44
45 WSLC_TEST_METHOD(FileCred_Store_And_Get_RoundTrips)
46 {
47 TestStoreAndGetRoundTrips(m_fileStorage);
48 }
49 WSLC_TEST_METHOD(WinCred_Store_And_Get_RoundTrips)
50 {
51 TestStoreAndGetRoundTrips(m_winCredStorage);
52 }
53
54 static void TestGetNonExistentReturnsEmpty(ICredentialStorage& storage)
55 {
56 auto [username, secret] = storage.Get("wslc-test-nonexistent-server");
57 VERIFY_IS_TRUE(username.empty());
58 VERIFY_IS_TRUE(secret.empty());
59 }
60
61 WSLC_TEST_METHOD(FileCred_Get_NonExistent_ReturnsEmpty)
62 {
63 TestGetNonExistentReturnsEmpty(m_fileStorage);
64 }
65 WSLC_TEST_METHOD(WinCred_Get_NonExistent_ReturnsEmpty)
66 {
67 TestGetNonExistentReturnsEmpty(m_winCredStorage);
68 }
69
70 static void TestStoreOverwritesExistingCredential(ICredentialStorage& storage)
71 {
72 auto cleanup = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [&]() { storage.Erase("wslc-test-server2"); });
73 storage.Store("wslc-test-server2", "old-user", "old-credential");
74 storage.Store("wslc-test-server2", "new-user", "new-credential");
75
76 auto [username, secret] = storage.Get("wslc-test-server2");
77 VERIFY_ARE_EQUAL(std::string("new-user"), username);
78 VERIFY_ARE_EQUAL(std::string("new-credential"), secret);
79 }
80
81 WSLC_TEST_METHOD(FileCred_Store_Overwrites_ExistingCredential)
82 {
83 TestStoreOverwritesExistingCredential(m_fileStorage);
84 }
85 WSLC_TEST_METHOD(WinCred_Store_Overwrites_ExistingCredential)
86 {
87 TestStoreOverwritesExistingCredential(m_winCredStorage);
88 }
89
90 static void TestListContainsStoredServers(ICredentialStorage& storage)
91 {
92 auto cleanup = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [&]() {
93 storage.Erase("wslc-test-list1");
94 storage.Erase("wslc-test-list2");
95 });
96 storage.Store("wslc-test-list1", "user1", "cred1");
97 storage.Store("wslc-test-list2", "user2", "cred2");
98
99 auto servers = storage.List();
100 bool found1 = false, found2 = false;
101 for (const auto& s : servers)
102 {
103 if (s == L"wslc-test-list1")
104 {
105 found1 = true;
106 }
107 if (s == L"wslc-test-list2")
108 {
109 found2 = true;
110 }
111 }
112
113 VERIFY_IS_TRUE(found1);
114 VERIFY_IS_TRUE(found2);
115 }
116
117 WSLC_TEST_METHOD(FileCred_List_ContainsStoredServers)
118 {
119 TestListContainsStoredServers(m_fileStorage);
120 }
121 WSLC_TEST_METHOD(WinCred_List_ContainsStoredServers)
122 {
123 TestListContainsStoredServers(m_winCredStorage);
124 }
125
126 static void TestEraseRemovesCredential(ICredentialStorage& storage)
127 {
128 storage.Store("wslc-test-erase", "user", "cred");
129 auto [username, secret] = storage.Get("wslc-test-erase");
130 VERIFY_IS_FALSE(username.empty());
131
132 storage.Erase("wslc-test-erase");
133 auto [username2, secret2] = storage.Get("wslc-test-erase");
134 VERIFY_IS_TRUE(username2.empty());
135 }
136
137 WSLC_TEST_METHOD(FileCred_Erase_RemovesCredential)
138 {
139 TestEraseRemovesCredential(m_fileStorage);
140 }
141 WSLC_TEST_METHOD(WinCred_Erase_RemovesCredential)
142 {
143 TestEraseRemovesCredential(m_winCredStorage);
144 }
145
146 static void TestEraseNonExistentThrows(ICredentialStorage& storage)
147 {
148 VERIFY_THROWS_SPECIFIC(storage.Erase("wslc-test-nonexistent-erase"), wil::ResultException, [](const wil::ResultException& e) {
149 return e.GetErrorCode() == E_NOT_SET;
150 });
151 }
152
153 WSLC_TEST_METHOD(FileCred_Erase_NonExistent_Throws)
154 {
155 TestEraseNonExistentThrows(m_fileStorage);
156 }
157 WSLC_TEST_METHOD(WinCred_Erase_NonExistent_Throws)
158 {
159 TestEraseNonExistentThrows(m_winCredStorage);
160 }
161 };
162
163 } // namespace WSLCCredStorageUnitTests