| 1 | /*************************************************************************** | 
|---|
| 2 | *                                  _   _ ____  _ | 
|---|
| 3 | *  Project                     ___| | | |  _ \| | | 
|---|
| 4 | *                             / __| | | | |_) | | | 
|---|
| 5 | *                            | (__| |_| |  _ <| |___ | 
|---|
| 6 | *                             \___|\___/|_| \_\_____| | 
|---|
| 7 | * | 
|---|
| 8 | * Copyright (C) 1998 - 2019, 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 | #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 | /* | 
|---|
| 38 | #include "memdebug.h" | 
|---|
| 39 | */ | 
|---|
| 40 |  | 
|---|
| 41 | static int seen_malloc = 0; | 
|---|
| 42 | static int seen_free = 0; | 
|---|
| 43 | static int seen_realloc = 0; | 
|---|
| 44 | static int seen_strdup = 0; | 
|---|
| 45 | static int seen_calloc = 0; | 
|---|
| 46 |  | 
|---|
| 47 | void *custom_malloc(size_t size); | 
|---|
| 48 | void custom_free(void *ptr); | 
|---|
| 49 | void *custom_realloc(void *ptr, size_t size); | 
|---|
| 50 | char *custom_strdup(const char *ptr); | 
|---|
| 51 | void *custom_calloc(size_t nmemb, size_t size); | 
|---|
| 52 |  | 
|---|
| 53 |  | 
|---|
| 54 | void *custom_calloc(size_t nmemb, size_t size) | 
|---|
| 55 | { | 
|---|
| 56 | if(!seen_calloc) { | 
|---|
| 57 | printf( "seen custom_calloc()\n"); | 
|---|
| 58 | seen_calloc = 1; | 
|---|
| 59 | } | 
|---|
| 60 | return (calloc)(nmemb, size); | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | void *custom_malloc(size_t size) | 
|---|
| 64 | { | 
|---|
| 65 | if(!seen_malloc && seen_calloc) { | 
|---|
| 66 | printf( "seen custom_malloc()\n"); | 
|---|
| 67 | seen_malloc = 1; | 
|---|
| 68 | } | 
|---|
| 69 | return (malloc)(size); | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | char *custom_strdup(const char *ptr) | 
|---|
| 73 | { | 
|---|
| 74 | if(!seen_strdup && seen_malloc) { | 
|---|
| 75 | /* currently (2013.03.13), memory tracking enabled builds do not call | 
|---|
| 76 | the strdup callback, in this case malloc callback and memcpy are used | 
|---|
| 77 | instead. If some day this is changed the following printf() should be | 
|---|
| 78 | uncommented, and a line added to test definition. | 
|---|
| 79 | printf("seen custom_strdup()\n"); | 
|---|
| 80 | */ | 
|---|
| 81 | seen_strdup = 1; | 
|---|
| 82 | } | 
|---|
| 83 | return (strdup)(ptr); | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | void *custom_realloc(void *ptr, size_t size) | 
|---|
| 87 | { | 
|---|
| 88 | if(!seen_realloc && seen_malloc) { | 
|---|
| 89 | printf( "seen custom_realloc()\n"); | 
|---|
| 90 | seen_realloc = 1; | 
|---|
| 91 | } | 
|---|
| 92 | return (realloc)(ptr, size); | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | void custom_free(void *ptr) | 
|---|
| 96 | { | 
|---|
| 97 | if(!seen_free && seen_realloc) { | 
|---|
| 98 | printf( "seen custom_free()\n"); | 
|---|
| 99 | seen_free = 1; | 
|---|
| 100 | } | 
|---|
| 101 | (free)(ptr); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 |  | 
|---|
| 105 | int test(char *URL) | 
|---|
| 106 | { | 
|---|
| 107 | unsigned char a[] = {0x2f, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, | 
|---|
| 108 | 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7}; | 
|---|
| 109 | CURLcode res; | 
|---|
| 110 | CURL *curl; | 
|---|
| 111 | int asize; | 
|---|
| 112 | char *str = NULL; | 
|---|
| 113 |  | 
|---|
| 114 | (void)URL; | 
|---|
| 115 |  | 
|---|
| 116 | res = curl_global_init_mem(CURL_GLOBAL_ALL, | 
|---|
| 117 | custom_malloc, | 
|---|
| 118 | custom_free, | 
|---|
| 119 | custom_realloc, | 
|---|
| 120 | custom_strdup, | 
|---|
| 121 | custom_calloc); | 
|---|
| 122 | if(res != CURLE_OK) { | 
|---|
| 123 | fprintf(stderr, "curl_global_init_mem() failed\n"); | 
|---|
| 124 | return TEST_ERR_MAJOR_BAD; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | curl = curl_easy_init(); | 
|---|
| 128 | if(!curl) { | 
|---|
| 129 | fprintf(stderr, "curl_easy_init() failed\n"); | 
|---|
| 130 | curl_global_cleanup(); | 
|---|
| 131 | return TEST_ERR_MAJOR_BAD; | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | test_setopt(curl, CURLOPT_USERAGENT, "test509"); /* uses strdup() */ | 
|---|
| 135 |  | 
|---|
| 136 | asize = (int)sizeof(a); | 
|---|
| 137 | str = curl_easy_escape(curl, (char *)a, asize); /* uses realloc() */ | 
|---|
| 138 |  | 
|---|
| 139 | test_cleanup: | 
|---|
| 140 |  | 
|---|
| 141 | if(str) | 
|---|
| 142 | curl_free(str); | 
|---|
| 143 |  | 
|---|
| 144 | curl_easy_cleanup(curl); | 
|---|
| 145 | curl_global_cleanup(); | 
|---|
| 146 |  | 
|---|
| 147 | return (int)res; | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|