FIRStorageTests.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Copyright 2017 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "FIRStorageTestHelpers.h"
  15. #import "FIRStorageReference.h"
  16. #import "FIRStorageReference_Private.h"
  17. #import "FIRStorage_Private.h"
  18. @interface FIRStorageTests : XCTestCase
  19. @property(strong, nonatomic) id app;
  20. @end
  21. @implementation FIRStorageTests
  22. - (void)setUp {
  23. [super setUp];
  24. id mockOptions = OCMClassMock([FIROptions class]);
  25. OCMStub([mockOptions storageBucket]).andReturn(@"bucket");
  26. self.app = OCMClassMock([FIRApp class]);
  27. OCMStub([self.app name]).andReturn(kFIRStorageAppName);
  28. OCMStub([(FIRApp *)self.app options]).andReturn(mockOptions);
  29. }
  30. - (void)tearDown {
  31. self.app = nil;
  32. [super tearDown];
  33. }
  34. - (void)testBucketNotEnforced {
  35. FIROptions *mockOptions = OCMClassMock([FIROptions class]);
  36. OCMStub([mockOptions storageBucket]).andReturn(@"");
  37. FIRApp *app = OCMClassMock([FIRApp class]);
  38. OCMStub([app name]).andReturn(kFIRStorageAppName);
  39. OCMStub([(FIRApp *)app options]).andReturn(mockOptions);
  40. FIRStorage *storage = [FIRStorage storageForApp:app];
  41. [storage referenceForURL:@"gs://benwu-test1.storage.firebase.com/child"];
  42. [storage referenceForURL:@"gs://benwu-test2.storage.firebase.com/child"];
  43. }
  44. - (void)testBucketEnforced {
  45. FIRStorage *storage =
  46. [FIRStorage storageForApp:self.app URL:@"gs://benwu-test1.storage.firebase.com"];
  47. [storage referenceForURL:@"gs://benwu-test1.storage.firebase.com/child"];
  48. storage = [FIRStorage storageForApp:self.app URL:@"gs://benwu-test1.storage.firebase.com/"];
  49. [storage referenceForURL:@"gs://benwu-test1.storage.firebase.com/child"];
  50. XCTAssertThrows([storage referenceForURL:@"gs://benwu-test2.storage.firebase.com/child"]);
  51. }
  52. - (void)testInitWithCustomUrl {
  53. FIRStorage *storage = [FIRStorage storageForApp:self.app URL:@"gs://foo-bar.appspot.com"];
  54. XCTAssertEqualObjects(@"gs://foo-bar.appspot.com/", [[storage reference] description]);
  55. storage = [FIRStorage storageForApp:self.app URL:@"gs://foo-bar.appspot.com/"];
  56. XCTAssertEqualObjects(@"gs://foo-bar.appspot.com/", [[storage reference] description]);
  57. }
  58. - (void)testInitWithWrongScheme {
  59. XCTAssertThrows([FIRStorage storageForApp:self.app URL:@"http://foo-bar.appspot.com"]);
  60. }
  61. - (void)testInitWithNoScheme {
  62. XCTAssertThrows([FIRStorage storageForApp:self.app URL:@"foo-bar.appspot.com"]);
  63. }
  64. - (void)testInitWithNilURL {
  65. XCTAssertThrows([FIRStorage storageForApp:self.app URL:(id _Nonnull)nil]);
  66. }
  67. - (void)testInitWithPath {
  68. XCTAssertThrows([FIRStorage storageForApp:self.app URL:@"gs://foo-bar.appspot.com/child"]);
  69. }
  70. - (void)testInitWithDefaultAndCustomUrl {
  71. FIRStorage *customInstance = [FIRStorage storageForApp:self.app URL:@"gs://foo-bar.appspot.com"];
  72. FIRStorage *defaultInstance = [FIRStorage storageForApp:self.app];
  73. XCTAssertEqualObjects(@"gs://foo-bar.appspot.com/", [[customInstance reference] description]);
  74. XCTAssertEqualObjects(@"gs://bucket/", [[defaultInstance reference] description]);
  75. }
  76. - (void)testStorageDefaultApp {
  77. FIRStorage *storage = [FIRStorage storageForApp:self.app];
  78. XCTAssertEqualObjects(storage.app.name, ((FIRApp *)self.app).name);
  79. XCTAssertNotNil(storage.fetcherServiceForApp);
  80. }
  81. - (void)testStorageCustomApp {
  82. id mockOptions = OCMClassMock([FIROptions class]);
  83. OCMStub([mockOptions storageBucket]).andReturn(@"bucket");
  84. id secondApp = OCMClassMock([FIRApp class]);
  85. OCMStub([secondApp name]).andReturn(@"secondApp");
  86. OCMStub([(FIRApp *)secondApp options]).andReturn(mockOptions);
  87. FIRStorage *storage = [FIRStorage storageForApp:secondApp];
  88. XCTAssertNotEqual(storage.app.name, ((FIRApp *)self.app).name);
  89. XCTAssertNotNil(storage.fetcherServiceForApp);
  90. XCTAssertNotEqualObjects(storage.fetcherServiceForApp,
  91. [FIRStorage storageForApp:self.app].fetcherServiceForApp);
  92. }
  93. - (void)testStorageNoBucketInConfig {
  94. id mockOptions = OCMClassMock([FIROptions class]);
  95. OCMStub([mockOptions storageBucket]).andReturn(nil);
  96. id secondApp = OCMClassMock([FIRApp class]);
  97. OCMStub([secondApp name]).andReturn(@"secondApp");
  98. OCMStub([(FIRApp *)secondApp options]).andReturn(mockOptions);
  99. XCTAssertThrows([FIRStorage storageForApp:secondApp]);
  100. }
  101. - (void)testStorageEmptyBucketInConfig {
  102. id mockOptions = OCMClassMock([FIROptions class]);
  103. OCMStub([mockOptions storageBucket]).andReturn(@"");
  104. id secondApp = OCMClassMock([FIRApp class]);
  105. OCMStub([secondApp name]).andReturn(@"secondApp");
  106. OCMStub([(FIRApp *)secondApp options]).andReturn(mockOptions);
  107. FIRStorage *storage = [FIRStorage storageForApp:secondApp];
  108. FIRStorageReference *storageRef = [storage referenceForURL:@"gs://bucket/path/to/object"];
  109. XCTAssertEqualObjects(storageRef.bucket, @"bucket");
  110. }
  111. - (void)testStorageWrongBucketInConfig {
  112. id mockOptions = OCMClassMock([FIROptions class]);
  113. OCMStub([mockOptions storageBucket]).andReturn(@"notMyBucket");
  114. id secondApp = OCMClassMock([FIRApp class]);
  115. OCMStub([secondApp name]).andReturn(@"secondApp");
  116. OCMStub([(FIRApp *)secondApp options]).andReturn(mockOptions);
  117. FIRStorage *storage = [FIRStorage storageForApp:secondApp];
  118. XCTAssertEqualObjects([(FIRApp *)secondApp options].storageBucket, @"notMyBucket");
  119. XCTAssertThrows([storage referenceForURL:@"gs://bucket/path/to/object"]);
  120. }
  121. - (void)testRefDefaultApp {
  122. FIRStorageReference *convenienceRef =
  123. [[FIRStorage storageForApp:self.app] referenceForURL:@"gs://bucket/path/to/object"];
  124. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:@"path/to/object"];
  125. FIRStorageReference *builtRef =
  126. [[FIRStorageReference alloc] initWithStorage:[FIRStorage storageForApp:self.app] path:path];
  127. XCTAssertEqualObjects([convenienceRef description], [builtRef description]);
  128. XCTAssertEqualObjects(convenienceRef.storage.app, builtRef.storage.app);
  129. }
  130. - (void)testRefCustomApp {
  131. id mockOptions = OCMClassMock([FIROptions class]);
  132. OCMStub([mockOptions storageBucket]).andReturn(@"bucket");
  133. id secondApp = OCMClassMock([FIRApp class]);
  134. OCMStub([secondApp name]).andReturn(@"secondApp");
  135. OCMStub([(FIRApp *)secondApp options]).andReturn(mockOptions);
  136. FIRStorageReference *convenienceRef =
  137. [[FIRStorage storageForApp:secondApp] referenceForURL:@"gs://bucket/path/to/object"];
  138. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:@"path/to/object"];
  139. FIRStorageReference *builtRef =
  140. [[FIRStorageReference alloc] initWithStorage:[FIRStorage storageForApp:secondApp] path:path];
  141. XCTAssertEqualObjects([convenienceRef description], [builtRef description]);
  142. XCTAssertEqualObjects(convenienceRef.storage.app, builtRef.storage.app);
  143. }
  144. - (void)testRootRefDefaultApp {
  145. FIRStorageReference *convenienceRef = [[FIRStorage storageForApp:self.app] reference];
  146. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:nil];
  147. FIRStorageReference *builtRef =
  148. [[FIRStorageReference alloc] initWithStorage:[FIRStorage storageForApp:self.app] path:path];
  149. XCTAssertEqualObjects([convenienceRef description], [builtRef description]);
  150. XCTAssertEqualObjects(convenienceRef.storage.app, builtRef.storage.app);
  151. }
  152. - (void)testRefWithPathDefaultApp {
  153. FIRStorageReference *convenienceRef =
  154. [[FIRStorage storageForApp:self.app] referenceWithPath:@"path/to/object"];
  155. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:@"path/to/object"];
  156. FIRStorageReference *builtRef =
  157. [[FIRStorageReference alloc] initWithStorage:[FIRStorage storageForApp:self.app] path:path];
  158. XCTAssertEqualObjects([convenienceRef description], [builtRef description]);
  159. XCTAssertEqualObjects(convenienceRef.storage.app, builtRef.storage.app);
  160. }
  161. - (void)testEqual {
  162. FIRStorage *storage = [FIRStorage storageForApp:self.app];
  163. FIRStorage *copy = [storage copy];
  164. XCTAssertEqualObjects(storage.app.name, copy.app.name);
  165. }
  166. - (void)testNotEqual {
  167. FIRStorage *storage = [FIRStorage storageForApp:self.app];
  168. id mockOptions = OCMClassMock([FIROptions class]);
  169. OCMStub([mockOptions storageBucket]).andReturn(@"bucket");
  170. id secondApp = OCMClassMock([FIRApp class]);
  171. OCMStub([secondApp name]).andReturn(@"secondApp");
  172. OCMStub([(FIRApp *)secondApp options]).andReturn(mockOptions);
  173. FIRStorage *secondStorage = [FIRStorage storageForApp:secondApp];
  174. XCTAssertNotEqualObjects(storage, secondStorage);
  175. }
  176. - (void)testHash {
  177. FIRStorage *storage = [FIRStorage storageForApp:self.app];
  178. FIRStorage *copy = [storage copy];
  179. XCTAssertEqual([storage hash], [copy hash]);
  180. }
  181. @end