lib500.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 counter;
  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", ++counter);
  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", counter--);
  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. counter = 0;
  49. }
  50. #else
  51. #define setupcallbacks(x) Curl_nop_stmt
  52. #endif
  53. int 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_starttransfer;
  88. curl_off_t time_total;
  89. fprintf(moo, "IP %s\n", ipstr);
  90. curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME_T, &time_namelookup);
  91. curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME_T, &time_connect);
  92. curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME_T,
  93. &time_pretransfer);
  94. curl_easy_getinfo(curl, CURLINFO_STARTTRANSFER_TIME_T,
  95. &time_starttransfer);
  96. curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME_T, &time_total);
  97. /* since the timing will always vary we only compare relative
  98. differences between these 5 times */
  99. if(time_namelookup > time_connect) {
  100. fprintf(moo, "namelookup vs connect: %" CURL_FORMAT_CURL_OFF_T
  101. ".%06ld %" CURL_FORMAT_CURL_OFF_T ".%06ld\n",
  102. (time_namelookup / 1000000),
  103. (long)(time_namelookup % 1000000),
  104. (time_connect / 1000000), (long)(time_connect % 1000000));
  105. }
  106. if(time_connect > time_pretransfer) {
  107. fprintf(moo, "connect vs pretransfer: %" CURL_FORMAT_CURL_OFF_T
  108. ".%06ld %" CURL_FORMAT_CURL_OFF_T ".%06ld\n",
  109. (time_connect / 1000000), (long)(time_connect % 1000000),
  110. (time_pretransfer / 1000000),
  111. (long)(time_pretransfer % 1000000));
  112. }
  113. if(time_pretransfer > time_starttransfer) {
  114. fprintf(moo, "pretransfer vs starttransfer: %" CURL_FORMAT_CURL_OFF_T
  115. ".%06ld %" CURL_FORMAT_CURL_OFF_T ".%06ld\n",
  116. (time_pretransfer / 1000000),
  117. (long)(time_pretransfer % 1000000),
  118. (time_starttransfer / 1000000),
  119. (long)(time_starttransfer % 1000000));
  120. }
  121. if(time_starttransfer > time_total) {
  122. fprintf(moo, "starttransfer vs total: %" CURL_FORMAT_CURL_OFF_T
  123. ".%06ld %" CURL_FORMAT_CURL_OFF_T ".%06ld\n",
  124. (time_starttransfer / 1000000),
  125. (long)(time_starttransfer % 1000000),
  126. (time_total / 1000000), (long)(time_total % 1000000));
  127. }
  128. fclose(moo);
  129. }
  130. }
  131. }
  132. test_cleanup:
  133. curl_easy_cleanup(curl);
  134. curl_global_cleanup();
  135. return (int)res;
  136. }