Debian Codegraph @

storage/maria/libmarias3/tests/snowman.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 basic calls using UTF-8 */
24
25 int main(int argc, char *argv[])
26 {
27 int res;
28 ms3_list_st *list = NULL, *list_it = NULL;
29 uint8_t *data;
30 size_t length;
31 int i;
32 bool found;
33 uint8_t list_version;
34 const char *test_string = "Another ☃☃☃ bites the dust";
35 ms3_status_st status;
36 ms3_st *ms3;
37 char *s3key = getenv("S3KEY");
38 char *s3secret = getenv("S3SECRET");
39 char *s3region = getenv("S3REGION");
40 char *s3bucket = getenv("S3BUCKET");
41 char *s3host = getenv("S3HOST");
42 char *s3noverify = getenv("S3NOVERIFY");
43 char *s3usehttp = getenv("S3USEHTTP");
44 char *s3port = getenv("S3PORT");
45
46 SKIP_IF_(!s3key, "Environemnt variable S3KEY missing");
47 SKIP_IF_(!s3secret, "Environemnt variable S3SECRET missing");
48 SKIP_IF_(!s3region, "Environemnt variable S3REGION missing");
49 SKIP_IF_(!s3bucket, "Environemnt variable S3BUCKET missing");
50
51 (void) argc;
52 (void) argv;
53
54 ms3_library_init();
55 ms3 = ms3_init(s3key, s3secret, s3region, s3host);
56
57 if (s3noverify && !strcmp(s3noverify, "1"))
58 {
59 ms3_set_option(ms3, MS3_OPT_DISABLE_SSL_VERIFY, NULL);
60 }
61
62 if (s3usehttp && !strcmp(s3usehttp, "1"))
63 {
64 ms3_set_option(ms3, MS3_OPT_USE_HTTP, NULL);
65 }
66
67 if (s3port)
68 {
69 int port = atoi(s3port);
70 ms3_set_option(ms3, MS3_OPT_PORT_NUMBER, &port);
71 }
72
73 // ms3_debug();
74 ASSERT_NOT_NULL(ms3);
75
76 res = ms3_put(ms3, s3bucket, "☃/☃.☃",
77 (const uint8_t *)test_string,
78 strlen(test_string));
79 ASSERT_EQ_(res, 0, "Result: %u", res);
80
81 // A prefix that will give no results;
82 res = ms3_list(ms3, s3bucket, "asdfghjkl", &list);
83 ASSERT_EQ_(res, 0, "Result: %u", res);
84 ASSERT_NULL_(list, "List not empty");
85
86 res = ms3_list(ms3, s3bucket, NULL, &list);
87 ASSERT_EQ_(res, 0, "Result: %u", res);
88 found = false;
89 list_it = list;
90
91 while (list_it)
92 {
93 if (!strncmp(list_it->key, "☃/☃.☃", 11))
94 {
95 found = true;
96 break;
97 }
98
99 list_it = list_it->next;
100 }
101
102 ASSERT_EQ_(found, 1, "Created file not found");
103
104 if (list_it)
105 {
106 ASSERT_EQ_(list_it->length, 32, "Created file is unexpected length");
107 ASSERT_NEQ_(list_it->created, 0, "Created file timestamp is bad");
108 }
109 else
110 {
111 ASSERT_TRUE_(false, "No resuts from list");
112 }
113
114 // Retry list with V1 API
115 list_version = 1;
116 list = NULL;
117 ms3_set_option(ms3, MS3_OPT_FORCE_LIST_VERSION, &list_version);
118 res = ms3_list(ms3, s3bucket, NULL, &list);
119 ASSERT_EQ_(res, 0, "Result: %u", res);
120 found = false;
121 list_it = list;
122
123 while (list_it)
124 {
125 if (!strncmp(list_it->key, "☃/☃.☃", 11))
126 {
127 found = true;
128 break;
129 }
130
131 list_it = list_it->next;
132 }
133
134 ASSERT_EQ_(found, 1, "Created file not found");
135
136 if (list_it)
137 {
138 ASSERT_EQ_(list_it->length, 32, "Created file is unexpected length");
139 ASSERT_NEQ_(list_it->created, 0, "Created file timestamp is bad");
140 }
141 else
142 {
143 ASSERT_TRUE_(false, "No resuts from list");
144 }
145
146 res = ms3_get(ms3, s3bucket, "☃/☃.☃", &data, &length);
147 ASSERT_EQ_(res, 0, "Result: %u", res);
148 ASSERT_EQ(length, 32);
149 ASSERT_STREQ((char *)data, test_string);
150
151 for (i = 0; i <= 3; i++)
152 {
153 res = ms3_status(ms3, s3bucket, "☃/☃.☃", &status);
154
155 if (res == MS3_ERR_NOT_FOUND)
156 {
157 continue;
158 }
159
160 ASSERT_EQ_(res, 0, "Result: %u", res);
161
162 if (res == 0)
163 {
164 break;
165 }
166 }
167
168 ASSERT_EQ(status.length, 32);
169 ASSERT_NEQ(status.created, 0);
170 res = ms3_delete(ms3, s3bucket, "☃/☃.☃");
171 ASSERT_EQ_(res, 0, "Result: %u", res);
172 ms3_free(data);
173 res = ms3_get(ms3, s3bucket, "☃/☃.☃", &data, &length);
174 ASSERT_NEQ_(res, 0, "Object should error");
175 ASSERT_NULL_(data, "Data should be NULL");
176 ASSERT_EQ_(length, 0, "There should be no data");
177 ms3_deinit(ms3);
178 ms3_library_deinit();
179 return 0;
180 }