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
24 #include <string.h>
25
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
37 static int seen;
38
custom_calloc(size_t nmemb,size_t size)39 static void *custom_calloc(size_t nmemb, size_t size)
40 {
41 seen++;
42 return (calloc)(nmemb, size);
43 }
44
custom_malloc(size_t size)45 static void *custom_malloc(size_t size)
46 {
47 seen++;
48 return (malloc)(size);
49 }
50
custom_strdup(const char * ptr)51 static char *custom_strdup(const char *ptr)
52 {
53 seen++;
54 return (strdup)(ptr);
55 }
56
custom_realloc(void * ptr,size_t size)57 static void *custom_realloc(void *ptr, size_t size)
58 {
59 seen++;
60 return (realloc)(ptr, size);
61 }
62
custom_free(void * ptr)63 static void custom_free(void *ptr)
64 {
65 seen++;
66 (free)(ptr);
67 }
68
69
test(char * URL)70 int test(char *URL)
71 {
72 unsigned char a[] = {0x2f, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
73 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7};
74 CURLcode res;
75 CURL *curl;
76 int asize;
77 char *str = NULL;
78 (void)URL;
79
80 res = curl_global_init_mem(CURL_GLOBAL_ALL,
81 custom_malloc,
82 custom_free,
83 custom_realloc,
84 custom_strdup,
85 custom_calloc);
86 if(res != CURLE_OK) {
87 fprintf(stderr, "curl_global_init_mem() failed\n");
88 return TEST_ERR_MAJOR_BAD;
89 }
90
91 curl = curl_easy_init();
92 if(!curl) {
93 fprintf(stderr, "curl_easy_init() failed\n");
94 curl_global_cleanup();
95 return TEST_ERR_MAJOR_BAD;
96 }
97
98 test_setopt(curl, CURLOPT_USERAGENT, "test509"); /* uses strdup() */
99
100 asize = (int)sizeof(a);
101 str = curl_easy_escape(curl, (char *)a, asize); /* uses realloc() */
102
103 if(seen)
104 printf("Callbacks were invoked!\n");
105
106 test_cleanup:
107
108 if(str)
109 curl_free(str);
110
111 curl_easy_cleanup(curl);
112 curl_global_cleanup();
113
114 return (int)res;
115 }
116