unit1303.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #include "urldata.h"
  26. #include "connect.h"
  27. #include "memdebug.h" /* LAST include file */
  28. static struct Curl_easy *testdata;
  29. static CURLcode unit_setup(void)
  30. {
  31. CURLcode res = CURLE_OK;
  32. global_init(CURL_GLOBAL_ALL);
  33. testdata = curl_easy_init();
  34. if(!testdata) {
  35. curl_global_cleanup();
  36. return CURLE_OUT_OF_MEMORY;
  37. }
  38. return res;
  39. }
  40. static void unit_stop(void)
  41. {
  42. curl_easy_cleanup(testdata);
  43. curl_global_cleanup();
  44. }
  45. /* BASE is just a define to make us fool around with decently large number so
  46. that we aren't zero-based */
  47. #define BASE 1000000
  48. /* macro to set the pretended current time */
  49. #define NOW(x,y) now.tv_sec = x; now.tv_usec = y
  50. /* macro to set the millisecond based timeouts to use */
  51. #define TIMEOUTS(x,y) testdata->set.timeout = x; \
  52. testdata->set.connecttimeout = y
  53. /*
  54. * To test:
  55. *
  56. * 00/10/01/11 timeouts set
  57. * 0/1 during connect
  58. * T various values on the timeouts
  59. * N various values of now
  60. */
  61. struct timetest {
  62. int now_s;
  63. int now_us;
  64. unsigned int timeout_ms;
  65. unsigned int connecttimeout_ms;
  66. bool connecting;
  67. timediff_t result;
  68. const char *comment;
  69. };
  70. UNITTEST_START
  71. {
  72. struct curltime now;
  73. unsigned int i;
  74. const struct timetest run[] = {
  75. /* both timeouts set, not connecting */
  76. {BASE + 4, 0, 10000, 8000, FALSE, 6000, "6 seconds should be left"},
  77. {BASE + 4, 990000, 10000, 8000, FALSE, 5010, "5010 ms should be left"},
  78. {BASE + 10, 0, 10000, 8000, FALSE, -1, "timeout is -1, expired"},
  79. {BASE + 12, 0, 10000, 8000, FALSE, -2000, "-2000, overdue 2 seconds"},
  80. /* both timeouts set, connecting */
  81. {BASE + 4, 0, 10000, 8000, TRUE, 4000, "4 seconds should be left"},
  82. {BASE + 4, 990000, 10000, 8000, TRUE, 3010, "3010 ms should be left"},
  83. {BASE + 8, 0, 10000, 8000, TRUE, -1, "timeout is -1, expired"},
  84. {BASE + 10, 0, 10000, 8000, TRUE, -2000, "-2000, overdue 2 seconds"},
  85. /* no connect timeout set, not connecting */
  86. {BASE + 4, 0, 10000, 0, FALSE, 6000, "6 seconds should be left"},
  87. {BASE + 4, 990000, 10000, 0, FALSE, 5010, "5010 ms should be left"},
  88. {BASE + 10, 0, 10000, 0, FALSE, -1, "timeout is -1, expired"},
  89. {BASE + 12, 0, 10000, 0, FALSE, -2000, "-2000, overdue 2 seconds"},
  90. /* no connect timeout set, connecting */
  91. {BASE + 4, 0, 10000, 0, TRUE, 6000, "6 seconds should be left"},
  92. {BASE + 4, 990000, 10000, 0, TRUE, 5010, "5010 ms should be left"},
  93. {BASE + 10, 0, 10000, 0, TRUE, -1, "timeout is -1, expired"},
  94. {BASE + 12, 0, 10000, 0, TRUE, -2000, "-2000, overdue 2 seconds"},
  95. /* only connect timeout set, not connecting */
  96. {BASE + 4, 0, 0, 10000, FALSE, 0, "no timeout active"},
  97. {BASE + 4, 990000, 0, 10000, FALSE, 0, "no timeout active"},
  98. {BASE + 10, 0, 0, 10000, FALSE, 0, "no timeout active"},
  99. {BASE + 12, 0, 0, 10000, FALSE, 0, "no timeout active"},
  100. /* only connect timeout set, connecting */
  101. {BASE + 4, 0, 0, 10000, TRUE, 6000, "6 seconds should be left"},
  102. {BASE + 4, 990000, 0, 10000, TRUE, 5010, "5010 ms should be left"},
  103. {BASE + 10, 0, 0, 10000, TRUE, -1, "timeout is -1, expired"},
  104. {BASE + 12, 0, 0, 10000, TRUE, -2000, "-2000, overdue 2 seconds"},
  105. /* no timeout set, not connecting */
  106. {BASE + 4, 0, 0, 0, FALSE, 0, "no timeout active"},
  107. {BASE + 4, 990000, 0, 0, FALSE, 0, "no timeout active"},
  108. {BASE + 10, 0, 0, 0, FALSE, 0, "no timeout active"},
  109. {BASE + 12, 0, 0, 0, FALSE, 0, "no timeout active"},
  110. /* no timeout set, connecting */
  111. {BASE + 4, 0, 0, 0, TRUE, 296000, "no timeout active"},
  112. {BASE + 4, 990000, 0, 0, TRUE, 295010, "no timeout active"},
  113. {BASE + 10, 0, 0, 0, TRUE, 290000, "no timeout active"},
  114. {BASE + 12, 0, 0, 0, TRUE, 288000, "no timeout active"},
  115. /* both timeouts set, connecting, connect timeout the longer one */
  116. {BASE + 4, 0, 10000, 12000, TRUE, 6000, "6 seconds should be left"},
  117. };
  118. /* this is the pretended start time of the transfer */
  119. testdata->progress.t_startsingle.tv_sec = BASE;
  120. testdata->progress.t_startsingle.tv_usec = 0;
  121. testdata->progress.t_startop.tv_sec = BASE;
  122. testdata->progress.t_startop.tv_usec = 0;
  123. for(i = 0; i < sizeof(run)/sizeof(run[0]); i++) {
  124. timediff_t timeout;
  125. NOW(run[i].now_s, run[i].now_us);
  126. TIMEOUTS(run[i].timeout_ms, run[i].connecttimeout_ms);
  127. timeout = Curl_timeleft(testdata, &now, run[i].connecting);
  128. if(timeout != run[i].result)
  129. fail(run[i].comment);
  130. }
  131. }
  132. UNITTEST_STOP