summaryrefslogtreecommitdiffstats
path: root/tcllib/modules/amazon-s3/S3.test
blob: b79227ae151c4d5f54dd9f3771625fbb3843c8f5 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
# -*- tcl -*-
# S3.test:  tests for the S3 access package.

# This file contains a collection of tests for the S3
# package. Sourcing this file into Tcl runs the tests and generates
# output for errors.  No output means no errors were found.

# Copyright (c) 2006,2008 Darren New. All Rights Reserved.
# Copyright (c) 2008 Andreas Kupries <andreas_kupries@users.sourceforge.net>
#               (Boilerplate stuff (header, footer))
# All rights reserved.
#
# RCS: @(#) $Id: S3.test,v 1.3 2008/09/04 02:11:12 andreas_kupries Exp $

# -------------------------------------------------------------------------

source [file join \
	[file dirname [file dirname [file join [pwd] [info script]]]] \
	devtools testutilities.tcl]

testsNeedTcl     8.5
testsNeedTcltest 2.0

if {[catch {package require xml}]} {
    puts "    Aborting the tests found in \"[file tail [info script]]\""
    puts "    Requiring xml package, not found."
    return
}

support {
    # Requires xml (TclXML)
    useLocal xsxp.tcl xsxp
}
testing {
    useLocal S3.tcl S3
}

# -------------------------------------------------------------------------

# I normally leave BucketDeletion false, because Amazon gets cranky
# if you delete a bucket and then try to recreate it any time soon.

# This may clobber files starting with the characers "S3T". Don't
# run it in a directory with such files you want.

# Put your own keys in S3-test.config.

tcltest::customMatch S3err S3ErrorMatch

tcltest::testConstraint BucketDeletion false
tcltest::testConstraint REST true
tcltest::testConstraint BucketIO true
tcltest::testConstraint ItemIO true
tcltest::testConstraint Put true
tcltest::testConstraint Get true
tcltest::testConstraint Acl true
tcltest::testConstraint Head true
tcltest::testConstraint Directory true
tcltest::testConstraint Delete true

tcltest::configure -verbose {body error pass skip start}
tcltest::configure -debug 1

# Allow easy testing of S3-style errorCode returns.

proc S3expectErr {code} {
    global errorCode
    set errorCode {}
    set x [catch $code result]
    return [concat $x $errorCode]
}

proc S3ErrorMatch {expected actual} {
    if {$expected eq [lrange $actual 0 [expr {[llength $expected]-1}]]} {
	return true
    } else {
	return false
    }
}

# Allow easy testing of background tasks.

proc S3expectBackgroundREST {req} {
    # Might be done better, tho...
    set ::S3::afterResult {}
    set ::S3::afterRan 0
    set y [after 1 {set ::S3::afterRan 1}]
    S3::REST $req
    vwait [dict get $req resultvar]
    set x [set [dict get $req resultvar]]
    after cancel $y
    #if {$::S3::afterResult eq "AFTER-FAILURE"} {
	#error "Background task never returned value" "" [after info $x]
    #}
    if {[string match "BGERROR*" $::S3::afterResult]} {
	error "BGError triggered: $::S3::afterResult" "" $::S3::afterResult
    }
    if {0 == $::S3::afterRan} {
	error "Concurrent events did not run" "" "S3 test afterRan"
    }
    return $x
}

proc S3expectBackground {code} {
    # Might be done better, tho...
    set ::S3::afterResult {}
    set ::S3::afterRan 0
    set y [after 1 {set ::S3::afterRan 1}]
    set x [eval $code]
    after cancel $y
    #if {$::S3::afterResult eq "AFTER-FAILURE"} {
	#error "Background task never returned value" "" [after info $x]
    #}
    if {[string match "BGERROR*" $::S3::afterResult]} {
	error "BGError triggered: $::S3::afterResult" "" $::S3::afterResult
    }
    if {0 == $::S3::afterRan} {
	error "Concurrent events did not run" "" "S3 test afterRan"
    }
    return $x
}

proc bgerror {args} {set ::S3::afterResult [list "BGERROR" $args $::errorInfo]}

# Allow easy incorporation of user's AccessID and SecretKey

proc S3loadKeys {} {
    source test-S3.config
}

namespace import ::tcltest::test

proc CleanUpBuckets {{buckets 0}} {
    S3loadKeys
    set bucket [S3::SuggestBucket TclTestS3b]
    for {set i 0} {$i < 25} {incr i} {
	puts "Deleting $i of 25"
	for {set j 0} {$j < 10} {incr j} {
	    set q [format %02d $i]
	    set d [S3::REST \
		[dict create verb DELETE resource /$bucket/thing/$q/$j]]
	    S3::throwhttp $d
	}
    }
    S3::REST [dict create verb DELETE resource /$bucket/fred ]
    S3::REST [dict create verb DELETE resource /$bucket/barney ]
    S3::REST [dict create verb DELETE resource /$bucket/wilma ]
    S3::REST [dict create verb DELETE resource /$bucket/betty ]
    S3::REST [dict create verb DELETE resource /$bucket/cartman ]
    S3::REST [dict create verb DELETE resource /$bucket/cartoon/tweety ]
    S3::REST [dict create verb DELETE resource /$bucket/cartoon/sylvester ]
    S3::REST [dict create verb DELETE resource "/$bucket/cartoon/road runner" ]
    S3::REST [dict create verb DELETE \
	resource "/$bucket/cartoon/wile e. coyote" ]
    if {$buckets} {S3::REST [dict create verb DELETE resource /$bucket]}
}

# CleanUpBuckets 0 ; exit

# Test URL encoding

test S3-1.10 {URL encoding no parameters} -body {
    S3::to_url /quotes/nelson {}
} -result {/quotes/nelson}

test S3-1.20 {URL encoding with parameters} -body {
    S3::to_url /quotes/nelson {alpha one beta two}
} -result {/quotes/nelson?alpha=one&beta=two}

test S3-1.30 {URL encoding with parameters and query} -body {
    S3::to_url /quotes/nelson?acl {alpha one beta two}
} -result {/quotes/nelson?acl&alpha=one&beta=two}

test S3-1.40 {URL with non-ASCII characters} -body {
    set funky "/xyzzy/zz+fun\(\)good?junk space"
    append funky "&and_utf-8\u2211Sigma\u5927Da"
    S3::encode_url $funky
} -result {/xyzzy/zz%2bfun%28%29good%3fjunk%20space%26and_utf-8%e2%88%91Sigma%e5%a4%a7Da}

test S3-1.50 {Check out content types A} -setup {
    tcltest::makeFile "This is just text" "S3junk.txt"
} -body {
    S3::contenttype S3junk.txt
} -cleanup {
    tcltest::removeFile "S3junk.txt"
} -result "text/plain"

test S3-1.60 {Check out content types A} -body {
    # May be unhappy under UNIX?
    S3::contenttype origT1.jpg
} -result "image/jpeg"

test S3-2.10 {Config no args} -body {
    array set x [S3::Configure]
    foreach key [lsort [array names x]] {
	puts $key ; puts $x($key)
    }
} -cleanup {unset x} -output "-accesskeyid\n\n-bucket-prefix\nTclS3\n-default-acl\n\n-default-bucket\n\n-default-compare\nalways\n-default-separator\n/\n-reset\nfalse\n-retries\n3\n-secretaccesskey\n\n-service-access-point\ns3.amazonaws.com\n-slop-seconds\n3\n-use-tls\nfalse\n"
 
test S3-2.20 {Config, one arg} -body {
    S3::Configure -bucket-prefix
} -result {TclS3}

test S3-2.30 {Config, set bucket prefix} -body {
    S3::Configure -bucket-prefix TclTestS3
    S3::Configure -bucket-prefix
} -result {TclTestS3}

test S3-2.40 {Config, bad first argument} -body {
    S3expectErr {S3::Configure -xyzzy}
} -result "1 S3 usage -xyzzy" -match S3err

test S3-2.50 {Config, wrong number of pairs} -body {
    set ::errorCode {}
    S3::Configure -bucket-prefix TclTestS3
    set x [catch {S3::Configure -bucket-prefix 1234 -use-tls}]
    set y [S3::Configure -bucket-prefix]
    return [concat $x [lrange $::errorCode 0 1] $y]
} -result {1 S3 usage TclTestS3} -cleanup {unset x ; unset y}

test S3-2.60 {Config, test reset} -body {
    S3::Configure -bucket-prefix XYZZY -reset true
    return [S3::Configure -bucket-prefix]
} -result TclS3

test S3-2.70 {Suggest bucket name} -body {
    S3::Configure -accesskeyid 44CF9590006BF252F707 \
	-secretaccesskey OtxrzxIsfpFjA7SwPzILwy8Bw21TLhquhboDYROV
    set x [S3::SuggestBucket Bloop]
    return [concat [string match *Bloop* $x] \
	[string match *44CF9590006BF252F707* $x] \
	[string match *OtxrzxIsfpFjA7SwPzILwy8Bw21TLhquhboDYROV* $x]]
} -result {1 1 0}

# Now test the stuff from the manual

test S3-3.10 {First documentation example of AUTH} -body {
    S3::Configure -accesskeyid 44CF9590006BF252F707 \
	-secretaccesskey OtxrzxIsfpFjA7SwPzILwy8Bw21TLhquhboDYROV
    set verb put
    set resource /quotes/nelson
    set content-type text/html
    set headers {
	date "Thu, 17 Nov 2005 18:49:58 GMT"
	content-md5 c8fdb181845a4ca6b8fec737b3581d76
	x-amz-meta-author foo@bar.com
	x-amz-magic abracadabra
    }
    set res [S3::authREST $verb $resource ${content-type} $headers]
    dict get $res authorization
} -result {AWS 44CF9590006BF252F707:jZNOcbfWmD/A/f3hSvVzXZjM2HU=}

test S3-3.20 {Second documentation example of AUTH} -body {
    S3::Configure -accesskeyid 44CF9590006BF252F707 \
	-secretaccesskey OtxrzxIsfpFjA7SwPzILwy8Bw21TLhquhboDYROV
    set verb GET
    set resource /quotes/nelson
    set headers {
	date XXXXXXX
	x-amz-magic abracadabra
	x-amz-date "Thu, 17 Nov 2005 18:49:58 GMT"
    }
    set res [S3::authREST $verb $resource "" $headers]
    dict get $res authorization
} -result {AWS 44CF9590006BF252F707:5m+HAmc5JsrgyDelh9+a2dNrzN8=}

test S3-4.10 {REST Blocking list of buckets} -constraints "BucketIO REST" \
	-setup S3loadKeys -body {
    set req [dict create verb GET resource /]
    set res [S3::REST $req]
    return [list [lsort [dict keys $res]] [dict get $res httpstatus] \
	[expr {0<[string length [dict get $res outbody]]}]]
} -result {{httpmessage httpstatus outbody outheaders resource verb} 200 1}

