Debian Codegraph @

storage/maria/libmarias3/tests/custom_malloc.c

raw
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 * Copyright 2019 MariaDB Corporation Ab. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 */
19
20 #include <yatl/lite.h>
21 #include <libmarias3/marias3.h>
22
23 /* Tests functions using custom allocators */
24
25 static void *cust_malloc(size_t size)
26 {
27 printf("Malloc of %zu bytes\n", size);
28 return malloc(size);
29 }
30
31 static void cust_free(void *ptr)
32 {
33 printf("Free called\n");
34 free(ptr);
35 }
36
37 static void *cust_realloc(void *ptr, size_t size)
38 {
39 printf("Realloc of %zu bytes\n", size);
40 return realloc(ptr, size);
41 }
42
43 static char *cust_strdup(const char *str)
44 {
45 printf("Strdup called\n");
46 return strdup(str);
47 }
48
49 static void *cust_calloc(size_t nmemb, size_t size)
50 {
51 printf("Calloc of %zu elements, %zu size\n", nmemb, size);
52 return calloc(nmemb, size);
53 }
54
55 int main(int argc, char *argv[])
56 {
57 int res;
58 int i;
59 ms3_list_st *list = NULL, *list_it = NULL;
60 uint8_t *data;
61 size_t length;
62 bool found;
63 uint8_t list_version;
64 const char *test_string = "Another one bites the dust";
65 ms3_status_st status;
66 ms3_st *ms3;
67 char *s3key = getenv("S3KEY");
68 char *s3secret = getenv("S3SECRET");
69 char *s3region = getenv("S3REGION");
70 char *s3bucket = getenv("S3BUCKET");
71 char *s3host = getenv("S3HOST");
72 char *s3noverify = getenv("S3NOVERIFY");
73 char *s3usehttp = getenv("S3USEHTTP");
74 char *s3port = getenv("S3PORT");
75
76 SKIP_IF_(!s3key, "Environemnt variable S3KEY missing");
77 SKIP_IF_(!s3secret, "Environemnt variable S3SECRET missing");
78 SKIP_IF_(!s3region, "Environemnt variable S3REGION missing");
79 SKIP_IF_(!s3bucket, "Environemnt variable S3BUCKET missing");
80
81 (void) argc;
82 (void) argv;
83
84 ms3_library_init_malloc(cust_malloc, cust_free, cust_realloc, cust_strdup,
85 cust_calloc);
86 ms3 = ms3_init(s3key, s3secret, s3region, s3host);
87
88 if (s3noverify && !strcmp(s3noverify, "1"))
89 {
90 ms3_set_option(ms3, MS3_OPT_DISABLE_SSL_VERIFY, NULL);
91 }
92
93 if (s3usehttp && !strcmp(s3usehttp, "1"))
94 {
95 ms3_set_option(ms3, MS3_OPT_USE_HTTP, NULL);
96 }
97
98 if (s3port)
99 {
100 int port = atoi(s3port);
101 ms3_set_option(ms3, MS3_OPT_PORT_NUMBER, &port);
102 }
103
104 // ms3_debug(true);
105 ASSERT_NOT_NULL(ms3);
106
107 res = ms3_put(ms3, s3bucket, "test/custom_malloc.txt",
108 (const uint8_t *)test_string,
109 strlen(test_string));
110 ASSERT_EQ_(res, 0, "Result: %u", res);
111
112 // A prefix that will give no results;
113 res = ms3_list(ms3, s3bucket, "asdfghjkl", &list);
114 ASSERT_EQ_(res, 0, "Result: %u", res);
115 ASSERT_NULL_(list, "List not empty");
116
117 res = ms3_list(ms3, s3bucket, NULL, &list);
118 ASSERT_EQ_(res, 0, "Result: %u", res);
119 found = false;
120 list_it = list;
121
122 while (list_it)
123 {
124 if (!strncmp(list_it->key, "test/custom_malloc.txt", 12))
125 {
126 found = true;
127 break;
128 }
129
130 list_it = list_it->next;
131 }
132
133 ASSERT_EQ_(found, 1, "Created file not found");
134
135 if (list_it)
136 {
137 ASSERT_EQ_(list_it->length, 26, "Created file is unexpected length");
138 ASSERT_NEQ_(list_it->created, 0, "Created file timestamp is bad");
139 }
140 else
141 {
142 ASSERT_TRUE_(false, "No resuts from list");
143 }
144
145 // Retry list with V1 API
146 list_version = 1;
147 list = NULL;
148 ms3_set_option(ms3, MS3_OPT_FORCE_LIST_VERSION, &list_version);
149 res = ms3_list(ms3, s3bucket, NULL, &list);
150 ASSERT_EQ_(res, 0, "Result: %u", res);
151 found = false;
152 list_it = list;
153
154 while (list_it)
155 {
156 if (!strncmp(list_it->key, "test/custom_malloc.txt", 12))
157 {
158 found = true;
159 break;
160 }
161
162 list_it = list_it->next;
163 }
164
165 ASSERT_EQ_(found, 1, "Created file not found");
166
167 if (list_it)
168 {
169 ASSERT_EQ_(list_it->length, 26, "Created file is unexpected length");
170 ASSERT_NEQ_(list_it->created, 0, "Created file timestamp is bad");
171 }
172 else
173 {
174 ASSERT_TRUE_(false, "No resuts from list");
175 }
176
177 res = ms3_get(ms3, s3bucket, "test/custom_malloc.txt", &data, &length);
178 ASSERT_EQ_(res, 0, "Result: %u", res);
179 ASSERT_EQ(length, 26);
180 ASSERT_STREQ((char *)data, test_string);
181
182 for (i = 0; i <= 3; i++)
183 {
184 res = ms3_status(ms3, s3bucket, "test/custom_malloc.txt", &status);
185
186 if (res == MS3_ERR_NOT_FOUND)
187 {
188 continue;
189 }
190
191 ASSERT_EQ_(res, 0, "Result: %u", res);
192
193 if (res == 0)
194 {
195 break;
196 }
197 }
198
199 ASSERT_EQ(status.length, 26);
200 ASSERT_NEQ(status.created, 0);
201 res = ms3_delete(ms3, s3bucket, "test/custom_malloc.txt");
202 ASSERT_EQ_(res, 0, "Result: %u", res);
203 ms3_free(data);
204 ms3_deinit(ms3);
205 ms3_library_deinit();
206 return 0;
207 }