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