lib509.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, 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 /*
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 
35 /*
36 #include "memdebug.h"
37 */
38 
39 static int seen_malloc = 0;
40 static int seen_free = 0;
41 static int seen_realloc = 0;
42 static int seen_strdup = 0;
43 static int seen_calloc = 0;
44 
45 void *custom_malloc(size_t size);
46 void custom_free(void *ptr);
47 void *custom_realloc(void *ptr, size_t size);
48 char *custom_strdup(const char *ptr);
49 void *custom_calloc(size_t nmemb, size_t size);
50 
51 
52 void *custom_calloc(size_t nmemb, size_t size)
53 {
54  if(!seen_calloc) {
55  printf("seen custom_calloc()\n");
56  seen_calloc = 1;
57  }
58  return (calloc)(nmemb, size);
59 }
60 
61 void *custom_malloc(size_t size)
62 {
63  if(!seen_malloc && seen_calloc) {
64  printf("seen custom_malloc()\n");
65  seen_malloc = 1;
66  }
67  return (malloc)(size);
68 }
69 
70 char *custom_strdup(const char *ptr)
71 {
72  if(!seen_strdup && seen_malloc) {
73  /* currently (2013.03.13), memory tracking enabled builds do not call
74  the strdup callback, in this case malloc callback and memcpy are used
75  instead. If some day this is changed the following printf() should be
76  uncommented, and a line added to test definition.
77  printf("seen custom_strdup()\n");
78  */
79  seen_strdup = 1;
80  }
81  return (strdup)(ptr);
82 }
83 
84 void *custom_realloc(void *ptr, size_t size)
85 {
86  if(!seen_realloc && seen_malloc) {
87  printf("seen custom_realloc()\n");
88  seen_realloc = 1;
89  }
90  return (realloc)(ptr, size);
91 }
92 
93 void custom_free(void *ptr)
94 {
95  if(!seen_free && seen_realloc) {
96  printf("seen custom_free()\n");
97  seen_free = 1;
98  }
99  (free)(ptr);
100 }
101 
102 
103 int test(char *URL)
104 {
105  unsigned char a[] = {0x2f, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
106  0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7};
107  CURLcode res;
108  CURL *curl;
109  int asize;
110  char *str = NULL;
111 
112  (void)URL;
113 
116  custom_free,
119  custom_calloc);
120  if(res != CURLE_OK) {
121  fprintf(stderr, "curl_global_init_mem() failed\n");
122  return TEST_ERR_MAJOR_BAD;
123  }
124 
125  curl = curl_easy_init();
126  if(!curl) {
127  fprintf(stderr, "curl_easy_init() failed\n");
129  return TEST_ERR_MAJOR_BAD;
130  }
131 
132  test_setopt(curl, CURLOPT_USERAGENT, "test509"); /* uses strdup() */
133 
134  asize = (int)sizeof(a);
135  str = curl_easy_escape(curl, (char *)a, asize); /* uses realloc() */
136 
137 test_cleanup:
138 
139  if(str)
140  curl_free(str);
141 
142  curl_easy_cleanup(curl);
144 
145  return (int)res;
146 }
147 
#define free(ptr)
Definition: curl_memory.h:130
#define test_setopt(A, B, C)
Definition: test.h:47
CURL_EXTERN void curl_free(void *p)
Definition: escape.c:239
CURL_EXTERN char * curl_easy_escape(CURL *handle, const char *string, int length)
static int seen_realloc
Definition: lib509.c:41
#define strdup(ptr)
Definition: curl_memory.h:122
UNITTEST_START char * ptr
Definition: unit1330.c:38
CURLcode
Definition: curl.h:454
static int res
#define realloc(ptr, size)
Definition: curl_memory.h:128
#define malloc(size)
Definition: curl_memory.h:124
#define TEST_ERR_MAJOR_BAD
Definition: test.h:85
void * custom_malloc(size_t size)
Definition: lib509.c:61
void custom_free(void *ptr)
Definition: lib509.c:93
const char * str
Definition: unit1398.c:33
static int seen_strdup
Definition: lib509.c:42
#define printf
Definition: curl_printf.h:40
Definition: unit1323.c:36
CURL_EXTERN CURL * curl_easy_init(void)
Definition: easy.c:343
char * custom_strdup(const char *ptr)
Definition: lib509.c:70
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
Definition: curl.h:455
CURL_EXTERN CURLcode curl_global_init_mem(long flags, curl_malloc_callback m, curl_free_callback f, curl_realloc_callback r, curl_strdup_callback s, curl_calloc_callback c)
Definition: easy.c:280
static int seen_malloc
Definition: lib509.c:39
int test(char *URL)
Definition: lib509.c:103
void * custom_calloc(size_t nmemb, size_t size)
Definition: lib509.c:52
void CURL
Definition: curl.h:102
void * custom_realloc(void *ptr, size_t size)
Definition: lib509.c:84
static int seen_free
Definition: lib509.c:40
size_t size
Definition: unit1302.c:52
#define fprintf
Definition: curl_printf.h:41
CURL_EXTERN void curl_global_cleanup(void)
curl_global_cleanup() globally cleanups curl, uses the value of "init_flags" to determine what needs ...
Definition: easy.c:312
#define CURL_GLOBAL_ALL
Definition: curl.h:2519
static CURL * curl
Definition: sessioninfo.c:35
#define calloc(nbelem, size)
Definition: curl_memory.h:126
static int seen_calloc
Definition: lib509.c:43


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:15