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