summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicoleYarroch <nicole@livio.io>2017-08-02 16:52:26 -0400
committerNicoleYarroch <nicole@livio.io>2017-08-02 16:52:26 -0400
commit3de70af62573df871afeaed902d60efc4cb0c83e (patch)
tree9bad8b53a2a394f1d89dbb8a260d91103f6a8cdc
parent995d19ddd7279d7f59704411bfc34c9b889b2364 (diff)
downloadsdl_ios-feature/issue_536_input_stream_file_manager.tar.gz
- Class now returns an error if user passes an invalid file path url or if they pass invalid or empty data.feature/issue_536_input_stream_file_manager
- Added test cases for checking invalid file paths and invalid data - Made some fixes to the highestCorrelationIDReceived() method Signed-off-by: NicoleYarroch <nicole@livio.io>
-rw-r--r--SmartDeviceLink/SDLError.h1
-rw-r--r--SmartDeviceLink/SDLError.m11
-rw-r--r--SmartDeviceLink/SDLErrorConstants.h4
-rw-r--r--SmartDeviceLink/SDLUploadFileOperation.m14
-rw-r--r--SmartDeviceLinkTests/DevAPISpecs/SDLUploadFileOperationSpec.m55
5 files changed, 77 insertions, 8 deletions
diff --git a/SmartDeviceLink/SDLError.h b/SmartDeviceLink/SDLError.h
index 09ec20ee8..717fb7ac5 100644
--- a/SmartDeviceLink/SDLError.h
+++ b/SmartDeviceLink/SDLError.h
@@ -39,6 +39,7 @@ extern SDLErrorDomain *const SDLErrorDomainFileManager;
+ (NSError *)sdl_fileManager_noKnownFileError;
+ (NSError *)sdl_fileManager_unableToStartError;
+ (NSError *)sdl_fileManager_unableToUploadError;
++ (NSError *)sdl_fileManager_fileDoesNotExistError;
@end
diff --git a/SmartDeviceLink/SDLError.m b/SmartDeviceLink/SDLError.m
index 511f91deb..69ec729d5 100644
--- a/SmartDeviceLink/SDLError.m
+++ b/SmartDeviceLink/SDLError.m
@@ -140,6 +140,17 @@ SDLErrorDomain *const SDLErrorDomainFileManager = @"com.sdl.filemanager.error";
return [NSError errorWithDomain:SDLErrorDomainFileManager code:SDLFileManagerErrorUnableToUpload userInfo:userInfo];
}
+#pragma mark SDLUploadFileOperation
+
++ (NSError *)sdl_fileManager_fileDoesNotExistError {
+ NSDictionary<NSString *, NSString *> *userInfo = @{
+ NSLocalizedDescriptionKey: NSLocalizedString(@"The file manager was unable to send the file", nil),
+ NSLocalizedFailureReasonErrorKey: NSLocalizedString(@"This could be because the file does not exist at the specified file path or that passed data is invalid", nil),
+ NSLocalizedRecoverySuggestionErrorKey: NSLocalizedString(@"Make sure that the the correct file path is being set and that the passed data is valid", nil)
+ };
+return [NSError errorWithDomain:SDLErrorDomainFileManager code:SDLFileManagerErrorFileDoesNotExist userInfo:userInfo];
+}
+
@end
diff --git a/SmartDeviceLink/SDLErrorConstants.h b/SmartDeviceLink/SDLErrorConstants.h
index c439d3fd6..9f532a6fe 100644
--- a/SmartDeviceLink/SDLErrorConstants.h
+++ b/SmartDeviceLink/SDLErrorConstants.h
@@ -62,4 +62,8 @@ typedef NS_ENUM(NSInteger, SDLFileManagerError) {
* The file manager was unable to send this file.
*/
SDLFileManagerErrorUnableToUpload = -4,
+ /**
+ * The file manager could not find the local file
+ */
+ SDLFileManagerErrorFileDoesNotExist = -5,
};
diff --git a/SmartDeviceLink/SDLUploadFileOperation.m b/SmartDeviceLink/SDLUploadFileOperation.m
index c3b849663..acde2d721 100644
--- a/SmartDeviceLink/SDLUploadFileOperation.m
+++ b/SmartDeviceLink/SDLUploadFileOperation.m
@@ -9,6 +9,7 @@
#import "SDLUploadFileOperation.h"
#import "SDLConnectionManagerType.h"
+#import "SDLError.h"
#import "SDLFile.h"
#import "SDLFileWrapper.h"
#import "SDLGlobals.h"
@@ -65,6 +66,13 @@ NS_ASSUME_NONNULL_BEGIN
__block NSUInteger bytesAvailable = 0;
__block NSInteger highestCorrelationIDReceived = -1;
+ NSInputStream *inputStream = [self sdl_openInputStreamWithFile:file];
+
+ // If the file does not exist or the passed data is nil, return an error
+ if (inputStream == nil) {
+ return completion(NO, bytesAvailable, [NSError sdl_fileManager_fileDoesNotExistError]);
+ }
+
dispatch_group_t putFileGroup = dispatch_group_create();
dispatch_group_enter(putFileGroup);
@@ -80,8 +88,6 @@ NS_ASSUME_NONNULL_BEGIN
[weakself finishOperation];
});
- NSInputStream *inputStream = [self sdl_openInputStreamWithFile:file];
-
// Break the data into small pieces, each of which will be sent in a separate putfile
NSUInteger currentOffset = 0;
for (int i = 0; i < (((file.fileSize - 1) / mtuSize) + 1); i++) {
@@ -117,7 +123,7 @@ NS_ASSUME_NONNULL_BEGIN
}
// If no errors, watch for a response containing the amount of storage left on the SDL Core
- if ([self sdl_newHighestCorrelationID:request highestCorrelationIDReceived:highestCorrelationIDReceived]) {
+ if ([[self class] sdl_newHighestCorrelationID:request highestCorrelationIDReceived:highestCorrelationIDReceived]) {
highestCorrelationIDReceived = [request.correlationID integerValue];
bytesAvailable = [(SDLPutFileResponse *)response spaceAvailable].unsignedIntegerValue;
}
@@ -213,7 +219,7 @@ NS_ASSUME_NONNULL_BEGIN
@param highestCorrelationIDReceived The largest currently received correlation id
@return Whether or not the newest request contains the highest correlationId
*/
-- (Boolean)sdl_newHighestCorrelationID:(nullable SDLRPCRequest *)request highestCorrelationIDReceived:(NSInteger)highestCorrelationIDReceived {
++ (BOOL)sdl_newHighestCorrelationID:(SDLRPCRequest *)request highestCorrelationIDReceived:(NSInteger)highestCorrelationIDReceived {
if ([request.correlationID integerValue] > highestCorrelationIDReceived) {
return true;
}
diff --git a/SmartDeviceLinkTests/DevAPISpecs/SDLUploadFileOperationSpec.m b/SmartDeviceLinkTests/DevAPISpecs/SDLUploadFileOperationSpec.m
index a4ebf2828..970a75ebe 100644
--- a/SmartDeviceLinkTests/DevAPISpecs/SDLUploadFileOperationSpec.m
+++ b/SmartDeviceLinkTests/DevAPISpecs/SDLUploadFileOperationSpec.m
@@ -46,7 +46,7 @@ describe(@"Streaming upload of data", ^{
context(@"When uploading data", ^{
context(@"data should be split into smaller packets if too large to send all at once", ^{
- context(@"both data in memory and on disk should be uploaded", ^{
+ context(@"both data in memory and on disk can be uploaded", ^{
it(@"should split the data from a short chunk of text in memory correctly", ^{
testFileName = @"TestSmallMemory";
testFileData = [@"test1234" dataUsingEncoding:NSUTF8StringEncoding];
@@ -118,7 +118,7 @@ describe(@"Streaming upload of data", ^{
// The last pufile contains the remaining data size
expect(putFile.length).to(equal(@([testFile fileSize] - (index * [SDLGlobals sharedGlobals].maxMTUSize))));
} else {
- // All other putfiles contain the max data size
+ // All other putfiles contain the max data size for a putfile packet
expect(putFile.length).to(equal(@([SDLGlobals sharedGlobals].maxMTUSize)));
}
}
@@ -128,7 +128,7 @@ describe(@"Streaming upload of data", ^{
afterEach(^{
__block SDLPutFileResponse *goodResponse = nil;
- // We must send responses otherwise the test cases will crash
+ // We must do some cleanup here otherwise the unit test cases will crash
NSInteger spaceLeft = 11212512;
for (int i = 0; i < numberOfPutFiles; i++) {
spaceLeft -= 1024;
@@ -138,7 +138,6 @@ describe(@"Streaming upload of data", ^{
[testConnectionManager respondToRequestWithResponse:goodResponse requestNumber:i error:nil];
}
- // We must wait here for the dispatch_group_notify to finish up, otherwise test cases will crash
expect(@(successResult)).toEventually(equal(@YES));
expect(@(bytesAvailableResult)).toEventually(equal(spaceLeft));
expect(errorResult).toEventually(beNil());
@@ -271,6 +270,54 @@ describe(@"Streaming upload of data", ^{
});
});
});
+
+ context(@"When an incorrect file url is passed", ^{
+ beforeEach(^{
+ NSString *fileName = @"testImagePNG";
+ testFileName = fileName;
+ NSString *imageFilePath = [[NSBundle bundleForClass:[self class]] pathForResource:fileName ofType:@"png"];
+ NSURL *imageFileURL = [[NSURL alloc] initWithString:imageFilePath]; // This will fail because local file paths need to be init with initFileURLWithPath
+ testFile = [SDLFile fileAtFileURL:imageFileURL name:fileName];
+
+ testFileWrapper = [SDLFileWrapper wrapperWithFile:testFile completionHandler:^(BOOL success, NSUInteger bytesAvailable, NSError * _Nullable error) {
+ successResult = success;
+ bytesAvailableResult = bytesAvailable;
+ errorResult = error;
+ }];
+
+ testConnectionManager = [[TestConnectionManager alloc] init];
+ testOperation = [[SDLUploadFileOperation alloc] initWithFile:testFileWrapper connectionManager:testConnectionManager];
+ [testOperation start];
+ });
+
+ it(@"should have called the completion handler with an error", ^{
+ expect(errorResult).toEventually(equal([NSError sdl_fileManager_fileDoesNotExistError]));
+ expect(@(successResult)).toEventually(equal(@NO));
+ });
+ });
+
+ context(@"When empty data is passed", ^{
+ beforeEach(^{
+ testFileName = @"TestEmptyMemory";
+ testFileData = [@"" dataUsingEncoding:NSUTF8StringEncoding];
+ testFile = [SDLFile fileWithData:testFileData name:testFileName fileExtension:@"bin"];
+
+ testFileWrapper = [SDLFileWrapper wrapperWithFile:testFile completionHandler:^(BOOL success, NSUInteger bytesAvailable, NSError * _Nullable error) {
+ successResult = success;
+ bytesAvailableResult = bytesAvailable;
+ errorResult = error;
+ }];
+
+ testConnectionManager = [[TestConnectionManager alloc] init];
+ testOperation = [[SDLUploadFileOperation alloc] initWithFile:testFileWrapper connectionManager:testConnectionManager];
+ [testOperation start];
+ });
+
+ it(@"should have called the completion handler with an error", ^{
+ expect(errorResult).toEventually(equal([NSError sdl_fileManager_fileDoesNotExistError]));
+ expect(@(successResult)).toEventually(equal(@NO));
+ });
+ });
});
QuickSpecEnd