test S3-4.20 {REST Nonblocking list of buckets} -constraints "BucketIO REST" \
	-setup S3loadKeys -body {
    set req [dict create verb GET resource / resultvar ::S3RES]
    set res [S3expectBackgroundREST $req]
    return [list [lsort [dict keys $res]] [dict get $res httpstatus] \
	[expr {0<[string length [dict get $res outbody]]}]]
} -result {{httpmessage httpstatus outbody outheaders resource resultvar verb} 200 1}

test S3-4.30 {REST blocking create bucket} -constraints "BucketIO REST" \
	-setup S3loadKeys -body {
    set b "TclTestS3.REST.[S3::Configure -accesskeyid]"
    set req [dict create verb PUT resource /$b headers {x-amz-acl public-read}]
    set res [S3::REST $req]
    return [dict get $res httpstatus]
} -result 200

test S3-4.40 {REST get bucket acl} -constraints "BucketIO REST" \
	-setup S3loadKeys -body {
    set b "TclTestS3.REST.[S3::Configure -accesskeyid]"
    set req [dict create verb GET resource /$b rtype acl]
    set res [S3::REST $req]
    set lookfor {<URI>http://acs.amazonaws.com/groups/global/AllUsers</URI></Grantee><Permission>READ</Permission>}
    set found [expr {-1 != [string first $lookfor $res]}]
    return [list $found [dict get $res httpstatus]]
} -result "1 200"

test S3-4.50 {REST blocking put,get,compare contents} \
	-constraints "ItemIO REST" \
	-setup S3loadKeys -body {
    set body "This is a test. This is only a test.\nHad this been a real emergency, you would be dead.\n"
    set b "TclTestS3.REST.[S3::Configure -accesskeyid]"
    set req [dict create verb PUT resource /$b/t1.txt inbody $body \
	headers {x-amz-acl public-read}]
    set res [S3::REST $req]
    set r1 [dict get $res httpstatus]
    set req [dict create verb GET resource /$b/t1.txt rtype acl]
    set res [S3::REST $req]
    set lookfor {<URI>http://acs.amazonaws.com/groups/global/AllUsers</URI></Grantee><Permission>READ</Permission>}
    set r2 [expr {-1 != [string first $lookfor $res]}]
    set req [dict create verb GET resource /$b/t1.txt]
    set res [S3::REST $req]
    set r3 [string compare $body [dict get $res outbody]]
    return [list $r1 $r2 $r3]
} -result "200 1 0"

test S3-4.60 {REST nonblocking put,get,compare contents} \
	-constraints "ItemIO REST" \
	-setup S3loadKeys -body {
    set body "This is a test. This is only a test.\nHad this been a real emergency, you would be dead.\n"
    set b "TclTestS3.REST.[S3::Configure -accesskeyid]"
    set req [dict create verb PUT resource /$b/t1.txt inbody $body \
	headers {x-amz-acl public-read} resultvar ::S3REST]
    set res [S3expectBackgroundREST $req]
    set r1 [dict get $res httpstatus]
    set req [dict create verb GET resource /$b/t1.txt rtype acl resultvar ::S3REST]
    set res [S3expectBackgroundREST $req]
    set lookfor {<URI>http://acs.amazonaws.com/groups/global/AllUsers</URI></Grantee><Permission>READ</Permission>}
    set r2 [expr {-1 != [string first $lookfor $res]}]
    set req [dict create verb GET resource /$b/t1.txt resultvar ::S3REST]
    set res [S3expectBackgroundREST $req]
    set r3 [string compare $body [dict get $res outbody]]
    return [list $r1 $r2 $r3]
} -result "200 1 0"

test S3-4.70 {REST blocking put,delete} \
	-constraints "ItemIO REST" \
	-setup S3loadKeys -body {
    set body "This is a test. This is only a test.\nHad this been a real emergency, you would be dead.\n"
    set b "TclTestS3.REST.[S3::Configure -accesskeyid]"
    set req [dict create verb PUT resource /$b/t1.txt inbody $body] 
    set res [S3::REST $req]
    set r1 [dict get $res httpstatus]
    set req [dict create verb DELETE resource /$b/t1.txt]
    set res [S3::REST $req]
    set r2 [dict get $res httpstatus]
    return [list $r1 $r2]
} -result "200 204" ; # Delete returns "no content"

test S3-4.80 {REST nonblocking put,delete} \
	-constraints "ItemIO REST" \
	-setup S3loadKeys -body {
    set body "This is a test. This is only a test.\nHad this been a real emergency, you would be dead.\n"
    set b "TclTestS3.REST.[S3::Configure -accesskeyid]"
    set req [dict create verb PUT resource /$b/t1.txt inbody $body \
	resultvar ::S3RES] 
    set res [S3expectBackgroundREST $req]
    set r1 [dict get $res httpstatus]
    set req [dict create verb DELETE resource /$b/t1.txt resultvar ::S3RES]
    set res [S3expectBackgroundREST $req]
    set r2 [dict get $res httpstatus]
    return [list $r1 $r2]
} -result "200 204" ; # Delete returns "no content"

test S3-4.90 {REST blocking put,head,delete} \
	-constraints "ItemIO REST" \
	-setup S3loadKeys -body {
    set body "This is a test. This is only a test.\nHad this been a real emergency, you would be dead.\n"
    set b "TclTestS3.REST.[S3::Configure -accesskeyid]"
    set req [dict create verb PUT resource /$b/t1.txt inbody $body]
    set res [S3::REST $req]
    set r1 [dict get $res httpstatus]
    set req [dict create verb HEAD resource /$b/t1.txt]
    set res [S3::REST $req]
    set r2 [dict get $res httpstatus]
    set r3 [string length [dict get $res outbody]]
    set req [dict create verb DELETE resource /$b/t1.txt]
    set res [S3::REST $req]
    set r4 [dict get $res httpstatus]
    set req [dict create verb HEAD resource /$b/t1.txt]
    set res [S3::REST $req]
    set r5 [dict get $res httpstatus]
    return [list $r1 $r2 $r3 $r4 $r5]
} -result "200 200 0 204 404" 

test S3-4.100 {REST blocking put,head,delete from big body} \
	-constraints "ItemIO REST" \
	-setup S3loadKeys -body {
    set body "0123456789"
    set body [string repeat $body 50000] ; # Make body 500,000 bytes.
    set b "TclTestS3.REST.[S3::Configure -accesskeyid]"
    set req [dict create verb PUT resource /$b/t1.txt inbody $body]
    set res [S3::REST $req]
    set r1 [dict get $res httpstatus]
    set req [dict create verb HEAD resource /$b/t1.txt]
    set res [S3::REST $req]
    set r2 [dict get $res httpstatus]
    set r3 [string length [dict get $res outbody]]
    set r4 [dict get $res outheaders content-length]
    set req [dict create verb DELETE resource /$b/t1.txt]
    set res [S3::REST $req]
    set r5 [dict get $res httpstatus]
    set req [dict create verb HEAD resource /$b/t1.txt]
    set res [S3::REST $req]
    set r6 [dict get $res httpstatus]
    return [list $r1 $r2 $r3 $r4 $r5 $r6]
} -result "200 200 0 500000 204 404" 

test S3-4.110 {REST nonblocking put,head,delete from big body} \
	-constraints "ItemIO REST" \
	-setup S3loadKeys -body {
    set body "0123456789"
    set body [string repeat $body 50000] ; # Make body 500,000 bytes.
    set b "TclTestS3.REST.[S3::Configure -accesskeyid]"
    set req [dict create verb PUT resource /$b/t2.txt inbody $body resultvar ::S3RES]
    set res [S3expectBackgroundREST $req]
    set r1 [dict get $res httpstatus]
    set req [dict create verb HEAD resource /$b/t2.txt]
    set res [S3::REST $req]
    set r2 [dict get $res httpstatus]
    set r3 [string length [dict get $res outbody]]
    set r4 [dict get $res outheaders content-length]
    set req [dict create verb DELETE resource /$b/t2.txt]
    set res [S3::REST $req]
    set r5 [dict get $res httpstatus]
    set req [dict create verb HEAD resource /$b/t2.txt]
    set res [S3::REST $req]
    set r6 [dict get $res httpstatus]
    return [list $r1 $r2 $r3 $r4 $r5 $r6]
} -result "200 200 0 500000 204 404" 

test S3-4.120 {REST nonblocking put,head,delete from big file} \
	-constraints "ItemIO REST" \
	-setup S3loadKeys -body {
    set body "0123456789"
    set body [string repeat $body 50000] ; # Make body 500,000 bytes.
    tcltest::makeFile "XXX" S3Tone.txt
    set x [open S3Tone.txt w] ; puts -nonewline $x $body ; close $x
    set b "TclTestS3.REST.[S3::Configure -accesskeyid]"
    set req [dict create verb PUT resource /$b/t3.txt infile S3Tone.txt resultvar ::S3RES]
    set res [S3expectBackgroundREST $req]
    set r1 [dict get $res httpstatus]
    set req [dict create verb HEAD resource /$b/t3.txt]
    set res [S3::REST $req]
    set r2 [dict get $res httpstatus]
    set r3 [string length [dict get $res outbody]]
    set r4 [dict get $res outheaders content-length]
    set req [dict create verb DELETE resource /$b/t3.txt]
    set res [S3::REST $req]
    set r5 [dict get $res httpstatus]
    set req [dict create verb HEAD resource /$b/t3.txt]
    set res [S3::REST $req]
    set r6 [dict get $res httpstatus]
    tcltest::removeFile S3Tone.txt
    return [list $r1 $r2 $r3 $r4 $r5 $r6]
} -result "200 200 0 500000 204 404" 

test S3-4.130 {REST blocking put,head,delete from big file} \
	-constraints "ItemIO REST" \
	-setup S3loadKeys -body {
    set body "0123456789"
    set body [string repeat $body 50000] ; # Make body 500,000 bytes.
    tcltest::makeFile "XXX" S3Tone.txt
    set x [open S3Tone.txt w] ; puts -nonewline $x $body ; close $x
    set b "TclTestS3.REST.[S3::Configure -accesskeyid]"
    set req [dict create verb PUT resource /$b/t3.txt infile S3Tone.txt]
    set res [S3::REST $req]
    set r1 [dict get $res httpstatus]
    set req [dict create verb HEAD resource /$b/t3.txt]
    set res [S3::REST $req]
    set r2 [dict get $res httpstatus]
    set r3 [string length [dict get $res outbody]]
    set r4 [dict get $res outheaders content-length]
    set req [dict create verb DELETE resource /$b/t3.txt]
    set res [S3::REST $req]
    set r5 [dict get $res httpstatus]
    set req [dict create verb HEAD resource /$b/t3.txt]
    set res [S3::REST $req]
    set r6 [dict get $res httpstatus]
    tcltest::removeFile S3Tone.txt
    return [list $r1 $r2 $r3 $r4 $r5 $r6]
} -result "200 200 0 500000 204 404" 

test S3-4.140 {REST nonblocking put,get,delete into file} \
	-constraints "ItemIO REST" \
	-setup S3loadKeys -body {
    set body "0123456789"
    set body [string repeat $body 50000] ; # Make body 500,000 bytes.
    set b "TclTestS3.REST.[S3::Configure -accesskeyid]"
    set req [dict create verb PUT resource /$b/t5.txt inbody $body resultvar ::S3RES]
    set res [S3expectBackgroundREST $req]
    set r1 [dict get $res httpstatus]
    tcltest::makeFile "blah" S3Ttwo.txt
    set x [open S3Ttwo.txt w] ; fconfigure $x -translation binary -encoding binary
    set req [dict create verb GET resource /$b/t5.txt outchan $x]
    set res [S3::REST $req]
    close $x
    set r2 [dict get $res httpstatus]
    set r3 [file size S3Ttwo.txt]
    tcltest::removeFile S3Ttwo.txt
    set req [dict create verb DELETE resource /$b/t3.txt]
    set res [S3::REST $req]
    set r4 [dict get $res httpstatus]
    set req [dict create verb HEAD resource /$b/t3.txt]
    set res [S3::REST $req]
    set r5 [dict get $res httpstatus]
    return [list $r1 $r2 $r3 $r4 $r5]
} -result "200 200 500000 204 404" 

test S3-4.150 {REST blocking put,get,delete into file} \
	-constraints "ItemIO REST" \
	-setup S3loadKeys -body {
    set body "0123456789"
    set body [string repeat $body 50000] ; # Make body 500,000 bytes.
    set b "TclTestS3.REST.[S3::Configure -accesskeyid]"
    set req [dict create verb PUT resource /$b/t5.txt inbody $body]
    set res [S3::REST $req]
    set r1 [dict get $res httpstatus]
    tcltest::makeFile "blah" S3Ttwo.txt
    set x [open S3Ttwo.txt w] ; fconfigure $x -translation binary -encoding binary
    set req [dict create verb GET resource /$b/t5.txt outchan $x]
    set res [S3::REST $req]
    close $x
    set r2 [dict get $res httpstatus]
    set r3 [file size S3Ttwo.txt]
    tcltest::removeFile S3Ttwo.txt
    set req [dict create verb DELETE resource /$b/t5.txt]
    set res [S3::REST $req]
    set r4 [dict get $res httpstatus]
    set req [dict create verb HEAD resource /$b/t5.txt]
    set res [S3::REST $req]
    set r5 [dict get $res httpstatus]
    return [list $r1 $r2 $r3 $r4 $r5]
} -result "200 200 500000 204 404" 

test S3-4.160 {REST blocking put,get,delete of file with encoded name} \
	-constraints "ItemIO REST" \
	-setup S3loadKeys -body {
    set body "0123456789"
    set b "TclTestS3.REST.[S3::Configure -accesskeyid]"
    set funky "/$b/zz+fun\(\)good?junk space"
    append funky "&and_utf-8\u2211Sigma\u5927Da"
    set req [dict create verb PUT resource $funky inbody $body]
    set res [S3::REST $req]
    set r1 [dict get $res httpstatus]
    set req [dict create verb GET resource $funky]
    set res [S3::REST $req]
    set r2 [dict get $res httpstatus]
    set req [dict create verb DELETE resource $funky]
    set res [S3::REST $req]
    set r3 [dict get $res httpstatus]
    set req [dict create verb HEAD resource $funky]
    set res [S3::REST $req]
    set r4 [dict get $res httpstatus]
    return [list $r1 $r2 $r3 $r4]
} -result "200 200 204 404" 

test S3-4.170 {REST delete bucket} \
	-constraints "BucketDeletion REST" \
	-setup S3loadKeys -body {
    # Bucket ought to be empty by now. 
    # Of course, if a delete fails for some reason...
    set b "TclTestS3.REST.[S3::Configure -accesskeyid]"
    set req [dict create verb PUT resource /$b headers {x-amz-acl public-read}]
    set res [S3::REST $req]
    set r1 [dict get $res httpstatus]
    after 5000 ; # Give AWS a chance to remember it.
    set req [dict create verb DELETE resource /$b]
    set res [S3::REST $req]
    after 5000 ; # Give AWS a chance to remember it.
    set r2 [dict get $res httpstatus]
    set req [dict create verb GET resource /$b]
    set res [S3::REST $req]
    set r3 [dict get $res httpstatus]
    return [list $r1 $r2 $r3]
} -result "200 204 404"

test S3-10.10 {ListAllMyBuckets auth failure} -constraints BucketIO \
	-body {
    S3expectErr {
	S3::Configure -accesskeyid 44CF9590006BF252F707 \
	    -secretaccesskey OtxrzxIsfpFjA7SwPzILwy8Bw21TLhquhboDYROV
	S3::ListAllMyBuckets
    }
} -result "1 S3 remote 403" -match S3err

test S3-10.20 {ListAllMyBuckets usage params} -body {
    S3expectErr {
	S3::ListAllMyBuckets -blocking false -parse-xml {} -result-type REST
    }
} -result "1 S3 usage -parse-xml" -match S3err

test S3-10.30 {ListAllMyBuckets bad params two} -body {
    S3expectErr {S3::ListAllMyBuckets -xyz hello}
} -result "1 S3 usage -xyz" -match S3err

test S3-10.40 {ListAllMyBuckets bad params three} -body {
    S3expectErr {S3::ListAllMyBuckets -blocking false -parse-xml}
} -result "1 S3 usage -parse-xml" -match S3err

set testLAMB {<?xml version="1.0" encoding="UTF-8"?>
<ListAllMyBucketsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Owner><ID>9fb13c24488e3d7556693247d5a463c1837c3c8ede28f4094228e6c4eb5d70bd</ID><DisplayName>dnew@san.rr.com</DisplayName></Owner><Buckets><Bucket><Name>darren</Name><CreationDate>2006-10-29T07:04:48.000Z</CreationDate></Bucket><Bucket><Name>darren-test</Name><CreationDate>2006-10-29T07:04:48.000Z</CreationDate></Bucket><Bucket><Name>darren3</Name><CreationDate>2006-10-30T22:45:34.000Z</CreationDate></Bucket></Buckets></ListAllMyBucketsResult>}

test S3-10.50 {ListAllMyBuckets result parsing RAW} -body {
    S3::ListAllMyBuckets -parse-xml $testLAMB -result-type xml
} -result $testLAMB

test S3-10.60 {ListAllMyBuckets result parsing REST} -constraints BucketIO -body {
    set dict [S3::ListAllMyBuckets -result-type REST]
    dict get $dict httpstatus
} -result "403"

test S3-10.70 {ListAllMyBuckets result parsing PXML} -body {
    set pxml [S3::ListAllMyBuckets -result-type pxml -parse-xml $testLAMB]
    concat [lindex $pxml 0] [llength $pxml]
} -result "ListAllMyBucketsResult 4"

test S3-10.80 {ListAllMyBuckets result parsing NAMES} -body {
    # Note these are defined to be alphabetical, so no sorting needed
    S3::ListAllMyBuckets -result-type names -parse-xml $testLAMB
} -result "darren darren-test darren3"

test S3-10.90 {ListAllMyBuckets result parsing DICT} -body {
    set dict [S3::ListAllMyBuckets -result-type dict -parse-xml $testLAMB]
    puts [llength $dict]
    puts [dict get $dict Owner/ID]
    puts [dict get $dict Owner/DisplayName]
    puts [dict get $dict Bucket/Name]
    puts [dict get $dict Bucket/Date]
} -output {8
9fb13c24488e3d7556693247d5a463c1837c3c8ede28f4094228e6c4eb5d70bd
dnew@san.rr.com
darren darren-test darren3
2006-10-29T07:04:48.000Z 2006-10-29T07:04:48.000Z 2006-10-30T22:45:34.000Z
}

test S3-10.100 {ListAllMyBuckets result parsing OWNER} -body {
    S3::ListAllMyBuckets -result-type owner -parse-xml $testLAMB
} -result {9fb13c24488e3d7556693247d5a463c1837c3c8ede28f4094228e6c4eb5d70bd dnew@san.rr.com}

test S3-10.110 {ListAllMyBuckets result parsing error} -body {
    S3expectErr [list S3::ListAllMyBuckets -result-type xyzzy \
	-parse-xml $testLAMB]
} -result "1 S3 usage -result-type" -match S3err

test S3-10.120 {ListAllMyBuckets result parsing error} -body {
    S3expectErr {S3::ListAllMyBuckets -result-type xyzzy -parse-xml "<Hello"}
} -result "1 S3 usage xml" -match S3err

test S3-10.130 {ListAllMyBuckets background good} -constraints BucketIO -body {
    S3loadKeys
    set x [S3expectBackground {S3::ListAllMyBuckets -result-type REST -blocking false}]
    dict get $x httpstatus
} -result "200" 

test S3-10.140 {ListAllMyBuckets background bad} -constraints BucketIO -body {
    S3loadKeys
    S3expectErr {
	S3expectBackground {
	    S3::ListAllMyBuckets -result-type REST -blocking true
	}
    }
} -result "1 S3 test afterRan"  -match S3err

test S3-20.10 {PutBucket your own bucket} -constraints BucketIO -body {
    S3loadKeys
    set b [S3::SuggestBucket TclTestS3]
    S3::PutBucket -bucket $b
}

test S3-20.20 {PutBucket someone else's bucket} -constraints BucketIO -body {
    S3loadKeys
    S3expectErr {S3::PutBucket -bucket /test/}
} -result "1 S3 remote 409" -match S3err

test S3-20.30 {PutBucket background failure} -constraints BucketIO -body {
    S3loadKeys
    set b [S3::SuggestBucket TclTestS3]
    S3expectErr [list S3expectBackground [list S3::PutBucket -bucket $b]]
} -result "1 S3 test afterRan" -match S3err

test S3-20.40 {PutBucket background success} -constraints BucketIO -body {
    S3loadKeys
    set b [S3::SuggestBucket TclTestS3]
    S3expectBackground [list S3::PutBucket -bucket $b -blocking false]
}

test S3-20.50 {PutBucket test no acl} -constraints BucketIO -body {
    S3loadKeys
    set b [S3::SuggestBucket TclTestS3]
    S3::PutBucket -bucket $b 
    set d1 [dict create verb GET resource /$b rtype acl]
    set d2 [S3::REST $d1]
    set d3 [string first "READ" $d2]
    return [expr -1 == $d3]
} -result 1

test S3-20.60 {PutBucket test pubread acl} -constraints BucketIO -body {
    S3loadKeys
    set b [S3::SuggestBucket TclTestS3]
    S3::PutBucket -bucket $b -acl public-read
    set d1 [dict create verb GET resource /$b rtype acl]
    set d2 [S3::REST $d1]
    set d3 [string first "AllUsers" $d2]
    set d4 [string first "READ" $d2]
    return [expr 0 < $d3 && $d3 < $d4]
} -result 1

test S3-20.70 {PutBucket test given overrides default acl} \
	-constraints BucketIO -body {
    S3loadKeys
    set b [S3::SuggestBucket TclTestS3]
    S3::Configure -default-acl public-read-write
    S3::PutBucket -bucket $b -acl public-read
    S3::Configure -reset true
    S3loadKeys
    set d1 [dict create verb GET resource /$b rtype acl]
    set d2 [S3::REST $d1]
    set d3 [string first "AllUsers" $d2]
    set d4 [string first "READ" $d2]
    set d5 [string first "WRITE" $d2]
    return [expr 0 < $d3 && $d3 < $d4 && $d5 == -1]
} -result 1

test S3-20.80 {PutBucket test default acl} -constraints BucketIO -body {
    S3loadKeys
    set b [S3::SuggestBucket TclTestS3]
    S3::Configure -default-acl public-read-write
    S3::PutBucket -bucket $b 
    S3::Configure -reset true
    S3loadKeys
    set d1 [dict create verb GET resource /$b rtype acl]
    set d2 [S3::REST $d1]
    set d3 [string first "AllUsers" $d2]
    set d4 [string first "READ" $d2]
    set d5 [string first "WRITE" $d2]
    return [expr 0 < $d3 && $d3 < $d4 && $d3 < $d5]
} -result 1

test S3-30.10 {DeleteBucket error} \
	-constraints "BucketIO BucketDeletion" -body {
    S3loadKeys
    set b [S3::SuggestBucket TclTestS3]
    after 10000 ; # Wait for amazon to catch up
    S3expectErr {S3::DeleteBucket}
} -result "1 S3 usage -bucket" -match S3err

test S3-30.20 {DeleteBucket good} \
	-constraints "BucketIO BucketDeletion" -body {
    S3loadKeys
    set b [S3::SuggestBucket TclTestS3]
    after 10000 ; # Wait for amazon to catch up
    set x [S3::DeleteBucket -bucket $b]
    after 10000 ; # Wait for amazon to catch up
    return $x
}

test S3-30.30 {DeleteBucket fails on someone else's bucket} \
	-constraints "BucketIO BucketDeletion" -body {
    S3loadKeys
    set b "test"
    after 10000 ; # Wait for amazon to catch up
    S3expectErr [list S3::DeleteBucket -bucket $b]
} -result "1 S3 remote 403" -match S3err

# Since bucket create/delete is high overhead for Amazon,
# and it's flakey as well, don't test the background version,
# since it uses the same code.

# OK, since we need a bucket to test stuff, let's continue on.
S3loadKeys
set bucket [S3::SuggestBucket TclTestS3b]
set req [dict create verb HEAD resource /$bucket]
set res [S3::REST $req]
set r1 [dict get $res httpstatus]
set req [dict create verb HEAD resource /$bucket/fred]
set res [S3::REST $req]
set r2 [dict get $res httpstatus]
if {200 != $r1 || 200 != $r2} {
    S3::PutBucket -bucket $bucket
    if {[tcltest::testConstraint Directory]} {
	for {set i 0} {$i < 25} {incr i} {
	    puts "Creating $i of 25"
	    for {set j 0} {$j < 10} {incr j} {
		set q [format %02d $i]
		set d [S3::REST \
		    [dict create verb PUT resource /$bucket/thing/$q/$j \
			inbody "This is $j inside $i"]]
		S3::throwhttp $d
	    }
	}
    }
    S3::REST [dict create verb PUT resource /$bucket/fred inbody "Fred"]
    S3::REST [dict create verb PUT resource /$bucket/barney inbody "Barney"]
    S3::REST [dict create verb PUT resource /$bucket/wilma inbody "Wilma"]
    S3::REST [dict create verb PUT resource /$bucket/betty inbody "Betty"]
    S3::REST [dict create verb PUT resource /$bucket/cartman inbody "Cartman" ]
    S3::REST [dict create verb PUT resource /$bucket/cartoon/tweety \
	inbody "Tweety"]
    S3::REST [dict create verb PUT resource /$bucket/cartoon/sylvester \
	inbody "Sylvester"]
    S3::REST [dict create verb PUT resource "/$bucket/cartoon/road runner" \
	inbody "RoadRunner"]
    S3::REST [dict create verb PUT resource "/$bucket/cartoon/wile e. coyote" \
	inbody "Coyote"]
}

# Note that -result-type REST or xml or pxml without a maxcount all
# return lists of results of that type, since they don't really merge well.
test S3-40.10 {GetBucket basic call} -constraints BucketIO -body {
    set res [S3::GetBucket -bucket $bucket -result-type REST]
    set x1 [llength $res]
    set x2 [dict get [lindex $res 0] httpstatus]
    return "$x1 $x2"
} -result "1 200"

test S3-40.20 {GetBucket get xml} -constraints BucketIO -body {
    set res [S3::GetBucket -bucket $bucket -result-type xml]
    set x1 [llength $res]
    set x2 [lindex $res 0]
    set x3 [lindex [::xsxp::parse $x2] 0]
    return "$x1 $x3"
} -result "1 ListBucketResult"

test S3-40.30 {GetBucket get pxml} -constraints BucketIO -body {
    set res [S3::GetBucket -bucket $bucket -result-type pxml]
    set x1 [llength $res]
    set x2 [lindex $res 0]
    set x3 [lindex $x2 0]
    return "$x1 $x3"
} -result "1 ListBucketResult"

test S3-40.40 {GetBucket names} -constraints BucketIO -body {
    set r1 [S3::GetBucket -bucket $bucket -result-type names]
    set r2 [lsort $r1]
    set r3 [lsort -unique $r1]
    return [list [llength $r1] [expr {$r1 eq $r2}] [expr {$r2 eq $r3}]]
} -result "259 1 1"

test S3-40.50 {GetBucket simple looping} -constraints BucketIO -body {
    set res [S3::GetBucket -bucket $bucket -result-type REST -TEST 50]
    return [llength $res]
} -result "6" ; # 259, 50 at a time.

test S3-40.60 {GetBucket looping, return names} -constraints BucketIO -body {
    set r1 [S3::GetBucket -bucket $bucket -result-type names -TEST 50]
    set r2 [lsort $r1]
    set r3 [lsort -unique $r1]
    return [list [llength $r1] [expr {$r1 eq $r2}] [expr {$r2 eq $r3}]]
    return [llength $res]
} -result "259 1 1"; # Shouldn't see the inners here.

test S3-40.70 {GetBucket looping, return dict} -constraints BucketIO -body {
    set res [S3::GetBucket -bucket $bucket -result-type dict -TEST 50]
    set r1 [llength [dict get $res Key]]
    set r2 [string compare [dict get $res Key] [lsort [dict get $res Key]]]
    set r3 [llength [dict get $res LastModified]]
    set r4 [llength [dict get $res ETag]]
    set r5 [llength [dict get $res Size]]
    set r6 [llength [dict get $res Owner/ID]]
    set r7 [llength [dict get $res Owner/DisplayName]]
    set r8 [llength [dict get $res CommonPrefixes/Prefix]]
    return "$r1 $r2 $r3 $r4 $r5 $r6 $r7 $r8"
} -result "259 0 259 259 259 259 259 0"

test S3-40.80 {GetBucket non-looping, return dict} -constraints BucketIO -body {
    set res [S3::GetBucket -bucket $bucket -result-type dict]
    set r1 [llength [dict get $res Key]]
    set r2 [string compare [dict get $res Key] [lsort [dict get $res Key]]]
    set r3 [llength [dict get $res LastModified]]
    set r4 [llength [dict get $res ETag]]
    set r5 [llength [dict get $res Size]]
    set r6 [llength [dict get $res Owner/ID]]
    set r7 [llength [dict get $res Owner/DisplayName]]
    set r8 [llength [dict get $res CommonPrefixes/Prefix]]
    return "$r1 $r2 $r3 $r4 $r5 $r6 $r7 $r8"
} -result "259 0 259 259 259 259 259 0"

test S3-40.90 {GetBucket looping, prefix} -constraints BucketIO -body {
    set r [S3::GetBucket -bucket $bucket \
	-result-type names -TEST 50 -prefix "car"]
    join $r \n
} -result {cartman
cartoon/road runner
cartoon/sylvester
cartoon/tweety
cartoon/wile e. coyote}

test S3-40.100 {GetBucket delimiter, prefix} -constraints BucketIO -body {
    S3::GetBucket -bucket $bucket -result-type names -TEST 50 \
	-prefix /thing/ -delimiter /
} -result {thing/00/ thing/01/ thing/02/ thing/03/ thing/04/ thing/05/ thing/06/ thing/07/ thing/08/ thing/09/ thing/10/ thing/11/ thing/12/ thing/13/ thing/14/ thing/15/ thing/16/ thing/17/ thing/18/ thing/19/ thing/20/ thing/21/ thing/22/ thing/23/ thing/24/}

test S3-40.110 {GetBucket delimiter, prefix again} -constraints BucketIO -body {
    S3::GetBucket -bucket $bucket -result-type names -TEST 50 \
	-prefix thing -delimiter /
} -result {thing/}

test S3-40.120 {GetBucket delimiter, no prefix} -constraints BucketIO -body {
    S3::GetBucket -bucket $bucket -result-type names -TEST 50 -delimiter /
} -result {barney betty cartman cartoon/ fred thing/ wilma}

test S3-40.130 {GetBucket no default bucket} -constraints BucketIO -body {
    S3expectErr {
	S3::GetBucket -result-type names -TEST 50 -delimiter /
    }
} -result "1 S3 usage -bucket" -match S3err

test S3-40.140 {GetBucket with default bucket} -constraints BucketIO -body {
    S3::Configure -default-bucket $bucket
    set res [S3::GetBucket -result-type names -TEST 50 -delimiter /]
    S3::Configure -default-bucket ""
    return $res
} -result {barney betty cartman cartoon/ fred thing/ wilma}

set bucket [S3::SuggestBucket TclTestS3] ; # Maybe delete later.

proc getbody {resource} {
    set req [dict create verb GET resource $resource]
    set res [S3::REST $req]
    S3::throwhttp $res
    set body [dict get $res outbody]
    return $body
}

proc delbody {resource} {
    set req [dict create verb DELETE resource $resource]
    set res [S3::REST $req]
    S3::throwhttp $res
}

proc existsbody {resource} {
    set req [dict create verb HEAD resource $resource]
    set res [S3::REST $req]
    return [expr {[dict get $res httpstatus] eq "200"}]
}

# Make a setup/cleanup pair for checking constraints on PUT and GET
set pgsu {
    # Create an old file, and a new file, with different contents
    tcltest::makeFile "FILEONE" S3Tone.txt
    tcltest::makeFile "FILETWO" S3Ttwo.txt
    tcltest::makeFile "FILETHREE" S3Tthree.txt
    tcltest::makeFile "This is some random content" S3Talpha.txt
    tcltest::makeFile "This is some random content" S3Tbeta.txt
    tcltest::makeFile "This is some random content" S3Tgamma.txt
    tcltest::makeFile "Junk contents" S3junk.txt
    set now [clock seconds]
    file mtime S3Tone.txt [expr $now-300]
    file mtime S3Ttwo.txt [expr $now+300]
    file mtime S3Tbeta.txt [expr $now+300]
    S3::REST [dict create verb PUT resource /$bucket/ABC inbody "ABC HERE" \
	headers {x-amz-meta-thing stuff} content-type application/tcltest]
    if {[file exists S3junk.txt]} {file delete S3junk.txt}
}

set pgcu {
    tcltest::removeFile S3Tone.txt
    tcltest::removeFile S3Ttwo.txt
    tcltest::removeFile S3Tthree.txt
    tcltest::removeFile S3Talpha.txt
    tcltest::removeFile S3Tbeta.txt
    tcltest::removeFile S3Tgamma.txt
    if {[file exists S3junk.txt]} {file delete S3junk.txt}
    if {[existsbody /$bucket/XYZ]} {delbody /$bucket/XYZ}
    if {[existsbody /$bucket/PDQ]} {delbody /$bucket/PDQ}
    if {[existsbody /$bucket/ABC]} {delbody /$bucket/ABC}
}


test S3-50.10 {Put, basic content} -constraints "Put ItemIO" -body {
    set c "This is a test\n"
    set x [S3::Put -bucket $bucket -content $c -resource "XYZ"]
    set y [getbody /$bucket/XYZ]
    set z [expr {$y eq $c}]
    return "$x $z"
} -cleanup {
    delbody /$bucket/XYZ
} -result "1 1"

test S3-50.20 {Put, with a file} -constraints "Put ItemIO" -setup {
    set c "This is the second test.\nIt is still a test.\n"
    tcltest::makeFile $c "S3junk.txt" 
} -body {
    set x [S3::Put -bucket $bucket -file "S3junk.txt" -resource "XYZ"]
    set y [getbody /$bucket/XYZ]
    set z [expr {$y eq $c}]
    return "$x $z"
} -cleanup {
    delbody /$bucket/XYZ
    tcltest::removeFile "S3junk.txt"
} -result "1 1"

test S3-50.30 {Put with ACL, content-type, meta} \
	-constraints "Put ItemIO" -setup {
    set c "This is the third test.\nIt is still a test.\n"
    tcltest::makeFile $c "S3junk.txt" 
} -body {
    set x [S3::Put -bucket $bucket -file "S3junk.txt" -resource "XYZ" \
	-content-type "application/frobulate" -acl "public-read" \
        -x-amz-meta-one ONE -x-amz-meta-two TWO]
    set y {} ; set z {} 
    set req [dict create verb GET resource /$bucket/XYZ]
    set res [S3::REST $req]
    S3::throwhttp $res
    set headers [dict get $res outheaders]
    set y [dict get $headers content-type]
    set w1 [dict get $headers x-amz-meta-one]
    set w2 [dict get $headers x-amz-meta-two]

    set d1 [dict create verb GET resource /$bucket/XYZ rtype acl]
    set d2 [S3::REST $d1]
    set d3 [string first "AllUsers" $d2]
    set d4 [string first "READ" $d2]
    set z [expr 0 < $d3 && $d3 < $d4]
    return [list $x $y $z $w1 $w2]
} -cleanup {
    delbody /$bucket/XYZ
    tcltest::removeFile "S3junk.txt"
} -result "1 application/frobulate 1 ONE TWO"

test S3-50.40 {Put -compare never} -constraints "Put ItemIO" -body {
    set x [S3::Put -file S3junk.txt -bucket $bucket -resource "XYZ" \
	-compare never]
    set y [existsbody /$bucket/XYZ]
    return "$x $y"
} -cleanup {
    if {[existsbody /$bucket/XYZ]} {delbody /$bucket/XYZ}
} -result "0 0"

test S3-50.50 {Put -compare always} \
	-setup $pgsu -cleanup $pgcu -constraints "Put ItemIO" -body {
    set x [S3::Put -file S3Talpha.txt -bucket $bucket -resource "XYZ" \
	-compare always]
    set y [existsbody /$bucket/XYZ]
    set z [S3::Put -file S3Talpha.txt -bucket $bucket -resource "XYZ" \
	-compare always]
    return "$x $y $z"
} -result "1 1 1"

test S3-50.60 {Put -compare exists} \
	-setup $pgsu -cleanup $pgcu -constraints "Put ItemIO" -body {
    set x1 [S3::Put -file S3Tone.txt -bucket $bucket -resource "XYZ" \
	-compare exists]
    set x2 [existsbody /$bucket/XYZ]
    S3::Put -file S3Tone.txt -bucket $bucket -resource "XYZ" ; # really make it
    set y1 [S3::Put -file S3Ttwo.txt -bucket $bucket -resource "XYZ" \
	-compare exists]
    set y2 [existsbody /$bucket/XYZ]
    set y3 [string trim [getbody /$bucket/XYZ]]
    return [list $x1 $x2 $y1 $y2 $y3]
} -result "0 0 1 1 FILETWO"

test S3-50.70 {Put -compare missing} \
	-setup $pgsu -cleanup $pgcu -constraints "Put ItemIO" -body {
    set x1 [S3::Put -file S3Tone.txt -bucket $bucket -resource "XYZ" \
	-compare missing]
    set x2 [existsbody /$bucket/XYZ]
    set y1 [S3::Put -file S3Ttwo.txt -bucket $bucket -resource "XYZ" \
	-compare missing]
    set y2 [existsbody /$bucket/XYZ]
    set y3 [string trim [getbody /$bucket/XYZ]]
    return [list $x1 $x2 $y1 $y2 $y3]
} -result "1 1 0 1 FILEONE"

test S3-50.80 {Put -compare newer} \
	-setup $pgsu -cleanup $pgcu -constraints "Put ItemIO" -body {
    # Create the file with the current date
    S3::Put -file S3Tthree.txt -bucket $bucket -resource "XYZ"
    # Make sure ONE (old) doesn't overwrite it.
    set x1 [S3::Put -file S3Tone.txt -bucket $bucket -resource "XYZ" \
	-compare newer]
    set x2 [string trim [getbody /$bucket/XYZ]]
    set y1 [S3::Put -file S3Ttwo.txt -bucket $bucket -resource "XYZ" \
	-compare newer]
    set y2 [string trim [getbody /$bucket/XYZ]]
    return [list $x1 $x2 $y1 $y2]
} -result "0 FILETHREE 1 FILETWO"

test S3-50.90 {Put -compare date} \
	-setup $pgsu -cleanup $pgcu -constraints "Put ItemIO" -body {
    S3::Configure -slop-seconds 60
    S3::Put -file S3Tthree.txt -bucket $bucket -resource "XYZ"
    set x1 [S3::Put -file S3Tone.txt -bucket $bucket -resource "XYZ" \
	-compare date]
    set x2 [string trim [getbody /$bucket/XYZ]]
    set y1 [S3::Put -file S3Tthree.txt -bucket $bucket -resource "XYZ" \
	-compare date]
    set y2 [string trim [getbody /$bucket/XYZ]]
    set z1 [S3::Put -file S3Tthree.txt -bucket $bucket -resource "PDQ" \
	-compare date]
    set z2 [string trim [getbody /$bucket/PDQ]]
    return [list $x1 $x2 $y1 $y2 $z1 $z2]
} -result "1 FILEONE 0 FILEONE 1 FILETHREE"

test S3-50.100 {Put -compare checksum} \
	-setup $pgsu -cleanup $pgcu -constraints "Put ItemIO" -body {
    S3::Put -file S3Talpha.txt -bucket $bucket -resource "XYZ"
    set x1 [S3::Put -file S3Tbeta.txt -bucket $bucket -resource "XYZ" \
	-compare checksum]
    set x2 [S3::Put -file S3Tbeta.txt -bucket $bucket -resource "PDQ" \
	-compare checksum]
    set x3 [S3::Put -content "This is some random content\n" \
	-bucket $bucket -resource "XYZ" \
	-compare checksum]
    set funky  "One\u2211Sigma\u5927Da"
    S3::Put -content $funky -bucket $bucket -resource "XYZ"
    set x4 [S3::Put -content $funky -bucket $bucket -resource "XYZ" \
	-compare checksum]
    return [list $x1 $x2 $x3 $x4]
} -result "0 1 0 0"

test S3-50.110 {Put -compare different} \
	-setup $pgsu -cleanup $pgcu -constraints "Put ItemIO" -body {
    S3::Configure -slop-seconds 60
    S3::Put -file S3Talpha.txt -bucket $bucket -resource "XYZ"
    set x1 [S3::Put -file S3Tbeta.txt -bucket $bucket -resource "XYZ" \
	-compare different]
    set x2 [S3::Put -file S3Tgamma.txt -bucket $bucket -resource "XYZ" \
	-compare different]
    set x3 [S3::Put -file S3Tthree.txt -bucket $bucket -resource "XYZ" \
	-compare different]
    set x4 [string trim [getbody /$bucket/XYZ]]
    set x5 [S3::Put -content "FILETHREE\n" -bucket $bucket -resource "XYZ" \
	-compare different]
    return [list $x1 $x2 $x3 $x4 $x5]
} -result "1 0 1 FILETHREE 0"

test S3-50.120 {Put -compare error} -constraints "Put ItemIO" -body {
    S3expectErr [list S3::Put -content "STUFF" \
	-bucket $bucket -resource "XYZ" \
	-compare other]
} -result "1 S3 usage -compare" -match S3err

test S3-50.130 {Put -file nonexistant} -constraints "Put ItemIO" -body {
    S3expectErr [list S3::Put -file nonexistant.txt \
	-bucket $bucket -resource "XYZ"]
} -result "1 S3 usage -file" -match S3err


test S3-60.10 {Get, basic content} \
	-setup $pgsu -cleanup $pgcu -constraints "Get ItemIO" -body {
    set x [S3::Get -bucket $bucket -content abc -resource "ABC"]
    set y [getbody /$bucket/ABC]
    set z [expr {$y eq $abc}]
    return "$x $z"
} -result "1 1"

test S3-60.20 {Get, with a file} \
	-setup $pgsu -cleanup $pgcu -constraints "Get ItemIO" -body {
    set x [S3::Get -bucket $bucket -file "S3junk.txt" -resource "ABC"]
    set y [tcltest::viewFile S3junk.txt]
    set z [expr {$y eq "ABC HERE"}]
    return "$x $z"
} -result "1 1"

test S3-60.30 {Get with meta} \
	-setup $pgsu -cleanup $pgcu -constraints "Get ItemIO" -body {
    set x [S3::Get -bucket $bucket -file "S3junk.txt" -resource "ABC" \
	-headers thishead] 
    set y [dict get $thishead content-type]
    set z [dict get $thishead x-amz-meta-thing]
    return [list $x $y $z]
} -result "1 application/tcltest stuff"

test S3-60.40 {Get -compare never} \
	-setup $pgsu -cleanup $pgcu -constraints "Get ItemIO" -body {
    set x [S3::Get -file S3junk.txt -bucket $bucket -resource "ABC" \
	-compare never]
    set y [file exists S3junk.txt]
    return "$x $y"
} -result "0 0"

test S3-60.50 {Get -compare always} \
	-setup $pgsu -cleanup $pgcu -constraints "Get ItemIO" -body {
    set x [S3::Get -file S3junk.txt -bucket $bucket -resource "ABC" \
	-compare always]
    set y [file exists S3junk.txt]
    set z [S3::Get -file S3junk.txt -bucket $bucket -resource "ABC" \
	-compare always]
    set q [S3::Get -content plover -bucket $bucket -resource "ABC" \
	-compare always]
    return "$x $y $z $q $plover"
} -result "1 1 1 1 ABC HERE"

test S3-60.60 {Get -compare exists} \
	-setup $pgsu -cleanup $pgcu -constraints "Get ItemIO" -body {
set x0 [file exists S3junk.txt]
    set x1 [S3::Get -file S3junk.txt -bucket $bucket -resource "ABC" \
	-compare exists]
    set x2 [file exists S3junk.txt]
    set y1 [S3::Get -file S3Tone.txt -bucket $bucket -resource "ABC" \
	-compare exists]
    set y2 [file exists S3Tone.txt]
    set y3 [tcltest::viewFile S3Tone.txt]
    return [list $x0 $x1 $x2 $y1 $y2 $y3]
} -result "0 0 0 1 1 {ABC HERE}"

test S3-60.70 {Get -compare missing} \
	-setup $pgsu -cleanup $pgcu -constraints "Get ItemIO" -body {
    set x1 [S3::Get -file S3Tone.txt -bucket $bucket -resource "ABC" \
	-compare missing]
    set x2 [file exists S3Tone.txt]
    set y1 [S3::Get -file S3junk.txt -bucket $bucket -resource "ABC" \
	-compare missing]
    set y2 [file exists S3junk.txt]
    set y3 [tcltest::viewFile S3junk.txt]
    return [list $x1 $x2 $y1 $y2 $y3]
} -result "0 1 1 1 {ABC HERE}"

test S3-60.80 {Get -compare newer} \
	-setup $pgsu -cleanup $pgcu -constraints "Get ItemIO" -body {
    set x1 [S3::Get -file S3Tone.txt -bucket $bucket -resource "ABC" \
	-compare newer]
    set x2 [tcltest::viewFile S3Tone.txt]
    set y1 [S3::Get -file S3Ttwo.txt -bucket $bucket -resource "ABC" \
	-compare newer]
    set y2 [tcltest::viewFile S3Ttwo.txt]
    set z1 [S3::Get -file S3junk.txt -bucket $bucket -resource "ABC" \
	-compare newer]
    set z2 [tcltest::viewFile S3junk.txt]
    set w1 [S3::Get -content w2 -bucket $bucket -resource "ABC" \
	-compare newer]

    return [list $x1 $x2 $y1 $y2 $z1 $z2 $w1 $w2]
} -result "1 {ABC HERE} 0 FILETWO 1 {ABC HERE} 1 {ABC HERE}"

test S3-60.90 {Get -compare date} \
	-setup $pgsu -cleanup $pgcu -constraints "Get ItemIO" -body {
    S3::Configure -slop-seconds 60
    set x1 [S3::Get -file S3Tone.txt -bucket $bucket -resource "ABC" \
	-compare date]
    set x2 [tcltest::viewFile S3Tone.txt]
    set y1 [S3::Get -file S3Ttwo.txt -bucket $bucket -resource "ABC" \
	-compare date]
    set y2 [tcltest::viewFile S3Ttwo.txt]
    set z1 [S3::Get -file S3Tthree.txt -bucket $bucket -resource "ABC" \
	-compare date]
    set z2 [tcltest::viewFile S3Tthree.txt]
    set w1 [S3::Get -content w2 -bucket $bucket -resource "ABC" \
	-compare date]
    return [list $x1 $x2 $y1 $y2 $z1 $z2 $w1 $w2]
} -result "1 {ABC HERE} 1 {ABC HERE} 0 FILETHREE 1 {ABC HERE}"

test S3-60.100 {Get -compare checksum} \
	-setup $pgsu -cleanup $pgcu -constraints "Get ItemIO" -body {
    S3::Put -file S3Talpha.txt -bucket $bucket -resource "XYZ"
    set x1 [S3::Get -file S3Tbeta.txt -bucket $bucket -resource "XYZ" \
	-compare checksum]
    set x2 [S3::Get -file S3Tbeta.txt -bucket $bucket -resource "ABC" \
	-compare checksum]
    set x3 [tcltest::viewFile S3Tbeta.txt]
    set x4 [S3::Get -content x5 -bucket $bucket -resource "ABC" \
	-compare checksum]
    return [list $x1 $x2 $x3 $x4 $x5]
} -result "0 1 {ABC HERE} 1 {ABC HERE}"

test S3-60.110 {Get -compare different} \
	-setup $pgsu -cleanup $pgcu -constraints "Get ItemIO" -body {
    S3::Configure -slop-seconds 60
    S3::Put -file S3Talpha.txt -bucket $bucket -resource "XYZ"
    set x0 [S3::Get -file S3junk.txt -bucket $bucket -resource "XYZ" \
	-compare different] ; # Yes, file nonexistant
    set x1 [S3::Get -file S3Talpha.txt -bucket $bucket -resource "XYZ" \
	-compare different] ; # no, same date, same contents
    set x2 [S3::Get -file S3Tbeta.txt -bucket $bucket -resource "XYZ" \
	-compare different] ; # Yes, diff date, same contents.
    set x3 [S3::Get -file S3Tbeta.txt -bucket $bucket -resource "ABC" \
	-compare different] ; # Yes, diff contents, same date
    set x4 [S3::Get -content x5 -bucket $bucket -resource "ABC" \
	-compare different] ; # Yes, variable
    set x6 [tcltest::viewFile S3Tbeta.txt]
    return [list $x0 $x1 $x2 $x3 $x4 $x5 $x6]
} -result "1 0 1 1 1 {ABC HERE} {ABC HERE}"

test S3-60.120 {Get -compare error} -constraints "Get ItemIO" -body {
    S3expectErr [list S3::Get -file S3Tone.txt \
	-bucket $bucket -resource "XYZ" \
	-compare other]
} -result "1 S3 usage -compare" -match S3err

test S3-60.130 {Get resource nonexistant, file nonexistant A} \
	-setup $pgsu -cleanup $pgcu -constraints "Get ItemIO" -body {
    S3expectErr [list S3::Get -file nonexistant.txt \
	-bucket $bucket -resource "XYZ"]
} -result "1 S3 remote 404" -match S3err

test S3-60.131 {Get resource nonexistant, file nonexistant B} \
	-setup $pgsu -cleanup $pgcu -constraints "Get ItemIO" -body {
    catch {S3::Get -file nonexistant.txt -bucket $bucket -resource "XYZ"}
    file exists nonexistant.txt
} -result "0" 

test S3-60.132 {Get resource nonexistant, file existant B} \
	-setup $pgsu -cleanup $pgcu -constraints "Get ItemIO" -body {
    S3expectErr [list S3::Get -file S3Talpha.txt \
	-bucket $bucket -resource "XYZ"]
} -result "1 S3 remote 404" -match S3err

test S3-60.133 {Get resource nonexistant, file existant A} \
	-setup $pgsu -cleanup $pgcu -constraints "Get ItemIO" -body {
    catch {S3::Get -file S3Talpha.txt -bucket $bucket -resource "XYZ"}
    file exists S3Talpha.txt
} -result "1" 

test S3-60.140 {Get with -timestamp options} \
	-constraints "Get ItemIO" -body {
    # This test assumes your clock and amazon's clock are within 10 seconds
    tcltest::makeFile "RandomJunk" ts1.txt
    tcltest::makeFile "RandomJunk" ts2.txt
    after 10000
    S3::Put -content "More random junk" -bucket $bucket -resource "TIMESTAMP"
    after 5000
    set tick [clock seconds]
    after 5000
    S3::Get -file ts1.txt -timestamp aws -bucket $bucket -resource "TIMESTAMP"
    S3::Get -file ts2.txt -timestamp now -bucket $bucket -resource "TIMESTAMP"
    set x1 [file mtime ts1.txt]
    set x2 [file mtime ts2.txt]
    return [list [expr $x1 < $tick] [expr $x2 < $tick]]
} -cleanup {
    tcltest::removeFile ts1.txt
    tcltest::removeFile ts2.txt
    if {[existsbody /$bucket/TIMESTAMP]} {delbody /$bucket/TIMESTAMP}
} -result "1 0"

test S3-70.10 {Head, resource exists} \
	-setup $pgsu -cleanup $pgcu -constraints "Head ItemIO" -body {
    set x1 [S3::Head -bucket $bucket -resource "ABC" -dict dict \
	-headers headers -status status]
    return [list $x1 [dict get $dict httpmessage] [dict exists $headers last-modified] $status] 
} -result "1 OK 1 {200 OK}"

test S3-70.20 {Head, resource does not exist} \
	-setup $pgsu -cleanup $pgcu -constraints "Head ItemIO" -body {
    set x1 [S3::Head -bucket $bucket -resource "XYZ" -dict dict \
	-headers headers -status status]
    return [list $x1 $status]
} -result "0 {404 {Not Found}}"

test S3-80.10 {Delete, resource exists} \
	-setup $pgsu -cleanup $pgcu -constraints "Delete ItemIO" -body {
    set x1 [S3::Delete -bucket $bucket -resource "ABC" -status status]
    return [list $x1 $status]
} -result "1 {204 {No Content}}"

test S3-80.20 {Delete, resource nonexistant} \
	-setup $pgsu -cleanup $pgcu -constraints "Delete ItemIO" -body {
    set x1 [S3::Delete -bucket $bucket -resource "XYZ" -status status]
    return [list $x1 $status]
} -result "1 {204 {No Content}}"

test S3-80.30 {Delete, resource not mine} \
	-setup $pgsu -cleanup $pgcu -constraints "Delete ItemIO" -body {
    # Note that ami.prizecapital.net is also mine, but owned by a client.
    set x1 [S3::Delete -bucket "ami.prizecapital.net" \
	-resource "README.txt" -status status]
    return [list $x1 $status]
} -result "0 {403 Forbidden}"

test S3-90.10 {GetAcl REST} \
	-setup $pgsu -cleanup $pgcu -constraints "Acl" -body {
#set x1 [S3::GetAcl -bucket $bucket -resource "ABC" -result-type xml]
#puts "\n\n$x1\n\n"
    set x2 [S3::GetAcl -bucket $bucket -resource "ABC" -result-type REST]
    return [list [dict get $x2 httpstatus] [string index [dict get $x2 outbody] 0]]
} -result "200 <"

#test S3-90.11 {GetAcl XML} \
	#-setup $pgsu -constraints "Zap" -body {
    #set x2 [S3::GetAcl -bucket $bucket -resource "ABC" -result-type xml]
    #set x3 [open xyzzy.xml w] 
    #fconfigure $x3 -translation binary -encoding binary 
    #puts $x3 $x2
    #close $x3
    #exit
    #set x2 [S3::PutAcl -bucket $bucket -resource "ABC" -acl \
	#[string trim [read [open xyzzy.xml]]]]
    #puts $x2 ; exit
#} -result 1

test S3-90.20 {GetAcl pxml} \
	-setup $pgsu -cleanup $pgcu -constraints "Acl" -body {
    set x2 [S3::GetAcl -bucket $bucket -resource "ABC" -result-type pxml]
    return [list [lindex $x2 0] [lindex $x2 2 0]]
} -result "AccessControlPolicy Owner"

test S3-90.30 {GetAcl dict} \
	-setup $pgsu -cleanup $pgcu -constraints "Acl" -body {
    set x2 [S3::GetAcl -bucket $bucket -resource "ABC" -result-type dict]
    set owner [dict get $x2 owner]
    set acl [dict get $x2 acl]
    set z1 [dict get $acl FULL_CONTROL]
    set z2 [expr {$owner == $z1}]
    return $z2
} -result "1"

test S3-90.40 {GetAcl -parse-xml} \
	-constraints "Acl" -body {
    set xml {<?xml version="1.0" encoding="UTF-8"?>
<AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Owner><ID>9fb13c24488e3d7556693247d5a463c1837c3c8ede28f4094228e6c4eb5d70bd</ID><DisplayName>dnew@san.rr.com</DisplayName></Owner><AccessControlList><Grant><Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group"><URI>http://acs.amazonaws.com/groups/global/AllUsers</URI></Grantee><Permission>FULL_CONTROL</Permission></Grant><Grant><Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser"><ID>9fb13c24488e3d7556693247d5a463c1837c3c8ede28f4094228e6c4eb5d70bd</ID><DisplayName>dnew@san.rr.com</DisplayName></Grantee><Permission>FULL_CONTROL</Permission></Grant><Grant><Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser"><ID>a5ee16f393707820a7f2d58631351fe839972d25865f8fc423a754d77523e6d4</ID><DisplayName>darren</DisplayName></Grantee><Permission>READ</Permission></Grant><Grant><Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser"><ID>a1bf9e3c79a243e04e31bf3d1f532aca94646ab917c188831241bf5d575fee92</ID><DisplayName>Darren</DisplayName></Grantee><Permission>WRITE</Permission></Grant></AccessControlList></AccessControlPolicy>}
    set x2 [S3::GetAcl -parse-xml $xml -result-type dict]
    return $x2
} -result "owner 9fb13c24488e3d7556693247d5a463c1837c3c8ede28f4094228e6c4eb5d70bd acl {READ a5ee16f393707820a7f2d58631351fe839972d25865f8fc423a754d77523e6d4 WRITE a1bf9e3c79a243e04e31bf3d1f532aca94646ab917c188831241bf5d575fee92 FULL_CONTROL {http://acs.amazonaws.com/groups/global/AllUsers 9fb13c24488e3d7556693247d5a463c1837c3c8ede28f4094228e6c4eb5d70bd}}"

test S3-90.50 {PutAcl private} \
	-setup $pgsu -cleanup $pgcu -constraints "Acl" -body {
    set x1 [S3::PutAcl -bucket $bucket -resource "ABC" -acl private]
    set x2 [S3::GetAcl -bucket $bucket -resource "ABC" -result-type dict]
    set x3 [dict get $x2 acl]
    set x4 [llength $x3]
    return [list [string range $x1 0 19] $x4 [lindex $x3 0]]
} -result "<AccessControlPolicy 2 FULL_CONTROL"

test S3-90.60 {PutAcl nonexistant get} \
	-setup $pgsu -cleanup $pgcu -constraints "Acl" -body {
    S3expectErr [list S3::PutAcl -bucket $bucket -resource XYZ -acl private]
} -result "1 S3 remote 404" -match S3err

test S3-90.70 {PutAcl nonexistant put} \
	-setup $pgsu -cleanup $pgcu -constraints "Acl" -body {
    set x2 [S3::GetAcl -bucket $bucket -resource "ABC" -result-type dict]
    set owner [dict get $x2 owner]
    S3expectErr [list S3::PutAcl -owner $owner \
	-bucket $bucket -resource XYZ -acl private]
} -result "1 S3 remote 404" -match S3err

test S3-90.80 {PutAcl from xml} \
	-setup $pgsu -cleanup $pgcu -constraints "Acl" -body {
    set x0 [S3::GetAcl -bucket $bucket -resource "ABC" -result-type xml]
    set x1 [S3::PutAcl -bucket $bucket -resource "ABC" -acl $x0]
    set x2 [S3::GetAcl -bucket $bucket -resource "ABC" -result-type dict]
    set x3 [dict get $x2 acl]
    set x4 [llength $x3]
    if {"<?xml" == [string range $x1 0 4]} {
	set x1 [string range $x1 [expr 1+[string first "\n" $x1]] end]
    }
    return [list [string range $x1 0 19] $x4 [lindex $x3 0]]
} -result "<AccessControlPolicy 2 FULL_CONTROL"

test S3-90.90 {PutAcl public} \
	-setup $pgsu -cleanup $pgcu -constraints "Acl" -body {
    S3expectErr [list S3::PutAcl -bucket $bucket -resource "ABC" -acl public]
} -result "1 S3 usage -acl public" -match S3err

test S3-90.100 {PutAcl public-read} \
	-setup $pgsu -cleanup $pgcu -constraints "Acl" -body {
    set x1 [S3::PutAcl -bucket $bucket -resource "ABC" -acl public-read]
    set x2 [S3::GetAcl -bucket $bucket -resource "ABC" -result-type dict]
    set x3 [dict get $x2 acl]
    set x4 [llength $x3]
    set x5 [lsort [dict keys $x3]]
    return [list [string range $x1 0 19] $x4 $x5]
} -result "<AccessControlPolicy 4 {FULL_CONTROL READ}"

test S3-90.110 {PutAcl public-read-write} \
	-setup $pgsu -cleanup $pgcu -constraints "Acl" -body {
    set x1 [S3::PutAcl -bucket $bucket -resource "ABC" -acl public-read-write]
    set x2 [S3::GetAcl -bucket $bucket -resource "ABC" -result-type dict]
    set x3 [dict get $x2 acl]
    set x4 [llength $x3]
    set x5 [lsort [dict keys $x3]]
    return [list [string range $x1 0 19] $x4 $x5]
} -result "<AccessControlPolicy 6 {FULL_CONTROL READ WRITE}"

test S3-90.120 {PutAcl authenticated-read} \
	-setup $pgsu -cleanup $pgcu -constraints "Acl" -body {
    set x1 [S3::PutAcl -bucket $bucket -resource "ABC" -acl authenticated-read]
    set x2 [S3::GetAcl -bucket $bucket -resource "ABC" -result-type dict]
    set x3 [dict get $x2 acl]
    set x4 [llength $x3]
    set x5 [lsort [dict keys $x3]]
    return [list [string range $x1 0 19] $x4 $x5]
} -result "<AccessControlPolicy 4 {FULL_CONTROL READ}"

test S3-90.130 {PutAcl complex} \
	-setup $pgsu -cleanup $pgcu -constraints "Acl" -body {
    set dict [dict create \
	FULL_CONTROL {9fb13c24488e3d7556693247d5a463c1837c3c8ede28f4094228e6c4eb5d70bd AuthenticatedUsers} \
	WRITE darren@prizecapital.net \
	READ http://acs.amazonaws.com/groups/global/AllUsers ]
    set x1 [S3::PutAcl -bucket $bucket -resource "ABC" -acl $dict]
    set x2 [S3::GetAcl -bucket $bucket -resource "ABC" -result-type dict]
    set x3 [dict get $x2 acl]
    set x4 [llength $x3]
    return [list [string range $x1 0 19] [lsort [dict keys $x3]]]
} -result "<AccessControlPolicy {FULL_CONTROL READ WRITE}"

test S3-90.140 {Put with keep on existing object} \
	-setup $pgsu -cleanup $pgcu -constraints "Acl" -body {
    set dict [dict create \
	FULL_CONTROL {9fb13c24488e3d7556693247d5a463c1837c3c8ede28f4094228e6c4eb5d70bd AuthenticatedUsers} \
	WRITE darren@prizecapital.net \
	READ http://acs.amazonaws.com/groups/global/AllUsers ]
    set x1 [S3::PutAcl -bucket $bucket -resource "ABC" -acl $dict]
    S3::Put -bucket $bucket -resource "ABC" -file "S3Tone.txt" -acl keep
    set x2 [S3::GetAcl -bucket $bucket -resource "ABC" -result-type dict]
    set x3 [dict get $x2 acl]
    set x4 [llength $x3]
    return [list [string range $x1 0 19] [lsort [dict keys $x3]]]
} -result "<AccessControlPolicy {FULL_CONTROL READ WRITE}"

test S3-90.150 {Put with keep on new object} \
	-setup $pgsu -cleanup $pgcu -constraints "Acl" -body {
    S3::Put -bucket $bucket -resource "XYZ" -file "S3Tone.txt" -acl keep
    set x2 [S3::GetAcl -bucket $bucket -resource "ABC" -result-type dict]
    set x3 [dict get $x2 acl]
    set x4 [llength $x3]
    return [list [string range $x1 0 19] [lsort [dict keys $x3]]]
} -result "<AccessControlPolicy FULL_CONTROL"


test S3-100.10 {Pull} \
	-setup S3loadKeys -constraints "Directory ItemIO" -body {
    # I actually tested this manually much more extensively,
    # but some of the tests are difficult, due to needing to
    # set up a bunch of directories with different permissions, etc.
    set bucket [S3::SuggestBucket TclTestS3b]
    set dir S3Tdir
    catch {file delete -force -- $dir}
    file mkdir $dir
    set res [S3::Pull -bucket $bucket -prefix thing \
	-directory $dir -compare always -delete true]
    set r1 [dict get $res {} filescopied]
    set r2 [dict get $res {} errorskipped]
    set r3 [dict get $res {} filesdeleted]
    set r4 [file exists [file join $dir 00/6]]
    return [list $r1 $r2 $r3 $r4]
} -cleanup {
    file delete -force -- $dir
} -result {250 0 0 1}

test S3-100.20 {Push} \
	-setup S3loadKeys -constraints "Directory ItemIO" -body {
    set bucket [S3::SuggestBucket TclTestS3b]
    set dir S3Tdir
    catch {file delete -force -- $dir}
    file mkdir $dir
    set res [S3::Pull -bucket $bucket -prefix thing \
	-directory $dir -compare always -delete true]
    set r1 [dict get $res {} filescopied]
    set r2 [dict get $res {} errorskipped]
    set r3 [dict get $res {} filesdeleted]
    set r4 [file exists [file join $dir 00/6]]
    # Now the rest of the test... :-)
    set bucket [S3::SuggestBucket TclTestS3] ; # different bucket
    set res [S3::Push -bucket $bucket -prefix hither/yon \
	-directory $dir -compare always -delete true]
    set r5 [dict get $res {} filescopied]
    set r6 [dict get $res {} errorskipped]
    set r7 [dict get $res {} filesdeleted]
    set res [S3::REST [dict create verb HEAD resource /$bucket/hither/yon/00/6]]
    set r8 [dict get $res httpstatus]
    return [list $r1 $r2 $r3 $r4 $r5 $r6 $r7 $r8]
} -cleanup {
    file delete -force -- $dir
    set bucket [S3::SuggestBucket TclTestS3]
    set names [S3::GetBucket -bucket $bucket -prefix hither/yon \
	-result-type names]
    foreach name $names {
	S3::Delete -bucket $bucket -resource $name
    }
} -result {250 0 0 1 250 0 0 200}

test S3-100.30 {Push with deletes and stuff} \
	-setup S3loadKeys -constraints "Directory ItemIO" -body {
    set bucket [S3::SuggestBucket TclTestS3b]
    set dir S3Tdir
    catch {file delete -force -- $dir}
    file mkdir $dir
    set res [S3::Pull -bucket $bucket -prefix thing \
	-directory $dir -compare always -delete true]
    set r1 [dict get $res {} filescopied]
    set r2 [dict get $res {} errorskipped]
    set r3 [dict get $res {} filesdeleted]
    set r4 [file exists [file join $dir 00/6]]
    set bucket [S3::SuggestBucket TclTestS3] ; # different bucket
    set res [S3::Push -bucket $bucket -prefix hither/yon \
	-directory $dir -compare always -delete true]
    set r5 [dict get $res {} filescopied]
    set r6 [dict get $res {} errorskipped]
    set r7 [dict get $res {} filesdeleted]
    set res [S3::REST [dict create verb HEAD resource /$bucket/hither/yon/00/6]]
    set r8 [dict get $res httpstatus]
    # Now the rest of the test... :-)
    file delete -force [file join $dir 03]
    tcltest::makeFile "xxx" [file join $dir "j1.txt"]
    tcltest::makeFile "xxx" [file join $dir "j2.txt"]
    # Sadly, makefile insists on adding newlines
    set x [open [file join $dir j1.txt] w];puts -nonewline $x "123456";close $x
    set x [open [file join $dir j2.txt] w];puts -nonewline $x "678901";close $x
    set res [S3::Push -bucket $bucket -prefix hither/yon \
	-directory $dir -compare missing -delete true]
    set r9 [dict get $res {} filescopied]
    set r10 [dict get $res {} errorskipped]
    set r11 [dict get $res {} filesdeleted]
    set r12 [dict get $res {} bytescopied]
    set res [S3::REST [dict create verb HEAD resource /$bucket/hither/yon/08/7]]
    set r13 [dict get $res httpstatus]
    set res [S3::REST [dict create verb HEAD resource /$bucket/hither/yon/j1.txt]]
    set r14 [dict get $res httpstatus]
    return [list $r1 $r2 $r3 $r4 $r5 $r6 $r7 $r8 $r9 $r10 $r11 $r12 $r13 $r14]
} -cleanup {
    file delete -force -- $dir
    set bucket [S3::SuggestBucket TclTestS3]
    set names [S3::GetBucket -bucket $bucket -prefix hither/yon \
	-result-type names]
    foreach name $names {
	S3::Delete -bucket $bucket -resource $name
    }
} -result {250 0 0 1 250 0 0 200 2 0 10 12 200 200}

test S3-100.40 {Pull with deletes and stuff} \
	-setup S3loadKeys -constraints "Directory ItemIO" -body {
    set bucket [S3::SuggestBucket TclTestS3b]
    set dir S3Tdir
    catch {file delete -force -- $dir}
    file mkdir $dir
    set res [S3::Pull -bucket $bucket -prefix thing \
	-directory $dir -compare always -delete true]
    set r1 [dict get $res {} filescopied]
    set r2 [dict get $res {} errorskipped]
    set r3 [dict get $res {} filesdeleted]
    set r4 [file exists [file join $dir 00/6]]
    set bucket [S3::SuggestBucket TclTestS3] ; # different bucket
    set res [S3::Push -bucket $bucket -prefix hither/yon \
	-directory $dir -compare always -delete true]
    set r5 [dict get $res {} filescopied]
    set r6 [dict get $res {} errorskipped]
    set r7 [dict get $res {} filesdeleted]
    set res [S3::REST [dict create verb HEAD resource /$bucket/hither/yon/00/6]]
    set r8 [dict get $res httpstatus]
    file delete -force [file join $dir 03]
    tcltest::makeFile "xxx" [file join $dir "j1.txt"]
    tcltest::makeFile "xxx" [file join $dir "j2.txt"]
    # Sadly, makefile insists on adding newlines
    set x [open [file join $dir j1.txt] w];puts -nonewline $x "123456";close $x
    set x [open [file join $dir j2.txt] w];puts -nonewline $x "678901";close $x
    set res [S3::Push -bucket $bucket -prefix hither/yon \
	-directory $dir -compare missing -delete true]
    set r9 [dict get $res {} filescopied]
    set r10 [dict get $res {} errorskipped]
    set r11 [dict get $res {} filesdeleted]
    set r12 [dict get $res {} bytescopied]
    set res [S3::REST [dict create verb HEAD resource /$bucket/hither/yon/08/7]]
    set r13 [dict get $res httpstatus]
    set res [S3::REST [dict create verb HEAD resource /$bucket/hither/yon/j1.txt]]
    set r14 [dict get $res httpstatus]
    # Now the rest of the test... :-)
    file mkdir [file join $dir ToDelete]
    set x [open [file join $dir ToDelete T1.txt] w];puts $x "Hello";close $x
    set x [open [file join $dir ToDelete T2.txt] w];puts $x "World";close $x
    set bucket [S3::SuggestBucket TclTestS3b]
    set res [S3::Pull -bucket $bucket -prefix thing \
	-directory $dir -compare missing -delete true]
    set r15 [dict get $res {} filescopied] ; # The 03 directory
    set r16 [dict get $res {} compareskipped] ; # The rest.
    set r17 [dict get $res {} filesdeleted] ; # j1, j2, T1, T2, ToDelete
    return [list $r1 $r2 $r3 $r4 $r5 $r6 $r7 $r8 $r9 $r10 $r11 $r12 $r13 $r14 $r15 $r16 $r17]
} -cleanup {
    file delete -force -- $dir
    set bucket [S3::SuggestBucket TclTestS3]
    set names [S3::GetBucket -bucket $bucket -prefix hither/yon \
	-result-type names]
    foreach name $names {
	S3::Delete -bucket $bucket -resource $name
    }
} -result {250 0 0 1 250 0 0 200 2 0 10 12 200 200 10 240 5}

test S3-100.50 {Push and Pull with -compare never -delete true} \
	-setup S3loadKeys -constraints "Directory ItemIO" -body {
    # This test creates 00 thru 09 in a bucket and a local dir.
    # It then deletes 07 from the bucket and 03 locally.
    # It then pushes and pulls with -compare never -delete true.
    # It expects 0 files copied and 10/11 deleted.
    # It then checks the deletes happened.
    set bucket [S3::SuggestBucket TclTestS3b]
    set dir S3Tdir
    catch {file delete -force -- $dir}
    file mkdir $dir
    set res [S3::Pull -bucket $bucket -prefix thing \
	-directory $dir -compare always -delete true]
    set bucket [S3::SuggestBucket TclTestS3] ; # different bucket
    set res [S3::Push -bucket $bucket -prefix hither/yon \
	-directory $dir -compare always -delete true]
    for {set i 0} {$i <= 9} {incr i} {
	S3::Delete -bucket $bucket -resource hither/yon/07/$i
    }
    file delete -force [file join $dir 03]
    set res [S3::Push -bucket $bucket -prefix hither/yon \
	-directory $dir -compare never -delete true]
    set r1 [dict get $res {} filescopied]
    set r2 [dict get $res {} errorskipped]
    set r3 [dict get $res {} filesdeleted]
    set res [S3::REST [dict create verb HEAD resource /$bucket/hither/yon/03/7]]
    set r4 [dict get $res httpstatus]
    set res [S3::Pull -bucket $bucket -prefix hither/yon \
	-directory $dir -compare never -delete true]
    set r5 [dict get $res {} filescopied]
    set r6 [dict get $res {} errorskipped]
    set r7 [dict get $res {} filesdeleted]
    set r8 [file exists [file join $dir 07 4]]
    set r9 [file exists [file join $dir 07]]
    return [list $r1 $r2 $r3 $r4 $r5 $r6 $r7 $r8 $r9]
} -cleanup {
    file delete -force -- $dir
    set bucket [S3::SuggestBucket TclTestS3]
    set names [S3::GetBucket -bucket $bucket -prefix hither/yon \
	-result-type names]
    foreach name $names {
	S3::Delete -bucket $bucket -resource $name
    }
} -result {0 0 10 404 0 0 11 0 0}

test S3-100.60 {Toss} \
	-setup S3loadKeys -constraints "Directory ItemIO" -body {
    set bucket [S3::SuggestBucket TclTestS3b]
    set dir S3Tdir
    catch {file delete -force -- $dir}
    file mkdir $dir
    set res [S3::Pull -bucket $bucket -prefix thing \
	-directory $dir -compare missing -delete true]
    set bucket [S3::SuggestBucket TclTestS3] ; # different bucket
    set res [S3::Push -bucket $bucket -prefix hither/yon \
	-directory $dir -compare missing -delete true]
    set res [S3::Toss -bucket $bucket -prefix /hither]
    set r1 [dict get $res {} filesdeleted]
    set r2 [dict get $res {} filesnotdeleted]
    return [list $r1 $r2]
} -cleanup {
    file delete -force -- $dir
    set bucket [S3::SuggestBucket TclTestS3]
    set names [S3::GetBucket -bucket $bucket -prefix hither/yon \
	-result-type names]
    foreach name $names {
	S3::Delete -bucket $bucket -resource $name
    }
} -result {250 0}

# set res [S3::REST {resource /darren/xyzzyplover verb HEAD}]
# puts $res\n\n\n ; after 3000
# set res [S3::REST [list resource /$bucket/fred verb HEAD]]
# puts $res\n\n\n ; after 3000
# set res [dict get $res outheaders]
# set remote_length [dict get $res content-length]
# set remote_etag [string trim [dict get $res etag] \"]
# set remote_date [clock scan [dict get $res last-modified]]
# puts "remote_length=$remote_length"
# puts "remote_etag=$remote_etag"
# puts "remote_date=$remote_date"
# puts "\n\n"
# set body "ABC\u2211S\u5927D"
# set res [S3::REST [list resource /darren/plover verb PUT inbody $body]]
# set res [S3::REST [list resource /darren/plover verb HEAD]]
# puts $res\n\n\n ; after 3000

CleanUpBuckets [tcltest::testConstraint BucketDeletion]

#----------------------------------------------------------------------

testsuiteCleanup
puts "(If anything failed, check all test buckets got cleaned up!)"
puts "Done!" ; after 5000