| 1 |
/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: |
| 2 |
* Copyright 2023 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 |
#define NUM_BYTES 64 * 1024 |
| 24 |
|
| 25 |
/* Tests basic GET with a custom read callback */ |
| 26 |
int total_reads = 0; |
| 27 |
|
| 28 |
static size_t read_cb(void *buf, size_t size, size_t nitems, void *userdata) |
| 29 |
{ |
| 30 |
char** dat = (char**)userdata; |
| 31 |
char* ptr = *dat; |
| 32 |
memcpy(ptr, buf, size * nitems); |
| 33 |
ptr += size * nitems; |
| 34 |
*dat = ptr; |
| 35 |
total_reads++; |
| 36 |
return nitems * size; |
| 37 |
} |
| 38 |
|
| 39 |
int main(int argc, char *argv[]) |
| 40 |
{ |
| 41 |
int res; |
| 42 |
int initial_reads; |
| 43 |
uint8_t *data; |
| 44 |
size_t length; |
| 45 |
ms3_st *ms3; |
| 46 |
size_t buffer_size; |
| 47 |
char *test_string = malloc(NUM_BYTES); |
| 48 |
char *dest = malloc(NUM_BYTES); |
| 49 |
char* userdata = dest; |
| 50 |
char *s3key = getenv("S3KEY"); |
| 51 |
char *s3secret = getenv("S3SECRET"); |
| 52 |
char *s3region = getenv("S3REGION"); |
| 53 |
char *s3bucket = getenv("S3BUCKET"); |
| 54 |
char *s3host = getenv("S3HOST"); |
| 55 |
char *s3noverify = getenv("S3NOVERIFY"); |
| 56 |
char *s3usehttp = getenv("S3USEHTTP"); |
| 57 |
char *s3port = getenv("S3PORT"); |
| 58 |
|
| 59 |
SKIP_IF_(!s3key, "Environment variable S3KEY missing"); |
| 60 |
SKIP_IF_(!s3secret, "Environment variable S3SECRET missing"); |
| 61 |
SKIP_IF_(!s3region, "Environment variable S3REGION missing"); |
| 62 |
SKIP_IF_(!s3bucket, "Environment variable S3BUCKET missing"); |
| 63 |
|
| 64 |
(void) argc; |
| 65 |
(void) argv; |
| 66 |
|
| 67 |
memset(test_string, 'a', NUM_BYTES); |
| 68 |
memset(dest, 'b', NUM_BYTES); |
| 69 |
|
| 70 |
ms3_library_init(); |
| 71 |
ms3 = ms3_init(s3key, s3secret, s3region, s3host); |
| 72 |
|
| 73 |
if (s3noverify && !strcmp(s3noverify, "1")) |
| 74 |
{ |
| 75 |
ms3_set_option(ms3, MS3_OPT_DISABLE_SSL_VERIFY, NULL); |
| 76 |
} |
| 77 |
|
| 78 |
if (s3usehttp && !strcmp(s3usehttp, "1")) |
| 79 |
{ |
| 80 |
ms3_set_option(ms3, MS3_OPT_USE_HTTP, NULL); |
| 81 |
} |
| 82 |
|
| 83 |
if (s3port) |
| 84 |
{ |
| 85 |
int port = atoi(s3port); |
| 86 |
ms3_set_option(ms3, MS3_OPT_PORT_NUMBER, &port); |
| 87 |
} |
| 88 |
|
| 89 |
ASSERT_NOT_NULL(ms3); |
| 90 |
|
| 91 |
res = ms3_put(ms3, s3bucket, "test/read_cb.dat", |
| 92 |
(const uint8_t *)test_string, |
| 93 |
NUM_BYTES); |
| 94 |
ASSERT_EQ_(res, 0, "Result: %u", res); |
| 95 |
res = ms3_set_option(ms3, MS3_OPT_READ_CB, read_cb); |
| 96 |
ASSERT_EQ_(res, 0, "Result: %u", res); |
| 97 |
res = ms3_set_option(ms3, MS3_OPT_USER_DATA, &userdata); |
| 98 |
ASSERT_EQ_(res, 0, "Result: %u", res); |
| 99 |
length = 0; |
| 100 |
data = 0; |
| 101 |
res = ms3_get(ms3, s3bucket, "test/read_cb.dat", &data, &length); |
| 102 |
ASSERT_EQ_(res, 0, "Result: %u", res); |
| 103 |
ASSERT_EQ(data, 0); |
| 104 |
ASSERT_EQ(length, 0); |
| 105 |
ASSERT_EQ(memcmp(test_string, dest, NUM_BYTES), 0); |
| 106 |
|
| 107 |
/** Test that the callbacks work with a smaller chunk size */ |
| 108 |
memset(dest, 'c', NUM_BYTES); |
| 109 |
userdata = dest; |
| 110 |
buffer_size = 1024; |
| 111 |
res = ms3_set_option(ms3, MS3_OPT_BUFFER_CHUNK_SIZE, &buffer_size); |
| 112 |
ASSERT_EQ_(res, 0, "Result: %u", res); |
| 113 |
initial_reads = total_reads; |
| 114 |
res = ms3_get(ms3, s3bucket, "test/read_cb.dat", &data, &length); |
| 115 |
ASSERT_EQ_(res, 0, "Result: %u", res); |
| 116 |
ASSERT_EQ(memcmp(test_string, dest, NUM_BYTES), 0); |
| 117 |
ASSERT_TRUE_((total_reads - initial_reads) > initial_reads * 2, |
| 118 |
"Expected more than %d reads but got only %d", |
| 119 |
initial_reads * 2, total_reads - initial_reads); |
| 120 |
|
| 121 |
res = ms3_delete(ms3, s3bucket, "test/read_cb.dat"); |
| 122 |
ASSERT_EQ_(res, 0, "Result: %u", res); |
| 123 |
free(test_string); |
| 124 |
free(dest); |
| 125 |
ms3_free(data); |
| 126 |
ms3_deinit(ms3); |
| 127 |
ms3_library_deinit(); |
| 128 |
return 0; |
| 129 |
} |