mirror of https://github.com/oxen-io/session-ios
Add tests around database conversion.
parent
dc73342573
commit
5ba5b763e4
@ -0,0 +1,13 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#import <XCTest/XCTest.h>
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
@interface OWSDatabaseConverterTest : XCTestCase
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_END
|
@ -0,0 +1,80 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#import "OWSDatabaseConverterTest.h"
|
||||||
|
#import "OWSDatabaseConverter.h"
|
||||||
|
#import <Curve25519Kit/Randomness.h>
|
||||||
|
#import <SignalServiceKit/OWSStorage.h>
|
||||||
|
#import <YapDatabase/YapDatabase.h>
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
@interface OWSStorage (OWSDatabaseConverterTest)
|
||||||
|
|
||||||
|
+ (YapDatabaseDeserializer)logOnFailureDeserializer;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
#pragma mark -
|
||||||
|
|
||||||
|
@interface OWSDatabaseConverter (OWSDatabaseConverterTest)
|
||||||
|
|
||||||
|
+ (BOOL)doesDatabaseNeedToBeConverted:(NSString *)databaseFilePath;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
#pragma mark -
|
||||||
|
|
||||||
|
@implementation OWSDatabaseConverterTest
|
||||||
|
|
||||||
|
- (NSData *)randomDatabasePassword
|
||||||
|
{
|
||||||
|
return [Randomness generateRandomBytes:30];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (nullable NSString *)createUnconvertedDatabase:(NSData *)passwordData
|
||||||
|
{
|
||||||
|
NSString *temporaryDirectory = NSTemporaryDirectory();
|
||||||
|
NSString *filename = [NSUUID UUID].UUIDString;
|
||||||
|
NSString *databaseFilePath = [temporaryDirectory stringByAppendingPathComponent:filename];
|
||||||
|
|
||||||
|
YapDatabaseOptions *options = [[YapDatabaseOptions alloc] init];
|
||||||
|
options.corruptAction = YapDatabaseCorruptAction_Fail;
|
||||||
|
options.cipherKeyBlock = ^{
|
||||||
|
return passwordData;
|
||||||
|
};
|
||||||
|
options.enableMultiProcessSupport = YES;
|
||||||
|
|
||||||
|
YapDatabase *database = [[YapDatabase alloc] initWithPath:databaseFilePath
|
||||||
|
serializer:nil
|
||||||
|
deserializer:[OWSStorage logOnFailureDeserializer]
|
||||||
|
options:options];
|
||||||
|
OWSAssert(database);
|
||||||
|
return database ? databaseFilePath : nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)testDoesDatabaseNeedToBeConverted_Unconverted
|
||||||
|
{
|
||||||
|
NSData *passwordData = [self randomDatabasePassword];
|
||||||
|
NSString *_Nullable databaseFilePath = [self createUnconvertedDatabase:passwordData];
|
||||||
|
XCTAssertTrue([OWSDatabaseConverter doesDatabaseNeedToBeConverted:databaseFilePath]);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)testDoesDatabaseNeedToBeConverted_Converted
|
||||||
|
{
|
||||||
|
// TODO: When we can create converted databases.
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)testDatabaseConversion
|
||||||
|
{
|
||||||
|
NSData *passwordData = [self randomDatabasePassword];
|
||||||
|
NSString *_Nullable databaseFilePath = [self createUnconvertedDatabase:passwordData];
|
||||||
|
XCTAssertTrue([OWSDatabaseConverter doesDatabaseNeedToBeConverted:databaseFilePath]);
|
||||||
|
[OWSDatabaseConverter convertDatabaseIfNecessary];
|
||||||
|
XCTAssertFalse([OWSDatabaseConverter doesDatabaseNeedToBeConverted:databaseFilePath]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_END
|
Loading…
Reference in New Issue