2
0

lib500.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 "test.h"
  25. #include "testtrace.h"
  26. #include "memdebug.h"
  27. #ifdef LIB585
  28. static int testcounter;
  29. static curl_socket_t tst_opensocket(void *clientp,
  30. curlsocktype purpose,
  31. struct curl_sockaddr *addr)
  32. {
  33. (void)clientp;
  34. (void)purpose;
  35. printf("[OPEN] counter: %d\n", ++testcounter);
  36. return socket(addr->family, addr->socktype, addr->protocol);
  37. }
  38. static int tst_closesocket(void *clientp, curl_socket_t sock)
  39. {
  40. (void)clientp;
  41. printf("[CLOSE] counter: %d\n", testcounter--);
  42. return sclose(sock);
  43. }
  44. static void setupcallbacks(CURL *curl)
  45. {
  46. curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, tst_opensocket);
  47. curl_easy_setopt(curl, CURLOPT_CLOSESOCKETFUNCTION, tst_closesocket);
  48. testcounter = 0;
  49. }
  50. #else
  51. #define setupcallbacks(x) Curl_nop_stmt
  52. #endif
  53. CURLcode test(char *URL)
  54. {
  55. CURLcode res;
  56. CURL *curl;
  57. char *ipstr = NULL;
  58. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  59. fprintf(stderr, "curl_global_init() failed\n");
  60. return TEST_ERR_MAJOR_BAD;
  61. }
  62. curl = curl_easy_init();
  63. if(!curl) {
  64. fprintf(stderr, "curl_easy_init() failed\n");
  65. curl_global_cleanup();
  66. return TEST_ERR_MAJOR_BAD;
  67. }
  68. test_setopt(curl, CURLOPT_URL, URL);
  69. test_setopt(curl, CURLOPT_HEADER, 1L);
  70. libtest_debug_config.nohex = 1;
  71. libtest_debug_config.tracetime = 1;
  72. test_setopt(curl, CURLOPT_DEBUGDATA, &libtest_debug_config);
  73. test_setopt(curl, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
  74. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  75. if(libtest_arg3 && !strcmp(libtest_arg3, "activeftp"))
  76. test_setopt(curl, CURLOPT_FTPPORT, "-");
  77. setupcallbacks(curl);
  78. res = curl_easy_perform(curl);
  79. if(!res) {
  80. res = curl_easy_getinfo(curl, CURLINFO_PRIMARY_IP, &ipstr);
  81. if(libtest_arg2) {
  82. FILE *moo = fopen(libtest_arg2, "wb");
  83. if(moo) {
  84. curl_off_t time_namelookup;
  85. curl_off_t time_connect;
  86. curl_off_t time_pretransfer;
  87. curl_off_t time_posttransfer;
  88. curl_off_t time_starttransfer;
  89. curl_off_t time_total;
  90. fprintf(moo, "IP %s\n", ipstr);
  91. curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME_T, &time_namelookup);
  92. curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME_T, &time_connect);
  93. curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME_T,
  94. &time_pretransfer);
  95. curl_easy_getinfo(curl, CURLINFO_POSTTRANSFER_TIME_T,
  96. &time_posttransfer);
  97. curl_easy_getinfo(curl, CURLINFO_STARTTRANSFER_TIME_T,
  98. &time_starttransfer);
  99. curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME_T, &time_total);
  100. /* since the timing will always vary we only compare relative
  101. differences between these 5 times */
  102. if(time_namelookup > time_connect) {
  103. fprintf(moo, "namelookup vs connect: %" CURL_FORMAT_CURL_OFF_T
  104. ".%06ld %" CURL_FORMAT_CURL_OFF_T ".%06ld\n",
  105. (time_namelookup / 1000000),
  106. (long)(time_namelookup % 1000000),
  107. (time_connect / 1000000), (long)(time_connect % 1000000));
  108. }
  109. if(time_connect > time_pretransfer) {
  110. fprintf(moo, "connect vs pretransfer: %" CURL_FORMAT_CURL_OFF_T
  111. ".%06ld %" CURL_FORMAT_CURL_OFF_T ".%06ld\n",
  112. (time_connect / 1000000), (long)(time_connect % 1000000),
  113. (time_pretransfer / 1000000),
  114. (long)(time_pretransfer % 1000000));
  115. }
  116. if(time_pretransfer > time_posttransfer) {
  117. fprintf(moo, "pretransfer vs posttransfer: %" CURL_FORMAT_CURL_OFF_T
  118. ".%06ld %" CURL_FORMAT_CURL_OFF_T ".%06ld\n",
  119. (time_pretransfer / 1000000),
  120. (long)(time_pretransfer % 1000000),
  121. (time_posttransfer / 1000000),
  122. (long)(time_posttransfer % 1000000));
  123. }
  124. if(time_pretransfer > time_starttransfer) {
  125. fprintf(moo, "pretransfer vs starttransfer: %" CURL_FORMAT_CURL_OFF_T
  126. ".%06ld %" CURL_FORMAT_CURL_OFF_T ".%06ld\n",
  127. (time_pretransfer / 1000000),
  128. (long)(time_pretransfer % 1000000),
  129. (time_starttransfer / 1000000),
  130. (long)(time_starttransfer % 1000000));
  131. }
  132. if(time_starttransfer > time_total) {
  133. fprintf(moo, "starttransfer vs total: %" CURL_FORMAT_CURL_OFF_T
  134. ".%06ld %" CURL_FORMAT_CURL_OFF_T ".%06ld\n",
  135. (time_starttransfer / 1000000),
  136. (long)(time_starttransfer % 1000000),
  137. (time_total / 1000000), (long)(time_total % 1000000));
  138. }
  139. if(time_posttransfer > time_total) {
  140. fprintf(moo, "posttransfer vs total: %" CURL_FORMAT_CURL_OFF_T
  141. ".%06ld %" CURL_FORMAT_CURL_OFF_T ".%06ld\n",
  142. (time_posttransfer / 1000000),
  143. (long)(time_posttransfer % 1000000),
  144. (time_total / 1000000), (long)(time_total % 1000000));
  145. }
  146. fclose(moo);
  147. }
  148. }
  149. }
  150. test_cleanup:
  151. curl_easy_cleanup(curl);
  152. curl_global_cleanup();
  153. return res;
  154. }
  155. #undef setupcallbacks