Skip to content

Commit 3300195

Browse files
committed
rework changes
1 parent a05c9f8 commit 3300195

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

src/windows/WslcSDK/wslcsdk.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,26 @@ try
442442
}
443443
CATCH_RETURN();
444444

445+
STDAPI WslcGetCliSession(_Out_ WslcSession* session, _Outptr_opt_result_z_ PWSTR* errorMessage)
446+
try
447+
{
448+
RETURN_HR_IF_NULL(E_POINTER, session);
449+
*session = nullptr;
450+
ErrorInfoWrapper errorInfoWrapper{errorMessage};
451+
452+
wil::com_ptr<IWSLCSessionManager> sessionManager = CreateSessionManager();
453+
454+
auto result = std::make_unique<WslcSessionImpl>();
455+
if (SUCCEEDED(errorInfoWrapper.CaptureResult(sessionManager->OpenSessionByName(nullptr, &result->session))))
456+
{
457+
wsl::windows::common::security::ConfigureForCOMImpersonation(result->session.get());
458+
*session = reinterpret_cast<WslcSession>(result.release());
459+
}
460+
461+
return errorInfoWrapper;
462+
}
463+
CATCH_RETURN();
464+
445465
STDAPI WslcTerminateSession(_In_ WslcSession session)
446466
try
447467
{

src/windows/WslcSDK/wslcsdk.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ WslcInitContainerSettings
99
WslcInitProcessSettings
1010

1111
WslcCreateSession
12+
WslcGetCliSession
1213
WslcCreateContainer
1314

1415
WslcReleaseSession

src/windows/WslcSDK/wslcsdk.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ STDAPI WslcInitSessionSettings(_In_ PCWSTR name, _In_ PCWSTR storagePath, _Out_
9898

9999
STDAPI WslcCreateSession(_In_ WslcSessionSettings* sessionSettings, _Out_ WslcSession* session, _Outptr_opt_result_z_ PWSTR* errorMessage);
100100

101+
STDAPI WslcGetCliSession(_Out_ WslcSession* session, _Outptr_opt_result_z_ PWSTR* errorMessage);
102+
101103
// OPTIONAL SESSION SETTINGS
102104
STDAPI WslcSetSessionSettingsCpuCount(_In_ WslcSessionSettings* sessionSettings, _In_ uint32_t cpuCount);
103105
STDAPI WslcSetSessionSettingsMemory(_In_ WslcSessionSettings* sessionSettings, _In_ uint32_t memoryMb);

test/windows/WslcSdkTests.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Module Name:
1818
#include "WslcsdkPrivate.h"
1919
#include "WSLCContainerLauncher.h"
2020
#include "wslc_schema.h"
21+
#include "e2e/WSLCExecutor.h"
2122
#include <optional>
2223

2324
extern std::wstring g_testDataPath;
@@ -42,6 +43,16 @@ void CloseSession(WslcSession session)
4243

4344
using UniqueSession = wil::unique_any<WslcSession, decltype(CloseSession), CloseSession>;
4445

46+
void ReleaseSession(WslcSession session)
47+
{
48+
if (session)
49+
{
50+
WslcReleaseSession(session);
51+
}
52+
}
53+
54+
using UniqueSessionRef = wil::unique_any<WslcSession, decltype(ReleaseSession), ReleaseSession>;
55+
4556
void CloseContainer(WslcContainer container)
4657
{
4758
if (container)
@@ -265,6 +276,29 @@ class WslcSdkTests
265276
VERIFY_ARE_EQUAL(WslcCreateSession(nullptr, &session2, nullptr), E_POINTER);
266277
}
267278

279+
WSLC_TEST_METHOD(GetCliSession)
280+
{
281+
// Null output pointer must fail.
282+
VERIFY_ARE_EQUAL(WslcGetCliSession(nullptr, nullptr), E_POINTER);
283+
284+
// Ensure no CLI session is running.
285+
WSLCE2ETests::RunWslc(L"session terminate");
286+
287+
// WslcGetCliSession must return ERROR_NOT_FOUND when no CLI session exists.
288+
UniqueSessionRef notFoundSession;
289+
VERIFY_ARE_EQUAL(WslcGetCliSession(&notFoundSession, nullptr), HRESULT_FROM_WIN32(ERROR_NOT_FOUND));
290+
VERIFY_IS_NULL(notFoundSession.get());
291+
292+
// Start the CLI session by running a wslc command.
293+
auto result = WSLCE2ETests::RunWslc(L"container list");
294+
VERIFY_ARE_EQUAL(result.ExitCode.value(), (DWORD)0);
295+
296+
// Now WslcGetCliSession should find the running CLI session.
297+
UniqueSessionRef foundSession;
298+
VERIFY_SUCCEEDED(WslcGetCliSession(&foundSession, nullptr));
299+
VERIFY_IS_NOT_NULL(foundSession.get());
300+
}
301+
268302
WSLC_TEST_METHOD(TerminationCallbackViaTerminate)
269303
{
270304
std::promise<WslcSessionTerminationReason> promise;

0 commit comments

Comments
 (0)