1 /* MIT License
2 *
3 * Copyright (c) The c-ares project and its contributors
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * SPDX-License-Identifier: MIT
25 */
26 #include <signal.h>
27 #include <stdlib.h>
28
29 #include "ares-test.h"
30
31 #ifdef __APPLE__
32 # include <mach/mach.h>
33 # include <mach/mach_time.h>
34 # include <pthread.h>
35
thread_set_realtime(pthread_t pthread)36 static void thread_set_realtime(pthread_t pthread)
37 {
38 mach_timebase_info_data_t timebase_info;
39 const uint64_t NANOS_PER_MSEC = 1000000ULL;
40 double clock2abs;
41 int rv;
42 thread_time_constraint_policy_data_t policy;
43
44 mach_timebase_info(&timebase_info);
45 clock2abs = ((double)timebase_info.denom / (double)timebase_info.numer)
46 * NANOS_PER_MSEC;
47
48 policy.period = 0;
49 policy.computation = (uint32_t)(5 * clock2abs); // 5 ms of work
50 policy.constraint = (uint32_t)(10 * clock2abs);
51 policy.preemptible = FALSE;
52
53 rv = thread_policy_set(pthread_mach_thread_np(pthread),
54 THREAD_TIME_CONSTRAINT_POLICY,
55 (thread_policy_t)&policy,
56 THREAD_TIME_CONSTRAINT_POLICY_COUNT);
57 if (rv != KERN_SUCCESS) {
58 mach_error("thread_policy_set:", rv);
59 exit(1);
60 }
61 }
62 #endif
63
main(int argc,char * argv[])64 int main(int argc, char* argv[]) {
65 std::vector<char*> gtest_argv = {argv[0]};
66 for (int ii = 1; ii < argc; ii++) {
67 if (strcmp(argv[ii], "-v") == 0) {
68 ares::test::verbose = true;
69 } else if ((strcmp(argv[ii], "-p") == 0) && (ii + 1 < argc)) {
70 ii++;
71 ares::test::mock_port = (unsigned short)atoi(argv[ii]);
72 } else if (strcmp(argv[ii], "-4") == 0) {
73 ares::test::families = ares::test::ipv4_family;
74 ares::test::families_modes = ares::test::ipv4_family_both_modes;
75 ares::test::evsys_families = ares::test::all_evsys_ipv4_family;
76 ares::test::evsys_families_modes = ares::test::all_evsys_ipv4_family_both_modes;
77 } else if (strcmp(argv[ii], "-6") == 0) {
78 ares::test::families = ares::test::ipv6_family;
79 ares::test::families_modes = ares::test::ipv6_family_both_modes;
80 ares::test::evsys_families = ares::test::all_evsys_ipv6_family;
81 ares::test::evsys_families_modes = ares::test::all_evsys_ipv6_family_both_modes;
82 } else {
83 gtest_argv.push_back(argv[ii]);
84 }
85 }
86 int gtest_argc = (int)gtest_argv.size();
87 gtest_argv.push_back(nullptr);
88 ::testing::InitGoogleTest(>est_argc, gtest_argv.data());
89
90 #ifdef WIN32
91 WORD wVersionRequested = MAKEWORD(2, 2);
92 WSADATA wsaData;
93 WSAStartup(wVersionRequested, &wsaData);
94 #else
95 signal(SIGPIPE, SIG_IGN);
96 #endif
97
98 #ifdef __APPLE__
99 /* We need to increase the priority in order for some timing-sensitive tests
100 * to succeed reliably. On CI systems, the host can be overloaded and things
101 * like sleep timers can wait many multiples of the time specified otherwise.
102 * This is sort of a necessary hack for test reliability. Not something that
103 * would generally be used */
104 thread_set_realtime(pthread_self());
105 #endif
106
107 int rc = RUN_ALL_TESTS();
108
109 #ifdef WIN32
110 WSACleanup();
111 #endif
112
113 return rc;
114 }
115