lib500.c 5.2 KB

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