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