forked from minio/minio-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionalTest.java
More file actions
3136 lines (2723 loc) · 116 KB
/
Copy pathFunctionalTest.java
File metadata and controls
3136 lines (2723 loc) · 116 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* MinIO Java SDK for Amazon S3 Compatible Cloud Storage,
* (C) 2015, 2016, 2017, 2018 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.security.*;
import java.math.BigInteger;
import java.util.*;
import java.io.*;
import java.util.concurrent.TimeUnit;
import static java.nio.file.StandardOpenOption.*;
import java.nio.file.*;
import java.nio.charset.StandardCharsets;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.joda.time.DateTime;
import okhttp3.OkHttpClient;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.MultipartBody;
import okhttp3.Response;
import io.minio.*;
import io.minio.messages.*;
import io.minio.errors.*;
import io.minio.notification.*;
@SuppressFBWarnings(value = "REC", justification = "Allow catching super class Exception since it's tests")
public class FunctionalTest {
private static final String PASS = "PASS";
private static final String FAILED = "FAIL";
private static final String IGNORED = "NA";
private static final int KB = 1024;
private static final int MB = 1024 * 1024;
private static final Random random = new Random(new SecureRandom().nextLong());
private static final String customContentType = "application/javascript";
private static final String nullContentType = null;
private static String bucketName = getRandomName();
private static boolean mintEnv = false;
private static Path dataFile1Kb;
private static Path dataFile6Mb;
private static String endpoint;
private static String accessKey;
private static String secretKey;
private static String region;
private static MinioClient client = null;
/**
* Do no-op.
*/
public static void ignore(Object... args) {
}
/**
* Create given sized file and returns its name.
*/
public static String createFile(int size) throws IOException {
String filename = getRandomName();
try (OutputStream os = Files.newOutputStream(Paths.get(filename), CREATE, APPEND)) {
int totalBytesWritten = 0;
int bytesToWrite = 0;
byte[] buf = new byte[1 * MB];
while (totalBytesWritten < size) {
random.nextBytes(buf);
bytesToWrite = size - totalBytesWritten;
if (bytesToWrite > buf.length) {
bytesToWrite = buf.length;
}
os.write(buf, 0, bytesToWrite);
totalBytesWritten += bytesToWrite;
}
}
return filename;
}
/**
* Create 1 KB temporary file.
*/
public static String createFile1Kb() throws IOException {
if (mintEnv) {
String filename = getRandomName();
Files.createSymbolicLink(Paths.get(filename).toAbsolutePath(), dataFile1Kb);
return filename;
}
return createFile(1 * KB);
}
/**
* Create 6 MB temporary file.
*/
public static String createFile6Mb() throws IOException {
if (mintEnv) {
String filename = getRandomName();
Files.createSymbolicLink(Paths.get(filename).toAbsolutePath(), dataFile6Mb);
return filename;
}
return createFile(6 * MB);
}
/**
* Generate random name.
*/
public static String getRandomName() {
return "minio-java-test-" + new BigInteger(32, random).toString(32);
}
/**
* Returns byte array contains all data in given InputStream.
*/
public static byte[] readAllBytes(InputStream is) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
return buffer.toByteArray();
}
/**
* Prints a success log entry in JSON format.
*/
public static void mintSuccessLog(String function, String args, long startTime) {
if (mintEnv) {
System.out
.println(new MintLogger(function, args, System.currentTimeMillis() - startTime, PASS, null, null, null));
}
}
/**
* Prints a failure log entry in JSON format.
*/
public static void mintFailedLog(String function, String args, long startTime, String message, String error) {
if (mintEnv) {
System.out.println(
new MintLogger(function, args, System.currentTimeMillis() - startTime, FAILED, null, message, error));
}
}
/**
* Prints a ignore log entry in JSON format.
*/
public static void mintIgnoredLog(String function, String args, long startTime) {
if (mintEnv) {
System.out
.println(new MintLogger(function, args, System.currentTimeMillis() - startTime, IGNORED, null, null, null));
}
}
/**
* Read object content of the given url.
*/
public static byte[] readObject(String urlString) throws Exception {
Request.Builder requestBuilder = new Request.Builder();
Request request = requestBuilder.url(HttpUrl.parse(urlString)).method("GET", null).build();
OkHttpClient transport = new OkHttpClient().newBuilder().connectTimeout(20, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS).build();
Response response = transport.newCall(request).execute();
if (response == null) {
throw new Exception("empty response");
}
if (!response.isSuccessful()) {
String errorXml = "";
// read entire body stream to string.
Scanner scanner = new Scanner(response.body().charStream());
scanner.useDelimiter("\\A");
if (scanner.hasNext()) {
errorXml = scanner.next();
}
scanner.close();
response.body().close();
throw new Exception("failed to read object. Response: " + response + ", Response body: " + errorXml);
}
return readAllBytes(response.body().byteStream());
}
/**
* Write data to given object url.
*/
public static void writeObject(String urlString, byte[] dataBytes) throws Exception {
Request.Builder requestBuilder = new Request.Builder();
// Set header 'x-amz-acl' to 'bucket-owner-full-control', so objects created
// anonymously, can be downloaded by bucket owner in AWS S3.
Request request = requestBuilder.url(HttpUrl.parse(urlString)).method("PUT", RequestBody.create(null, dataBytes))
.addHeader("x-amz-acl", "bucket-owner-full-control").build();
OkHttpClient transport = new OkHttpClient().newBuilder().connectTimeout(20, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS).build();
Response response = transport.newCall(request).execute();
if (response == null) {
throw new Exception("empty response");
}
if (!response.isSuccessful()) {
String errorXml = "";
// read entire body stream to string.
Scanner scanner = new Scanner(response.body().charStream());
scanner.useDelimiter("\\A");
if (scanner.hasNext()) {
errorXml = scanner.next();
}
scanner.close();
response.body().close();
throw new Exception("failed to create object. Response: " + response + ", Response body: " + errorXml);
}
}
/**
* Test: makeBucket(String bucketName).
*/
public static void makeBucket_test1() throws Exception {
if (!mintEnv) {
System.out.println("Test: makeBucket(String bucketName)");
}
long startTime = System.currentTimeMillis();
try {
String name = getRandomName();
client.makeBucket(name);
client.removeBucket(name);
mintSuccessLog("makeBucket(String bucketName)", null, startTime);
} catch (Exception e) {
mintFailedLog("makeBucket(String bucketName)", null, startTime, null,
e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
}
/**
* Test: makeBucket(String bucketName, String region).
*/
public static void makeBucketwithRegion_test() throws Exception {
if (!mintEnv) {
System.out.println("Test: makeBucket(String bucketName, String region)");
}
long startTime = System.currentTimeMillis();
try {
String name = getRandomName();
client.makeBucket(name, "eu-west-1");
client.removeBucket(name);
mintSuccessLog("makeBucket(String bucketName, String region)", "region: eu-west-1", startTime);
} catch (Exception e) {
mintFailedLog("makeBucket(String bucketName, String region)", "region: eu-west-1", startTime, null,
e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
}
/**
* Test: makeBucket(String bucketName, String region) where bucketName has
* periods in its name.
*/
public static void makeBucketWithPeriod_test() throws Exception {
if (!mintEnv) {
System.out.println("Test: makeBucket(String bucketName, String region)");
}
long startTime = System.currentTimeMillis();
String name = getRandomName() + ".withperiod";
try {
client.makeBucket(name, "eu-central-1");
client.removeBucket(name);
mintSuccessLog("makeBucket(String bucketName, String region)", "name: " + name + ", region: eu-central-1",
startTime);
} catch (Exception e) {
mintFailedLog("makeBucket(String bucketName, String region)", "name: " + name + ", region: eu-central-1",
startTime, null, e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
}
/**
* Test: listBuckets().
*/
public static void listBuckets_test() throws Exception {
if (!mintEnv) {
System.out.println("Test: listBuckets()");
}
long startTime = System.currentTimeMillis();
try {
Date expectedTime = new Date();
String bucketName = getRandomName();
boolean found = false;
client.makeBucket(bucketName);
for (Bucket bucket : client.listBuckets()) {
if (bucket.name().equals(bucketName)) {
if (found) {
throw new Exception("[FAILED] duplicate entry " + bucketName + " found in list buckets");
}
found = true;
Date time = bucket.creationDate();
long diff = time.getTime() - expectedTime.getTime();
// excuse 15 minutes
if (diff > (15 * 60 * 1000)) {
throw new Exception(
"[FAILED] bucket creation time too apart. expected: " + expectedTime + ", got: " + time);
}
}
}
client.removeBucket(bucketName);
if (!found) {
throw new Exception("[FAILED] created bucket not found in list buckets");
}
mintSuccessLog("listBuckets()", null, startTime);
} catch (Exception e) {
mintFailedLog("listBuckets()", null, startTime, null,
e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
}
/**
* Test: bucketExists(String bucketName).
*/
public static void bucketExists_test() throws Exception {
if (!mintEnv) {
System.out.println("Test: bucketExists(String bucketName)");
}
long startTime = System.currentTimeMillis();
try {
String name = getRandomName();
client.makeBucket(name);
if (!client.bucketExists(name)) {
throw new Exception("[FAILED] bucket does not exist");
}
client.removeBucket(name);
mintSuccessLog("bucketExists(String bucketName)", null, startTime);
} catch (Exception e) {
mintFailedLog("bucketExists(String bucketName)", null, startTime, null,
e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
}
/**
* Test: removeBucket(String bucketName).
*/
public static void removeBucket_test() throws Exception {
if (!mintEnv) {
System.out.println("Test: removeBucket(String bucketName)");
}
long startTime = System.currentTimeMillis();
try {
String name = getRandomName();
client.makeBucket(name);
client.removeBucket(name);
mintSuccessLog("removeBucket(String bucketName)", null, startTime);
} catch (Exception e) {
mintFailedLog("removeBucket(String bucketName)", null, startTime, null,
e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
}
/**
* Tear down test setup.
*/
public static void setup() throws Exception {
client.makeBucket(bucketName);
}
/**
* Tear down test setup.
*/
public static void teardown() throws Exception {
client.removeBucket(bucketName);
}
/**
* Test: putObject(String bucketName, String objectName, String filename).
*/
public static void putObject_test1() throws Exception {
if (!mintEnv) {
System.out.println("Test: putObject(String bucketName, String objectName, String filename)");
}
long startTime = System.currentTimeMillis();
try {
String filename = createFile1Kb();
client.putObject(bucketName, filename, filename);
Files.delete(Paths.get(filename));
client.removeObject(bucketName, filename);
mintSuccessLog("putObject(String bucketName, String objectName, String filename)", "filename: 1KB", startTime);
} catch (Exception e) {
mintFailedLog("putObject(String bucketName, String objectName, String filename)", "filename: 1KB", startTime,
null, e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
}
/**
* Test: multipart: putObject(String bucketName, String objectName, String
* filename).
*/
public static void putObject_test2() throws Exception {
if (!mintEnv) {
System.out.println("Test: multipart: putObject(String bucketName, String objectName, String filename)");
}
long startTime = System.currentTimeMillis();
try {
String filename = createFile6Mb();
client.putObject(bucketName, filename, filename);
Files.delete(Paths.get(filename));
client.removeObject(bucketName, filename);
mintSuccessLog("putObject(String bucketName, String objectName, String filename)", "filename: 65MB", startTime);
} catch (Exception e) {
mintFailedLog("putObject(String bucketName, String objectName, String filename)", "filename: 65MB", startTime,
null, e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
}
/**
* Test: With content-type: putObject(String bucketName, String objectName,
* String filename, String contentType).
*/
public static void putObject_test3() throws Exception {
if (!mintEnv) {
System.out
.println("Test: putObject(String bucketName, String objectName, String filename," + " String contentType)");
}
long startTime = System.currentTimeMillis();
try {
String filename = createFile1Kb();
client.putObject(bucketName, filename, filename, customContentType);
Files.delete(Paths.get(filename));
client.removeObject(bucketName, filename);
mintSuccessLog("putObject(String bucketName, String objectName, String filename, String contentType)",
"contentType: " + customContentType, startTime);
} catch (Exception e) {
mintFailedLog("putObject(String bucketName, String objectName, String filename, String contentType)",
"contentType: " + customContentType, startTime, null,
e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
}
/**
* Test: putObject(String bucketName, String objectName, InputStream body, long
* size, String contentType).
*/
public static void putObject_test4() throws Exception {
if (!mintEnv) {
System.out.println("Test: putObject(String bucketName, String objectName, InputStream body, "
+ "long size, String contentType)");
}
long startTime = System.currentTimeMillis();
try {
String objectName = getRandomName();
try (final InputStream is = new ContentInputStream(1 * KB)) {
client.putObject(bucketName, objectName, is, 1 * KB, customContentType);
}
client.removeObject(bucketName, objectName);
mintSuccessLog(
"putObject(String bucketName, String objectName, InputStream body, long size," + " String contentType)",
"size: 1 KB, objectName: " + customContentType, startTime);
} catch (Exception e) {
mintFailedLog("putObject(String bucketName, String objectName, InputStream body, long size, String contentType)",
"size: 1 KB, objectName: " + customContentType, startTime, null,
e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
}
/**
* Test: putObject(String bucketName, String objectName, InputStream body, long
* size, String contentType). where objectName has multiple path segments.
*/
public static void putObject_test6() throws Exception {
if (!mintEnv) {
System.out.println("Test: objectName with path segments: "
+ "putObject(String bucketName, String objectName, InputStream body, " + "long size, String contentType)");
}
long startTime = System.currentTimeMillis();
try {
String objectName = "/path/to/" + getRandomName();
try (final InputStream is = new ContentInputStream(1 * KB)) {
client.putObject(bucketName, objectName, is, 1 * KB, customContentType);
}
client.removeObject(bucketName, objectName);
mintSuccessLog(
"putObject(String bucketName, String objectName, InputStream body, long size," + " String contentType)",
"size: 1 KB, contentType: " + customContentType, startTime);
} catch (Exception e) {
mintFailedLog(
"putObject(String bucketName, String objectName, InputStream body, long size," + " String contentType)",
"size: 1 KB, contentType: " + customContentType, startTime, null,
e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
}
/**
* Test put object with unknown sized stream.
*/
public static void testPutObjectUnknownStreamSize(long size) throws Exception {
if (!mintEnv) {
System.out
.println("Test: putObject(String bucketName, String objectName, InputStream body, " + "String contentType)");
}
long startTime = System.currentTimeMillis();
try {
String objectName = getRandomName();
try (final InputStream is = new ContentInputStream(size)) {
client.putObject(bucketName, objectName, is, customContentType);
}
client.removeObject(bucketName, objectName);
mintSuccessLog("putObject(String bucketName, String objectName, InputStream body, String contentType)",
"contentType: " + customContentType, startTime);
} catch (Exception e) {
mintFailedLog("putObject(String bucketName, String objectName, InputStream body, long size, String contentType)",
"contentType: " + customContentType, startTime, null,
e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
}
/**
* Test: putObject(String bucketName, String objectName, InputStream body,
* String contentType).
*/
public static void putObject_test7() throws Exception {
testPutObjectUnknownStreamSize(3 * KB);
}
/**
* Test: multipart: putObject(String bucketName, String objectName, InputStream
* body, String contentType).
*/
public static void putObject_test8() throws Exception {
testPutObjectUnknownStreamSize(10 * MB);
}
/**
* Test: putObject(String bucketName, String objectName, InputStream stream,
* long size, Map<String, String> headerMap).
*/
public static void putObject_test9() throws Exception {
if (!mintEnv) {
System.out.println("Test: putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, Map<String, String> headerMap).");
}
long startTime = System.currentTimeMillis();
try {
String objectName = getRandomName();
Map<String, String> headerMap = new HashMap<>();
headerMap.put("Content-Type", customContentType);
try (final InputStream is = new ContentInputStream(1 * KB)) {
client.putObject(bucketName, objectName, is, 1 * KB, headerMap);
}
client.removeObject(bucketName, objectName);
mintSuccessLog("putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, Map<String, String> headerMap)", "size: 1 KB", startTime);
} catch (Exception e) {
mintFailedLog(
"putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, Map<String, String> headerMap)",
"size: 1 KB", startTime, null, e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
}
/**
* Test: putObject(String bucketName, String objectName, InputStream stream,
* long size, Map<String, String> headerMap) with Storage Class
* REDUCED_REDUNDANCY.
*/
@SuppressFBWarnings("UCF")
public static void putObject_test10() throws Exception {
if (!mintEnv) {
System.out.println("Test: putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, Map<String, String> headerMap). with Storage Class REDUCED_REDUNDANCY set");
}
long startTime = System.currentTimeMillis();
try {
String storageClass = "REDUCED_REDUNDANCY";
String objectName = getRandomName();
Map<String, String> headerMap = new HashMap<>();
headerMap.put("Content-Type", customContentType);
headerMap.put("X-Amz-Storage-Class", storageClass);
try (final InputStream is = new ContentInputStream(1 * KB)) {
client.putObject(bucketName, objectName, is, 1 * KB, headerMap);
}
ObjectStat objectStat = client.statObject(bucketName, objectName);
Map<String, List<String>> returnHeader = objectStat.httpHeaders();
List<String> returnStorageClass = returnHeader.get("X-Amz-Storage-Class");
if ((returnStorageClass != null) && (!storageClass.equals(returnStorageClass.get(0)))) {
throw new Exception("Metadata mismatch");
}
client.removeObject(bucketName, objectName);
mintSuccessLog("putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, Map<String, String> headerMap)", "size: 1 KB", startTime);
} catch (Exception e) {
mintFailedLog(
"putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, Map<String, String> headerMap)",
"size: 1 KB", startTime, null, e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
}
/**
* Test: putObject(String bucketName, String objectName, InputStream stream,
* long size, Map<String, String> headerMap) with Storage Class STANDARD.
*/
public static void putObject_test11() throws Exception {
if (!mintEnv) {
System.out.println("Test: putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, Map<String, String> headerMap). with Storage Class STANDARD set");
}
long startTime = System.currentTimeMillis();
try {
String storageClass = "STANDARD";
String objectName = getRandomName();
Map<String, String> headerMap = new HashMap<>();
headerMap.put("Content-Type", customContentType);
headerMap.put("X-Amz-Storage-Class", storageClass);
try (final InputStream is = new ContentInputStream(1 * KB)) {
client.putObject(bucketName, objectName, is, 1 * KB, headerMap);
}
ObjectStat objectStat = client.statObject(bucketName, objectName);
Map<String, List<String>> returnHeader = objectStat.httpHeaders();
List<String> returnStorageClass = returnHeader.get("X-Amz-Storage-Class");
// Standard storage class shouldn't be present in metadata response
if (returnStorageClass != null) {
throw new Exception("Did not expect: " + storageClass + " in response metadata");
}
client.removeObject(bucketName, objectName);
mintSuccessLog("putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, Map<String, String> headerMap)", "size: 1 KB", startTime);
} catch (Exception e) {
mintFailedLog(
"putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, Map<String, String> headerMap)",
"size: 1 KB", startTime, null, e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
}
/**
* Test: putObject(String bucketName, String objectName, InputStream stream,
* long size, Map<String, String> headerMap). with invalid Storage Class
* set
*/
public static void putObject_test12() throws Exception {
if (!mintEnv) {
System.out.println("Test: putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, Map<String, String> headerMap). with invalid Storage Class set");
}
long startTime = System.currentTimeMillis();
try {
String storageClass = "INVALID";
String objectName = getRandomName();
Map<String, String> headerMap = new HashMap<>();
headerMap.put("Content-Type", customContentType);
headerMap.put("X-Amz-Storage-Class", storageClass);
try (final InputStream is = new ContentInputStream(1 * KB)) {
client.putObject(bucketName, objectName, is, 1 * KB, headerMap);
} catch (ErrorResponseException e) {
if (!e.errorResponse().code().equals("InvalidStorageClass")) {
throw e;
}
}
client.removeObject(bucketName, objectName);
mintSuccessLog("putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, Map<String, String> headerMap)", "size: 1 KB", startTime);
} catch (Exception e) {
mintFailedLog(
"putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, Map<String, String> headerMap)",
"size: 1 KB", startTime, null, e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
}
/**
* Test: putObject(String bucketName, String objectName, InputStream stream,
* long size, ServerSideEncryption sse). To test SSE_C
*/
public static void putObject_test13() throws Exception {
if (!mintEnv) {
System.out.println("Test: putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, ServerSideEncryption sse) using SSE_C. ");
}
long startTime = System.currentTimeMillis();
// Generate a new 256 bit AES key - This key must be remembered by the client.
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256);
ServerSideEncryption sse = ServerSideEncryption.withCustomerKey(keyGen.generateKey());
try {
String objectName = getRandomName();
try (final InputStream is = new ContentInputStream(1 * KB)) {
client.putObject(bucketName, objectName, is, 1 * KB, sse);
}
client.removeObject(bucketName, objectName);
mintSuccessLog("putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, ServerSideEncryption sse) using SSE_C.", "size: 1 KB", startTime);
} catch (Exception e) {
mintFailedLog(
"putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, ServerSideEncryption sse) using SSE_C.",
"size: 1 KB", startTime, null, e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
}
/**
* Test: putObject(String bucketName, String objectName, InputStream stream,
* long size, ServerSideEncryption sse). To test SSE_S3
*/
public static void putObject_test14() throws Exception {
if (!mintEnv) {
System.out.println("Test: putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, ServerSideEncryption sse) using SSE_S3.");
}
long startTime = System.currentTimeMillis();
ServerSideEncryption sse = ServerSideEncryption.atRest();
try {
String objectName = getRandomName();
try (final InputStream is = new ContentInputStream(1 * KB)) {
client.putObject(bucketName, objectName, is, 1 * KB, sse);
}
client.removeObject(bucketName, objectName);
mintSuccessLog("putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, ServerSideEncryption sse) using SSE_S3.", "size: 1 KB", startTime);
} catch (Exception e) {
mintFailedLog(
"putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, ServerSideEncryption sse) using SSE_S3.",
"size: 1 KB", startTime, null, e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
}
/**
* Test: putObject(String bucketName, String objectName, InputStream stream,
* long size, ServerSideEncryption sse). To test SSE_KMS
*/
public static void putObject_test15() throws Exception {
if (!mintEnv) {
System.out.println("Test: putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, ServerSideEncryption sse) using SSE_KMS.");
}
long startTime = System.currentTimeMillis();
Map<String, String> myContext = new HashMap<>();
myContext.put("key1", "value1");
String keyId = "";
keyId = System.getenv("MINT_KEY_ID");
if (keyId.equals("")) {
mintIgnoredLog("getBucketPolicy(String bucketName)", null, startTime);
}
ServerSideEncryption sse = ServerSideEncryption.withManagedKeys("keyId", myContext);
try {
String objectName = getRandomName();
try (final InputStream is = new ContentInputStream(1 * KB)) {
client.putObject(bucketName, objectName, is, 1 * KB, sse);
}
client.removeObject(bucketName, objectName);
mintSuccessLog("putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, ServerSideEncryption sse) using SSE_KMS.", "size: 1 KB", startTime);
} catch (Exception e) {
mintFailedLog(
"putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, ServerSideEncryption sse) using SSE_KMS.",
"size: 1 KB", startTime, null, e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
}
/**
* Test: putObject(String bucketName, String objectName, InputStream stream,
* long size, ServerSideEncryption sse). To test SSE_C (multi-part upload).
*/
public static void putObject_test16() throws Exception {
if (!mintEnv) {
System.out.println("Test: putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, ServerSideEncryption sse) using SSE_C (Multi-part upload). ");
}
long startTime = System.currentTimeMillis();
// Generate a new 256 bit AES key - This key must be remembered by the client.
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256);
ServerSideEncryption sse = ServerSideEncryption.withCustomerKey(keyGen.generateKey());
try {
String objectName = getRandomName();
try (final InputStream is = new ContentInputStream(6 * MB)) {
client.putObject(bucketName, objectName, is, 6 * MB, sse);
}
client.removeObject(bucketName, objectName);
mintSuccessLog("putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, ServerSideEncryption sse) using SSE_C (Multi-part upload).", "size: 6 MB", startTime);
} catch (Exception e) {
mintFailedLog(
"putObject(String bucketName, String objectName, InputStream stream, "
+ "long size, ServerSideEncryption sse) using SSE_C (Multi-part upload).",
"size: 6 MB", startTime, null, e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
}
/**
* Test: putObject(String bucketName, String objectName, ServerSideEncryption
* sse, String filename). To test SSE_C
*
*/
public static void putObject_test17() throws Exception {
if (!mintEnv) {
System.out.println("Test: putObject(String bucketName, String objectName, ServerSideEncryption sse, "
+ "String filename). To test SSE_C. ");
}
long startTime = System.currentTimeMillis();
// Generate a new 256 bit AES key - This key must be remembered by the client.
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256);
ServerSideEncryption sse = ServerSideEncryption.withCustomerKey(keyGen.generateKey());
try {
String objectName = getRandomName();
String filename = createFile1Kb();
client.putObject(bucketName, objectName, sse, filename);
Files.delete(Paths.get(filename));
client.removeObject(bucketName, objectName);
mintSuccessLog("putObject(String bucketName, String objectName, ServerSideEncryption sse, "
+ "String filename). To test SSE_C", "size: 1 KB", startTime);
} catch (Exception e) {
mintFailedLog(
"putObject(String bucketName, String objectName, ServerSideEncryption sse, "
+ "String filename). To test SSE_C",
"size: 1 KB", startTime, null, e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
}
/**
* Test: statObject(String bucketName, String objectName).
*/
public static void statObject_test1() throws Exception {
if (!mintEnv) {
System.out.println("Test: statObject(String bucketName, String objectName)");
}
long startTime = System.currentTimeMillis();
try {
String objectName = getRandomName();
Map<String, String> headerMap = new HashMap<>();
headerMap.put("Content-Type", customContentType);
headerMap.put("my-custom-data", "foo");
try (final InputStream is = new ContentInputStream(1)) {
client.putObject(bucketName, objectName, is, 1, headerMap);
}
ObjectStat objectStat = client.statObject(bucketName, objectName);
if (!(objectName.equals(objectStat.name()) && (objectStat.length() == 1)
&& bucketName.equals(objectStat.bucketName()) && objectStat.contentType().equals(customContentType))) {
throw new Exception("[FAILED] object stat differs");
}
Map<String, List<String>> httpHeaders = objectStat.httpHeaders();
if (!httpHeaders.containsKey("x-amz-meta-my-custom-data")) {
throw new Exception("[FAILED] metadata not found in object stat");
}
List<String> values = httpHeaders.get("x-amz-meta-my-custom-data");
if (values.size() != 1) {
throw new Exception("[FAILED] too many metadata value. expected: 1, got: " + values.size());
}
if (!values.get(0).equals("foo")) {
throw new Exception("[FAILED] wrong metadata value. expected: foo, got: " + values.get(0));
}
client.removeObject(bucketName, objectName);
mintSuccessLog("statObject(String bucketName, String objectName)", null, startTime);
} catch (Exception e) {
mintFailedLog("statObject(String bucketName, String objectName)", null, startTime, null,
e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
}
/**
* Test: statObject(String bucketName, String objectName, ServerSideEncryption
* sse). To test statObject using SSE_C.
*/
public static void statObject_test2() throws Exception {
if (!mintEnv) {
System.out.println(
"Test: statObject(String bucketName, String objectName, ServerSideEncryption sse)" + " using SSE_C.");
}
long startTime = System.currentTimeMillis();
try {
String objectName = getRandomName();
// Generate a new 256 bit AES key - This key must be remembered by the client.
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256);
ServerSideEncryption sse = ServerSideEncryption.withCustomerKey(keyGen.generateKey());
try (final InputStream is = new ContentInputStream(1)) {
client.putObject(bucketName, objectName, is, 1, sse);
}
ObjectStat objectStat = client.statObject(bucketName, objectName, sse);
if (!(objectName.equals(objectStat.name()) && (objectStat.length() == 1)
&& bucketName.equals(objectStat.bucketName()))) {
throw new Exception("[FAILED] object stat differs");
}
Map<String, List<String>> httpHeaders = objectStat.httpHeaders();
if (!httpHeaders.containsKey("X-Amz-Server-Side-Encryption-Customer-Algorithm")) {
throw new Exception("[FAILED] metadata not found in object stat");
}
List<String> values = httpHeaders.get("X-Amz-Server-Side-Encryption-Customer-Algorithm");
if (values.size() != 1) {
throw new Exception("[FAILED] too many metadata value. expected: 1, got: " + values.size());
}
if (!values.get(0).equals("AES256")) {
throw new Exception("[FAILED] wrong metadata value. expected: AES256, got: " + values.get(0));
}
client.removeObject(bucketName, objectName);