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