unit1303.c 5.1 KB

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