Debian Codegraph @

storage/maria/libmarias3/tests/prefix.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 #include <unistd.h>
24 /* Test adds two files and checks that list prefix filters one out */
25
26 int main(int argc, char *argv[])
27 {
28 int res;
29 ms3_list_st *list = NULL, *list_it = NULL;
30 int i;
31 ms3_st *ms3;
32 bool found_good, found_bad;
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 = 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(true);
70 ASSERT_NOT_NULL(ms3);
71
72 res = ms3_put(ms3, s3bucket, "test/prefix.txt", (const uint8_t *)test_string,
73 strlen(test_string));
74 ASSERT_EQ(res, 0);
75 res = ms3_put(ms3, s3bucket, "other/prefix.txt", (const uint8_t *)test_string,
76 strlen(test_string));
77 ASSERT_EQ(res, 0);
78
79 found_good = false;
80 found_bad = false;
81
82 for (i = 0; i <= 3; i++)
83 {
84 uint8_t file_count;
85 res = ms3_list(ms3, s3bucket, "test", &list);
86 ASSERT_EQ(res, 0);
87 list_it = list;
88 file_count = 0;
89
90 while (list_it)
91 {
92 if (!strncmp(list_it->key, "test/prefix.txt", 12))
93 {
94 found_good = true;
95 }
96
97 if (!strncmp(list_it->key, "other/prefix.txt", 12))
98 {
99 found_bad = true;
100 }
101
102 list_it = list_it->next;
103 file_count++;
104 }
105
106 if ((file_count == 0) || !found_good)
107 {
108 sleep(1);
109 printf("Bad file count, retrying");
110 found_good = false;
111 found_bad = false;
112 continue;
113 }
114 else
115 {
116 break;
117 }
118 }
119
120 ASSERT_EQ_(found_good, 1, "Created file not found");
121 ASSERT_EQ_(found_bad, 0, "Filter found file it shouldn't");
122 res = ms3_delete(ms3, s3bucket, "test/prefix.txt");
123 ASSERT_EQ(res, 0);
124 res = ms3_delete(ms3, s3bucket, "other/prefix.txt");
125 ASSERT_EQ(res, 0);
126 ms3_deinit(ms3);
127 ms3_library_deinit();
128 return 0;
129 }