1 | /* $Id: tstRTS3.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Simple Storage Service (S3) Communication API
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/s3.h>
|
---|
42 | #include <iprt/stream.h>
|
---|
43 | #include <iprt/initterm.h>
|
---|
44 | #include <iprt/errcore.h>
|
---|
45 | #include <iprt/test.h>
|
---|
46 |
|
---|
47 |
|
---|
48 | /*********************************************************************************************************************************
|
---|
49 | * Defined Constants And Macros *
|
---|
50 | *********************************************************************************************************************************/
|
---|
51 | /* Manual configuration of this testcase */
|
---|
52 | #define TSTS3_CREATEBUCKET
|
---|
53 | #define TSTS3_PUTGETKEY
|
---|
54 | //#define TSTS3_SHOWPROGRESS
|
---|
55 |
|
---|
56 | #ifdef TSTS3_CREATEBUCKET
|
---|
57 | //# define TSTS3_CREATEBUCKET_BUCKETNAME "tstS3CreateBucket"
|
---|
58 | # define TSTS3_CREATEBUCKET_BUCKETNAME "tt9"
|
---|
59 | #endif /* TSTS3_CREATEBUCKET */
|
---|
60 |
|
---|
61 | #ifdef TSTS3_PUTGETKEY
|
---|
62 | # define TSTS3_PUTGETKEY_BUCKETNAME "tstS3PutGetBucket"
|
---|
63 | # define TSTS3_PUTGETKEY_KEYNAME "tstS3PutGetKey"
|
---|
64 | # define TSTS3_PUTGETKEY_PUTFILE "tstS3"
|
---|
65 | # define TSTS3_PUTGETKEY_GETFILE "tstS3_fetched"
|
---|
66 | #endif /* TSTS3_PUTGETKEY */
|
---|
67 |
|
---|
68 | static DECLCALLBACK(int) progress(unsigned uPercent, void *pvUser)
|
---|
69 | {
|
---|
70 | #ifdef TSTS3_SHOWPROGRESS
|
---|
71 | RTTestIPrintf(RTTESTLVL_ALWAYS, " Progress for %s - %d%% done.\n", (char*)pvUser, (int)uPercent);
|
---|
72 | #else
|
---|
73 | RT_NOREF2(uPercent, pvUser);
|
---|
74 | #endif
|
---|
75 | return VINF_SUCCESS;
|
---|
76 | }
|
---|
77 |
|
---|
78 | void fetchAllBuckets(RTS3 hS3)
|
---|
79 | {
|
---|
80 | /* Fetch all available buckets */
|
---|
81 | RTTestIPrintf(RTTESTLVL_ALWAYS, " List all buckets...\n");
|
---|
82 | char pszTitle[] = "RTS3GetBuckets";
|
---|
83 | RTS3SetProgressCallback(hS3, progress, pszTitle);
|
---|
84 | PCRTS3BUCKETENTRY pBuckets = NULL;
|
---|
85 | int rc = RTS3GetBuckets(hS3, &pBuckets);
|
---|
86 | if (RT_SUCCESS(rc))
|
---|
87 | {
|
---|
88 | if (pBuckets)
|
---|
89 | {
|
---|
90 | PCRTS3BUCKETENTRY pTmpBuckets = pBuckets;
|
---|
91 | while (pBuckets)
|
---|
92 | {
|
---|
93 | RTTestIPrintf(RTTESTLVL_ALWAYS, " > %s, %s\n", pBuckets->pszName, pBuckets->pszCreationDate);
|
---|
94 | pBuckets = pBuckets->pNext;
|
---|
95 | }
|
---|
96 | RTS3BucketsDestroy(pTmpBuckets);
|
---|
97 | }
|
---|
98 | else
|
---|
99 | RTTestIPrintf(RTTESTLVL_ALWAYS, " > empty\n");
|
---|
100 | }
|
---|
101 | else
|
---|
102 | RTTestIFailed("RTS3GetBuckets -> %Rrc", rc);
|
---|
103 | }
|
---|
104 |
|
---|
105 | void createBucket(RTS3 hS3, const char *pszBucketName)
|
---|
106 | {
|
---|
107 | /* Create the bucket */
|
---|
108 | RTTestIPrintf(RTTESTLVL_ALWAYS, " Create bucket '%s'...\n", pszBucketName);
|
---|
109 | char pszTitle[] = "RTS3CreateBucket";
|
---|
110 | RTS3SetProgressCallback(hS3, progress, pszTitle);
|
---|
111 | int rc = RTS3CreateBucket(hS3, pszBucketName);
|
---|
112 | if (RT_FAILURE(rc))
|
---|
113 | RTTestIFailed("RTS3CreateBucket -> %Rrc", rc);
|
---|
114 | }
|
---|
115 |
|
---|
116 | void deleteBucket(RTS3 hS3, const char *pszBucketName)
|
---|
117 | {
|
---|
118 | /* Delete the bucket */
|
---|
119 | RTTestIPrintf(RTTESTLVL_ALWAYS, " Delete bucket '%s'...\n", pszBucketName);
|
---|
120 | char pszTitle[] = "RTS3DeleteBucket";
|
---|
121 | RTS3SetProgressCallback(hS3, progress, pszTitle);
|
---|
122 | int rc = RTS3DeleteBucket(hS3, pszBucketName);
|
---|
123 | if (RT_FAILURE(rc))
|
---|
124 | RTTestIFailed("RTS3DeleteBucket -> %Rrc", rc);
|
---|
125 | }
|
---|
126 |
|
---|
127 | void fetchAllKeys(RTS3 hS3, const char *pszBucketName)
|
---|
128 | {
|
---|
129 | /* Fetch all available keys of a specific bucket */
|
---|
130 | RTTestIPrintf(RTTESTLVL_ALWAYS, " List all keys of bucket '%s'...\n", pszBucketName);
|
---|
131 | PCRTS3KEYENTRY pKeys = NULL;
|
---|
132 | char pszTitle[] = "RTS3GetBucketKeys";
|
---|
133 | RTS3SetProgressCallback(hS3, progress, pszTitle);
|
---|
134 | int rc = RTS3GetBucketKeys(hS3, pszBucketName, &pKeys);
|
---|
135 | if (RT_SUCCESS(rc))
|
---|
136 | {
|
---|
137 | if (pKeys)
|
---|
138 | {
|
---|
139 | PCRTS3KEYENTRY pTmpKeys = pKeys;
|
---|
140 | while (pKeys)
|
---|
141 | {
|
---|
142 | RTTestIPrintf(RTTESTLVL_ALWAYS, " > %s, %s, %lu\n", pKeys->pszName, pKeys->pszLastModified, pKeys->cbFile);
|
---|
143 | pKeys = pKeys->pNext;
|
---|
144 | }
|
---|
145 | RTS3KeysDestroy(pTmpKeys);
|
---|
146 | }
|
---|
147 | else
|
---|
148 | RTTestIPrintf(RTTESTLVL_ALWAYS, " > empty\n");
|
---|
149 | }
|
---|
150 | else
|
---|
151 | RTTestIFailed("RTS3GetBucketKeys -> %Rrc", rc);
|
---|
152 | }
|
---|
153 |
|
---|
154 | void deleteKey(RTS3 hS3, const char *pszBucketName, const char *pszKeyName)
|
---|
155 | {
|
---|
156 | /* Delete the key */
|
---|
157 | RTTestIPrintf(RTTESTLVL_ALWAYS, " Delete key '%s' in bucket '%s'...\n", pszKeyName, pszBucketName);
|
---|
158 | char pszTitle[] = "RTS3DeleteKey";
|
---|
159 | RTS3SetProgressCallback(hS3, progress, pszTitle);
|
---|
160 | int rc = RTS3DeleteKey(hS3, pszBucketName, pszKeyName);
|
---|
161 | if (RT_FAILURE(rc))
|
---|
162 | RTTestIFailed("RTS3DeleteKey -> %Rrc", rc);
|
---|
163 | }
|
---|
164 |
|
---|
165 | void getKey(RTS3 hS3, const char *pszBucketName, const char *pszKeyName, const char *pszFilename)
|
---|
166 | {
|
---|
167 | /* Fetch the content of a key */
|
---|
168 | RTTestIPrintf(RTTESTLVL_ALWAYS, " Get key '%s' from bucket '%s' into '%s' ...\n", pszKeyName, pszBucketName, pszFilename);
|
---|
169 | char pszTitle[] = "RTS3GetKey";
|
---|
170 | RTS3SetProgressCallback(hS3, progress, pszTitle);
|
---|
171 | int rc = RTS3GetKey(hS3, pszBucketName, pszKeyName, pszFilename);
|
---|
172 | if (RT_FAILURE(rc))
|
---|
173 | RTTestIFailed("RTS3GetKey -> %Rrc", rc);
|
---|
174 | }
|
---|
175 |
|
---|
176 | void putKey(RTS3 hS3, const char *pszBucketName, const char *pszKeyName, const char *pszFilename)
|
---|
177 | {
|
---|
178 | /* Fetch the content of a key */
|
---|
179 | RTTestIPrintf(RTTESTLVL_ALWAYS, " Put '%s' into key '%s' in bucket '%s' ...\n", pszFilename, pszKeyName, pszBucketName);
|
---|
180 | char pszTitle[] = "RTS3PutKey";
|
---|
181 | RTS3SetProgressCallback(hS3, progress, pszTitle);
|
---|
182 | int rc = RTS3PutKey(hS3, pszBucketName, pszKeyName, pszFilename);
|
---|
183 | if (RT_FAILURE(rc))
|
---|
184 | RTTestIFailed("RTS3PutKey -> %Rrc", rc);
|
---|
185 | }
|
---|
186 |
|
---|
187 | int main(int argc, char **argv)
|
---|
188 | {
|
---|
189 | /*
|
---|
190 | * Initialize IPRT and create the test.
|
---|
191 | */
|
---|
192 | RTTEST hTest;
|
---|
193 | int rc = RTTestInitAndCreate("tstRTS3", &hTest);
|
---|
194 | if (rc)
|
---|
195 | return rc;
|
---|
196 | RTTestBanner(hTest);
|
---|
197 |
|
---|
198 | /*
|
---|
199 | * If no args, display usage.
|
---|
200 | */
|
---|
201 | if (argc <= 2)
|
---|
202 | {
|
---|
203 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Syntax: %s [Access Key] [Secret Key]\n", argv[0]);
|
---|
204 | return RTTestSkipAndDestroy(hTest, "Missing required arguments\n");
|
---|
205 | }
|
---|
206 |
|
---|
207 | RTTestSubF(hTest, "Create S3");
|
---|
208 | RTS3 hS3;
|
---|
209 | rc = RTS3Create(&hS3, argv[1], argv[2], "object.storage.network.com", "tstS3-agent/1.0");
|
---|
210 | if (RT_FAILURE(rc))
|
---|
211 | {
|
---|
212 | RTTestIFailed("RTS3Create -> %Rrc", rc);
|
---|
213 | return RTTestSummaryAndDestroy(hTest);
|
---|
214 | }
|
---|
215 |
|
---|
216 | RTTestSub(hTest, "Fetch buckets");
|
---|
217 | fetchAllBuckets(hS3);
|
---|
218 | RTTestSub(hTest, "Fetch keys");
|
---|
219 | fetchAllKeys(hS3, "bla");
|
---|
220 |
|
---|
221 | #ifdef TSTS3_CREATEBUCKET
|
---|
222 | RTTestSub(hTest, "Create bucket");
|
---|
223 | createBucket(hS3, TSTS3_CREATEBUCKET_BUCKETNAME);
|
---|
224 | fetchAllBuckets(hS3);
|
---|
225 | deleteBucket(hS3, TSTS3_CREATEBUCKET_BUCKETNAME);
|
---|
226 | fetchAllBuckets(hS3);
|
---|
227 | #endif /* TSTS3_CREATEBUCKET */
|
---|
228 |
|
---|
229 |
|
---|
230 | #ifdef TSTS3_PUTGETKEY
|
---|
231 | RTTestSub(hTest, "Put key");
|
---|
232 | createBucket(hS3, TSTS3_PUTGETKEY_BUCKETNAME);
|
---|
233 | putKey(hS3, TSTS3_PUTGETKEY_BUCKETNAME, TSTS3_PUTGETKEY_KEYNAME, TSTS3_PUTGETKEY_PUTFILE);
|
---|
234 | fetchAllKeys(hS3, TSTS3_PUTGETKEY_BUCKETNAME);
|
---|
235 | getKey(hS3, TSTS3_PUTGETKEY_BUCKETNAME, TSTS3_PUTGETKEY_KEYNAME, TSTS3_PUTGETKEY_GETFILE);
|
---|
236 | deleteKey(hS3, TSTS3_PUTGETKEY_BUCKETNAME, TSTS3_PUTGETKEY_KEYNAME);
|
---|
237 | fetchAllKeys(hS3, TSTS3_PUTGETKEY_BUCKETNAME);
|
---|
238 | deleteBucket(hS3, TSTS3_PUTGETKEY_BUCKETNAME);
|
---|
239 | #endif /* TSTS3_PUTGETKEY */
|
---|
240 |
|
---|
241 | RTS3Destroy(hS3);
|
---|
242 |
|
---|
243 | /*
|
---|
244 | * Summary
|
---|
245 | */
|
---|
246 | return RTTestSummaryAndDestroy(hTest);
|
---|
247 | }
|
---|
248 |
|
---|