IOSAD-55 cleans tests

IOS-898
buff 5 years ago
parent 7096563e15
commit 919a7aa4f9

@ -11,6 +11,8 @@
15206CAA1F8BA183003FF880 /* PEPSessionProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 15206CA91F8BA183003FF880 /* PEPSessionProvider.m */; };
15206CC71F8E078B003FF880 /* PEPSession.m in Sources */ = {isa = PBXBuildFile; fileRef = 15206CC61F8E078B003FF880 /* PEPSession.m */; };
15D3D761200F61BB00A0DA20 /* PEPSessionProviderTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 15D3D760200F61BB00A0DA20 /* PEPSessionProviderTest.m */; };
15D3D77D2010DDC500A0DA20 /* PEPTestUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 15D3D77C2010DDC500A0DA20 /* PEPTestUtils.m */; };
15D3D7802010E20400A0DA20 /* PEPTestSyncDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 15D3D77F2010E20400A0DA20 /* PEPTestSyncDelegate.m */; };
43209B3D1ECC2DA7007E7E2E /* NSArray+Extension.m in Sources */ = {isa = PBXBuildFile; fileRef = 43209B271ECC2ACD007E7E2E /* NSArray+Extension.m */; };
43209B3E1ECC2DAB007E7E2E /* PEPCSVScanner.m in Sources */ = {isa = PBXBuildFile; fileRef = 43209B291ECC2ACD007E7E2E /* PEPCSVScanner.m */; };
43209B3F1ECC2DB0007E7E2E /* PEPLanguage.m in Sources */ = {isa = PBXBuildFile; fileRef = 43209B2B1ECC2ACD007E7E2E /* PEPLanguage.m */; };
@ -116,6 +118,10 @@
15206CC51F8E078B003FF880 /* PEPSession.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PEPSession.h; sourceTree = "<group>"; };
15206CC61F8E078B003FF880 /* PEPSession.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = PEPSession.m; sourceTree = "<group>"; };
15D3D760200F61BB00A0DA20 /* PEPSessionProviderTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = PEPSessionProviderTest.m; sourceTree = "<group>"; };
15D3D77B2010DDC500A0DA20 /* PEPTestUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PEPTestUtils.h; sourceTree = "<group>"; };
15D3D77C2010DDC500A0DA20 /* PEPTestUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PEPTestUtils.m; sourceTree = "<group>"; };
15D3D77E2010E20400A0DA20 /* PEPTestSyncDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PEPTestSyncDelegate.h; sourceTree = "<group>"; };
15D3D77F2010E20400A0DA20 /* PEPTestSyncDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = PEPTestSyncDelegate.m; sourceTree = "<group>"; };
352988AA1AEF964100FA7E2E /* libpEpObjCAdapter.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libpEpObjCAdapter.a; sourceTree = BUILT_PRODUCTS_DIR; };
35FB0ABB1B57F97E00377032 /* CFNetwork.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CFNetwork.framework; path = System/Library/Frameworks/CFNetwork.framework; sourceTree = SDKROOT; };
35FB0ABD1B57F99D00377032 /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; };
@ -197,6 +203,10 @@
15D3D762200F6C6A00A0DA20 /* TestUtils */ = {
isa = PBXGroup;
children = (
15D3D77B2010DDC500A0DA20 /* PEPTestUtils.h */,
15D3D77C2010DDC500A0DA20 /* PEPTestUtils.m */,
15D3D77E2010E20400A0DA20 /* PEPTestSyncDelegate.h */,
15D3D77F2010E20400A0DA20 /* PEPTestSyncDelegate.m */,
);
path = TestUtils;
sourceTree = "<group>";
@ -454,9 +464,11 @@
43209B431ECC2DC0007E7E2E /* PEPInternalSession.m in Sources */,
43209B3F1ECC2DB0007E7E2E /* PEPLanguage.m in Sources */,
43209B401ECC2DB4007E7E2E /* PEPMessageUtil.m in Sources */,
15D3D77D2010DDC500A0DA20 /* PEPTestUtils.m in Sources */,
43209B3D1ECC2DA7007E7E2E /* NSArray+Extension.m in Sources */,
43209B3E1ECC2DAB007E7E2E /* PEPCSVScanner.m in Sources */,
43209B421ECC2DBC007E7E2E /* PEPQueue.m in Sources */,
15D3D7802010E20400A0DA20 /* PEPTestSyncDelegate.m in Sources */,
15206CC71F8E078B003FF880 /* PEPSession.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;

@ -15,7 +15,7 @@
Fake session to handle to the client.
Assures all calls are handled on the correct internal session for the thread it is called on.
You can instatntiate and use this session how often and wherever you want. Also over multiple threads.
You can instantiate and use this session how often and wherever you want. Also over multiple threads.
Note: You must call `cleanup()` once before your process gets terminated to be able to gracefully shutdown.
It is the clients responsibility not to make any calls to PEPSession in between the last call

@ -45,15 +45,6 @@ static PEPInternalSession *s_sessionForMainThread = nil;
}
[self nullifySessionsOfFinishedThreads];
// NSLog(@"#################\nnum sessions is now %lu", //BUFF: clean up
// (unsigned long)[self sessionForThreadDict].count + (s_sessionForMainThread ? 1 : 0));
// NSLog(@"Threads:");
// for (PEPCopyableThread *thread in dict.allKeys) {
// NSLog(@"%@", thread.description);
// }
// NSLog(@"Session for main thread: %@", s_sessionForMainThread);
// NSLog(@"##################################");
[[self sessionForThreadLock] unlock];
return session;
@ -116,8 +107,6 @@ static PEPInternalSession *s_sessionForMainThread = nil;
}
s_sessionForMainThread = nil;
[dict removeAllObjects];
// NSLog(@"All sessions have been cleaned up. Session count is %lu",
// (unsigned long)dict.count + (s_sessionForMainThread ? 1 : 0));
}
/**

@ -0,0 +1,107 @@
//
// PEPSyncTest.m
// pEpObjCAdapterTests
//
// Created by Andreas Buff on 17.01.18.
// Copyright © 2018 pp. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "PEPIdentity.h"
#import "PEPObjCAdapter.h"
// Commented. See testSyncSession
// MARK: - PEPSyncDelegate
@interface SomeSyncDelegate : NSObject<PEPSyncDelegate>
- (BOOL)waitUntilSent:(time_t)maxSec;
@property (nonatomic) bool sendWasCalled;
@property (nonatomic, strong) NSCondition *cond;
@end
@implementation SomeSyncDelegate
//- (id)init
//{
// if (self = [super init]) {
// self.sendWasCalled = false;
// self.cond = [[NSCondition alloc] init];
// }
// return self;
//}
//
//- (PEP_STATUS)notifyHandshakeWithSignal:(sync_handshake_signal)signal me:(id)me
// partner:(id)partner
//{
// return PEP_STATUS_OK;
//}
//
//- (PEP_STATUS)sendMessage:(id)msg //never used afaics. Delete?
//{
// [_cond lock];
//
// self.sendWasCalled = true;
// [_cond signal];
// [_cond unlock];
//
// return PEP_STATUS_OK;
//}
//
//- (PEP_STATUS)fastPolling:(bool)isfast
//{
// return PEP_STATUS_OK;
//}
//
//- (BOOL)waitUntilSent:(time_t)maxSec
//{
// bool res;
// [_cond lock];
// [_cond waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:maxSec]];
// res = _sendWasCalled;
// [_cond unlock];
// return res;
//}
//
//@end
//
//@interface PEPSyncTest : XCTestCase
//
//@end
//
//@implementation PEPSyncTest
//
////BUFF: ?? commented due to: Can't currently work, engine doesn't contain sync.
//- (void)testSyncSession
//{
// PEPSession *session = [PEPSession new];
// SomeSyncDelegate *syncDelegate = [[SomeSyncDelegate alloc] init];
// [self pEpSetUp];
//
// // This should attach session just created
// [PEPObjCAdapter startSync:syncDelegate];
//
// PEPIdentity *identMe = [[PEPIdentity alloc]
// initWithAddress:@"pep.test.iosgenkey@pep-project.org"
// userID:@"Me"
// userName:@"pEp Test iOS GenKey"
// isOwn:YES];
//
// [session mySelf:identMe];
//
// bool res = [syncDelegate waitUntilSent:1];
//
// // Can't currently work, engine doesn't contain sync.
// XCTAssertFalse(res);
//
// // This should detach session just created
// [PEPObjCAdapter stopSync];
//
// [self pEpCleanUp];
//}
//}
@end

@ -0,0 +1,21 @@
//
// PEPTestSyncDelegate.h
// pEpObjCAdapter
//
// Created by Andreas Buff on 18.01.18.
// Copyright © 2018 p≡p. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "PEPObjCAdapter.h"
@interface PEPTestSyncDelegate : NSObject<PEPSyncDelegate>
- (BOOL)waitUntilSent:(time_t)maxSec;
@property (nonatomic) bool sendWasCalled;
@property (nonatomic, strong) NSCondition *cond;
@end

@ -0,0 +1,54 @@
//
// PEPTestSyncDelegate.m
// pEpObjCAdapter
//
// Created by Andreas Buff on 18.01.18.
// Copyright © 2018 pp. All rights reserved.
//
#import "PEPTestSyncDelegate.h"
@implementation PEPTestSyncDelegate
- (id)init
{
if (self = [super init]) {
self.sendWasCalled = false;
self.cond = [[NSCondition alloc] init];
}
return self;
}
- (PEP_STATUS)notifyHandshakeWithSignal:(sync_handshake_signal)signal me:(id)me
partner:(id)partner
{
return PEP_STATUS_OK;
}
- (PEP_STATUS)sendMessage:(id)msg
{
[_cond lock];
self.sendWasCalled = true;
[_cond signal];
[_cond unlock];
return PEP_STATUS_OK;
}
- (PEP_STATUS)fastPolling:(bool)isfast
{
return PEP_STATUS_OK;
}
- (BOOL)waitUntilSent:(time_t)maxSec
{
bool res;
[_cond lock];
[_cond waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:maxSec]];
res = _sendWasCalled;
[_cond unlock];
return res;
}
@end

@ -0,0 +1,41 @@
//
// PEPTestUtils.h
// pEpObjCAdapter
//
// Created by Andreas Buff on 17.01.18.
// Copyright © 2018 p≡p. All rights reserved.
//
#import <Foundation/Foundation.h>
@class PEPIdentity;
@class PEPInternalSession;
@class PEPMessage;
NS_ASSUME_NONNULL_BEGIN
extern NSString * const ownUserId;
@interface PEPTestUtils : NSObject
+ (void)importBundledKey:(NSString *)item;
+ (NSString *)loadStringFromFileName:(NSString *)fileName;
+ (NSDictionary *)unarchiveDictionary:(NSString *)fileName;
+ (PEPMessage * _Nonnull) mailFrom:(PEPIdentity * _Nullable) fromIdent
toIdent: (PEPIdentity * _Nullable) toIdent
shortMessage:(NSString *)shortMessage
longMessage: (NSString *)longMessage
outgoing:(BOOL) outgoing;
+ (void)deleteWorkFilesAfterBackingUpWithBackupName:(NSString * _Nullable)backup;
+ (void)restoreWorkFilesFromBackupNamed:(NSString *)backup;
+ (NSArray *)pEpWorkFiles;
@end
NS_ASSUME_NONNULL_END

@ -0,0 +1,141 @@
//
// PEPTestUtils.m
// pEpObjCAdapter
//
// Created by Andreas Buff on 17.01.18.
// Copyright © 2018 pp. All rights reserved.
//
#import "PEPTestUtils.h"
#import "NSDictionary+Extension.h"
#import "PEPIdentity.h"
#import "PEPInternalSession.h"
#import "PEPMessage.h"
#import "PEPSession.h"
/**
For now, safer to use that, until the engine copes with our own.
Should mimick the value of PEP_OWN_USERID.
*/
NSString * const ownUserId = @"pEp_own_userId";
@implementation PEPTestUtils
+ (void)importBundledKey:(NSString *)item;
{
PEPSession *session = [PEPSession new];
NSString *txtFileContents = [self loadStringFromFileName:item];
[session importKey:txtFileContents];
}
+ (NSString *)loadStringFromFileName:(NSString *)fileName;
{
NSString *txtFilePath = [[[NSBundle bundleForClass:[self class]] resourcePath]
stringByAppendingPathComponent:fileName];
NSString *txtFileContents = [NSString stringWithContentsOfFile:txtFilePath
encoding:NSUTF8StringEncoding error:NULL];
return txtFileContents;
}
+ (NSDictionary *)unarchiveDictionary:(NSString *)fileName
{
NSString *filePath = [[[NSBundle bundleForClass:[self class]]
resourcePath] stringByAppendingPathComponent:fileName];
NSMutableData *data = [NSMutableData dataWithContentsOfFile:filePath];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSDictionary *dict = [unarchiver decodeObject];
[unarchiver finishDecoding];
return dict;
}
+ (PEPMessage * _Nonnull) mailFrom:(PEPIdentity * _Nullable) fromIdent
toIdent: (PEPIdentity * _Nullable) toIdent
shortMessage:(NSString *)shortMessage
longMessage: (NSString *)longMessage
outgoing:(BOOL) outgoing;
{
PEPMessage *message = [PEPMessage new];
message.from = fromIdent;
message.to = @[toIdent];
message.direction = outgoing ? PEP_dir_outgoing:PEP_dir_incoming;
message.shortMessage = shortMessage;
message.longMessage = longMessage;
return message;
}
+ (NSArray *)pEpWorkFiles;
{
// Only files whose content is affected by tests.
NSString* home = [[[NSProcessInfo processInfo]environment]objectForKey:@"HOME"];
NSString* gpgHome = [home stringByAppendingPathComponent:@".gnupg"];
return @[[home stringByAppendingPathComponent:@".pEp_management.db"],
[home stringByAppendingPathComponent:@".pEp_management.db-shm"],
[home stringByAppendingPathComponent:@".pEp_management.db-wal"],
[gpgHome stringByAppendingPathComponent:@"pubring.gpg"],
[gpgHome stringByAppendingPathComponent:@"secring.gpg"]];
}
+ (void)deleteWorkFilesAfterBackingUpWithBackupName:(NSString *_Nullable)backup;
{
[PEPSession cleanup];
for (id path in [self pEpWorkFiles]) {
[self delFilePath:path backup:backup];
}
}
+ (void)restoreWorkFilesFromBackupNamed:(NSString *)backup;
{
if (!backup) {
return;
}
[PEPSession cleanup];
for (id path in [self pEpWorkFiles]) {
[self undelFileWithPath:path backup:backup];
}
}
#pragma mark - PRIVATE
+ (void)delFilePath:(NSString *)path backup:(NSString * _Nullable)bkpsfx;
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
if ([fileManager fileExistsAtPath:path]) {
BOOL success;
if (!bkpsfx) {
success = [fileManager removeItemAtPath:path error:&error];
} else {
NSString *toPath = [path stringByAppendingString:bkpsfx];
if ([fileManager fileExistsAtPath:toPath]) {
[fileManager removeItemAtPath:toPath error:&error];
}
success = [fileManager moveItemAtPath:path toPath:toPath error:&error];
}
if (!success) {
NSLog(@"Error: %@", [error localizedDescription]);
}
}
}
+ (void)undelFileWithPath:(NSString *)path backup:(NSString *)backup;
{
NSParameterAssert(backup);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString* bpath = [path stringByAppendingString:backup];
BOOL fileExists = [fileManager fileExistsAtPath:bpath];
if (fileExists) {
NSError *error = nil;
BOOL success = [fileManager moveItemAtPath:bpath toPath:path error:&error];
if (!success) {
NSLog(@"Error: %@", [error localizedDescription]);
}
}
}
@end

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save