xref: /curl/tests/unit/unit1303.c (revision 71cf0d1f)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at https://curl.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 #include "curlcheck.h"
25 
26 #include "urldata.h"
27 #include "connect.h"
28 #include "memdebug.h" /* LAST include file */
29 
30 static struct Curl_easy *testdata;
31 
unit_setup(void)32 static CURLcode unit_setup(void)
33 {
34   CURLcode res = CURLE_OK;
35 
36   global_init(CURL_GLOBAL_ALL);
37   testdata = curl_easy_init();
38   if(!testdata) {
39     curl_global_cleanup();
40     return CURLE_OUT_OF_MEMORY;
41   }
42   return res;
43 }
44 
unit_stop(void)45 static void unit_stop(void)
46 {
47   curl_easy_cleanup(testdata);
48   curl_global_cleanup();
49 }
50 
51 /* BASE is just a define to make us fool around with decently large number so
52    that we aren't zero-based */
53 #define BASE 1000000
54 
55 /* macro to set the pretended current time */
56 #define NOW(x,y) now.tv_sec = x; now.tv_usec = y
57 /* macro to set the millisecond based timeouts to use */
58 #define TIMEOUTS(x,y) testdata->set.timeout = x; \
59                       testdata->set.connecttimeout = y
60 
61 /*
62  * To test:
63  *
64  * 00/10/01/11 timeouts set
65  * 0/1         during connect
66  * T           various values on the timeouts
67  * N           various values of now
68  */
69 
70 struct timetest {
71   int now_s;
72   int now_us;
73   unsigned int timeout_ms;
74   unsigned int connecttimeout_ms;
75   bool connecting;
76   timediff_t result;
77   const char *comment;
78 };
79 
80 UNITTEST_START
81 {
82   struct curltime now;
83   unsigned int i;
84 
85   const struct timetest run[] = {
86   /* both timeouts set, not connecting */
87   {BASE + 4, 0,      10000, 8000, FALSE, 6000, "6 seconds should be left"},
88   {BASE + 4, 990000, 10000, 8000, FALSE, 5010, "5010 ms should be left"},
89   {BASE + 10, 0,     10000, 8000, FALSE, -1,   "timeout is -1, expired"},
90   {BASE + 12, 0,     10000, 8000, FALSE, -2000, "-2000, overdue 2 seconds"},
91 
92   /* both timeouts set, connecting */
93   {BASE + 4, 0,      10000, 8000, TRUE, 4000, "4 seconds should be left"},
94   {BASE + 4, 990000, 10000, 8000, TRUE, 3010, "3010 ms should be left"},
95   {BASE + 8, 0,      10000, 8000, TRUE, -1,   "timeout is -1, expired"},
96   {BASE + 10, 0,     10000, 8000, TRUE, -2000, "-2000, overdue 2 seconds"},
97 
98   /* no connect timeout set, not connecting */
99   {BASE + 4, 0,      10000, 0, FALSE, 6000, "6 seconds should be left"},
100   {BASE + 4, 990000, 10000, 0, FALSE, 5010, "5010 ms should be left"},
101   {BASE + 10, 0,     10000, 0, FALSE, -1,   "timeout is -1, expired"},
102   {BASE + 12, 0,     10000, 0, FALSE, -2000, "-2000, overdue 2 seconds"},
103 
104   /* no connect timeout set, connecting */
105   {BASE + 4, 0,      10000, 0, TRUE, 6000, "6 seconds should be left"},
106   {BASE + 4, 990000, 10000, 0, TRUE, 5010, "5010 ms should be left"},
107   {BASE + 10, 0,     10000, 0, TRUE, -1,   "timeout is -1, expired"},
108   {BASE + 12, 0,     10000, 0, TRUE, -2000, "-2000, overdue 2 seconds"},
109 
110   /* only connect timeout set, not connecting */
111   {BASE + 4, 0,      0, 10000, FALSE, 0, "no timeout active"},
112   {BASE + 4, 990000, 0, 10000, FALSE, 0, "no timeout active"},
113   {BASE + 10, 0,     0, 10000, FALSE, 0, "no timeout active"},
114   {BASE + 12, 0,     0, 10000, FALSE, 0, "no timeout active"},
115 
116   /* only connect timeout set, connecting */
117   {BASE + 4, 0,      0, 10000, TRUE, 6000, "6 seconds should be left"},
118   {BASE + 4, 990000, 0, 10000, TRUE, 5010, "5010 ms should be left"},
119   {BASE + 10, 0,     0, 10000, TRUE, -1,   "timeout is -1, expired"},
120   {BASE + 12, 0,     0, 10000, TRUE, -2000, "-2000, overdue 2 seconds"},
121 
122   /* no timeout set, not connecting */
123   {BASE + 4, 0,      0, 0, FALSE, 0, "no timeout active"},
124   {BASE + 4, 990000, 0, 0, FALSE, 0, "no timeout active"},
125   {BASE + 10, 0,     0, 0, FALSE, 0, "no timeout active"},
126   {BASE + 12, 0,     0, 0, FALSE, 0, "no timeout active"},
127 
128   /* no timeout set, connecting */
129   {BASE + 4, 0,      0, 0, TRUE, 296000, "no timeout active"},
130   {BASE + 4, 990000, 0, 0, TRUE, 295010, "no timeout active"},
131   {BASE + 10, 0,     0, 0, TRUE, 290000, "no timeout active"},
132   {BASE + 12, 0,     0, 0, TRUE, 288000, "no timeout active"},
133 
134   /* both timeouts set, connecting, connect timeout the longer one */
135   {BASE + 4, 0,      10000, 12000, TRUE, 6000, "6 seconds should be left"},
136 
137   };
138 
139   /* this is the pretended start time of the transfer */
140   testdata->progress.t_startsingle.tv_sec = BASE;
141   testdata->progress.t_startsingle.tv_usec = 0;
142   testdata->progress.t_startop.tv_sec = BASE;
143   testdata->progress.t_startop.tv_usec = 0;
144 
145   for(i = 0; i < sizeof(run)/sizeof(run[0]); i++) {
146     timediff_t timeout;
147     NOW(run[i].now_s, run[i].now_us);
148     TIMEOUTS(run[i].timeout_ms, run[i].connecttimeout_ms);
149     timeout =  Curl_timeleft(testdata, &now, run[i].connecting);
150     if(timeout != run[i].result)
151       fail(run[i].comment);
152   }
153 }
154 UNITTEST_STOP
155