lib509.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 <string.h>
  26. /*
  27. * This test uses these funny custom memory callbacks for the only purpose
  28. * of verifying that curl_global_init_mem() functionality is present in
  29. * libcurl and that it works unconditionally no matter how libcurl is built,
  30. * nothing more.
  31. *
  32. * Do not include memdebug.h in this source file, and do not use directly
  33. * memory related functions in this file except those used inside custom
  34. * memory callbacks which should be calling 'the real thing'.
  35. */
  36. static int seen;
  37. static void *custom_calloc(size_t nmemb, size_t size)
  38. {
  39. seen++;
  40. return (calloc)(nmemb, size);
  41. }
  42. static void *custom_malloc(size_t size)
  43. {
  44. seen++;
  45. return (malloc)(size);
  46. }
  47. static char *custom_strdup(const char *ptr)
  48. {
  49. seen++;
  50. return (strdup)(ptr);
  51. }
  52. static void *custom_realloc(void *ptr, size_t size)
  53. {
  54. seen++;
  55. return (realloc)(ptr, size);
  56. }
  57. static void custom_free(void *ptr)
  58. {
  59. seen++;
  60. (free)(ptr);
  61. }
  62. int test(char *URL)
  63. {
  64. unsigned char a[] = {0x2f, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  65. 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7};
  66. CURLcode res;
  67. CURL *curl;
  68. int asize;
  69. char *str = NULL;
  70. (void)URL;
  71. res = curl_global_init_mem(CURL_GLOBAL_ALL,
  72. custom_malloc,
  73. custom_free,
  74. custom_realloc,
  75. custom_strdup,
  76. custom_calloc);
  77. if(res != CURLE_OK) {
  78. fprintf(stderr, "curl_global_init_mem() failed\n");
  79. return TEST_ERR_MAJOR_BAD;
  80. }
  81. curl = curl_easy_init();
  82. if(!curl) {
  83. fprintf(stderr, "curl_easy_init() failed\n");
  84. curl_global_cleanup();
  85. return TEST_ERR_MAJOR_BAD;
  86. }
  87. test_setopt(curl, CURLOPT_USERAGENT, "test509"); /* uses strdup() */
  88. asize = (int)sizeof(a);
  89. str = curl_easy_escape(curl, (char *)a, asize); /* uses realloc() */
  90. if(seen)
  91. printf("Callbacks were invoked!\n");
  92. test_cleanup:
  93. if(str)
  94. curl_free(str);
  95. curl_easy_cleanup(curl);
  96. curl_global_cleanup();
  97. return (int)res;
  98. }