Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/tests/socket_async_ut/keep_alive.h

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 // This file is made an integral part of socket_async_ut.c with a #include. It
5 // is broken out for readability.
6
7
8 static int keep_alive; // < 0 for system defaults, 0 to disable, > 0 to use supplied idle, interval, and count
9 static int keep_idle; // seconds before first keepalive packet (ignored if keep_alive <= 0)
10 static int keep_interval; // seconds between keepalive packets (ignored if keep_alive <= 0)
11 static int keep_count; // number of times to try before declaring failure (ignored if keep_alive <= 0)
12 #define test_keep_alive 1
13 #define test_keep_alive_sys_default -1
14 #define test_keep_idle 22
15 #define test_keep_interval 33
16 #define test_keep_count 66
17 #define KEEP_ALIVE_UNDEFINED -1
18 #define KEEP_ALIVE_FALSE 0
19
20 static int my_setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen)
21 {
22 int value;
23 (void)optlen;
24 // All options are integers
25 ASSERT_ARE_EQUAL(int, sockfd, test_socket);
26 value = *((int*)optval);
27 if (level == IPPROTO_TCP)
28 {
29 switch (optname)
30 {
31 case TCP_KEEPIDLE: keep_idle = value; break;
32 case TCP_KEEPINTVL: keep_interval = value; break;
33 case TCP_KEEPCNT: keep_count = value; break;
34 default:
35 {
36 ASSERT_FAIL("Unexpected setsockopt IPPROTO_TCP parameters");
37 break;
38 }
39 }
40 }
41 else if (level == SOL_SOCKET && optname == SO_KEEPALIVE)
42 {
43 keep_alive = value;
44 ASSERT_IS_TRUE(value == 0 || value == 1, "Unexpected keep-alive value");
45 }
46 else
47 {
48 ASSERT_FAIL("Unexpected setsockopt parameters");
49 }
50 return 0;
51 }
52
53 static void init_keep_alive_values()
54 {
55 keep_alive = KEEP_ALIVE_UNDEFINED;
56 keep_idle = KEEP_ALIVE_UNDEFINED;
57 keep_interval = KEEP_ALIVE_UNDEFINED;
58 keep_count = KEEP_ALIVE_UNDEFINED;
59 }
60
61 static void ASSERT_KEEP_ALIVE_UNTOUCHED()
62 {
63 ASSERT_ARE_EQUAL(int, keep_alive, KEEP_ALIVE_UNDEFINED, "keep_alive in ASSERT_KEEP_ALIVE_UNTOUCHED");
64 ASSERT_ARE_EQUAL(int, keep_idle, KEEP_ALIVE_UNDEFINED, "keep_idle in ASSERT_KEEP_ALIVE_UNTOUCHED");
65 ASSERT_ARE_EQUAL(int, keep_interval, KEEP_ALIVE_UNDEFINED, "keep_interval in ASSERT_KEEP_ALIVE_UNTOUCHED");
66 ASSERT_ARE_EQUAL(int, keep_count, KEEP_ALIVE_UNDEFINED, "keep_count in ASSERT_KEEP_ALIVE_UNTOUCHED");
67 }
68
69 static void ASSERT_KEEP_ALIVE_FALSE()
70 {
71 ASSERT_ARE_EQUAL(int, keep_alive, KEEP_ALIVE_FALSE, "keep_alive in ASSERT_KEEP_ALIVE_FALSE");
72 ASSERT_ARE_EQUAL(int, keep_idle, KEEP_ALIVE_UNDEFINED, "keep_idle in ASSERT_KEEP_ALIVE_FALSE");
73 ASSERT_ARE_EQUAL(int, keep_interval, KEEP_ALIVE_UNDEFINED, "keep_interval in ASSERT_KEEP_ALIVE_FALSE");
74 ASSERT_ARE_EQUAL(int, keep_count, KEEP_ALIVE_UNDEFINED, "keep_count in ASSERT_KEEP_ALIVE_FALSE");
75 }
76
77 /* Tests_SRS_SOCKET_ASYNC_30_014: [ If the optional options parameter is non-NULL and is_UDP is false, socket_async_create shall set the socket options to the provided values. ]*/
78 static void ASSERT_KEEP_ALIVE_SET()
79 {
80 ASSERT_ARE_EQUAL(int, keep_alive, test_keep_alive, "keep_count in ASSERT_KEEP_ALIVE_SET");
81 ASSERT_ARE_EQUAL(int, keep_idle, test_keep_idle, "keep_idle in ASSERT_KEEP_ALIVE_SET");
82 ASSERT_ARE_EQUAL(int, keep_interval, test_keep_interval, "keep_interval in ASSERT_KEEP_ALIVE_SET");
83 ASSERT_ARE_EQUAL(int, keep_count, test_keep_count, "keep_count in ASSERT_KEEP_ALIVE_SET");
84 }
85
86