lib509.c 3.0 KB

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