Debian Codegraph @

storage/maria/libmarias3/tests/basic_host.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 with an expicit hostname */
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 protocol_version;
34 const char *test_string = "Another one 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 SKIP_IF_(s3host, "Test is for non-explicit hostnames");
52
53 (void) argc;
54 (void) argv;
55
56 if (!s3host || s3host[0] == '\0')
57 {
58 const char *default_host = "s3.amazonaws.com";
59 s3host = (char *)default_host;
60 }
61
62 ms3_library_init();
63 ms3 = ms3_init(s3key, s3secret, s3region, s3host);
64 protocol_version = 2;
65 ms3_set_option(ms3, MS3_OPT_FORCE_PROTOCOL_VERSION, &protocol_version);
66
67 if (s3noverify && !strcmp(s3noverify, "1"))
68 {
69 ms3_set_option(ms3, MS3_OPT_DISABLE_SSL_VERIFY, NULL);
70 }
71
72 if (s3usehttp && !strcmp(s3usehttp, "1"))
73 {
74 ms3_set_option(ms3, MS3_OPT_USE_HTTP, NULL);
75 }
76
77 if (s3port)
78 {
79 int port = atoi(s3port);
80 ms3_set_option(ms3, MS3_OPT_PORT_NUMBER, &port);
81 }
82
83 // ms3_debug();
84 ASSERT_NOT_NULL(ms3);
85
86 res = ms3_put(ms3, s3bucket, "test/basic_host.txt",
87 (const uint8_t *)test_string,
88 strlen(test_string));
89 ASSERT_EQ_(res, 0, "Result: %u", res);
90
91 // A prefix that will give no results;
92 res = ms3_list(ms3, s3bucket, "asdfghjkl", &list);
93 ASSERT_EQ_(res, 0, "Result: %u", res);
94 ASSERT_NULL_(list, "List not empty");
95
96 res = ms3_list(ms3, s3bucket, NULL, &list);
97 ASSERT_EQ_(res, 0, "Result: %u", res);
98 found = false;
99 list_it = list;
100
101 while (list_it)
102 {
103 if (!strncmp(list_it->key, "test/basic_host.txt", 19))
104 {
105 found = true;
106 break;
107 }
108
109 list_it = list_it->next;
110 }
111
112 ASSERT_EQ_(found, 1, "Created file not found");
113
114 if (list_it)
115 {
116 ASSERT_EQ_(list_it->length, 26, "Created file is unexpected length");
117 ASSERT_NEQ_(list_it->created, 0, "Created file timestamp is bad");
118 }
119 else
120 {
121 ASSERT_TRUE_(false, "No resuts from list");
122 }
123
124 res = ms3_get(ms3, s3bucket, "test/basic_host.txt", &data, &length);
125 ASSERT_EQ_(res, 0, "Result: %u", res);
126 ASSERT_EQ(length, 26);
127 ASSERT_STREQ((char *)data, test_string);
128
129 for (i = 0; i <= 3; i++)
130 {
131 res = ms3_status(ms3, s3bucket, "test/basic_host.txt", &status);
132
133 if (res == MS3_ERR_NOT_FOUND)
134 {
135 continue;
136 }
137
138 ASSERT_EQ_(res, 0, "Result: %u", res);
139
140 if (res == 0)
141 {
142 break;
143 }
144 }
145
146 ASSERT_EQ(status.length, 26);
147 ASSERT_NEQ(status.created, 0);
148 res = ms3_delete(ms3, s3bucket, "test/basic_host.txt");
149 ASSERT_EQ_(res, 0, "Result: %u", res);
150 ms3_free(data);
151 res = ms3_get(ms3, s3bucket, "test/basic_host.txt", &data, &length);
152 ASSERT_NEQ_(res, 0, "Object should error");
153 ASSERT_NULL_(data, "Data should be NULL");
154 ASSERT_EQ_(length, 0, "There should be no data");
155 ms3_deinit(ms3);
156 ms3_library_deinit();
157 return 0;
158 }