Files
firebase-ios-sdk/Example/Storage/Tests/Integration/FIRStorageIntegrationTests.m
T

811 lines
31 KiB
Objective-C
Raw Normal View History

2017-05-15 12:27:07 -07:00
// Copyright 2017 Google
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import <XCTest/XCTest.h>
#import "FirebaseStorage.h"
#import <FirebaseCore/FIRApp.h>
#import <FirebaseCore/FIROptions.h>
2017-05-15 12:27:07 -07:00
2019-05-21 06:56:48 -07:00
NSTimeInterval kFIRStorageIntegrationTestTimeout = 60;
2017-05-15 12:27:07 -07:00
/**
* Firebase Storage Integration tests
*
* To run these tests, you need to define the following access rights for your Firebase App:
* - unauthentication read/write access to /ios/public
* - authentication read/write access to /ios/private
*
* A sample configuration may look like:
2017-06-30 10:47:37 -07:00
*
2019-06-13 12:01:29 -07:00
* rules_version = '2';
2017-05-15 12:27:07 -07:00
* service firebase.storage {
* match /b/{bucket}/o {
2017-05-15 12:27:07 -07:00
* ...
* match /ios {
* match /public/{allPaths=**} {
* allow read, write;
* }
* match /private/{allPaths=**} {
* allow none;
* }
* }
* }
* }
*
* You can define these access rights in the Firebase Console of your project.
*/
@interface FIRStorageIntegrationTests : XCTestCase
@property(strong, nonatomic) FIRApp *app;
@property(strong, nonatomic) FIRStorage *storage;
@end
@implementation FIRStorageIntegrationTests
+ (void)setUp {
2017-11-09 14:43:53 -08:00
[FIRApp configure];
2017-05-15 12:27:07 -07:00
}
- (void)setUp {
2017-11-09 14:43:53 -08:00
[super setUp];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
self.app = [FIRApp defaultApp];
self.storage = [FIRStorage storageForApp:self.app];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
static dispatch_once_t once;
dispatch_once(&once, ^{
2019-06-13 12:01:29 -07:00
XCTestExpectation *setUpExpectation = [self expectationWithDescription:@"setUp"];
2017-06-30 10:47:37 -07:00
2019-06-13 12:01:29 -07:00
NSArray<NSString *> *largeFiles = @[ @"ios/public/1mb" ];
NSArray<NSString *> *emptyFiles = @[
@"ios/public/empty", @"ios/public/list/a", @"ios/public/list/b", @"ios/public/list/prefix/c"
];
setUpExpectation.expectedFulfillmentCount = largeFiles.count + emptyFiles.count;
2019-05-21 06:56:48 -07:00
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"1mb" ofType:@"dat"];
if (filePath == nil) {
// Use bundleForClass to allow 1mb.dat to be in the test target's bundle.
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
filePath = [bundle pathForResource:@"1mb" ofType:@"dat"];
}
NSData *data = [NSData dataWithContentsOfFile:filePath];
2017-11-09 14:43:53 -08:00
XCTAssertNotNil(data, "Could not load bundled file");
2019-06-13 12:01:29 -07:00
for (NSString *largeFile in largeFiles) {
FIRStorageReference *file = [[FIRStorage storage].reference child:largeFile];
[file putData:data
metadata:nil
completion:^(FIRStorageMetadata *metadata, NSError *error) {
XCTAssertNil(error);
[setUpExpectation fulfill];
}];
}
for (NSString *emptyFile in emptyFiles) {
FIRStorageReference *file = [[FIRStorage storage].reference child:emptyFile];
[file putData:[NSData data]
metadata:nil
completion:^(FIRStorageMetadata *metadata, NSError *error) {
XCTAssertNil(error);
[setUpExpectation fulfill];
}];
}
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[self waitForExpectations];
});
2017-05-15 12:27:07 -07:00
}
- (void)tearDown {
2017-11-09 14:43:53 -08:00
self.app = nil;
self.storage = nil;
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[super tearDown];
2017-05-15 12:27:07 -07:00
}
- (void)testName {
2017-11-09 14:43:53 -08:00
NSString *aGSURI = [NSString
stringWithFormat:@"gs://%@.appspot.com/path/to", [[FIRApp defaultApp] options].projectID];
FIRStorageReference *ref = [self.storage referenceForURL:aGSURI];
XCTAssertEqualObjects(ref.description, aGSURI);
2017-05-15 12:27:07 -07:00
}
- (void)testUnauthenticatedGetMetadata {
2017-11-09 14:43:53 -08:00
XCTestExpectation *expectation =
2017-05-15 12:27:07 -07:00
[self expectationWithDescription:@"testUnauthenticatedGetMetadata"];
2017-11-09 14:43:53 -08:00
FIRStorageReference *ref = [self.storage.reference child:@"ios/public/1mb"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[ref metadataWithCompletion:^(FIRStorageMetadata *metadata, NSError *error) {
XCTAssertNotNil(metadata, "Metadata should not be nil");
XCTAssertNil(error, "Error should be nil");
[expectation fulfill];
}];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[self waitForExpectations];
2017-05-15 12:27:07 -07:00
}
- (void)testUnauthenticatedUpdateMetadata {
2017-11-09 14:43:53 -08:00
XCTestExpectation *expectation =
2017-05-15 12:27:07 -07:00
[self expectationWithDescription:@"testUnauthenticatedUpdateMetadata"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
FIRStorageMetadata *meta = [[FIRStorageMetadata alloc] init];
[meta setContentType:@"lol/custom"];
[meta setCustomMetadata:@{
@"lol" : @"custom metadata is neat",
@"ちかてつ" : @"🚇",
@"shinkansen" : @"新幹線"
}];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[ref updateMetadata:meta
completion:^(FIRStorageMetadata *metadata, NSError *error) {
XCTAssertEqualObjects(meta.contentType, metadata.contentType);
XCTAssertEqualObjects(meta.customMetadata[@"lol"], metadata.customMetadata[@"lol"]);
XCTAssertEqualObjects(meta.customMetadata[@"ちかてつ"],
metadata.customMetadata[@"ちかてつ"]);
XCTAssertEqualObjects(meta.customMetadata[@"shinkansen"],
metadata.customMetadata[@"shinkansen"]);
XCTAssertNil(error, "Error should be nil");
[expectation fulfill];
}];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[self waitForExpectations];
2017-05-15 12:27:07 -07:00
}
- (void)testUnauthenticatedDelete {
2017-11-09 14:43:53 -08:00
XCTestExpectation *expectation = [self expectationWithDescription:@"testUnauthenticatedDelete"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/fileToDelete"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
NSData *data = [@"Delete me!!!!!!!" dataUsingEncoding:NSUTF8StringEncoding];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[ref putData:data
2017-05-15 12:27:07 -07:00
metadata:nil
completion:^(FIRStorageMetadata *metadata, NSError *error) {
2017-11-09 14:43:53 -08:00
XCTAssertNotNil(metadata, "Metadata should not be nil");
XCTAssertNil(error, "Error should be nil");
[ref deleteWithCompletion:^(NSError *error) {
2017-05-15 12:27:07 -07:00
XCTAssertNil(error, "Error should be nil");
2017-11-09 14:43:53 -08:00
[expectation fulfill];
}];
2017-05-15 12:27:07 -07:00
}];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[self waitForExpectations];
2017-05-15 12:27:07 -07:00
}
- (void)testDeleteWithNilCompletion {
2017-11-09 14:43:53 -08:00
XCTestExpectation *expectation = [self expectationWithDescription:@"testDeleteWithNilCompletion"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/fileToDelete"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
NSData *data = [@"Delete me!!!!!!!" dataUsingEncoding:NSUTF8StringEncoding];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[ref putData:data
2017-05-15 12:27:07 -07:00
metadata:nil
completion:^(FIRStorageMetadata *metadata, NSError *error) {
2017-11-09 14:43:53 -08:00
XCTAssertNotNil(metadata, "Metadata should not be nil");
XCTAssertNil(error, "Error should be nil");
[ref deleteWithCompletion:nil];
[expectation fulfill];
2017-05-15 12:27:07 -07:00
}];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[self waitForExpectations];
2017-05-15 12:27:07 -07:00
}
- (void)testUnauthenticatedSimplePutData {
2017-11-09 14:43:53 -08:00
XCTestExpectation *expectation =
[self expectationWithDescription:@"testUnauthenticatedSimplePutData"];
FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/testBytesUpload"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
2017-06-30 10:47:37 -07:00
2018-10-23 16:53:13 -07:00
[ref putData:data
metadata:nil
completion:^(FIRStorageMetadata *metadata, NSError *error) {
XCTAssertNotNil(metadata, "Metadata should not be nil");
XCTAssertNil(error, "Error should be nil");
[expectation fulfill];
}];
[self waitForExpectations];
}
- (void)testUnauthenticatedSimplePutSpecialCharacter {
XCTestExpectation *expectation =
[self expectationWithDescription:@"testUnauthenticatedSimplePutDataEscapedName"];
FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/-._~!$'()*,=:@&+;"];
NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
2017-11-09 14:43:53 -08:00
[ref putData:data
2017-05-15 12:27:07 -07:00
metadata:nil
completion:^(FIRStorageMetadata *metadata, NSError *error) {
2017-11-09 14:43:53 -08:00
XCTAssertNotNil(metadata, "Metadata should not be nil");
XCTAssertNil(error, "Error should be nil");
[expectation fulfill];
2017-05-15 12:27:07 -07:00
}];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[self waitForExpectations];
2017-05-15 12:27:07 -07:00
}
- (void)testUnauthenticatedSimplePutDataInBackgroundQueue {
XCTestExpectation *expectation =
[self expectationWithDescription:@"testUnauthenticatedSimplePutDataInBackgroundQueue"];
FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/testBytesUpload"];
NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[ref putData:data
metadata:nil
completion:^(FIRStorageMetadata *metadata, NSError *error) {
XCTAssertNotNil(metadata, "Metadata should not be nil");
XCTAssertNil(error, "Error should be nil");
[expectation fulfill];
}];
});
[self waitForExpectations];
}
2017-05-15 12:27:07 -07:00
- (void)testUnauthenticatedSimplePutEmptyData {
2017-11-09 14:43:53 -08:00
XCTestExpectation *expectation =
2017-05-15 12:27:07 -07:00
[self expectationWithDescription:@"testUnauthenticatedSimplePutEmptyData"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
FIRStorageReference *ref =
2017-05-15 12:27:07 -07:00
[self.storage referenceWithPath:@"ios/public/testUnauthenticatedSimplePutEmptyData"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
NSData *data = [[NSData alloc] init];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[ref putData:data
2017-05-15 12:27:07 -07:00
metadata:nil
completion:^(FIRStorageMetadata *metadata, NSError *error) {
2017-11-09 14:43:53 -08:00
XCTAssertNotNil(metadata, "Metadata should not be nil");
XCTAssertNil(error, "Error should be nil");
[expectation fulfill];
2017-05-15 12:27:07 -07:00
}];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[self waitForExpectations];
2017-05-15 12:27:07 -07:00
}
- (void)testUnauthenticatedSimplePutDataUnauthorized {
2017-11-09 14:43:53 -08:00
XCTestExpectation *expectation =
2017-05-15 12:27:07 -07:00
[self expectationWithDescription:@"testUnauthenticatedSimplePutDataUnauthorized"];
2017-11-09 14:43:53 -08:00
FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/private/secretfile.txt"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[ref putData:data
2017-05-15 12:27:07 -07:00
metadata:nil
completion:^(FIRStorageMetadata *metadata, NSError *error) {
2017-11-09 14:43:53 -08:00
XCTAssertNil(metadata, "Metadata should be nil");
XCTAssertNotNil(error, "Error should not be nil");
XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthorized);
[expectation fulfill];
2017-05-15 12:27:07 -07:00
}];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[self waitForExpectations];
2017-05-15 12:27:07 -07:00
}
- (void)testUnauthenticatedSimplePutFile {
2017-11-09 14:43:53 -08:00
XCTestExpectation *expectation =
2017-05-15 12:27:07 -07:00
[self expectationWithDescription:@"testUnauthenticatedSimplePutFile"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
FIRStorageReference *ref =
2017-05-15 12:27:07 -07:00
[self.storage referenceWithPath:@"ios/public/testUnauthenticatedSimplePutFile"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
NSURL *fileURL =
[[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"txt"];
[data writeToURL:fileURL atomically:YES];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
FIRStorageUploadTask *task = [ref putFile:fileURL
metadata:nil
completion:^(FIRStorageMetadata *metadata, NSError *error) {
XCTAssertNotNil(metadata, "Metadata should not be nil");
XCTAssertNil(error, "Error should be nil");
}];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
__block long uploadedBytes = -1;
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[task observeStatus:FIRStorageTaskStatusSuccess
handler:^(FIRStorageTaskSnapshot *snapshot) {
XCTAssertEqualObjects([snapshot description], @"<State: Success>");
[expectation fulfill];
}];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[task observeStatus:FIRStorageTaskStatusProgress
handler:^(FIRStorageTaskSnapshot *_Nonnull snapshot) {
XCTAssertTrue([[snapshot description] containsString:@"State: Progress"] ||
[[snapshot description] containsString:@"State: Resume"]);
NSProgress *progress = snapshot.progress;
XCTAssertGreaterThanOrEqual(progress.completedUnitCount, uploadedBytes);
2019-05-21 06:56:48 -07:00
uploadedBytes = (long)progress.completedUnitCount;
2017-11-09 14:43:53 -08:00
}];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[self waitForExpectations];
2017-05-15 12:27:07 -07:00
}
- (void)testPutFileWithSpecialCharacters {
2017-11-09 14:43:53 -08:00
XCTestExpectation *expectation =
2017-05-15 12:27:07 -07:00
[self expectationWithDescription:@"testPutFileWithSpecialCharacters"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
NSString *fileName = @"hello&+@_ .txt";
FIRStorageReference *ref =
[self.storage referenceWithPath:[@"ios/public/" stringByAppendingString:fileName]];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
NSURL *fileURL = [tmpDirURL URLByAppendingPathComponent:fileName];
[data writeToURL:fileURL atomically:YES];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[ref putFile:fileURL
2017-05-15 12:27:07 -07:00
metadata:nil
completion:^(FIRStorageMetadata *metadata, NSError *error) {
2017-11-09 14:43:53 -08:00
XCTAssertNotNil(metadata, "Metadata should not be nil");
XCTAssertNil(error, "Error should be nil");
XCTAssertEqualObjects(fileName, metadata.name);
FIRStorageReference *download =
[self.storage referenceWithPath:[@"ios/public/" stringByAppendingString:fileName]];
[download metadataWithCompletion:^(FIRStorageMetadata *metadata, NSError *error) {
2017-05-15 12:27:07 -07:00
XCTAssertNotNil(metadata, "Metadata should not be nil");
XCTAssertNil(error, "Error should be nil");
XCTAssertEqualObjects(fileName, metadata.name);
2017-11-09 14:43:53 -08:00
[expectation fulfill];
}];
2017-05-15 12:27:07 -07:00
}];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[self waitForExpectations];
2017-05-15 12:27:07 -07:00
}
- (void)testUnauthenticatedSimplePutDataNoMetadata {
2017-11-09 14:43:53 -08:00
XCTestExpectation *expectation =
2017-05-15 12:27:07 -07:00
[self expectationWithDescription:@"testUnauthenticatedSimplePutDataNoMetadata"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
FIRStorageReference *ref =
2017-05-15 12:27:07 -07:00
[self.storage referenceWithPath:@"ios/public/testUnauthenticatedSimplePutDataNoMetadata"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[ref putData:data
2017-05-15 12:27:07 -07:00
metadata:nil
completion:^(FIRStorageMetadata *metadata, NSError *error) {
2017-11-09 14:43:53 -08:00
XCTAssertNotNil(metadata, "Metadata should not be nil");
XCTAssertNil(error, "Error should be nil");
[expectation fulfill];
2017-05-15 12:27:07 -07:00
}];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[self waitForExpectations];
2017-05-15 12:27:07 -07:00
}
- (void)testUnauthenticatedSimplePutFileNoMetadata {
2017-11-09 14:43:53 -08:00
XCTestExpectation *expectation =
2017-05-15 12:27:07 -07:00
[self expectationWithDescription:@"testUnauthenticatedSimplePutFileNoMetadata"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
FIRStorageReference *ref =
2017-05-15 12:27:07 -07:00
[self.storage referenceWithPath:@"ios/public/testUnauthenticatedSimplePutFileNoMetadata"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
NSURL *fileURL =
[[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"txt"];
[data writeToURL:fileURL atomically:YES];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[ref putFile:fileURL
2017-05-15 12:27:07 -07:00
metadata:nil
completion:^(FIRStorageMetadata *metadata, NSError *error) {
2017-11-09 14:43:53 -08:00
XCTAssertNotNil(metadata, "Metadata should not be nil");
XCTAssertNil(error, "Error should be nil");
[expectation fulfill];
2017-05-15 12:27:07 -07:00
}];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[self waitForExpectations];
2017-05-15 12:27:07 -07:00
}
- (void)testUnauthenticatedSimpleGetData {
2017-11-09 14:43:53 -08:00
XCTestExpectation *expectation =
2017-05-15 12:27:07 -07:00
[self expectationWithDescription:@"testUnauthenticatedSimpleGetData"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[ref dataWithMaxSize:1 * 1024 * 1024
completion:^(NSData *data, NSError *error) {
XCTAssertNotNil(data, "Data should not be nil");
XCTAssertNil(error, "Error should be nil");
[expectation fulfill];
}];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[self waitForExpectations];
2017-05-15 12:27:07 -07:00
}
- (void)testUnauthenticatedSimpleGetDataInBackgroundQueue {
XCTestExpectation *expectation =
[self expectationWithDescription:@"testUnauthenticatedSimpleGetDataInBackgroundQueue"];
FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[ref dataWithMaxSize:1 * 1024 * 1024
completion:^(NSData *data, NSError *error) {
XCTAssertNotNil(data, "Data should not be nil");
XCTAssertNil(error, "Error should be nil");
[expectation fulfill];
}];
});
[self waitForExpectations];
}
2017-05-15 12:27:07 -07:00
- (void)testUnauthenticatedSimpleGetDataTooSmall {
2017-11-09 14:43:53 -08:00
XCTestExpectation *expectation =
2017-05-15 12:27:07 -07:00
[self expectationWithDescription:@"testUnauthenticatedSimpleGetDataTooSmall"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
/// Only allow 1kB size, which is smaller than our file
[ref dataWithMaxSize:1 * 1024
completion:^(NSData *data, NSError *error) {
2018-04-11 10:38:31 -07:00
XCTAssertNil(data);
2017-11-09 14:43:53 -08:00
XCTAssertEqual(error.code, FIRStorageErrorCodeDownloadSizeExceeded);
[expectation fulfill];
}];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[self waitForExpectations];
2017-05-15 12:27:07 -07:00
}
2018-04-09 14:21:00 -07:00
- (void)testUnauthenticatedSimpleGetDownloadURL {
XCTestExpectation *expectation =
[self expectationWithDescription:@"testUnauthenticatedSimpleGetDownloadURL"];
FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
2018-04-13 12:17:36 -07:00
// Download URL format is
// "https://firebasestorage.googleapis.com/v0/b/{bucket}/o/{path}?alt=media&token={token}"
NSString *downloadURLPattern =
@"^https:\\/\\/firebasestorage.googleapis.com\\/v0\\/b\\/[^\\/]*\\/o\\/"
@"ios%2Fpublic%2F1mb\\?alt=media&token=[a-z0-9-]*$";
2018-04-09 14:21:00 -07:00
[ref downloadURLWithCompletion:^(NSURL *downloadURL, NSError *error) {
XCTAssertNil(error);
2018-04-13 12:17:36 -07:00
NSRegularExpression *testRegex =
[NSRegularExpression regularExpressionWithPattern:downloadURLPattern options:0 error:nil];
NSString *urlString = [downloadURL absoluteString];
XCTAssertEqual([testRegex numberOfMatchesInString:urlString
options:0
range:NSMakeRange(0, [urlString length])],
1);
2018-04-09 14:21:00 -07:00
[expectation fulfill];
}];
[self waitForExpectations];
}
2017-05-15 12:27:07 -07:00
- (void)testUnauthenticatedSimpleGetFile {
2017-11-09 14:43:53 -08:00
XCTestExpectation *expectation =
2017-05-15 12:27:07 -07:00
[self expectationWithDescription:@"testUnauthenticatedSimpleGetData"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/helloworld"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
NSURL *fileURL =
2017-05-15 12:27:07 -07:00
[[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"txt"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[ref putData:[@"Hello World" dataUsingEncoding:NSUTF8StringEncoding]
metadata:nil
completion:^(FIRStorageMetadata *metadata, NSError *error) {
2017-05-15 12:27:07 -07:00
FIRStorageDownloadTask *task = [ref writeToFile:fileURL];
2017-06-30 10:47:37 -07:00
2017-05-15 12:27:07 -07:00
[task observeStatus:FIRStorageTaskStatusSuccess
handler:^(FIRStorageTaskSnapshot *snapshot) {
2017-11-09 14:43:53 -08:00
NSString *data = [NSString stringWithContentsOfURL:fileURL
encoding:NSUTF8StringEncoding
error:NULL];
NSString *expectedData = @"Hello World";
XCTAssertEqualObjects(data, expectedData);
XCTAssertEqualObjects([snapshot description], @"<State: Success>");
[expectation fulfill];
2017-05-15 12:27:07 -07:00
}];
2017-06-30 10:47:37 -07:00
2017-05-15 12:27:07 -07:00
[task observeStatus:FIRStorageTaskStatusProgress
handler:^(FIRStorageTaskSnapshot *_Nonnull snapshot) {
2017-11-09 14:43:53 -08:00
NSProgress *progress = snapshot.progress;
NSLog(@"%lld of %lld", progress.completedUnitCount, progress.totalUnitCount);
2017-05-15 12:27:07 -07:00
}];
2017-06-30 10:47:37 -07:00
2017-05-15 12:27:07 -07:00
[task observeStatus:FIRStorageTaskStatusFailure
handler:^(FIRStorageTaskSnapshot *snapshot) {
2017-11-09 14:43:53 -08:00
XCTAssertNil(snapshot.error);
[expectation fulfill];
2017-05-15 12:27:07 -07:00
}];
2017-11-09 14:43:53 -08:00
}];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[self waitForExpectations];
2017-05-15 12:27:07 -07:00
}
- (void)testCancelDownload {
2017-11-09 14:43:53 -08:00
XCTestExpectation *expectation = [self expectationWithDescription:@"testCancelDownload"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
NSURL *fileURL =
[[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"dat"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
FIRStorageDownloadTask *task = [ref writeToFile:fileURL];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[task observeStatus:FIRStorageTaskStatusFailure
handler:^(FIRStorageTaskSnapshot *snapshot) {
XCTAssertTrue([[snapshot description] containsString:@"State: Failed"]);
[expectation fulfill];
}];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[task observeStatus:FIRStorageTaskStatusProgress
handler:^(FIRStorageTaskSnapshot *_Nonnull snapshot) {
[task cancel];
}];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[self waitForExpectations];
2017-05-15 12:27:07 -07:00
}
2017-11-09 14:43:53 -08:00
- (void)assertMetadata:(FIRStorageMetadata *)actualMetadata
contentType:(NSString *)expectedContentType
customMetadata:(NSDictionary *)expectedCustomMetadata {
XCTAssertEqualObjects(actualMetadata.cacheControl, @"cache-control");
XCTAssertEqualObjects(actualMetadata.contentDisposition, @"content-disposition");
XCTAssertEqualObjects(actualMetadata.contentEncoding, @"gzip");
XCTAssertEqualObjects(actualMetadata.contentLanguage, @"de");
XCTAssertEqualObjects(actualMetadata.contentType, expectedContentType);
2017-11-09 14:40:36 -08:00
XCTAssertTrue([actualMetadata.md5Hash length] == 24);
2017-11-09 14:43:53 -08:00
for (NSString *key in expectedCustomMetadata) {
XCTAssertEqualObjects([actualMetadata.customMetadata objectForKey:key],
[expectedCustomMetadata objectForKey:key]);
}
}
2017-11-09 14:43:53 -08:00
- (void)assertMetadataNil:(FIRStorageMetadata *)actualMetadata {
XCTAssertNil(actualMetadata.cacheControl);
XCTAssertNil(actualMetadata.contentDisposition);
XCTAssertEqualObjects(actualMetadata.contentEncoding, @"identity");
XCTAssertNil(actualMetadata.contentLanguage);
XCTAssertNil(actualMetadata.contentType);
2017-11-09 14:40:36 -08:00
XCTAssertTrue([actualMetadata.md5Hash length] == 24);
2017-11-09 14:43:53 -08:00
XCTAssertNil([actualMetadata.customMetadata objectForKey:@"a"]);
XCTAssertNil([actualMetadata.customMetadata objectForKey:@"c"]);
XCTAssertNil([actualMetadata.customMetadata objectForKey:@"f"]);
}
- (void)testUpdateMetadata {
XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateMetadata"];
FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
// Update all available metadata
FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] init];
metadata.cacheControl = @"cache-control";
metadata.contentDisposition = @"content-disposition";
metadata.contentEncoding = @"gzip";
metadata.contentLanguage = @"de";
metadata.contentType = @"content-type-a";
metadata.customMetadata = @{@"a" : @"b"};
2017-11-09 14:43:53 -08:00
[ref updateMetadata:metadata
completion:^(FIRStorageMetadata *updatedMetadata, NSError *error) {
XCTAssertNil(error);
[self assertMetadata:updatedMetadata
contentType:@"content-type-a"
customMetadata:@{@"a" : @"b"}];
2017-11-09 14:43:53 -08:00
// Update a subset of the metadata using the existing object.
FIRStorageMetadata *metadata = updatedMetadata;
metadata.contentType = @"content-type-b";
metadata.customMetadata = @{@"a" : @"b", @"c" : @"d"};
2017-11-09 14:43:53 -08:00
[ref updateMetadata:metadata
completion:^(FIRStorageMetadata *updatedMetadata, NSError *error) {
XCTAssertNil(error);
[self assertMetadata:updatedMetadata
contentType:@"content-type-b"
customMetadata:@{@"a" : @"b", @"c" : @"d"}];
2017-11-09 14:43:53 -08:00
// Clear all metadata.
FIRStorageMetadata *metadata = updatedMetadata;
metadata.cacheControl = nil;
metadata.contentDisposition = nil;
metadata.contentEncoding = nil;
metadata.contentLanguage = nil;
metadata.contentType = nil;
metadata.customMetadata = [NSDictionary dictionary];
2017-11-09 14:43:53 -08:00
[ref updateMetadata:metadata
completion:^(FIRStorageMetadata *updatedMetadata, NSError *error) {
XCTAssertNil(error);
[self assertMetadataNil:updatedMetadata];
[expectation fulfill];
}];
}];
}];
[self waitForExpectations];
}
2017-05-15 12:27:07 -07:00
- (void)testUnauthenticatedResumeGetFile {
2017-11-09 14:43:53 -08:00
XCTestExpectation *expectation =
2017-05-15 12:27:07 -07:00
[self expectationWithDescription:@"testUnauthenticatedResumeGetFile"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
NSURL *fileURL =
[[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"txt"];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
__block long resumeAtBytes = 256 * 1024;
__block long downloadedBytes = 0;
__block double computationResult = 0;
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
FIRStorageDownloadTask *task = [ref writeToFile:fileURL];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[task observeStatus:FIRStorageTaskStatusSuccess
handler:^(FIRStorageTaskSnapshot *snapshot) {
XCTAssertEqualObjects([snapshot description], @"<State: Success>");
[expectation fulfill];
}];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[task observeStatus:FIRStorageTaskStatusProgress
handler:^(FIRStorageTaskSnapshot *_Nonnull snapshot) {
XCTAssertTrue([[snapshot description] containsString:@"State: Progress"] ||
[[snapshot description] containsString:@"State: Resume"]);
NSProgress *progress = snapshot.progress;
XCTAssertGreaterThanOrEqual(progress.completedUnitCount, downloadedBytes);
2019-05-21 06:56:48 -07:00
downloadedBytes = (long)progress.completedUnitCount;
2017-11-09 14:43:53 -08:00
if (progress.completedUnitCount > resumeAtBytes) {
// Making sure the main run loop is busy.
for (int i = 0; i < 500; ++i) {
dispatch_async(dispatch_get_main_queue(), ^{
computationResult = sqrt(INT_MAX - i);
});
}
NSLog(@"Pausing");
[task pause];
resumeAtBytes = INT_MAX;
}
}];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[task observeStatus:FIRStorageTaskStatusPause
handler:^(FIRStorageTaskSnapshot *snapshot) {
XCTAssertEqualObjects([snapshot description], @"<State: Paused>");
NSLog(@"Resuming");
[task resume];
}];
2017-06-30 10:47:37 -07:00
2017-11-09 14:43:53 -08:00
[self waitForExpectations];
XCTAssertEqual(INT_MAX, resumeAtBytes);
XCTAssertEqualWithAccuracy(sqrt(INT_MAX - 499), computationResult, 0.1);
2017-05-15 12:27:07 -07:00
}
- (void)testUnauthenticatedResumeGetFileInBackgroundQueue {
XCTestExpectation *expectation =
[self expectationWithDescription:@"testUnauthenticatedResumeGetFileInBackgroundQueue"];
FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
NSURL *fileURL =
[[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"txt"];
__block long resumeAtBytes = 256 * 1024;
__block long downloadedBytes = 0;
FIRStorageDownloadTask *task = [ref writeToFile:fileURL];
[task observeStatus:FIRStorageTaskStatusSuccess
handler:^(FIRStorageTaskSnapshot *snapshot) {
XCTAssertEqualObjects([snapshot description], @"<State: Success>");
[expectation fulfill];
}];
[task observeStatus:FIRStorageTaskStatusProgress
handler:^(FIRStorageTaskSnapshot *_Nonnull snapshot) {
XCTAssertTrue([[snapshot description] containsString:@"State: Progress"] ||
[[snapshot description] containsString:@"State: Resume"]);
NSProgress *progress = snapshot.progress;
XCTAssertGreaterThanOrEqual(progress.completedUnitCount, downloadedBytes);
2019-05-21 06:56:48 -07:00
downloadedBytes = (long)progress.completedUnitCount;
if (progress.completedUnitCount > resumeAtBytes) {
NSLog(@"Pausing");
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[task pause];
});
resumeAtBytes = INT_MAX;
}
}];
[task observeStatus:FIRStorageTaskStatusPause
handler:^(FIRStorageTaskSnapshot *snapshot) {
XCTAssertEqualObjects([snapshot description], @"<State: Paused>");
NSLog(@"Resuming");
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[task resume];
});
}];
[self waitForExpectations];
XCTAssertEqual(INT_MAX, resumeAtBytes);
}
2019-06-13 12:01:29 -07:00
- (void)testPagedListFiles {
XCTestExpectation *expectation = [self expectationWithDescription:@"testPagedListFiles"];
FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/list"];
[ref listWithMaxResults:2
completion:^(FIRStorageListResult *_Nullable listResult, NSError *_Nullable error) {
XCTAssertNotNil(listResult);
XCTAssertNil(error);
XCTAssertEqualObjects(listResult.items, (@[ [ref child:@"a"], [ref child:@"b"] ]));
XCTAssertEqualObjects(listResult.prefixes, @[]);
XCTAssertNotNil(listResult.pageToken);
[ref listWithMaxResults:2
pageToken:listResult.pageToken
completion:^(FIRStorageListResult *_Nullable listResult,
NSError *_Nullable error) {
XCTAssertNotNil(listResult);
XCTAssertNil(error);
XCTAssertEqualObjects(listResult.items, @[]);
XCTAssertEqualObjects(listResult.prefixes,
@[ [ref child:@"prefix"] ]);
XCTAssertNil(listResult.pageToken);
[expectation fulfill];
}];
}];
[self waitForExpectations];
}
- (void)testListAllFiles {
XCTestExpectation *expectation = [self expectationWithDescription:@"testPagedListFiles"];
FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/list"];
[ref listAllWithCompletion:^(FIRStorageListResult *_Nullable listResult,
NSError *_Nullable error) {
XCTAssertNotNil(listResult);
XCTAssertNil(error);
XCTAssertEqualObjects(listResult.items, (@[ [ref child:@"a"], [ref child:@"b"] ]));
XCTAssertEqualObjects(listResult.prefixes, @[ [ref child:@"prefix"] ]);
XCTAssertNil(listResult.pageToken);
[expectation fulfill];
}];
[self waitForExpectations];
}
2017-05-15 12:27:07 -07:00
- (void)waitForExpectations {
2017-11-09 14:43:53 -08:00
[self waitForExpectationsWithTimeout:kFIRStorageIntegrationTestTimeout
handler:^(NSError *_Nullable error) {
if (error) {
NSLog(@"%@", error);
}
}];
2017-05-15 12:27:07 -07:00
}
@end