forked from openstacknetsdk/openstack.net
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathUserObjectStorageTests.cs
More file actions
2419 lines (2042 loc) · 108 KB
/
UserObjectStorageTests.cs
File metadata and controls
2419 lines (2042 loc) · 108 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
namespace Net.OpenStack.Testing.Integration.Providers.Rackspace
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using net.openstack.Core;
using net.openstack.Core.Domain;
using net.openstack.Core.Exceptions;
using net.openstack.Core.Exceptions.Response;
using net.openstack.Core.Providers;
using net.openstack.Providers.Rackspace;
using net.openstack.Providers.Rackspace.Objects;
using net.openstack.Providers.Rackspace.Objects.Response;
using Newtonsoft.Json;
using Container = net.openstack.Core.Domain.Container;
using File = System.IO.File;
using FileInfo = System.IO.FileInfo;
using HttpMethod = JSIStudios.SimpleRESTServices.Client.HttpMethod;
using HttpWebRequest = System.Net.HttpWebRequest;
using MD5 = System.Security.Cryptography.MD5;
using MemoryStream = System.IO.MemoryStream;
using Path = System.IO.Path;
using Stream = System.IO.Stream;
using StreamReader = System.IO.StreamReader;
using WebRequest = System.Net.WebRequest;
using WebResponse = System.Net.WebResponse;
/// <summary>
/// This class contains integration tests for the Rackspace Object Storage Provider
/// (Cloud Files) that can be run with user (non-admin) credentials.
/// </summary>
/// <seealso cref="CloudFilesProvider"/>
[TestClass]
public class UserObjectStorageTests
{
/// <summary>
/// This prefix is used for metadata keys created by unit tests, to avoid
/// overwriting metadata created by other applications.
/// </summary>
private const string TestKeyPrefix = "UnitTestMetadataKey-";
/// <summary>
/// This prefix is used for containers created by unit tests, to avoid
/// overwriting containers created by other applications.
/// </summary>
private const string TestContainerPrefix = "UnitTestContainer-";
/// <summary>
/// The minimum character allowed in metadata keys. This is drawn from
/// the HTTP/1.1 specification, which does not allow ASCII control
/// characters in header keys.
/// </summary>
private const char MinHeaderKeyCharacter = (char)32;
/// <summary>
/// The maximum character allowed in metadata keys. This is drawn from
/// the HTTP/1.1 specification, which restricts header keys to the 7-bit
/// ASCII character set.
/// </summary>
private const char MaxHeaderKeyCharacter = (char)127;
/// <summary>
/// The HTTP/1.1 separator characters.
/// </summary>
private const string SeparatorCharacters = "()<>@,;:\\\"/[]?={} \t\x7F";
/// <summary>
/// Characters which are technically allowed by HTTP/1.1, but cannot be used in
/// metadata keys for <see cref="CloudFilesProvider"/>.
/// </summary>
/// <remarks>
/// The underscore is disallowed by the Cloud Files implementation, which silently
/// converts it to a dash. The apostrophe is disallowed by <see cref="WebHeaderCollection"/>
/// which is used by the implementation.
/// </remarks>
private const string NotSupportedCharacters = "_'";
#region Container
/// <summary>
/// This test can be used to clear all of the metadata associated with every container in the storage provider.
/// </summary>
/// <remarks>
/// This test is normally disabled. To run the cleanup method, comment out or remove the
/// <see cref="IgnoreAttribute"/>.
/// </remarks>
[TestMethod]
[TestCategory(TestCategories.Cleanup)]
[Ignore]
public void CleanupAllContainerMetadata()
{
IObjectStorageProvider provider = Bootstrapper.CreateObjectStorageProvider();
IEnumerable<Container> containers = ListAllContainers(provider);
foreach (Container container in containers)
{
Dictionary<string, string> metadata = provider.GetContainerMetaData(container.Name);
provider.DeleteContainerMetadata(container.Name, metadata.Keys);
}
}
/// <summary>
/// This unit test clears the metadata associated with every container which is
/// created by the unit tests in this class.
/// </summary>
[TestMethod]
[TestCategory(TestCategories.Cleanup)]
public void CleanupTestContainerMetadata()
{
IObjectStorageProvider provider = Bootstrapper.CreateObjectStorageProvider();
IEnumerable<Container> containers = ListAllContainers(provider);
foreach (Container container in containers)
{
Dictionary<string, string> metadata = GetContainerMetadataWithPrefix(provider, container, TestKeyPrefix);
provider.DeleteContainerMetadata(container.Name, metadata.Keys);
}
}
/// <summary>
/// This unit test deletes all containers created by the unit tests, including all
/// objects within those containers.
/// </summary>
[TestMethod]
[TestCategory(TestCategories.Cleanup)]
public void CleanupTestContainers()
{
IObjectStorageProvider provider = Bootstrapper.CreateObjectStorageProvider();
IEnumerable<Container> containers = ListAllContainers(provider);
foreach (Container container in containers)
{
if (container.Name.StartsWith(TestContainerPrefix))
{
try
{
provider.DeleteContainer(container.Name, deleteObjects: true);
}
catch (ContainerNotEmptyException)
{
// this works around a bug in bulk delete, where files with trailing whitespace
// in the name do not get deleted
foreach (ContainerObject containerObject in ListAllObjects(provider, container.Name))
provider.DeleteObject(container.Name, containerObject.Name);
provider.DeleteContainer(container.Name, deleteObjects: false);
}
}
else if (container.Name.Equals(".CDN_ACCESS_LOGS"))
{
foreach (ContainerObject containerObject in ListAllObjects(provider, container.Name))
{
if (containerObject.Name.StartsWith(TestContainerPrefix))
provider.DeleteObject(container.Name, containerObject.Name);
}
}
}
}
[TestMethod]
[TestCategory(TestCategories.User)]
[TestCategory(TestCategories.ObjectStorage)]
public void TestListContainers()
{
IObjectStorageProvider provider = Bootstrapper.CreateObjectStorageProvider();
IEnumerable<Container> containers = ListAllContainers(provider);
if (!containers.Any())
Assert.Inconclusive("The account does not have any containers in the region.");
Console.WriteLine("Containers");
foreach (Container container in containers)
{
Console.WriteLine(" {0}", container.Name);
Console.WriteLine(" Objects: {0}", container.Count);
Console.WriteLine(" Bytes: {0}", container.Bytes);
}
}
[TestMethod]
[TestCategory(TestCategories.User)]
[TestCategory(TestCategories.ObjectStorage)]
public void TestContainerProperties()
{
IObjectStorageProvider provider = Bootstrapper.CreateObjectStorageProvider();
IEnumerable<Container> containers = ListAllContainers(provider);
if (!containers.Any())
Assert.Inconclusive("The account does not have any containers in the region.");
int containersTested = 0;
long objectsTested = 0;
long totalSizeTested = 0;
int nonEmptyContainersTested = 0;
int nonEmptyBytesContainersTested = 0;
foreach (Container container in containers)
{
Assert.IsTrue(container.Count >= 0);
Assert.IsTrue(container.Bytes >= 0);
containersTested++;
if (container.Count > 0)
nonEmptyContainersTested++;
if (container.Bytes > 0)
nonEmptyBytesContainersTested++;
long objectCount = 0;
long objectSize = 0;
foreach (var obj in ListAllObjects(provider, container.Name))
{
objectCount++;
objectSize += obj.Bytes;
}
objectsTested += objectCount;
totalSizeTested += objectSize;
Assert.AreEqual(container.Count, objectCount);
Assert.AreEqual(container.Bytes, objectSize);
if (containersTested >= 5 && nonEmptyContainersTested >= 5 && nonEmptyBytesContainersTested >= 5)
break;
}
if (containersTested == 0 || nonEmptyContainersTested == 0 || nonEmptyBytesContainersTested == 0)
Assert.Inconclusive("The account does not have any non-empty containers in the region.");
Console.WriteLine("Verified container properties for:");
Console.WriteLine(" {0} containers", containersTested);
Console.WriteLine(" {0} objects", objectsTested);
Console.WriteLine(" {0} bytes", totalSizeTested);
}
[TestMethod]
[TestCategory(TestCategories.User)]
[TestCategory(TestCategories.ObjectStorage)]
public void TestCreateContainer()
{
IObjectStorageProvider provider = Bootstrapper.CreateObjectStorageProvider();
string containerName = TestContainerPrefix + Path.GetRandomFileName();
ObjectStore result = provider.CreateContainer(containerName);
Assert.AreEqual(ObjectStore.ContainerCreated, result);
result = provider.CreateContainer(containerName);
Assert.AreEqual(ObjectStore.ContainerExists, result);
provider.DeleteContainer(containerName, deleteObjects: true);
}
[TestMethod]
[TestCategory(TestCategories.User)]
[TestCategory(TestCategories.ObjectStorage)]
public void TestVersionedContainer()
{
IObjectStorageProvider provider = Bootstrapper.CreateObjectStorageProvider();
string containerName = TestContainerPrefix + Path.GetRandomFileName();
string versionsContainerName = TestContainerPrefix + Path.GetRandomFileName();
ObjectStore result = provider.CreateContainer(versionsContainerName);
Assert.AreEqual(ObjectStore.ContainerCreated, result);
result = provider.CreateContainer(containerName, new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) { { CloudFilesProvider.VersionsLocation, UriUtility.UriEncode(versionsContainerName, UriPart.Any, Encoding.UTF8) } });
Assert.AreEqual(ObjectStore.ContainerCreated, result);
Dictionary<string, string> headers = provider.GetContainerHeader(containerName);
string location;
Assert.IsTrue(headers.TryGetValue(CloudFilesProvider.VersionsLocation, out location));
location = UriUtility.UriDecode(location);
Assert.AreEqual(versionsContainerName, location);
string objectName = Path.GetRandomFileName();
string fileData1 = "first-content";
string fileData2 = "second-content";
/*
* Create the object
*/
using (MemoryStream uploadStream = new MemoryStream(Encoding.UTF8.GetBytes(fileData1)))
{
provider.CreateObject(containerName, uploadStream, objectName);
}
string actualData = ReadAllObjectText(provider, containerName, objectName, Encoding.UTF8, verifyEtag: true);
Assert.AreEqual(fileData1, actualData);
/*
* Overwrite the object
*/
using (MemoryStream uploadStream = new MemoryStream(Encoding.UTF8.GetBytes(fileData2)))
{
provider.CreateObject(containerName, uploadStream, objectName);
}
actualData = ReadAllObjectText(provider, containerName, objectName, Encoding.UTF8, verifyEtag: true);
Assert.AreEqual(fileData2, actualData);
/*
* Delete the object once
*/
provider.DeleteObject(containerName, objectName);
actualData = ReadAllObjectText(provider, containerName, objectName, Encoding.UTF8, verifyEtag: true);
Assert.AreEqual(fileData1, actualData);
/*
* Cleanup
*/
provider.DeleteContainer(versionsContainerName, deleteObjects: true);
provider.DeleteContainer(containerName, deleteObjects: true);
}
[TestMethod]
[TestCategory(TestCategories.User)]
[TestCategory(TestCategories.ObjectStorage)]
public void TestDeleteContainer()
{
IObjectStorageProvider provider = Bootstrapper.CreateObjectStorageProvider();
string containerName = TestContainerPrefix + Path.GetRandomFileName();
string objectName = Path.GetRandomFileName();
string fileContents = "File contents!";
ObjectStore result = provider.CreateContainer(containerName);
Assert.AreEqual(ObjectStore.ContainerCreated, result);
Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(fileContents));
provider.CreateObject(containerName, stream, objectName);
try
{
provider.DeleteContainer(containerName, deleteObjects: false);
Assert.Fail("Expected a ContainerNotEmptyException");
}
catch (ContainerNotEmptyException)
{
}
provider.DeleteContainer(containerName, deleteObjects: true);
}
[TestMethod]
[TestCategory(TestCategories.User)]
[TestCategory(TestCategories.ObjectStorage)]
public void TestGetContainerHeader()
{
IObjectStorageProvider provider = Bootstrapper.CreateObjectStorageProvider();
string containerName = TestContainerPrefix + Path.GetRandomFileName();
ObjectStore result = provider.CreateContainer(containerName);
Assert.AreEqual(ObjectStore.ContainerCreated, result);
Dictionary<string, string> headers = provider.GetContainerHeader(containerName);
Console.WriteLine("Container Headers");
foreach (KeyValuePair<string, string> pair in headers)
Console.WriteLine(" {0}: {1}", pair.Key, pair.Value);
provider.DeleteContainer(containerName, deleteObjects: true);
}
[TestMethod]
[TestCategory(TestCategories.User)]
[TestCategory(TestCategories.ObjectStorage)]
public void TestGetContainerMetaData()
{
IObjectStorageProvider provider = Bootstrapper.CreateObjectStorageProvider();
string containerName = TestContainerPrefix + Path.GetRandomFileName();
ObjectStore result = provider.CreateContainer(containerName);
Assert.AreEqual(ObjectStore.ContainerCreated, result);
Dictionary<string, string> metadata = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "Key1", "Value 1" },
{ "Key2", "Value ²" },
};
provider.UpdateContainerMetadata(containerName, new Dictionary<string, string>(metadata, StringComparer.OrdinalIgnoreCase));
Dictionary<string, string> actualMetadata = provider.GetContainerMetaData(containerName);
Console.WriteLine("Container Metadata");
foreach (KeyValuePair<string, string> pair in actualMetadata)
Console.WriteLine(" {0}: {1}", pair.Key, pair.Value);
CheckMetadataCollections(metadata, actualMetadata);
provider.DeleteContainer(containerName, deleteObjects: true);
}
[TestMethod]
[TestCategory(TestCategories.User)]
[TestCategory(TestCategories.ObjectStorage)]
public void TestContainerHeaderKeyCharacters()
{
IObjectStorageProvider provider = Bootstrapper.CreateObjectStorageProvider();
string containerName = TestContainerPrefix + Path.GetRandomFileName();
ObjectStore result = provider.CreateContainer(containerName);
Assert.AreEqual(ObjectStore.ContainerCreated, result);
List<char> keyCharList = new List<char>();
for (char i = MinHeaderKeyCharacter; i <= MaxHeaderKeyCharacter; i++)
{
if (!SeparatorCharacters.Contains(i) && !NotSupportedCharacters.Contains(i))
keyCharList.Add(i);
}
string key = TestKeyPrefix + new string(keyCharList.ToArray());
Console.WriteLine("Expected key: {0}", key);
provider.UpdateContainerMetadata(
containerName,
new Dictionary<string, string>
{
{ key, "Value" }
});
Dictionary<string, string> metadata = provider.GetContainerMetaData(containerName);
Assert.IsNotNull(metadata);
string value;
Assert.IsTrue(metadata.TryGetValue(key, out value));
Assert.AreEqual("Value", value);
provider.UpdateContainerMetadata(
containerName,
new Dictionary<string, string>
{
{ key, null }
});
metadata = provider.GetContainerMetaData(containerName);
Assert.IsNotNull(metadata);
Assert.IsFalse(metadata.TryGetValue(key, out value));
provider.DeleteContainer(containerName, deleteObjects: true);
}
[TestMethod]
[TestCategory(TestCategories.User)]
[TestCategory(TestCategories.ObjectStorage)]
public void TestContainerInvalidHeaderKeyCharacters()
{
IObjectStorageProvider provider = Bootstrapper.CreateObjectStorageProvider();
string containerName = TestContainerPrefix + Path.GetRandomFileName();
ObjectStore result = provider.CreateContainer(containerName);
Assert.AreEqual(ObjectStore.ContainerCreated, result);
List<char> validKeyCharList = new List<char>();
for (char i = MinHeaderKeyCharacter; i <= MaxHeaderKeyCharacter; i++)
{
if (!SeparatorCharacters.Contains(i) && !NotSupportedCharacters.Contains(i))
validKeyCharList.Add(i);
}
for (int i = char.MinValue; i <= char.MaxValue; i++)
{
if (validKeyCharList.BinarySearch((char)i) >= 0)
continue;
string invalidKey = new string((char)i, 1);
try
{
provider.UpdateContainerMetadata(
containerName,
new Dictionary<string, string>
{
{ invalidKey, "Value" }
});
Assert.Fail("Should throw an exception for invalid keys.");
}
catch (ArgumentException)
{
if (i >= MinHeaderKeyCharacter && i <= MaxHeaderKeyCharacter)
StringAssert.Contains(SeparatorCharacters, invalidKey);
}
catch (NotSupportedException)
{
StringAssert.Contains(NotSupportedCharacters, invalidKey);
}
}
provider.DeleteContainer(containerName, deleteObjects: true);
}
[TestMethod]
[TestCategory(TestCategories.User)]
[TestCategory(TestCategories.ObjectStorage)]
public void TestUpdateContainerMetadata()
{
IObjectStorageProvider provider = Bootstrapper.CreateObjectStorageProvider();
string containerName = TestContainerPrefix + Path.GetRandomFileName();
ObjectStore result = provider.CreateContainer(containerName);
Assert.AreEqual(ObjectStore.ContainerCreated, result);
Dictionary<string, string> metadata = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "Key1", "Value 1" },
{ "Key2", "Value ²" },
};
provider.UpdateContainerMetadata(containerName, new Dictionary<string, string>(metadata, StringComparer.OrdinalIgnoreCase));
Dictionary<string, string> actualMetadata = provider.GetContainerMetaData(containerName);
Console.WriteLine("Container Metadata");
foreach (KeyValuePair<string, string> pair in actualMetadata)
Console.WriteLine(" {0}: {1}", pair.Key, pair.Value);
CheckMetadataCollections(metadata, actualMetadata);
metadata["Key2"] = "Value 2";
Dictionary<string, string> updatedMetadata = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "Key2", "Value 2" }
};
provider.UpdateContainerMetadata(containerName, new Dictionary<string, string>(updatedMetadata, StringComparer.OrdinalIgnoreCase));
actualMetadata = provider.GetContainerMetaData(containerName);
Console.WriteLine("Container Metadata");
foreach (KeyValuePair<string, string> pair in actualMetadata)
Console.WriteLine(" {0}: {1}", pair.Key, pair.Value);
CheckMetadataCollections(metadata, actualMetadata);
provider.DeleteContainer(containerName, deleteObjects: true);
}
[TestMethod]
[TestCategory(TestCategories.User)]
[TestCategory(TestCategories.ObjectStorage)]
public void TestDeleteContainerMetadata()
{
IObjectStorageProvider provider = Bootstrapper.CreateObjectStorageProvider();
string containerName = TestContainerPrefix + Path.GetRandomFileName();
ObjectStore result = provider.CreateContainer(containerName);
Assert.AreEqual(ObjectStore.ContainerCreated, result);
Dictionary<string, string> metadata = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "Key1", "Value 1" },
{ "Key2", "Value ²" },
{ "Key3", "Value 3" },
{ "Key4", "Value 4" },
};
provider.UpdateContainerMetadata(containerName, new Dictionary<string, string>(metadata, StringComparer.OrdinalIgnoreCase));
Dictionary<string, string> actualMetadata = provider.GetContainerMetaData(containerName);
Console.WriteLine("Container Metadata");
foreach (KeyValuePair<string, string> pair in actualMetadata)
Console.WriteLine(" {0}: {1}", pair.Key, pair.Value);
CheckMetadataCollections(metadata, actualMetadata);
/* Check the overload which takes a single key
*/
// remove Key3 first to make sure we still have a ² character in a remaining value
metadata.Remove("Key3");
provider.DeleteContainerMetadata(containerName, "Key3");
actualMetadata = provider.GetContainerMetaData(containerName);
Console.WriteLine("Container Metadata after removing Key3");
foreach (KeyValuePair<string, string> pair in actualMetadata)
Console.WriteLine(" {0}: {1}", pair.Key, pair.Value);
CheckMetadataCollections(metadata, actualMetadata);
/* Check the overload which takes multiple keys
*/
metadata.Remove("Key2");
metadata.Remove("Key4");
provider.DeleteContainerMetadata(containerName, new[] { "Key2", "Key4" });
actualMetadata = provider.GetContainerMetaData(containerName);
Console.WriteLine("Container Metadata after removing Key2, Key4");
foreach (KeyValuePair<string, string> pair in actualMetadata)
Console.WriteLine(" {0}: {1}", pair.Key, pair.Value);
CheckMetadataCollections(metadata, actualMetadata);
/* Check that duplicate removal is a NOP
*/
metadata.Remove("Key2");
metadata.Remove("Key4");
provider.DeleteContainerMetadata(containerName, new[] { "Key2", "Key4" });
actualMetadata = provider.GetContainerMetaData(containerName);
Console.WriteLine("Container Metadata after removing Key2, Key4");
foreach (KeyValuePair<string, string> pair in actualMetadata)
Console.WriteLine(" {0}: {1}", pair.Key, pair.Value);
CheckMetadataCollections(metadata, actualMetadata);
/* Cleanup
*/
provider.DeleteContainer(containerName, deleteObjects: true);
}
[TestMethod]
[TestCategory(TestCategories.User)]
[TestCategory(TestCategories.ObjectStorage)]
public void TestListCDNContainers()
{
IObjectStorageProvider provider = Bootstrapper.CreateObjectStorageProvider();
IEnumerable<ContainerCDN> containers = ListAllCDNContainers(provider);
Console.WriteLine("Containers");
foreach (ContainerCDN container in containers)
{
Console.WriteLine(" {1}{0}", container.Name, container.CDNEnabled ? "*" : "");
}
}
/// <summary>
/// This test covers most of the CDN functionality exposed by <see cref="IObjectStorageProvider"/>.
/// </summary>
[TestMethod]
[TestCategory(TestCategories.User)]
[TestCategory(TestCategories.ObjectStorage)]
public void TestCDNOnContainer()
{
IObjectStorageProvider provider = Bootstrapper.CreateObjectStorageProvider();
string containerName = TestContainerPrefix + Path.GetRandomFileName();
string objectName = Path.GetRandomFileName();
string fileContents = "File contents!";
ObjectStore result = provider.CreateContainer(containerName);
Assert.AreEqual(ObjectStore.ContainerCreated, result);
Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(fileContents));
provider.CreateObject(containerName, stream, objectName);
Dictionary<string, string> cdnHeaders = provider.EnableCDNOnContainer(containerName, false);
Assert.IsNotNull(cdnHeaders);
Console.WriteLine("CDN Headers from EnableCDNOnContainer");
foreach (var pair in cdnHeaders)
Console.WriteLine(" {0}: {1}", pair.Key, pair.Value);
ContainerCDN containerHeader = provider.GetContainerCDNHeader(containerName);
Assert.IsNotNull(containerHeader);
Console.WriteLine(JsonConvert.SerializeObject(containerHeader, Formatting.Indented));
Assert.IsTrue(containerHeader.CDNEnabled);
Assert.IsFalse(containerHeader.LogRetention);
Assert.IsTrue(
containerHeader.CDNUri != null
|| containerHeader.CDNIosUri != null
|| containerHeader.CDNSslUri != null
|| containerHeader.CDNStreamingUri != null);
// Call the other overloads of EnableCDNOnContainer
cdnHeaders = provider.EnableCDNOnContainer(containerName, containerHeader.Ttl);
ContainerCDN updatedHeader = provider.GetContainerCDNHeader(containerName);
Console.WriteLine(JsonConvert.SerializeObject(updatedHeader, Formatting.Indented));
Assert.IsNotNull(updatedHeader);
Assert.IsTrue(updatedHeader.CDNEnabled);
Assert.IsFalse(updatedHeader.LogRetention);
Assert.IsTrue(
updatedHeader.CDNUri != null
|| updatedHeader.CDNIosUri != null
|| updatedHeader.CDNSslUri != null
|| updatedHeader.CDNStreamingUri != null);
Assert.AreEqual(containerHeader.Ttl, updatedHeader.Ttl);
cdnHeaders = provider.EnableCDNOnContainer(containerName, containerHeader.Ttl, true);
updatedHeader = provider.GetContainerCDNHeader(containerName);
Console.WriteLine(JsonConvert.SerializeObject(updatedHeader, Formatting.Indented));
Assert.IsNotNull(updatedHeader);
Assert.IsTrue(updatedHeader.CDNEnabled);
Assert.IsTrue(updatedHeader.LogRetention);
Assert.IsTrue(
updatedHeader.CDNUri != null
|| updatedHeader.CDNIosUri != null
|| updatedHeader.CDNSslUri != null
|| updatedHeader.CDNStreamingUri != null);
Assert.AreEqual(containerHeader.Ttl, updatedHeader.Ttl);
// update the container CDN properties
Dictionary<string, string> headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ CloudFilesProvider.CdnTTL, (updatedHeader.Ttl + 1).ToString() },
{ CloudFilesProvider.CdnLogRetention, false.ToString() },
//{ CloudFilesProvider.CdnEnabled, true.ToString() },
};
provider.UpdateContainerCdnHeaders(containerName, headers);
updatedHeader = provider.GetContainerCDNHeader(containerName);
Console.WriteLine(JsonConvert.SerializeObject(updatedHeader, Formatting.Indented));
Assert.IsNotNull(updatedHeader);
Assert.IsTrue(updatedHeader.CDNEnabled);
Assert.IsFalse(updatedHeader.LogRetention);
Assert.IsTrue(
updatedHeader.CDNUri != null
|| updatedHeader.CDNIosUri != null
|| updatedHeader.CDNSslUri != null
|| updatedHeader.CDNStreamingUri != null);
Assert.AreEqual(containerHeader.Ttl + 1, updatedHeader.Ttl);
// attempt to access the container over the CDN
if (containerHeader.CDNUri != null || containerHeader.CDNSslUri != null)
{
string baseUri = containerHeader.CDNUri ?? containerHeader.CDNSslUri;
Uri uri = new Uri(containerHeader.CDNUri + '/' + objectName);
WebRequest request = HttpWebRequest.Create(uri);
using (WebResponse response = request.GetResponse())
{
Stream cdnStream = response.GetResponseStream();
StreamReader reader = new StreamReader(cdnStream, Encoding.UTF8);
string text = reader.ReadToEnd();
Assert.AreEqual(fileContents, text);
}
}
else
{
Assert.Inconclusive("This integration test relies on cdn_uri or cdn_ssl_uri.");
}
IEnumerable<ContainerCDN> containers = ListAllCDNContainers(provider);
Console.WriteLine("Containers");
foreach (ContainerCDN container in containers)
{
Console.WriteLine(" {1}{0}", container.Name, container.CDNEnabled ? "*" : "");
}
cdnHeaders = provider.DisableCDNOnContainer(containerName);
Assert.IsNotNull(cdnHeaders);
Console.WriteLine("CDN Headers from DisableCDNOnContainer");
foreach (var pair in cdnHeaders)
Console.WriteLine(" {0}: {1}", pair.Key, pair.Value);
updatedHeader = provider.GetContainerCDNHeader(containerName);
Console.WriteLine(JsonConvert.SerializeObject(updatedHeader, Formatting.Indented));
Assert.IsNotNull(updatedHeader);
Assert.IsFalse(updatedHeader.CDNEnabled);
Assert.IsFalse(updatedHeader.LogRetention);
Assert.IsTrue(
updatedHeader.CDNUri != null
|| updatedHeader.CDNIosUri != null
|| updatedHeader.CDNSslUri != null
|| updatedHeader.CDNStreamingUri != null);
Assert.AreEqual(containerHeader.Ttl + 1, updatedHeader.Ttl);
provider.DeleteContainer(containerName, deleteObjects: true);
}
[TestMethod]
[TestCategory(TestCategories.User)]
[TestCategory(TestCategories.ObjectStorage)]
public void TestStaticWebOnContainer()
{
IObjectStorageProvider provider = Bootstrapper.CreateObjectStorageProvider();
string containerName = TestContainerPrefix + Path.GetRandomFileName();
string objectName = Path.GetRandomFileName();
string fileContents = "File contents!";
ObjectStore result = provider.CreateContainer(containerName);
Assert.AreEqual(ObjectStore.ContainerCreated, result);
Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(fileContents));
provider.CreateObject(containerName, stream, objectName);
Dictionary<string, string> cdnHeaders = provider.EnableCDNOnContainer(containerName, false);
Assert.IsNotNull(cdnHeaders);
Console.WriteLine("CDN Headers");
foreach (var pair in cdnHeaders)
Console.WriteLine(" {0}: {1}", pair.Key, pair.Value);
string index = objectName;
string error = objectName;
string css = objectName;
provider.EnableStaticWebOnContainer(containerName, index: index, error: error, listing: false);
provider.DisableStaticWebOnContainer(containerName);
provider.DeleteContainer(containerName, deleteObjects: true);
}
/// <summary>
/// This is a regression test for openstacknetsdk/openstack.net#333.
/// </summary>
/// <seealso href="https://github.com/openstacknetsdk/openstack.net/issues/333">Chunked Encoding Issues (#333)</seealso>
[TestMethod]
[TestCategory(TestCategories.User)]
[TestCategory(TestCategories.ObjectStorage)]
public void TestProtocolViolation()
{
try
{
TestTempUrlWithControlCharactersInObjectName();
Assert.Inconclusive("This test relies on the previous call throwing a WebException placing the ServicePoint in a bad state.");
}
catch (WebException ex)
{
Assert.IsNotNull(ex.Response);
ServicePoint servicePoint = ServicePointManager.FindServicePoint(ex.Response.ResponseUri);
if (servicePoint.ProtocolVersion >= HttpVersion.Version11)
Assert.Inconclusive("The ServicePoint must be set to HTTP/1.0 in order to test the ProtocolViolationException handling.");
}
TestTempUrlExpired();
}
[TestMethod]
[TestCategory(TestCategories.User)]
[TestCategory(TestCategories.ObjectStorage)]
public void TestTempUrlValid()
{
IObjectStorageProvider provider = Bootstrapper.CreateObjectStorageProvider();
Assert.IsInstanceOfType(provider, typeof(CloudFilesProvider), "Temp URLs are a Rackspace-specific extension to the Object Storage service.");
string containerName = TestContainerPrefix + Path.GetRandomFileName();
string objectName = Path.GetRandomFileName();
string fileContents = "File contents!";
Dictionary<string, string> accountMetadata = provider.GetAccountMetaData();
string tempUrlKey;
if (!accountMetadata.TryGetValue(CloudFilesProvider.TempUrlKey, out tempUrlKey))
{
tempUrlKey = Guid.NewGuid().ToString("N");
accountMetadata = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
accountMetadata[CloudFilesProvider.TempUrlKey] = tempUrlKey;
provider.UpdateAccountMetadata(accountMetadata);
}
ObjectStore result = provider.CreateContainer(containerName);
Assert.AreEqual(ObjectStore.ContainerCreated, result);
Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(fileContents));
provider.CreateObject(containerName, stream, objectName);
// verify a future time works
DateTimeOffset expirationTime = DateTimeOffset.Now + TimeSpan.FromSeconds(10);
Uri uri = ((CloudFilesProvider)provider).CreateTemporaryPublicUri(HttpMethod.GET, containerName, objectName, tempUrlKey, expirationTime);
WebRequest request = HttpWebRequest.Create(uri);
using (WebResponse response = request.GetResponse())
{
Stream cdnStream = response.GetResponseStream();
StreamReader reader = new StreamReader(cdnStream, Encoding.UTF8);
string text = reader.ReadToEnd();
Assert.AreEqual(fileContents, text);
}
provider.DeleteContainer(containerName, deleteObjects: true);
}
[TestMethod]
[TestCategory(TestCategories.User)]
[TestCategory(TestCategories.ObjectStorage)]
public void TestTempUrlExpired()
{
IObjectStorageProvider provider = Bootstrapper.CreateObjectStorageProvider();
Assert.IsInstanceOfType(provider, typeof(CloudFilesProvider), "Temp URLs are a Rackspace-specific extension to the Object Storage service.");
string containerName = TestContainerPrefix + Path.GetRandomFileName();
string objectName = Path.GetRandomFileName();
string fileContents = "File contents!";
Dictionary<string, string> accountMetadata = provider.GetAccountMetaData();
string tempUrlKey;
if (!accountMetadata.TryGetValue(CloudFilesProvider.TempUrlKey, out tempUrlKey))
{
tempUrlKey = Guid.NewGuid().ToString("N");
accountMetadata = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
accountMetadata[CloudFilesProvider.TempUrlKey] = tempUrlKey;
provider.UpdateAccountMetadata(accountMetadata);
}
ObjectStore result = provider.CreateContainer(containerName);
Assert.AreEqual(ObjectStore.ContainerCreated, result);
Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(fileContents));
provider.CreateObject(containerName, stream, objectName);
// verify a past time does not work
try
{
DateTimeOffset expirationTime = DateTimeOffset.Now - TimeSpan.FromSeconds(3);
Uri uri = ((CloudFilesProvider)provider).CreateTemporaryPublicUri(HttpMethod.GET, containerName, objectName, tempUrlKey, expirationTime);
WebRequest request = HttpWebRequest.Create(uri);
using (WebResponse response = request.GetResponse())
{
Stream cdnStream = response.GetResponseStream();
StreamReader reader = new StreamReader(cdnStream, Encoding.UTF8);
string text = reader.ReadToEnd();
Assert.Fail("Expected an exception");
}
}
catch (WebException ex)
{
Assert.AreEqual(HttpStatusCode.Unauthorized, ((HttpWebResponse)ex.Response).StatusCode);
}
provider.DeleteContainer(containerName, deleteObjects: true);
}
[TestMethod]
[TestCategory(TestCategories.User)]
[TestCategory(TestCategories.ObjectStorage)]
public void TestTempUrlWithSpecialCharactersInObjectName()
{
IObjectStorageProvider provider = Bootstrapper.CreateObjectStorageProvider();
Assert.IsInstanceOfType(provider, typeof(CloudFilesProvider), "Temp URLs are a Rackspace-specific extension to the Object Storage service.");
string containerName = TestContainerPrefix + Path.GetRandomFileName();
string objectName = "§ / 你好";
string fileContents = "File contents!";
Dictionary<string, string> accountMetadata = provider.GetAccountMetaData();
string tempUrlKey;
if (!accountMetadata.TryGetValue(CloudFilesProvider.TempUrlKey, out tempUrlKey))
{
tempUrlKey = Guid.NewGuid().ToString("N");
accountMetadata = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
accountMetadata[CloudFilesProvider.TempUrlKey] = tempUrlKey;
provider.UpdateAccountMetadata(accountMetadata);
}
ObjectStore result = provider.CreateContainer(containerName);
Assert.AreEqual(ObjectStore.ContainerCreated, result);
Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(fileContents));
provider.CreateObject(containerName, stream, objectName);
// verify a future time works
DateTimeOffset expirationTime = DateTimeOffset.Now + TimeSpan.FromSeconds(10);
Uri uri = ((CloudFilesProvider)provider).CreateTemporaryPublicUri(HttpMethod.GET, containerName, objectName, tempUrlKey, expirationTime);
WebRequest request = HttpWebRequest.Create(uri);
using (WebResponse response = request.GetResponse())
{
Stream cdnStream = response.GetResponseStream();
StreamReader reader = new StreamReader(cdnStream, Encoding.UTF8);
string text = reader.ReadToEnd();
Assert.AreEqual(fileContents, text);
}
provider.DeleteContainer(containerName, deleteObjects: true);
}
[TestMethod]
[TestCategory(TestCategories.User)]
[TestCategory(TestCategories.ObjectStorage)]
public void TestTempUrlWithControlCharactersInObjectName()
{
IObjectStorageProvider provider = Bootstrapper.CreateObjectStorageProvider();
Assert.IsInstanceOfType(provider, typeof(CloudFilesProvider), "Temp URLs are a Rackspace-specific extension to the Object Storage service.");
string containerName = TestContainerPrefix + Path.GetRandomFileName();
string objectName = "foo\n\rbar";
string fileContents = "File contents!";
Dictionary<string, string> accountMetadata = provider.GetAccountMetaData();
string tempUrlKey;
if (!accountMetadata.TryGetValue(CloudFilesProvider.TempUrlKey, out tempUrlKey))
{
tempUrlKey = Guid.NewGuid().ToString("N");
accountMetadata = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
accountMetadata[CloudFilesProvider.TempUrlKey] = tempUrlKey;
provider.UpdateAccountMetadata(accountMetadata);
}
ObjectStore result = provider.CreateContainer(containerName);
Assert.AreEqual(ObjectStore.ContainerCreated, result);
Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(fileContents));
provider.CreateObject(containerName, stream, objectName);
// verify a future time works
DateTimeOffset expirationTime = DateTimeOffset.Now + TimeSpan.FromSeconds(10);
Uri uri = ((CloudFilesProvider)provider).CreateTemporaryPublicUri(HttpMethod.GET, containerName, objectName, tempUrlKey, expirationTime);
WebRequest request = HttpWebRequest.Create(uri);
using (WebResponse response = request.GetResponse())
{
Stream cdnStream = response.GetResponseStream();
StreamReader reader = new StreamReader(cdnStream, Encoding.UTF8);
string text = reader.ReadToEnd();
Assert.AreEqual(fileContents, text);
}
provider.DeleteContainer(containerName, deleteObjects: true);
}
[TestMethod]
[TestCategory(TestCategories.User)]
[TestCategory(TestCategories.ObjectStorage)]
public async Task TestFormPostValid()
{
IObjectStorageProvider provider = Bootstrapper.CreateObjectStorageProvider();
Assert.IsInstanceOfType(provider, typeof(CloudFilesProvider), "Temp URLs are a Rackspace-specific extension to the Object Storage service.");
string containerName = TestContainerPrefix + Path.GetRandomFileName();