diff --git a/internal/platform/implementation/apple/Tests/BUILD b/internal/platform/implementation/apple/Tests/BUILD index fc17ce20..d6d3e44a 100644 --- a/internal/platform/implementation/apple/Tests/BUILD +++ b/internal/platform/implementation/apple/Tests/BUILD @@ -38,6 +38,7 @@ objc_library( "GNCWifiHotspotMediumTest.mm", "GNCWifiLanMediumTest.mm", "UtilsTest.mm", + "ble_l2cap_server_socket_test.mm", "ble_medium_test.mm", "ble_peripheral_test.mm", "ble_server_socket_test.mm", diff --git a/internal/platform/implementation/apple/Tests/ble_l2cap_server_socket_test.mm b/internal/platform/implementation/apple/Tests/ble_l2cap_server_socket_test.mm new file mode 100644 index 00000000..88b47282 --- /dev/null +++ b/internal/platform/implementation/apple/Tests/ble_l2cap_server_socket_test.mm @@ -0,0 +1,96 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "internal/platform/implementation/apple/ble_l2cap_server_socket.h" + +#import + +#include +#include + +#include "internal/platform/exception.h" +#import "internal/platform/implementation/apple/Mediums/BLE/GNCBLEL2CAPConnection.h" +#import "internal/platform/implementation/apple/Mediums/BLE/GNCBLEL2CAPStream.h" +#import "internal/platform/implementation/apple/Mediums/BLE/Tests/GNCBLEL2CAPFakeInputOutputStream.h" +#include "internal/platform/implementation/apple/ble_l2cap_socket.h" + +@interface GNCBleL2capServerSocketTest : XCTestCase +@end + +@implementation GNCBleL2capServerSocketTest { + std::unique_ptr _serverSocket; +} + +- (void)setUp { + [super setUp]; + _serverSocket = std::make_unique(); +} + +- (void)tearDown { + _serverSocket.reset(); + [super tearDown]; +} + +- (void)testBleL2capServerSocketGetSetPSM { + int psm = 10; + _serverSocket->SetPSM(psm); + XCTAssertEqual(_serverSocket->GetPSM(), psm); +} + +- (void)testBleL2capServerSocketAccept { + XCTestExpectation *expectation = [self expectationWithDescription:@"accept"]; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + std::unique_ptr clientSocket = _serverSocket->Accept(); + XCTAssertNotEqual(clientSocket.get(), nullptr); + [expectation fulfill]; + }); + + GNCBLEL2CAPFakeInputOutputStream *fakeInputOutputStream = + [[GNCBLEL2CAPFakeInputOutputStream alloc] initWithBufferSize:1024]; + GNCBLEL2CAPStream *stream = [[GNCBLEL2CAPStream alloc] + initWithClosedBlock:^() { + } + inputStream:fakeInputOutputStream.inputStream + outputStream:fakeInputOutputStream.outputStream]; + GNCBLEL2CAPConnection *connection = + [GNCBLEL2CAPConnection connectionWithStream:stream + serviceID:nil + incomingConnection:NO + callbackQueue:dispatch_get_main_queue()]; + auto socket = std::make_unique(connection); + XCTAssertTrue(_serverSocket->AddPendingSocket(std::move(socket))); + + [self waitForExpectationsWithTimeout:1 handler:nil]; +} + +- (void)testBleL2capServerSocketClose { + XCTestExpectation *expectation = [self expectationWithDescription:@"close"]; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + std::unique_ptr clientSocket = _serverSocket->Accept(); + XCTAssertEqual(clientSocket.get(), nullptr); + [expectation fulfill]; + }); + + XCTAssertEqual(_serverSocket->Close().value, nearby::Exception::kSuccess); + [self waitForExpectationsWithTimeout:1 handler:nil]; +} + +- (void)testBleL2capServerSocketSetCloseNotifier { + bool notifierCalled = false; + _serverSocket->SetCloseNotifier([¬ifierCalled] { notifierCalled = true; }); + XCTAssertEqual(_serverSocket->Close().value, nearby::Exception::kSuccess); + XCTAssertTrue(notifierCalled); +} + +@end