Debian Codegraph @

storage/maria/libmarias3/tests/list.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 list command */
24
25 int main(int argc, char *argv[])
26 {
27 int res;
28 ms3_list_st *list = NULL, *list_it = NULL;
29 ms3_st *ms3;
30 bool found, found_bad;
31 uint8_t list_version;
32 const char *test_string = "Another one bites the dust";
33 char *s3key = getenv("S3KEY");
34 char *s3secret = getenv("S3SECRET");
35 char *s3region = getenv("S3REGION");
36 char *s3bucket = getenv("S3BUCKET");
37 char *s3host = getenv("S3HOST");
38 char *s3noverify = getenv("S3NOVERIFY");
39 char *s3usehttp = getenv("S3USEHTTP");
40 char *s3port = getenv("S3PORT");
41
42 SKIP_IF_(!s3key, "Environemnt variable S3KEY missing");
43 SKIP_IF_(!s3secret, "Environemnt variable S3SECRET missing");
44 SKIP_IF_(!s3region, "Environemnt variable S3REGION missing");
45 SKIP_IF_(!s3bucket, "Environemnt variable S3BUCKET missing");
46
47 (void) argc;
48 (void) argv;
49
50 ms3_library_init();
51 ms3 = ms3_init(s3key, s3secret, s3region, s3host);
52
53 if (s3noverify && !strcmp(s3noverify, "1"))
54 {
55 ms3_set_option(ms3, MS3_OPT_DISABLE_SSL_VERIFY, NULL);
56 }
57
58 if (s3usehttp && !strcmp(s3usehttp, "1"))
59 {
60 ms3_set_option(ms3, MS3_OPT_USE_HTTP, NULL);
61 }
62
63 if (s3port)
64 {
65 int port = atoi(s3port);
66 ms3_set_option(ms3, MS3_OPT_PORT_NUMBER, &port);
67 }
68
69 // ms3_debug();
70 ASSERT_NOT_NULL(ms3);
71
72 res = ms3_put(ms3, s3bucket, "list1/test1.txt",
73 (const uint8_t *)test_string,
74 strlen(test_string));
75 ASSERT_EQ_(res, 0, "Result: %u", res);
76
77 res = ms3_put(ms3, s3bucket, "list2/test2.txt",
78 (const uint8_t *)test_string,
79 strlen(test_string));
80 ASSERT_EQ_(res, 0, "Result: %u", res);
81
82 // A prefix that will give no results;
83 res = ms3_list_dir(ms3, s3bucket, "asdfghjkl", &list);
84 ASSERT_EQ_(res, 0, "Result: %u", res);
85 ASSERT_NULL_(list, "List not empty");
86
87 res = ms3_list_dir(ms3, s3bucket, NULL, &list);
88 ASSERT_EQ_(res, 0, "Result: %u", res);
89 found = false;
90 found_bad = false;
91 list_it = list;
92
93 while (list_it)
94 {
95 if (!strncmp(list_it->key, "list1/test1.txt", 15))
96 {
97 found_bad = true;
98 }
99
100 if (!strncmp(list_it->key, "list2/", 6))
101 {
102 found = true;
103 }
104
105 list_it = list_it->next;
106 }
107
108 ASSERT_EQ_(found, 1, "Created file not found");
109 ASSERT_NEQ_(found_bad, 1, "File listed should not be here");
110
111 if (!list)
112 {
113 ASSERT_TRUE_(false, "No resuts from list");
114 }
115
116 // Retry list with V1 API
117 list_version = 1;
118 list = NULL;
119 ms3_set_option(ms3, MS3_OPT_FORCE_LIST_VERSION, &list_version);
120 res = ms3_list_dir(ms3, s3bucket, NULL, &list);
121 ASSERT_EQ_(res, 0, "Result: %u", res);
122 found = false;
123 list_it = list;
124
125 while (list_it)
126 {
127 if (!strncmp(list_it->key, "list1/test1.txt", 15))
128 {
129 found_bad = true;
130 }
131
132 if (!strncmp(list_it->key, "list2/", 6))
133 {
134 found = true;
135 }
136
137 list_it = list_it->next;
138 }
139
140 ASSERT_EQ_(found, 1, "Created file not found");
141 ASSERT_NEQ_(found_bad, 1, "File listed should not be here");
142
143 if (!list)
144 {
145 ASSERT_TRUE_(false, "No resuts from list");
146 }
147
148 res = ms3_delete(ms3, s3bucket, "list1/test1.txt");
149 ASSERT_EQ_(res, 0, "Result: %u", res);
150 res = ms3_delete(ms3, s3bucket, "list2/test2.txt");
151 ASSERT_EQ_(res, 0, "Result: %u", res);
152
153 ms3_deinit(ms3);
154 ms3_library_deinit();
155 return 0;
156 }