FIRStorageTests.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 "FirebaseStorage/Tests/Unit/FIRStorageTestHelpers.h"
  15. #import "FirebaseStorage/Sources/Public/FirebaseStorage/FIRStorageReference.h"
  16. #import "FirebaseStorage/Sources/FIRStorageComponent.h"
  17. #import "FirebaseStorage/Sources/FIRStorageReference_Private.h"
  18. #import "FirebaseStorage/Sources/FIRStorage_Private.h"
  19. #import "SharedTestUtilities/FIRComponentTestUtilities.h"
  20. @interface FIRStorageTests : XCTestCase
  21. @property(strong, nonatomic) id app;
  22. @end
  23. @implementation FIRStorageTests
  24. - (void)setUp {
  25. [super setUp];
  26. id mockOptions = OCMClassMock([FIROptions class]);
  27. OCMStub([mockOptions storageBucket]).andReturn(@"bucket");
  28. self.app = [FIRStorageTestHelpers mockedApp];
  29. OCMStub([self.app name]).andReturn(kFIRStorageAppName);
  30. OCMStub([(FIRApp *)self.app options]).andReturn(mockOptions);
  31. }
  32. - (void)tearDown {
  33. self.app = nil;
  34. [super tearDown];
  35. }
  36. - (void)testBucketNotEnforced {
  37. FIROptions *mockOptions = OCMClassMock([FIROptions class]);
  38. OCMStub([mockOptions storageBucket]).andReturn(@"");
  39. FIRApp *app = [FIRStorageTestHelpers mockedApp];
  40. OCMStub([app name]).andReturn(kFIRStorageAppName);
  41. OCMStub([(FIRApp *)app options]).andReturn(mockOptions);
  42. FIRStorage *storage = [FIRStorage storageForApp:app];
  43. [storage referenceForURL:@"gs://benwu-test1.storage.firebase.com/child"];
  44. [storage referenceForURL:@"gs://benwu-test2.storage.firebase.com/child"];
  45. }
  46. - (void)testBucketEnforced {
  47. FIRStorage *storage = [FIRStorage storageForApp:self.app
  48. URL:@"gs://benwu-test1.storage.firebase.com"];
  49. [storage referenceForURL:@"gs://benwu-test1.storage.firebase.com/child"];
  50. storage = [FIRStorage storageForApp:self.app URL:@"gs://benwu-test1.storage.firebase.com/"];
  51. [storage referenceForURL:@"gs://benwu-test1.storage.firebase.com/child"];
  52. XCTAssertThrows([storage referenceForURL:@"gs://benwu-test2.storage.firebase.com/child"]);
  53. }
  54. - (void)testInitWithCustomUrl {
  55. FIRStorage *storage = [FIRStorage storageForApp:self.app URL:@"gs://foo-bar.appspot.com"];
  56. XCTAssertEqualObjects(@"gs://foo-bar.appspot.com/", [[storage reference] description]);
  57. storage = [FIRStorage storageForApp:self.app URL:@"gs://foo-bar.appspot.com/"];
  58. XCTAssertEqualObjects(@"gs://foo-bar.appspot.com/", [[storage reference] description]);
  59. }
  60. - (void)testInitWithWrongScheme {
  61. XCTAssertThrows([FIRStorage storageForApp:self.app URL:@"http://foo-bar.appspot.com"]);
  62. }
  63. - (void)testInitWithNoScheme {
  64. XCTAssertThrows([FIRStorage storageForApp:self.app URL:@"foo-bar.appspot.com"]);
  65. }
  66. - (void)testInitWithNilURL {
  67. XCTAssertThrows([FIRStorage storageForApp:self.app URL:(id _Nonnull)nil]);
  68. }
  69. - (void)testInitWithPath {
  70. XCTAssertThrows([FIRStorage storageForApp:self.app URL:@"gs://foo-bar.appspot.com/child"]);
  71. }
  72. - (void)testInitWithDefaultAndCustomUrl {
  73. FIRStorage *customInstance = [FIRStorage storageForApp:self.app URL:@"gs://foo-bar.appspot.com"];
  74. FIRStorage *defaultInstance = [FIRStorage storageForApp:self.app];
  75. XCTAssertEqualObjects(@"gs://foo-bar.appspot.com/", [[customInstance reference] description]);
  76. XCTAssertEqualObjects(@"gs://bucket/", [[defaultInstance reference] description]);
  77. }
  78. - (void)testStorageDefaultApp {
  79. FIRStorage *storage = [FIRStorage storageForApp:self.app];
  80. XCTAssertEqualObjects(storage.app.name, ((FIRApp *)self.app).name);
  81. [storage reference]; // Initialize Storage
  82. XCTAssertNotNil(storage.fetcherServiceForApp);
  83. }
  84. - (void)testStorageCustomApp {
  85. id mockOptions = OCMClassMock([FIROptions class]);
  86. OCMStub([mockOptions storageBucket]).andReturn(@"bucket");
  87. id secondApp = [FIRStorageTestHelpers mockedApp];
  88. OCMStub([secondApp name]).andReturn(@"secondApp");
  89. OCMStub([(FIRApp *)secondApp options]).andReturn(mockOptions);
  90. FIRStorage *storage = [FIRStorage storageForApp:secondApp];
  91. [storage reference]; // Initialize Storage
  92. XCTAssertNotEqual(storage.app.name, ((FIRApp *)self.app).name);
  93. XCTAssertNotNil(storage.fetcherServiceForApp);
  94. XCTAssertNotEqualObjects(storage.fetcherServiceForApp,
  95. [FIRStorage storageForApp:self.app].fetcherServiceForApp);
  96. }
  97. - (void)testStorageNoBucketInConfig {
  98. id mockOptions = OCMClassMock([FIROptions class]);
  99. OCMStub([mockOptions storageBucket]).andReturn(nil);
  100. id secondApp = [FIRStorageTestHelpers mockedApp];
  101. OCMStub([secondApp name]).andReturn(@"secondApp");
  102. OCMStub([(FIRApp *)secondApp options]).andReturn(mockOptions);
  103. XCTAssertThrows([FIRStorage storageForApp:secondApp]);
  104. }
  105. - (void)testStorageEmptyBucketInConfig {
  106. id mockOptions = OCMClassMock([FIROptions class]);
  107. OCMStub([mockOptions storageBucket]).andReturn(@"");
  108. id secondApp = [FIRStorageTestHelpers mockedApp];
  109. OCMStub([secondApp name]).andReturn(@"secondApp");
  110. OCMStub([(FIRApp *)secondApp options]).andReturn(mockOptions);
  111. FIRStorage *storage = [FIRStorage storageForApp:secondApp];
  112. FIRStorageReference *storageRef = [storage referenceForURL:@"gs://bucket/path/to/object"];
  113. XCTAssertEqualObjects(storageRef.bucket, @"bucket");
  114. }
  115. - (void)testStorageWrongBucketInConfig {
  116. id mockOptions = OCMClassMock([FIROptions class]);
  117. OCMStub([mockOptions storageBucket]).andReturn(@"notMyBucket");
  118. id secondApp = [FIRStorageTestHelpers mockedApp];
  119. OCMStub([secondApp name]).andReturn(@"secondApp");
  120. OCMStub([(FIRApp *)secondApp options]).andReturn(mockOptions);
  121. FIRStorage *storage = [FIRStorage storageForApp:secondApp];
  122. XCTAssertEqualObjects([(FIRApp *)secondApp options].storageBucket, @"notMyBucket");
  123. XCTAssertThrows([storage referenceForURL:@"gs://bucket/path/to/object"]);
  124. }
  125. - (void)testUseEmulator {
  126. FIRStorage *storage = [FIRStorage storageForApp:self.app URL:@"gs://foo-bar.appspot.com"];
  127. [storage useEmulatorWithHost:@"localhost" port:8080];
  128. XCTAssertNoThrow([storage reference]);
  129. }
  130. - (void)testUseEmulatorValidatesHost {
  131. FIRStorage *storage = [FIRStorage storageForApp:self.app URL:@"gs://foo-bar.appspot.com"];
  132. XCTAssertThrows([storage useEmulatorWithHost:@"" port:8080]);
  133. }
  134. - (void)testUseEmulatorValidatesPort {
  135. FIRStorage *storage = [FIRStorage storageForApp:self.app URL:@"gs://foo-bar.appspot.com"];
  136. XCTAssertThrows([storage useEmulatorWithHost:@"localhost" port:-1]);
  137. }
  138. - (void)testUseEmulatorCannotBeCalledAfterObtainingReference {
  139. FIRStorage *storage = [FIRStorage storageForApp:self.app
  140. URL:@"gs://benwu-test1.storage.firebase.com"];
  141. [storage reference];
  142. XCTAssertThrows([storage useEmulatorWithHost:@"localhost" port:8080]);
  143. }
  144. - (void)testRefDefaultApp {
  145. FIRStorageReference *convenienceRef =
  146. [[FIRStorage storageForApp:self.app] referenceForURL:@"gs://bucket/path/to/object"];
  147. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:@"path/to/object"];
  148. FIRStorageReference *builtRef =
  149. [[FIRStorageReference alloc] initWithStorage:[FIRStorage storageForApp:self.app] path:path];
  150. XCTAssertEqualObjects([convenienceRef description], [builtRef description]);
  151. XCTAssertEqualObjects(convenienceRef.storage.app, builtRef.storage.app);
  152. }
  153. - (void)testRefCustomApp {
  154. id mockOptions = OCMClassMock([FIROptions class]);
  155. OCMStub([mockOptions storageBucket]).andReturn(@"bucket");
  156. id secondApp = [FIRStorageTestHelpers mockedApp];
  157. OCMStub([secondApp name]).andReturn(@"secondApp");
  158. OCMStub([(FIRApp *)secondApp options]).andReturn(mockOptions);
  159. FIRStorageReference *convenienceRef =
  160. [[FIRStorage storageForApp:secondApp] referenceForURL:@"gs://bucket/path/to/object"];
  161. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:@"path/to/object"];
  162. FIRStorageReference *builtRef =
  163. [[FIRStorageReference alloc] initWithStorage:[FIRStorage storageForApp:secondApp] path:path];
  164. XCTAssertEqualObjects([convenienceRef description], [builtRef description]);
  165. XCTAssertEqualObjects(convenienceRef.storage.app, builtRef.storage.app);
  166. }
  167. - (void)testRootRefDefaultApp {
  168. FIRStorageReference *convenienceRef = [[FIRStorage storageForApp:self.app] reference];
  169. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:nil];
  170. FIRStorageReference *builtRef =
  171. [[FIRStorageReference alloc] initWithStorage:[FIRStorage storageForApp:self.app] path:path];
  172. XCTAssertEqualObjects([convenienceRef description], [builtRef description]);
  173. XCTAssertEqualObjects(convenienceRef.storage.app, builtRef.storage.app);
  174. }
  175. - (void)testRefWithPathDefaultApp {
  176. FIRStorageReference *convenienceRef =
  177. [[FIRStorage storageForApp:self.app] referenceWithPath:@"path/to/object"];
  178. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:@"path/to/object"];
  179. FIRStorageReference *builtRef =
  180. [[FIRStorageReference alloc] initWithStorage:[FIRStorage storageForApp:self.app] path:path];
  181. XCTAssertEqualObjects([convenienceRef description], [builtRef description]);
  182. XCTAssertEqualObjects(convenienceRef.storage.app, builtRef.storage.app);
  183. }
  184. - (void)testEqual {
  185. FIRStorage *storage = [FIRStorage storageForApp:self.app];
  186. FIRStorage *copy = [storage copy];
  187. XCTAssertEqualObjects(storage.app.name, copy.app.name);
  188. }
  189. - (void)testNotEqual {
  190. FIRStorage *storage = [FIRStorage storageForApp:self.app];
  191. id mockOptions = OCMClassMock([FIROptions class]);
  192. OCMStub([mockOptions storageBucket]).andReturn(@"bucket");
  193. id secondApp = [FIRStorageTestHelpers mockedApp];
  194. OCMStub([secondApp name]).andReturn(@"secondApp");
  195. OCMStub([(FIRApp *)secondApp options]).andReturn(mockOptions);
  196. FIRStorage *secondStorage = [FIRStorage storageForApp:secondApp];
  197. XCTAssertNotEqualObjects(storage, secondStorage);
  198. }
  199. - (void)testHash {
  200. FIRStorage *storage = [FIRStorage storageForApp:self.app];
  201. FIRStorage *copy = [storage copy];
  202. XCTAssertEqual([storage hash], [copy hash]);
  203. }
  204. @end