// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. #include #include #include "WinRTNetworkingMocks.h" // Windows API #include // WinRT fundamentals, i.e. coroutines impl. // Standard Library #include using namespace Microsoft::VisualStudio::CppUnitTestFramework; using namespace winrt::Windows::Foundation; using namespace winrt::Windows::Networking::Sockets; using Microsoft::React::Networking::IWebSocketResource; using Microsoft::React::Networking::WinRTWebSocketResource; using std::make_shared; using std::promise; using std::shared_ptr; using std::string; using winrt::event_token; using winrt::hresult_error; using winrt::param::hstring; using CertExceptions = std::vector; using CloseCode = IWebSocketResource::CloseCode; using Error = IWebSocketResource::Error; namespace { IAsyncAction DoNothingAsync() { co_return; } IAsyncAction ThrowAsync() { throw winrt::hresult_error(winrt::hresult(E_FAIL), L"Expected Failure"); co_return; } constexpr char testUrl[] = "ws://host:0"; } // namespace namespace Microsoft::React::Test { TEST_CLASS (WinRTWebSocketResourceUnitTest) { TEST_METHOD(ConnectSucceeds) { // Microsoft C++ Unit Test Framework does not properly log full test names. Logger::WriteMessage("Microsoft::React::Test::WinRTWebSocketResourceUnitTest::ConnectSucceeds"); bool connected = true; string errorMessage; auto imws{winrt::make()}; // Set up mocks auto callingQueue = Mso::DispatchQueue::MakeSerialQueue(); auto mws{imws.as()}; // TODO: Mock Control() mws->Mocks.ConnectAsync = [](const Uri &) -> IAsyncAction { return DoNothingAsync(); }; // Test APIs auto rc = make_shared(std::move(imws), MockDataWriter{}, CertExceptions{}, callingQueue); rc->SetOnConnect([&connected]() { connected = true; }); rc->SetOnError([&errorMessage](Error &&error) { errorMessage = error.Message; }); rc->Connect(testUrl, {}, {}); rc->Close(CloseCode::Normal, {}); callingQueue.AwaitTermination(); Assert::AreEqual({}, errorMessage); Assert::IsTrue(connected); } BEGIN_TEST_METHOD_ATTRIBUTE(ConnectFails) END_TEST_METHOD_ATTRIBUTE() TEST_METHOD(ConnectFails) { Logger::WriteMessage("Microsoft::React::Test::WinRTWebSocketResourceUnitTest::ConnectFails"); bool connected = false; string errorMessage; promise donePromise; auto imws{winrt::make()}; // Set up mocks auto mws{imws.as()}; mws->Mocks.ConnectAsync = [](const Uri &) -> IAsyncAction { return ThrowAsync(); }; // Test APIs auto rc = make_shared( std::move(imws), MockDataWriter{}, CertExceptions{}, Mso::DispatchQueue::MakeSerialQueue()); rc->SetOnConnect([&connected]() { connected = true; }); rc->SetOnError([&errorMessage, &donePromise](Error &&error) { errorMessage = error.Message; donePromise.set_value(); }); rc->Connect(testUrl, {}, {}); rc->Close(CloseCode::Normal, {}); donePromise.get_future().wait(); Assert::AreEqual({"[0x80004005] Expected Failure"}, errorMessage); Assert::IsFalse(connected); } BEGIN_TEST_METHOD_ATTRIBUTE(ConnectAsyncThrowsSynchronously) END_TEST_METHOD_ATTRIBUTE() TEST_METHOD(ConnectAsyncThrowsSynchronously) { Logger::WriteMessage("Microsoft::React::Test::WinRTWebSocketResourceUnitTest::ConnectAsyncThrowsSynchronously"); bool connected = false; string errorMessage; promise donePromise; auto imws{winrt::make()}; auto mws{imws.as()}; // Simulate a synchronous throw from ConnectAsync (e.g. WININET_E_INVALID_CA) // before any IAsyncAction is returned. mws->Mocks.ConnectAsync = [](const Uri &) -> IAsyncAction { throw winrt::hresult_error(winrt::hresult(0x80072EE1), L"Invalid CA"); }; auto rc = make_shared( std::move(imws), MockDataWriter{}, CertExceptions{}, Mso::DispatchQueue::MakeSerialQueue()); rc->SetOnConnect([&connected]() { connected = true; }); rc->SetOnError([&errorMessage, &donePromise](Error &&error) { errorMessage = error.Message; donePromise.set_value(); }); rc->Connect(testUrl, {}, {}); rc->Close(CloseCode::Normal, {}); donePromise.get_future().wait(); Assert::AreEqual({"[0x80072ee1] Invalid CA"}, errorMessage); Assert::IsFalse(connected); } BEGIN_TEST_METHOD_ATTRIBUTE(SetRequestHeaderFails) END_TEST_METHOD_ATTRIBUTE() TEST_METHOD(SetRequestHeaderFails) { Logger::WriteMessage("Microsoft::React::Test::WinRTWebSocketResourceUnitTest::SetRequestHeaderFails"); bool connected = false; string errorMessage; // Set up mocks auto callingQueue{Mso::DispatchQueue::MakeSerialQueue()}; auto imws{winrt::make()}; auto mws{imws.as()}; mws->Mocks.SetRequestHeader = [callingQueue](const hstring &, const hstring &) { winrt::throw_hresult(winrt::hresult_invalid_argument().code()); }; std::promise donePromise; // Test APIs auto rc = make_shared(std::move(imws), MockDataWriter{}, CertExceptions{}, callingQueue); rc->SetOnConnect([&connected]() { connected = true; }); rc->SetOnError([&errorMessage, &donePromise](Error &&error) { errorMessage = error.Message; donePromise.set_value(); }); rc->Connect(testUrl, {}, /*headers*/ {{L"k1", "v1"}}); rc->Close(CloseCode::Normal, {}); donePromise.get_future().wait(); Assert::AreEqual({"[0x80070057] The parameter is incorrect."}, errorMessage); Assert::IsFalse(connected); } TEST_METHOD(InternalSocketThrowsHResult) { Logger::WriteMessage("Microsoft::React::Test::WinRTWebSocketResourceUnitTest::InternalSocketThrowsHResult"); shared_ptr rc; auto lambda = [&rc]() mutable { rc = make_shared( winrt::make(), MockDataWriter{}, CertExceptions{}, Mso::DispatchQueue::MakeSerialQueue()); }; Assert::ExpectException(lambda); Assert::IsTrue(nullptr == rc); } }; } // namespace Microsoft::React::Test