1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2018, 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.haxx.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 ***************************************************************************/
22 #include "curlcheck.h"
23
24 #include "urldata.h"
25 #include "progress.h"
26
27 static int usec_magnitude = 1000000;
28
unit_setup(void)29 static bool unit_setup(void)
30 {
31 return CURLE_OK;
32 }
33
unit_stop(void)34 static void unit_stop(void)
35 {
36
37 }
38
39 /*
40 * Invoke Curl_pgrsTime for TIMER_STARTSINGLE to trigger the behavior that
41 * manages is_t_startransfer_set, but fake the t_startsingle time for purposes
42 * of the test.
43 */
fake_t_startsingle_time(struct Curl_easy * data,struct curltime fake_now,int seconds_offset)44 static void fake_t_startsingle_time(struct Curl_easy *data,
45 struct curltime fake_now,
46 int seconds_offset)
47 {
48 Curl_pgrsTime(data, TIMER_STARTSINGLE);
49 data->progress.t_startsingle.tv_sec = fake_now.tv_sec + seconds_offset;
50 data->progress.t_startsingle.tv_usec = fake_now.tv_usec;
51 }
52
usec_matches_seconds(time_t time_usec,int expected_seconds)53 static bool usec_matches_seconds(time_t time_usec, int expected_seconds)
54 {
55 int time_sec = (int)(time_usec / usec_magnitude);
56 bool same = (time_sec == expected_seconds);
57 fprintf(stderr, "is %d us same as %d seconds? %s\n",
58 (int)time_usec, expected_seconds,
59 same?"Yes":"No");
60 return same;
61 }
62
expect_timer_seconds(struct Curl_easy * data,int seconds)63 static void expect_timer_seconds(struct Curl_easy *data, int seconds)
64 {
65 char msg[64];
66 msnprintf(msg, sizeof(msg), "about %d seconds should have passed", seconds);
67 fail_unless(usec_matches_seconds(data->progress.t_nslookup, seconds), msg);
68 fail_unless(usec_matches_seconds(data->progress.t_connect, seconds), msg);
69 fail_unless(usec_matches_seconds(data->progress.t_appconnect, seconds), msg);
70 fail_unless(usec_matches_seconds(data->progress.t_pretransfer, seconds),
71 msg);
72 fail_unless(usec_matches_seconds(data->progress.t_starttransfer, seconds),
73 msg);
74 }
75
76 /* Scenario: simulate a redirect. When a redirect occurs, t_nslookup,
77 * t_connect, t_appconnect, t_pretransfer, and t_starttransfer are addative.
78 * E.g., if t_starttransfer took 2 seconds initially and took another 1
79 * second for the redirect request, then the resulting t_starttransfer should
80 * be 3 seconds. */
81 UNITTEST_START
82 struct Curl_easy data;
83 struct curltime now = Curl_now();
84
85 data.progress.t_nslookup = 0;
86 data.progress.t_connect = 0;
87 data.progress.t_appconnect = 0;
88 data.progress.t_pretransfer = 0;
89 data.progress.t_starttransfer = 0;
90 data.progress.t_redirect = 0;
91 data.progress.start.tv_sec = now.tv_sec - 2;
92 data.progress.start.tv_usec = now.tv_usec;
93 fake_t_startsingle_time(&data, now, -2);
94
95 Curl_pgrsTime(&data, TIMER_NAMELOOKUP);
96 Curl_pgrsTime(&data, TIMER_CONNECT);
97 Curl_pgrsTime(&data, TIMER_APPCONNECT);
98 Curl_pgrsTime(&data, TIMER_PRETRANSFER);
99 Curl_pgrsTime(&data, TIMER_STARTTRANSFER);
100
101 expect_timer_seconds(&data, 2);
102
103 /* now simulate the redirect */
104 data.progress.t_redirect = data.progress.t_starttransfer + 1;
105 fake_t_startsingle_time(&data, now, -1);
106
107 Curl_pgrsTime(&data, TIMER_NAMELOOKUP);
108 Curl_pgrsTime(&data, TIMER_CONNECT);
109 Curl_pgrsTime(&data, TIMER_APPCONNECT);
110 Curl_pgrsTime(&data, TIMER_PRETRANSFER);
111 /* ensure t_starttransfer is only set on the first invocation by attempting
112 * to set it twice */
113 Curl_pgrsTime(&data, TIMER_STARTTRANSFER);
114 Curl_pgrsTime(&data, TIMER_STARTTRANSFER);
115
116 expect_timer_seconds(&data, 3);
117 UNITTEST_STOP
118