From 1d8fc950e3697a02c7cd3ba98425dcf0b232af48 Mon Sep 17 00:00:00 2001 From: Edwin Wu Date: Wed, 29 Oct 2025 18:46:17 -0700 Subject: [PATCH] [NC Apple coverage] Add unit tests for `BleServerSocket` on Apple. PiperOrigin-RevId: 825799425 --- .../platform/implementation/apple/Tests/BUILD | 1 + .../apple/Tests/ble_server_socket_test.mm | 77 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 internal/platform/implementation/apple/Tests/ble_server_socket_test.mm diff --git a/internal/platform/implementation/apple/Tests/BUILD b/internal/platform/implementation/apple/Tests/BUILD index 8c998ab8..fc17ce20 100644 --- a/internal/platform/implementation/apple/Tests/BUILD +++ b/internal/platform/implementation/apple/Tests/BUILD @@ -40,6 +40,7 @@ objc_library( "UtilsTest.mm", "ble_medium_test.mm", "ble_peripheral_test.mm", + "ble_server_socket_test.mm", "ble_socket_test.mm", "network_utils_test.mm", ], diff --git a/internal/platform/implementation/apple/Tests/ble_server_socket_test.mm b/internal/platform/implementation/apple/Tests/ble_server_socket_test.mm new file mode 100644 index 00000000..1b887880 --- /dev/null +++ b/internal/platform/implementation/apple/Tests/ble_server_socket_test.mm @@ -0,0 +1,77 @@ +// 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_server_socket.h" + +#import + +#include +#include + +#include "internal/platform/exception.h" +#import "internal/platform/implementation/apple/Mediums/BLE/Tests/GNCMFakeConnection.h" +#include "internal/platform/implementation/apple/ble_socket.h" + +@interface GNCBleServerSocketTest : XCTestCase +@end + +@implementation GNCBleServerSocketTest { + std::unique_ptr _serverSocket; +} + +- (void)setUp { + [super setUp]; + _serverSocket = std::make_unique(); +} + +- (void)tearDown { + _serverSocket.reset(); + [super tearDown]; +} + +- (void)testBleServerSocketAccept { + 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]; + }); + + auto fakeConnection = [[GNCMFakeConnection alloc] init]; + auto socket = std::make_unique(fakeConnection); + XCTAssertTrue(_serverSocket->Connect(std::move(socket))); + + [self waitForExpectationsWithTimeout:1 handler:nil]; +} + +- (void)testBleServerSocketClose { + 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)testBleServerSocketSetCloseNotifier { + bool notifierCalled = false; + _serverSocket->SetCloseNotifier([¬ifierCalled] { notifierCalled = true; }); + XCTAssertEqual(_serverSocket->Close().value, nearby::Exception::kSuccess); + XCTAssertTrue(notifierCalled); +} + +@end