From bf4ab9654b81d6e8b05294cf395ada80e403b498 Mon Sep 17 00:00:00 2001 From: Hannah Shi Date: Mon, 18 Apr 2022 12:33:56 -0700 Subject: [PATCH] [Objc] iOS ThirdPartyTest suite for libuv dns test, add additional dns test (#28573) * add ios libuv additional dns test * rewrite LibuvGetAddrInfoTests.m * comment address * clang format --- .../Libuv/LibuvGetAddrInfoTests.m | 134 +++--------------- .../Libuv/LibuvGetHostnameTests.m | 46 ++++++ .../ThirdPartyTests/Libuv/LibuvGetNameInfo.m | 50 +++++++ .../Libuv/LibuvGetSockNameTests.m | 48 +++++++ .../ThirdPartyTests/Libuv/LibuvTimerTests.m | 12 +- third_party/libuv.BUILD | 4 + 6 files changed, 180 insertions(+), 114 deletions(-) create mode 100644 src/objective-c/tests/ThirdPartyTests/Libuv/LibuvGetHostnameTests.m create mode 100644 src/objective-c/tests/ThirdPartyTests/Libuv/LibuvGetNameInfo.m create mode 100644 src/objective-c/tests/ThirdPartyTests/Libuv/LibuvGetSockNameTests.m diff --git a/src/objective-c/tests/ThirdPartyTests/Libuv/LibuvGetAddrInfoTests.m b/src/objective-c/tests/ThirdPartyTests/Libuv/LibuvGetAddrInfoTests.m index b28ff21f764..9cbdca74783 100644 --- a/src/objective-c/tests/ThirdPartyTests/Libuv/LibuvGetAddrInfoTests.m +++ b/src/objective-c/tests/ThirdPartyTests/Libuv/LibuvGetAddrInfoTests.m @@ -18,55 +18,26 @@ #import -#import -#import - -#define ASSERT(x) XCTAssertTrue(x) -#define ASSERT_NULL(x) XCTAssertTrue(x == NULL) -#define ASSERT_NOT_NULL(x) XCTAssertFalse(x == NULL) -#define CONCURRENT_COUNT 10 - -static const char* name = "localhost"; - -static int getaddrinfo_cbs = 0; - -/* data used for running multiple calls concurrently */ -static uv_getaddrinfo_t* getaddrinfo_handle; -static uv_getaddrinfo_t getaddrinfo_handles[CONCURRENT_COUNT]; -static int callback_counts[CONCURRENT_COUNT]; -static int fail_cb_called; - -static void getaddrinfo_fail_cb(uv_getaddrinfo_t* req, int status, struct addrinfo* res) { - ASSERT(fail_cb_called == 0); - ASSERT(status < 0); - ASSERT_NULL(res); - uv_freeaddrinfo(res); /* Should not crash. */ - fail_cb_called++; -} - -static void getaddrinfo_basic_cb(uv_getaddrinfo_t* handle, int status, struct addrinfo* res) { - ASSERT(handle == getaddrinfo_handle); - getaddrinfo_cbs++; - free(handle); - uv_freeaddrinfo(res); -} - -static void getaddrinfo_cuncurrent_cb(uv_getaddrinfo_t* handle, int status, struct addrinfo* res) { - int i; - int* data = (int*)handle->data; - - for (i = 0; i < CONCURRENT_COUNT; i++) { - if (&getaddrinfo_handles[i] == handle) { - ASSERT(i == *data); - callback_counts[i]++; - break; - } - } - ASSERT(i < CONCURRENT_COUNT); - free(data); - uv_freeaddrinfo(res); - getaddrinfo_cbs++; -} +#import "test/runner.h" +#import "test/task.h" + +// Tests in: +// libuv/test/test-getaddrinfo.c + +TEST_DECLARE(getaddrinfo_fail) +TEST_DECLARE(getaddrinfo_fail_sync) +TEST_DECLARE(getaddrinfo_basic) +TEST_DECLARE(getaddrinfo_basic_sync) +TEST_DECLARE(getaddrinfo_concurrent) + +#define TASK_LIST_START_LOCAL static task_entry_t TEST_TASKS[] = { +TASK_LIST_START_LOCAL +TEST_ENTRY(getaddrinfo_fail) +TEST_ENTRY(getaddrinfo_fail_sync) +TEST_ENTRY(getaddrinfo_basic) +TEST_ENTRY(getaddrinfo_basic_sync) +TEST_ENTRY(getaddrinfo_concurrent) +TASK_LIST_END @interface LibuvGetAddrInfoTests : XCTestCase @@ -74,68 +45,9 @@ static void getaddrinfo_cuncurrent_cb(uv_getaddrinfo_t* handle, int status, stru @implementation LibuvGetAddrInfoTests -- (void)testGetAddrInfoFail { - uv_getaddrinfo_t req; - - ASSERT(UV_EINVAL == - uv_getaddrinfo(uv_default_loop(), &req, (uv_getaddrinfo_cb)abort, NULL, NULL, NULL)); - - /* Use a FQDN by ending in a period */ - ASSERT(0 == uv_getaddrinfo(uv_default_loop(), &req, getaddrinfo_fail_cb, "example.invalid.", NULL, - NULL)); - ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT)); - ASSERT(fail_cb_called == 1); -} - -- (void)testGetAddrInfoFailSync { - uv_getaddrinfo_t req; - - /* Use a FQDN by ending in a period */ - ASSERT(0 > uv_getaddrinfo(uv_default_loop(), &req, NULL, "example.invalid.", NULL, NULL)); - uv_freeaddrinfo(req.addrinfo); -} - -- (void)testGetAddrInfoBasic { - int r; - getaddrinfo_handle = (uv_getaddrinfo_t*)malloc(sizeof(uv_getaddrinfo_t)); - - r = uv_getaddrinfo(uv_default_loop(), getaddrinfo_handle, &getaddrinfo_basic_cb, name, NULL, - NULL); - ASSERT(r == 0); - - uv_run(uv_default_loop(), UV_RUN_DEFAULT); - - ASSERT(getaddrinfo_cbs == 1); -} - -- (void)testGetAddrInfoBasicSync { - uv_getaddrinfo_t req; - - ASSERT(0 == uv_getaddrinfo(uv_default_loop(), &req, NULL, name, NULL, NULL)); - uv_freeaddrinfo(req.addrinfo); -} - -- (void)testGetAddrInfoConcurrent { - int i, r; - int* data; - - for (i = 0; i < CONCURRENT_COUNT; i++) { - callback_counts[i] = 0; - - data = (int*)malloc(sizeof(int)); - ASSERT_NOT_NULL(data); - *data = i; - getaddrinfo_handles[i].data = data; - - r = uv_getaddrinfo(uv_default_loop(), &getaddrinfo_handles[i], &getaddrinfo_cuncurrent_cb, name, - NULL, NULL); - ASSERT(r == 0); - } - - uv_run(uv_default_loop(), UV_RUN_DEFAULT); - - for (i = 0; i < CONCURRENT_COUNT; i++) { - ASSERT(callback_counts[i] == 1); +- (void)testGetAddrInfoAll { + for (task_entry_t* task = TEST_TASKS; task->main; task++) { + task->main(); } } diff --git a/src/objective-c/tests/ThirdPartyTests/Libuv/LibuvGetHostnameTests.m b/src/objective-c/tests/ThirdPartyTests/Libuv/LibuvGetHostnameTests.m new file mode 100644 index 00000000000..10926a27fd9 --- /dev/null +++ b/src/objective-c/tests/ThirdPartyTests/Libuv/LibuvGetHostnameTests.m @@ -0,0 +1,46 @@ +/* + * + * Copyright 2021 gRPC authors. + * + * 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 + * + * http://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 + +#import "test/runner.h" +#import "test/task.h" + +// Tests in: +// libuv/test/test-gethostname.c + +TEST_DECLARE(gethostname) + +#define TASK_LIST_START_LOCAL static task_entry_t TEST_TASKS[] = { +TASK_LIST_START_LOCAL +TEST_ENTRY(gethostname) +TASK_LIST_END + +@interface LibuvGetHostNameTests : XCTestCase + +@end + +@implementation LibuvGetHostNameTests + +- (void)testGetHostNameAll { + for (task_entry_t* task = TEST_TASKS; task->main; task++) { + task->main(); + } +} + +@end diff --git a/src/objective-c/tests/ThirdPartyTests/Libuv/LibuvGetNameInfo.m b/src/objective-c/tests/ThirdPartyTests/Libuv/LibuvGetNameInfo.m new file mode 100644 index 00000000000..3aa8db32b3c --- /dev/null +++ b/src/objective-c/tests/ThirdPartyTests/Libuv/LibuvGetNameInfo.m @@ -0,0 +1,50 @@ +/* + * + * Copyright 2021 gRPC authors. + * + * 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 + * + * http://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 + +#import "test/runner.h" +#import "test/task.h" + +// Tests in: +// libuv/test/test-getnameinfo.c + +TEST_DECLARE(getnameinfo_basic_ip4) +TEST_DECLARE(getnameinfo_basic_ip4_sync) +TEST_DECLARE(getnameinfo_basic_ip6) + +#define TASK_LIST_START_LOCAL static task_entry_t TEST_TASKS[] = { +TASK_LIST_START_LOCAL +TEST_ENTRY(getnameinfo_basic_ip4) +TEST_ENTRY(getnameinfo_basic_ip4_sync) +TEST_ENTRY(getnameinfo_basic_ip6) +TASK_LIST_END + +@interface LibuvGetNameInfoTests : XCTestCase + +@end + +@implementation LibuvGetNameInfoTests + +- (void)testGetHostNameAll { + for (task_entry_t* task = TEST_TASKS; task->main; task++) { + task->main(); + } +} + +@end diff --git a/src/objective-c/tests/ThirdPartyTests/Libuv/LibuvGetSockNameTests.m b/src/objective-c/tests/ThirdPartyTests/Libuv/LibuvGetSockNameTests.m new file mode 100644 index 00000000000..c6d51af3290 --- /dev/null +++ b/src/objective-c/tests/ThirdPartyTests/Libuv/LibuvGetSockNameTests.m @@ -0,0 +1,48 @@ +/* + * + * Copyright 2021 gRPC authors. + * + * 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 + * + * http://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 + +#import "test/runner.h" +#import "test/task.h" + +// Tests in: +// libuv/test/test-getsockname.c + +TEST_DECLARE(getsockname_tcp) +TEST_DECLARE(getsockname_udp) + +#define TASK_LIST_START_LOCAL static task_entry_t TEST_TASKS[] = { +TASK_LIST_START_LOCAL +TEST_ENTRY(getsockname_tcp) +// TEST_ENTRY(getsockname_udp) +TASK_LIST_END + +@interface LibuvGetSockNameTests : XCTestCase + +@end + +@implementation LibuvGetSockNameTests + +- (void)testGetSockNameAll { + for (task_entry_t* task = TEST_TASKS; task->main; task++) { + task->main(); + } +} + +@end diff --git a/src/objective-c/tests/ThirdPartyTests/Libuv/LibuvTimerTests.m b/src/objective-c/tests/ThirdPartyTests/Libuv/LibuvTimerTests.m index 5c378845b4b..42e9ee486f1 100644 --- a/src/objective-c/tests/ThirdPartyTests/Libuv/LibuvTimerTests.m +++ b/src/objective-c/tests/ThirdPartyTests/Libuv/LibuvTimerTests.m @@ -21,6 +21,11 @@ #import "test/runner.h" #import "test/task.h" +// Tests in: +// libuv/test/test-timer.c +// libuv/test/test-timer-again.c +// libuv/test/test-timer-from-check.c + TEST_DECLARE(timer) TEST_DECLARE(timer_init) TEST_DECLARE(timer_again) @@ -34,7 +39,8 @@ TEST_DECLARE(timer_is_closing) TEST_DECLARE(timer_null_callback) TEST_DECLARE(timer_early_check) -TASK_LIST_START +#define TASK_LIST_START_LOCAL static task_entry_t TEST_TASKS[] = { +TASK_LIST_START_LOCAL TEST_ENTRY(timer) TEST_ENTRY(timer_init) TEST_ENTRY(timer_again) @@ -56,9 +62,9 @@ TASK_LIST_END @implementation LibuvTimerTests - (void)testTimerAll { - for (task_entry_t* task = TASKS; task->main; task++) { + for (task_entry_t* task = TEST_TASKS; task->main; task++) { task->main(); } } -@end \ No newline at end of file +@end diff --git a/third_party/libuv.BUILD b/third_party/libuv.BUILD index ebb8556fca3..a3a9355d7d9 100644 --- a/third_party/libuv.BUILD +++ b/third_party/libuv.BUILD @@ -331,6 +331,10 @@ cc_library( "test/test-timer.c", "test/test-timer-again.c", "test/test-timer-from-check.c", + "test/test-getaddrinfo.c", + "test/test-gethostname.c", + "test/test-getnameinfo.c", + "test/test-getsockname.c", ], hdrs = [ "test/runner.h",