FIRStorageReferenceTests.m 8.1 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 "FirebaseStorageInternal/Sources/Public/FirebaseStorageInternal/FIRStorage.h"
  15. //#import "FirebaseStorageInternal/Sources/FIRStorageComponent.h"
  16. #import "FirebaseStorageInternal/Sources/FIRStorageReference_Private.h"
  17. #import "FirebaseStorageInternal/Tests/Unit/FIRStorageTestHelpers.h"
  18. #import "SharedTestUtilities/FIRComponentTestUtilities.h"
  19. @interface FIRIMPLStorageReferenceTests : XCTestCase
  20. @property(strong, nonatomic) FIRIMPLStorage *storage;
  21. @end
  22. @implementation FIRIMPLStorageReferenceTests
  23. - (void)setUp {
  24. [super setUp];
  25. self.storage = [FIRStorageTestHelpers storageWithMockedApp];
  26. }
  27. - (void)tearDown {
  28. self.storage = nil;
  29. [super tearDown];
  30. }
  31. - (void)testRoot {
  32. FIRIMPLStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/path/to/object"];
  33. XCTAssertEqualObjects([ref.root stringValue], @"gs://bucket/");
  34. }
  35. - (void)testRootWithNoPath {
  36. FIRIMPLStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/"];
  37. XCTAssertEqualObjects([ref.root stringValue], @"gs://bucket/");
  38. }
  39. - (void)testSingleChild {
  40. FIRIMPLStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/"];
  41. FIRIMPLStorageReference *childRef = [ref child:@"path"];
  42. XCTAssertEqualObjects([childRef stringValue], @"gs://bucket/path");
  43. }
  44. - (void)testMultipleChildrenSingleString {
  45. FIRIMPLStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/"];
  46. FIRIMPLStorageReference *childRef = [ref child:@"path/to/object"];
  47. XCTAssertEqualObjects([childRef stringValue], @"gs://bucket/path/to/object");
  48. }
  49. - (void)testMultipleChildrenMultipleStrings {
  50. FIRIMPLStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/"];
  51. FIRIMPLStorageReference *childRef = [ref child:@"path"];
  52. childRef = [childRef child:@"to"];
  53. childRef = [childRef child:@"object"];
  54. XCTAssertEqualObjects([childRef stringValue], @"gs://bucket/path/to/object");
  55. }
  56. - (void)testSameChildDifferentRef {
  57. FIRIMPLStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/"];
  58. FIRIMPLStorageReference *firstRef = [ref child:@"1"];
  59. FIRIMPLStorageReference *secondRef = [ref child:@"1"];
  60. XCTAssertEqualObjects([ref stringValue], @"gs://bucket/");
  61. XCTAssertEqualObjects(firstRef, secondRef);
  62. XCTAssertNotEqual(firstRef, secondRef);
  63. }
  64. - (void)testDifferentChildDifferentRef {
  65. FIRIMPLStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/"];
  66. FIRIMPLStorageReference *firstRef = [ref child:@"1"];
  67. FIRIMPLStorageReference *secondRef = [ref child:@"2"];
  68. XCTAssertEqualObjects([ref stringValue], @"gs://bucket/");
  69. XCTAssertNotEqual(firstRef, secondRef);
  70. }
  71. - (void)testChildWithTrailingSlash {
  72. FIRIMPLStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/path/to/object/"];
  73. XCTAssertEqualObjects([ref stringValue], @"gs://bucket/path/to/object");
  74. }
  75. - (void)testChildWithLeadingSlash {
  76. FIRIMPLStorageReference *ref = [self.storage referenceForURL:@"gs://bucket//path/to/object/"];
  77. XCTAssertEqualObjects([ref stringValue], @"gs://bucket/path/to/object");
  78. }
  79. - (void)testChildCompressSlashes {
  80. FIRIMPLStorageReference *ref =
  81. [self.storage referenceForURL:@"gs://bucket//path///to////object////"];
  82. XCTAssertEqualObjects([ref stringValue], @"gs://bucket/path/to/object");
  83. }
  84. - (void)testParent {
  85. FIRIMPLStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/path/to/object"];
  86. FIRIMPLStorageReference *parentRef = [ref parent];
  87. XCTAssertEqualObjects([parentRef stringValue], @"gs://bucket/path/to");
  88. }
  89. - (void)testParentToRoot {
  90. FIRIMPLStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/path"];
  91. FIRIMPLStorageReference *parentRef = [ref parent];
  92. XCTAssertEqualObjects([parentRef stringValue], @"gs://bucket/");
  93. }
  94. - (void)testParentToRootTrailingSlash {
  95. FIRIMPLStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/path/"];
  96. FIRIMPLStorageReference *parentRef = [ref parent];
  97. XCTAssertEqualObjects([parentRef stringValue], @"gs://bucket/");
  98. }
  99. - (void)testParentAtRoot {
  100. FIRIMPLStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/"];
  101. FIRIMPLStorageReference *parentRef = [ref parent];
  102. XCTAssertNil(parentRef);
  103. }
  104. - (void)testBucket {
  105. FIRIMPLStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/path/to/object"];
  106. XCTAssertEqualObjects(ref.bucket, @"bucket");
  107. }
  108. - (void)testName {
  109. FIRIMPLStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/path/to/object"];
  110. XCTAssertEqualObjects(ref.name, @"object");
  111. }
  112. - (void)testNameNoObject {
  113. FIRIMPLStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/"];
  114. XCTAssertEqualObjects(ref.name, @"");
  115. }
  116. - (void)testFullPath {
  117. FIRIMPLStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/path/to/object"];
  118. XCTAssertEqualObjects(ref.fullPath, @"path/to/object");
  119. }
  120. - (void)testFullPathNoObject {
  121. FIRIMPLStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/"];
  122. XCTAssertEqualObjects(ref.fullPath, @"");
  123. }
  124. - (void)testCopy {
  125. FIRIMPLStorageReference *ref = [self.storage referenceForURL:@"gs://bucket/"];
  126. FIRIMPLStorageReference *copiedRef = [ref copy];
  127. XCTAssertEqualObjects(ref, copiedRef);
  128. XCTAssertNotEqual(ref, copiedRef);
  129. }
  130. - (void)testReferenceWithNonExistentFileFailsWithCompletion {
  131. NSString *tempFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"temp.data"];
  132. FIRIMPLStorageReference *ref = [self.storage referenceWithPath:tempFilePath];
  133. NSURL *dummyFileURL = [NSURL fileURLWithPath:@"some_non_existing-folder/file.data"];
  134. XCTestExpectation *expectation = [self expectationWithDescription:@"completionExpectation"];
  135. [ref putFile:dummyFileURL
  136. metadata:nil
  137. completion:^(FIRIMPLStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  138. [expectation fulfill];
  139. XCTAssertNotNil(error);
  140. XCTAssertNil(metadata);
  141. XCTAssertEqualObjects(error.domain, FIRStorageErrorDomainInternal);
  142. XCTAssertEqual(error.code, FIRIMPLStorageErrorCodeUnknown);
  143. NSString *expectedDescription = [NSString
  144. stringWithFormat:@"File at URL: %@ is not reachable. "
  145. @"Ensure file URL is not a directory, symbolic link, or invalid url.",
  146. dummyFileURL.absoluteString];
  147. XCTAssertEqualObjects(error.localizedDescription, expectedDescription);
  148. }];
  149. [self waitForExpectationsWithTimeout:0.5 handler:NULL];
  150. }
  151. - (void)testReferenceWithNilFileURLFailsWithCompletion {
  152. NSString *tempFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"temp.data"];
  153. FIRIMPLStorageReference *ref = [self.storage referenceWithPath:tempFilePath];
  154. NSURL *dummyFileURL = [NSURL URLWithString:@"bad-url"];
  155. XCTestExpectation *expectation = [self expectationWithDescription:@"completionExpectation"];
  156. [ref putFile:dummyFileURL
  157. metadata:nil
  158. completion:^(FIRIMPLStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  159. [expectation fulfill];
  160. XCTAssertNotNil(error);
  161. XCTAssertNil(metadata);
  162. XCTAssertEqualObjects(error.domain, FIRStorageErrorDomainInternal);
  163. XCTAssertEqual(error.code, FIRIMPLStorageErrorCodeUnknown);
  164. NSString *expectedDescription = [NSString
  165. stringWithFormat:@"File at URL: %@ is not reachable. "
  166. @"Ensure file URL is not a directory, symbolic link, or invalid url.",
  167. dummyFileURL.absoluteString];
  168. XCTAssertEqualObjects(error.localizedDescription, expectedDescription);
  169. }];
  170. [self waitForExpectationsWithTimeout:0.5 handler:NULL];
  171. }
  172. @end