lib509.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. /*
  24. * This test uses these funny custom memory callbacks for the only purpose
  25. * of verifying that curl_global_init_mem() functionality is present in
  26. * libcurl and that it works unconditionally no matter how libcurl is built,
  27. * nothing more.
  28. *
  29. * Do not include memdebug.h in this source file, and do not use directly
  30. * memory related functions in this file except those used inside custom
  31. * memory callbacks which should be calling 'the real thing'.
  32. */
  33. /*
  34. #include "memdebug.h"
  35. */
  36. static int seen_malloc = 0;
  37. static int seen_free = 0;
  38. static int seen_realloc = 0;
  39. static int seen_strdup = 0;
  40. static int seen_calloc = 0;
  41. void *custom_malloc(size_t size);
  42. void custom_free(void *ptr);
  43. void *custom_realloc(void *ptr, size_t size);
  44. char *custom_strdup(const char *ptr);
  45. void *custom_calloc(size_t nmemb, size_t size);
  46. void *custom_calloc(size_t nmemb, size_t size)
  47. {
  48. if(!seen_calloc) {
  49. printf("seen custom_calloc()\n");
  50. seen_calloc = 1;
  51. }
  52. return (calloc)(nmemb, size);
  53. }
  54. void *custom_malloc(size_t size)
  55. {
  56. if(!seen_malloc && seen_calloc) {
  57. printf("seen custom_malloc()\n");
  58. seen_malloc = 1;
  59. }
  60. return (malloc)(size);
  61. }
  62. char *custom_strdup(const char *ptr)
  63. {
  64. if(!seen_strdup && seen_malloc) {
  65. /* currently (2013.03.13), memory tracking enabled builds do not call
  66. the strdup callback, in this case malloc callback and memcpy are used
  67. instead. If some day this is changed the following printf() should be
  68. uncommented, and a line added to test definition.
  69. printf("seen custom_strdup()\n");
  70. */
  71. seen_strdup = 1;
  72. }
  73. return (strdup)(ptr);
  74. }
  75. void *custom_realloc(void *ptr, size_t size)
  76. {
  77. if(!seen_realloc && seen_malloc) {
  78. printf("seen custom_realloc()\n");
  79. seen_realloc = 1;
  80. }
  81. return (realloc)(ptr, size);
  82. }
  83. void custom_free(void *ptr)
  84. {
  85. if(!seen_free && seen_realloc) {
  86. printf("seen custom_free()\n");
  87. seen_free = 1;
  88. }
  89. (free)(ptr);
  90. }
  91. int test(char *URL)
  92. {
  93. unsigned char a[] = {0x2f, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  94. 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7};
  95. CURLcode res;
  96. CURL *curl;
  97. int asize;
  98. char *str = NULL;
  99. (void)URL;
  100. res = curl_global_init_mem(CURL_GLOBAL_ALL,
  101. custom_malloc,
  102. custom_free,
  103. custom_realloc,
  104. custom_strdup,
  105. custom_calloc);
  106. if(res != CURLE_OK) {
  107. fprintf(stderr, "curl_global_init_mem() failed\n");
  108. return TEST_ERR_MAJOR_BAD;
  109. }
  110. curl = curl_easy_init();
  111. if(!curl) {
  112. fprintf(stderr, "curl_easy_init() failed\n");
  113. curl_global_cleanup();
  114. return TEST_ERR_MAJOR_BAD;
  115. }
  116. test_setopt(curl, CURLOPT_USERAGENT, "test509"); /* uses strdup() */
  117. asize = (int)sizeof(a);
  118. str = curl_easy_escape(curl, (char *)a, asize); /* uses realloc() */
  119. test_cleanup:
  120. if(str)
  121. curl_free(str);
  122. curl_easy_cleanup(curl);
  123. curl_global_cleanup();
  124. return (int)res;
  125. }