Debian Codegraph @

storage/maria/libmarias3/tests/copy.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 copy and move calls */
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 bool found, found_orig, found_new;
32 ms3_st *ms3;
33 const char *test_string = "Another one bites the dust";
34 char *s3key = getenv("S3KEY");
35 char *s3secret = getenv("S3SECRET");
36 char *s3region = getenv("S3REGION");
37 char *s3bucket = getenv("S3BUCKET");
38 char *s3host = getenv("S3HOST");
39 char *s3noverify = getenv("S3NOVERIFY");
40 char *s3usehttp = getenv("S3USEHTTP");
41 char *s3port = getenv("S3PORT");
42
43 SKIP_IF_(!s3key, "Environemnt variable S3KEY missing");
44 SKIP_IF_(!s3secret, "Environemnt variable S3SECRET missing");
45 SKIP_IF_(!s3region, "Environemnt variable S3REGION missing");
46 SKIP_IF_(!s3bucket, "Environemnt variable S3BUCKET missing");
47
48 (void) argc;
49 (void) argv;
50
51 ms3_library_init();
52 ms3 = ms3_init(s3key, s3secret, s3region, s3host);
53
54 if (s3noverify && !strcmp(s3noverify, "1"))
55 {
56 ms3_set_option(ms3, MS3_OPT_DISABLE_SSL_VERIFY, NULL);
57 }
58
59 if (s3usehttp && !strcmp(s3usehttp, "1"))
60 {
61 ms3_set_option(ms3, MS3_OPT_USE_HTTP, NULL);
62 }
63
64 if (s3port)
65 {
66 int port = atoi(s3port);
67 ms3_set_option(ms3, MS3_OPT_PORT_NUMBER, &port);
68 }
69
70 // ms3_debug();
71 ASSERT_NOT_NULL(ms3);
72
73 res = ms3_put(ms3, s3bucket, "test/copy_test.txt",
74 (const uint8_t *)test_string,
75 strlen(test_string));
76 ASSERT_EQ_(res, 0, "Result: %u", res);
77
78 res = ms3_copy(ms3, s3bucket, "test/copy_test.txt", s3bucket,
79 "test/copied.txt");
80 ASSERT_EQ_(res, 0, "Result: %u", res);
81
82 res = ms3_list(ms3, s3bucket, NULL, &list);
83 ASSERT_EQ_(res, 0, "Result: %u", res);
84 found = false;
85 list_it = list;
86
87 while (list_it)
88 {
89 if (!strncmp(list_it->key, "test/copied.txt", 12))
90 {
91 found = true;
92 break;
93 }
94
95 list_it = list_it->next;
96 }
97
98 ASSERT_EQ_(found, 1, "Copied file not found");
99
100 // Test with hash chars in filename
101
102 res = ms3_put(ms3, s3bucket, "test/copy_###_test.txt",
103 (const uint8_t *)test_string,
104 strlen(test_string));
105 ASSERT_EQ_(res, 0, "Result: %u", res);
106
107 res = ms3_copy(ms3, s3bucket, "test/copy_###_test.txt", s3bucket,
108 "test/copied###.txt");
109 ASSERT_EQ_(res, 0, "Result: %u", res);
110
111 res = ms3_list(ms3, s3bucket, NULL, &list);
112 ASSERT_EQ_(res, 0, "Result: %u", res);
113 found = false;
114 list_it = list;
115
116 while (list_it)
117 {
118 if (!strncmp(list_it->key, "test/copied###.txt", 12))
119 {
120 found = true;
121 break;
122 }
123
124 list_it = list_it->next;
125 }
126
127 ASSERT_EQ_(found, 1, "Copied file not found");
128
129 if (list_it)
130 {
131 ASSERT_EQ_(list_it->length, 26, "Copied file is unexpected length");
132 ASSERT_NEQ_(list_it->created, 0, "Copied file timestamp is bad");
133 }
134 else
135 {
136 ASSERT_TRUE_(false, "No resuts from list");
137 }
138
139 res = ms3_move(ms3, s3bucket, "test/copy_test.txt", s3bucket, "test/moved.txt");
140 ASSERT_EQ_(res, 0, "Result: %u", res);
141
142 res = ms3_list(ms3, s3bucket, NULL, &list);
143 ASSERT_EQ_(res, 0, "Result: %u", res);
144 found_orig = false;
145 found_new = false;
146 list_it = list;
147
148 while (list_it)
149 {
150 if (!strncmp(list_it->key, "test/moved.txt", 12))
151 {
152 found_new = true;
153 }
154 else if (!strncmp(list_it->key, "test/copy_test.txt", 12))
155 {
156 found_orig = true;
157 }
158
159
160 list_it = list_it->next;
161 }
162
163 ASSERT_EQ_(found_new, 1, "Copied file not found");
164 ASSERT_EQ_(found_orig, 0, "Original file still exists after move");
165
166 res = ms3_get(ms3, s3bucket, "test/moved.txt", &data, &length);
167 ASSERT_EQ_(res, 0, "Result: %u", res);
168 ASSERT_EQ(length, 26);
169 ASSERT_STREQ((char *)data, test_string);
170
171 res = ms3_delete(ms3, s3bucket, "test/moved.txt");
172 ASSERT_EQ_(res, 0, "Result: %u", res);
173 ms3_delete(ms3, s3bucket, "test/copied.txt");
174 ms3_delete(ms3, s3bucket, "test/copy_###_test.txt");
175 ms3_delete(ms3, s3bucket, "test/copied###.txt");
176
177 ms3_free(data);
178 ms3_deinit(ms3);
179 ms3_library_deinit();
180 return 0;
181 }