mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.4 KiB
Matlab
81 lines
2.4 KiB
Matlab
7 years ago
|
//
|
||
|
// 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
|