ios: nearbyConnections: Move source to //third_party.

PiperOrigin-RevId: 408046875
This commit is contained in:
edwinwu
2021-11-06 09:45:54 -07:00
committed by Copybara-Service
parent 8c2dd35eac
commit fb0337ebfa
76 changed files with 6192 additions and 7 deletions
@@ -0,0 +1,52 @@
// Copyright 2020 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.
#import <XCTest/XCTest.h>
#include "third_party/nearby_connections/cpp/platform/api/crypto.h"
#include "third_party/nearby_connections/cpp/platform/base/byte_array.h"
@interface GNCCryptoTest : XCTestCase
@end
@implementation GNCCryptoTest
// Tests that the hash functions return the expected values.
- (void)testHashValues {
std::string string("com.google.location.nearby");
// SHA-256 test.
{
const uint8_t sha256ExpectedHash[] = {
0x09, 0x8e, 0x3c, 0x54, 0x2d, 0xee, 0x9e, 0x35,
0x1d, 0xe7, 0x5b, 0xcf, 0xda, 0xb5, 0x62, 0xd3,
0xde, 0xba, 0x14, 0x49, 0xbd, 0xf5, 0x95, 0x04,
0x1f, 0x1d, 0x99, 0x84, 0x87, 0xc3, 0xcb, 0x8a, };
location::nearby::ByteArray sha256Hash = location::nearby::Crypto::Sha256(string);
XCTAssert(sha256Hash.size() == sizeof(sha256ExpectedHash) &&
memcmp(sha256Hash.data(), sha256ExpectedHash, sizeof(sha256ExpectedHash)) == 0);
}
// MD5 test.
{
const uint8_t md5ExpectedHash[] = {
0x97, 0x78, 0x1d, 0x3d, 0xee, 0xd7, 0xdc, 0x5a,
0x6e, 0xee, 0x50, 0x08, 0xce, 0xd1, 0xb2, 0xe8 };
location::nearby::ByteArray md5Hash = location::nearby::Crypto::Md5(string);
XCTAssert(md5Hash.size() == sizeof(md5ExpectedHash) &&
memcmp(md5Hash.data(), md5ExpectedHash, sizeof(md5ExpectedHash)) == 0);
}
}
@end
@@ -0,0 +1,56 @@
// Copyright 2020 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.
#import <XCTest/XCTest.h>
#import "third_party/nearby_connections/cpp/platform/base/byte_array.h"
#import "third_party/nearby_connections/cpp/platform/base/exception.h"
#import "third_party/nearby_connections/cpp/platform/impl/ios/Source/Platform/input_file.h"
using ::location::nearby::ios::InputFile;
using ::location::nearby::ByteArray;
using ::location::nearby::ExceptionOr;
@interface GNCInputFileTest : XCTestCase
@end
@implementation GNCInputFileTest
// TODO(b/169292092): Find more tools (e.g. file/util/TempPath) to test fake file.
- (void)testNonExistentPath {
std::string cc_path("/foo/bar/test.ext");
NSString* objcPath = [NSString stringWithUTF8String:cc_path.c_str()];
NSURL *testURL = [NSURL URLWithString:objcPath];
auto input_file = std::make_unique<InputFile>(testURL);
XCTAssert(input_file != nullptr);
XCTAssertEqual(input_file->GetTotalSize(), 0);
ExceptionOr<ByteArray> read_result = input_file->Read(3);
XCTAssertFalse(read_result.ok());
}
- (void)testGetFilePath {
std::string cc_path("/foo/bar/test.ext");
NSString* objcPath = [NSString stringWithUTF8String:cc_path.c_str()];
NSURL *testURL = [NSURL URLWithString:objcPath];
auto input_file = std::make_unique<InputFile>(testURL);
XCTAssert(input_file != nullptr);
std::string input_file_path = input_file->GetFilePath();
XCTAssertTrue([objcPath isEqualToString:[NSString stringWithUTF8String:input_file_path.c_str()]]);
}
@end
@@ -0,0 +1,114 @@
// Copyright 2020 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.
#import <XCTest/XCTest.h>
#include "third_party/absl/time/time.h"
#include "third_party/nearby_connections/cpp/platform/api/cancelable.h"
#include "third_party/nearby_connections/cpp/platform/api/executor.h"
#include "third_party/nearby_connections/cpp/platform/api/platform.h"
#include "third_party/nearby_connections/cpp/platform/api/submittable_executor.h"
#include "third_party/nearby_connections/cpp/platform/base/runnable.h"
using location::nearby::Runnable;
using location::nearby::api::ImplementationPlatform;
using MultiThreadExecutor = location::nearby::api::SubmittableExecutor;
@interface GNCMultiThreadExecutorTest : XCTestCase
@property(atomic) int counter;
@property(atomic) int otherCounter;
@end
@implementation GNCMultiThreadExecutorTest
// Creates a MultiThreadExecutor.
- (std::unique_ptr<MultiThreadExecutor>)executor {
std::unique_ptr<MultiThreadExecutor> executor =
ImplementationPlatform::CreateMultiThreadExecutor(4);
XCTAssert(executor != nullptr);
return executor;
}
// Tests that the executor executes runnables as expected.
- (void)testExecute {
std::unique_ptr<MultiThreadExecutor> executor([self executor]);
// Execute two runnables that increment the counter.
const int kIncrements = 13;
Runnable incrementer = [self]() { self.counter++; };
for (int i = 0; i < kIncrements; i++) {
executor->Execute(std::move(incrementer));
}
// Check that the counter has the expected value after giving the runnables time to run.
[NSThread sleepForTimeInterval:0.01];
XCTAssertEqual(self.counter, kIncrements);
}
// Tests that the executor submits runnables as expected.
- (void)testSubmit {
std::unique_ptr<MultiThreadExecutor> executor([self executor]);
// Submit two runnables that increment the counter.
const int kIncrements = 13;
Runnable incrementer = [self]() { self.counter++; };
for (int i = 0; i < kIncrements; i++) {
executor->DoSubmit(std::move(incrementer));
}
// Check that the counter has the expected value after giving the runnables time to run.
[NSThread sleepForTimeInterval:0.01];
XCTAssertEqual(self.counter, kIncrements);
}
// Tests that fails to submit when the executor is shut down.
- (void)testFailtoSubmitAfterShutdown {
std::unique_ptr<MultiThreadExecutor> executor([self executor]);
executor->Shutdown();
XCTAssertFalse(executor->DoSubmit([self]() { self.counter++; }));
// Check that the counter has the expected value after giving the runnables time to run.
[NSThread sleepForTimeInterval:0.01];
XCTAssertEqual(self.counter, 0);
}
// Tests that when the executor is shut down, it waits for pending operations to finish, and no new
// operations will be executed.
- (void)testShutdownToAllowExistingTaskComplete {
std::unique_ptr<MultiThreadExecutor> executor([self executor]);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_TARGET_QUEUE_DEFAULT, 0);
XCTestExpectation *expectation = [self expectationWithDescription:@"finished"];
const int kRunnableCount = 1000;
for (int i = 0; i < kRunnableCount; i++) {
executor->Execute([self]() { self.counter++; });
}
executor->Shutdown();
executor->Execute([self]() { self.otherCounter++; });
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), queue, ^{
XCTAssertLessThanOrEqual(self.counter, kRunnableCount);
XCTAssertEqual(self.otherCounter, 0);
[expectation fulfill];
});
[self waitForExpectationsWithTimeout:0.5 handler:nil];
}
@end
@@ -0,0 +1,139 @@
// Copyright 2020 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.
#import <XCTest/XCTest.h>
#include "third_party/absl/time/time.h"
#include "third_party/nearby_connections/cpp/platform/api/cancelable.h"
#include "third_party/nearby_connections/cpp/platform/api/executor.h"
#include "third_party/nearby_connections/cpp/platform/api/platform.h"
#include "third_party/nearby_connections/cpp/platform/api/scheduled_executor.h"
#include "third_party/nearby_connections/cpp/platform/base/runnable.h"
using location::nearby::Runnable;
using location::nearby::api::ImplementationPlatform;
using location::nearby::api::ScheduledExecutor;
@interface GNCScheduledExecutorTest : XCTestCase
@property(atomic) int counter;
@end
@implementation GNCScheduledExecutorTest
// Creates a ScheduledExecutor.
- (std::unique_ptr<ScheduledExecutor>)executor {
std::unique_ptr<ScheduledExecutor> executor = ImplementationPlatform::CreateScheduledExecutor();
XCTAssert(executor != nullptr);
return executor;
}
// Tests that the executor schedules operation at the specified time.
- (void)testScheduling {
std::unique_ptr<ScheduledExecutor> executor([self executor]);
XCTestExpectation *expectation = [self expectationWithDescription:@"finished"];
Runnable incrementer = [self]() { self.counter++; };
void (^checkCounter)(int, NSTimeInterval, dispatch_block_t) =
^(int expectedCount, NSTimeInterval delay, dispatch_block_t finalBlock) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{
XCTAssertEqual(self.counter, expectedCount);
finalBlock();
});
};
// Schedule two runnables that increment the counter, at 0.4 and 0.8 seconds.
executor->Schedule(std::move(incrementer), absl::Seconds(0.4));
executor->Schedule(std::move(incrementer), absl::Seconds(0.8));
// Check that the counter contains the expected values at 0.2, 0.6, and 1.0 seconds.
checkCounter(0, 0.2, ^{});
checkCounter(1, 0.6, ^{});
checkCounter(2, 1.0, ^{ [expectation fulfill]; });
[self waitForExpectationsWithTimeout:1.2 handler:nil];
}
// Tests that fails to schedule when the executor is shut down.
- (void)testFailtoScheduleAfterShutdown {
std::unique_ptr<ScheduledExecutor> executor([self executor]);
executor->Shutdown();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_TARGET_QUEUE_DEFAULT, 0);
XCTestExpectation *expectation = [self expectationWithDescription:@"finished"];
const NSTimeInterval delay = 0.1;
executor->Schedule([self]() { self.counter++; }, absl::Milliseconds(delay));
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * 2 * NSEC_PER_SEC)), queue, ^{
XCTAssertEqual(self.counter, 0);
[expectation fulfill];
});
[self waitForExpectationsWithTimeout:delay * 5 handler:nil];
}
// Tests that fails to cancel when the executor is shut down.
- (void)testFailToCancelAfterShutdown {
std::unique_ptr<ScheduledExecutor> executor([self executor]);
executor->Shutdown();
auto cancelable = executor->Schedule([self]() { self.counter++; }, absl::Seconds(0.1));
XCTAssert(cancelable.get() == nullptr);
}
// Tests that shutting down an existing task fails to complete.
- (void)testShutdownToFailExistingTask {
std::unique_ptr<ScheduledExecutor> executor([self executor]);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_TARGET_QUEUE_DEFAULT, 0);
XCTestExpectation *expectation = [self expectationWithDescription:@"finished"];
const NSTimeInterval delay = 0.1;
executor->Schedule([self]() { self.counter++; }, absl::Milliseconds(delay));
executor->Shutdown();
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * 2 * NSEC_PER_SEC)), queue, ^{
XCTAssertEqual(self.counter, 0);
[expectation fulfill];
});
[self waitForExpectationsWithTimeout:delay * 5 handler:nil];
}
// Tests that a canceled runnable doesn't run.
- (void)testCancelable {
std::unique_ptr<ScheduledExecutor> executor([self executor]);
// This runnable should run but not increment the counter because it sleeps for longer than the
// cancel below.
const NSTimeInterval delay = 0.1;
auto cancelable = executor->Schedule([self]() { self.counter++; }, absl::Seconds(delay));
XCTAssert(cancelable.get() != nullptr);
// Cancel the runnable.
cancelable->Cancel();
// Wait for the time interval to pass and verify that it didn't run.
[NSThread sleepForTimeInterval:delay * 1.1];
XCTAssertEqual(self.counter, 0);
}
@end
@@ -0,0 +1,95 @@
// Copyright 2020 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.
#import <XCTest/XCTest.h>
#include "third_party/absl/time/time.h"
#include "third_party/nearby_connections/cpp/platform/api/cancelable.h"
#include "third_party/nearby_connections/cpp/platform/api/executor.h"
#include "third_party/nearby_connections/cpp/platform/api/platform.h"
#include "third_party/nearby_connections/cpp/platform/api/submittable_executor.h"
#include "third_party/nearby_connections/cpp/platform/base/runnable.h"
using location::nearby::Runnable;
using location::nearby::api::ImplementationPlatform;
using SingleThreadExecutor = location::nearby::api::SubmittableExecutor;
@interface GNCSingleThreadExecutorTest : XCTestCase
@property(atomic) int counter;
@end
@implementation GNCSingleThreadExecutorTest
// Creates a SingleThreadExecutor.
- (std::unique_ptr<SingleThreadExecutor>)executor {
std::unique_ptr<SingleThreadExecutor> executor =
ImplementationPlatform::CreateSingleThreadExecutor();
XCTAssert(executor != nullptr);
return executor;
}
// Tests that the executor executes runnables as expected.
- (void)testRunnables {
std::unique_ptr<SingleThreadExecutor> executor([self executor]);
Runnable incrementer = [self]() { self.counter++; };
// Schedule two runnables that increment the counter.
executor->Execute(std::move(incrementer));
executor->Execute(std::move(incrementer));
// Check that the counter has the expected value after a moment.
[NSThread sleepForTimeInterval:0.01];
XCTAssertEqual(self.counter, 2);
}
// Tests that fails to execute when the executor is shut down.
- (void)testShutdownBeforeInvokeTask {
std::unique_ptr<SingleThreadExecutor> executor([self executor]);
executor->Shutdown();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_TARGET_QUEUE_DEFAULT, 0);
XCTestExpectation *expectation = [self expectationWithDescription:@"finished"];
executor->Execute([self]() { self.counter++; });
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), queue, ^{
XCTAssertEqual(self.counter, 0);
[expectation fulfill];
});
[self waitForExpectationsWithTimeout:0.5 handler:nil];
}
// Tests that shutting down an existing task allows to complete.
- (void)testShutdownToAllowExistingTaskComplete {
std::unique_ptr<SingleThreadExecutor> executor([self executor]);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_TARGET_QUEUE_DEFAULT, 0);
XCTestExpectation *expectation = [self expectationWithDescription:@"finished"];
executor->Execute([self]() { self.counter++; });
executor->Shutdown();
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), queue, ^{
XCTAssertEqual(self.counter, 1);
[expectation fulfill];
});
[self waitForExpectationsWithTimeout:0.5 handler:nil];
}
@end