pEpObjCTests/TestUtils/PEPTestUtils.m
author Dirk Zimmermann <dz@pep.security>
Wed, 20 Mar 2019 16:40:35 +0100
branchIOS-1480
changeset 902 32760267cd59
parent 875 f5563630c1b3
child 1036 a78c36269f98
permissions -rw-r--r--
IOS-1480 Avoid framework imports.
andreas@391
     1
//
andreas@391
     2
//  PEPTestUtils.m
andreas@391
     3
//  pEpObjCAdapter
andreas@391
     4
//
andreas@391
     5
//  Created by Andreas Buff on 17.01.18.
andreas@391
     6
//  Copyright © 2018 p≡p. All rights reserved.
andreas@391
     7
//
andreas@391
     8
dirk@487
     9
#import <XCTest/XCTest.h>
dirk@487
    10
andreas@391
    11
#import "PEPTestUtils.h"
andreas@391
    12
dz@902
    13
#import "PEPObjCAdapterFramework.h"
andreas@391
    14
#import "PEPInternalSession.h"
andreas@391
    15
#import "PEPMessage.h"
andreas@391
    16
#import "PEPSession.h"
dirk@579
    17
#import "PEPAttachment.h"
dirk@761
    18
#import "PEPSessionProvider.h"
andreas@391
    19
andreas@391
    20
/**
andreas@391
    21
 For now, safer to use that, until the engine copes with our own.
andreas@391
    22
 Should mimick the value of PEP_OWN_USERID.
andreas@391
    23
 */
andreas@391
    24
NSString * const ownUserId = @"pEp_own_userId";
andreas@391
    25
dirk@754
    26
const NSInteger PEPTestInternalSyncTimeout = 20;
dirk@754
    27
andreas@391
    28
@implementation PEPTestUtils
andreas@391
    29
andreas@411
    30
+ (PEPIdentity *)foreignPepIdentityWithAddress:(NSString *)address userName:(NSString *)username;
andreas@411
    31
{
andreas@411
    32
    return [[PEPIdentity alloc] initWithAddress:address
andreas@411
    33
                                         userID:@"UNIT-TEST-USER-ID-FOREIGN-IDENTITY"
andreas@411
    34
                                       userName:username
andreas@411
    35
                                          isOwn:NO fingerPrint:nil];
andreas@411
    36
}
andreas@411
    37
andreas@411
    38
+ (PEPIdentity *)ownPepIdentityWithAddress:(NSString *)address userName:(NSString *)username;
andreas@411
    39
{
andreas@411
    40
    return [[PEPIdentity alloc] initWithAddress:address
andreas@411
    41
                                         userID:ownUserId
andreas@411
    42
                                       userName:username
andreas@411
    43
                                          isOwn:YES fingerPrint:nil];
andreas@411
    44
}
andreas@411
    45
andreas@392
    46
+ (NSString *)loadResourceByName:(NSString *)name;
andreas@391
    47
{
andreas@392
    48
    NSURL *url = [[NSBundle bundleForClass:[self class]] URLForResource:name withExtension:nil];
andreas@392
    49
    return [NSString stringWithContentsOfURL:url usedEncoding:nil error:nil];
andreas@391
    50
}
andreas@391
    51
andreas@391
    52
+ (NSString *)loadStringFromFileName:(NSString *)fileName;
andreas@391
    53
{
andreas@391
    54
    NSString *txtFilePath = [[[NSBundle bundleForClass:[self class]] resourcePath]
andreas@391
    55
                             stringByAppendingPathComponent:fileName];
andreas@391
    56
    NSString *txtFileContents = [NSString stringWithContentsOfFile:txtFilePath
andreas@391
    57
                                                          encoding:NSUTF8StringEncoding error:NULL];
andreas@391
    58
    return txtFileContents;
andreas@391
    59
}
andreas@391
    60
andreas@391
    61
+ (NSDictionary *)unarchiveDictionary:(NSString *)fileName
andreas@391
    62
{
andreas@391
    63
    NSString *filePath = [[[NSBundle bundleForClass:[self class]]
andreas@391
    64
                           resourcePath] stringByAppendingPathComponent:fileName];
andreas@391
    65
    NSMutableData *data = [NSMutableData dataWithContentsOfFile:filePath];
andreas@391
    66
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
andreas@391
    67
    NSDictionary *dict = [unarchiver decodeObject];
andreas@391
    68
    [unarchiver finishDecoding];
andreas@391
    69
    return dict;
andreas@391
    70
}
andreas@391
    71
dirk@579
    72
/**
dirk@579
    73
 Converts a given message dict to a version with correct attachments, using PEPAttachment.
dirk@579
    74
 Using unarchiveDirectory for a message object will yield the old attachment format,
dirk@579
    75
 which was just an array of dictionaries. Now the correct way is to use PEPAttachments.
dirk@579
    76
 */
dirk@579
    77
+ (void)migrateUnarchivedMessageDictionary:(NSMutableDictionary *)message
dirk@579
    78
{
dirk@579
    79
    NSMutableArray *attachments = [NSMutableArray new];
dirk@579
    80
    for (NSDictionary *attachmentDict in [message objectForKey:kPepAttachments]) {
dirk@581
    81
        PEPAttachment *attachment = [[PEPAttachment alloc]
dirk@581
    82
                                     initWithData:[attachmentDict objectForKey:@"data"]];
dirk@579
    83
        attachment.filename = [attachmentDict objectForKey:@"filename"];
dirk@579
    84
        attachment.mimeType = [attachmentDict objectForKey:@"mimeType"];
dirk@579
    85
        [attachments addObject:attachment];
dirk@579
    86
    }
dirk@579
    87
    [message setValue:[NSArray arrayWithArray:attachments] forKey:kPepAttachments];
dirk@579
    88
}
dirk@579
    89
dirk@488
    90
+ (BOOL)importBundledKey:(NSString *)item session:(PEPSession *)session
andreas@392
    91
{
dirk@488
    92
    if (!session) {
dirk@488
    93
        session = [PEPSession new];
dirk@488
    94
    }
dirk@488
    95
andreas@392
    96
    NSString *txtFileContents = [self loadStringFromFileName:item];
dirk@488
    97
    if (!txtFileContents) {
dirk@488
    98
        return NO;
dirk@488
    99
    } else {
dirk@551
   100
        return [session importKey:txtFileContents error:nil];
dirk@488
   101
    }
andreas@392
   102
}
andreas@392
   103
andreas@391
   104
+ (PEPMessage * _Nonnull) mailFrom:(PEPIdentity * _Nullable) fromIdent
andreas@391
   105
                           toIdent: (PEPIdentity * _Nullable) toIdent
andreas@391
   106
                      shortMessage:(NSString *)shortMessage
andreas@391
   107
                       longMessage: (NSString *)longMessage
andreas@391
   108
                          outgoing:(BOOL) outgoing;
andreas@391
   109
{
andreas@391
   110
    PEPMessage *message = [PEPMessage new];
andreas@391
   111
    message.from = fromIdent;
andreas@391
   112
    message.to = @[toIdent];
dz@875
   113
    message.direction = outgoing ? PEPMsgDirectionOutgoing : PEPMsgDirectionIncoming;
andreas@391
   114
    message.shortMessage = shortMessage;
andreas@391
   115
    message.longMessage = longMessage;
andreas@391
   116
    return message;
andreas@391
   117
}
andreas@391
   118
andreas@391
   119
+ (NSArray *)pEpWorkFiles;
andreas@391
   120
{
andreas@391
   121
    // Only files whose content is affected by tests.
dirk@442
   122
    NSString *home = [[[NSProcessInfo processInfo]environment]objectForKey:@"HOME"];
dirk@442
   123
    NSString *gpgHome = [home stringByAppendingPathComponent:@".gnupg"];
andreas@391
   124
    return @[[home stringByAppendingPathComponent:@".pEp_management.db"],
andreas@391
   125
             [home stringByAppendingPathComponent:@".pEp_management.db-shm"],
andreas@391
   126
             [home stringByAppendingPathComponent:@".pEp_management.db-wal"],
andreas@391
   127
             [gpgHome stringByAppendingPathComponent:@"pubring.gpg"],
andreas@391
   128
             [gpgHome stringByAppendingPathComponent:@"secring.gpg"]];
andreas@391
   129
andreas@391
   130
}
andreas@391
   131
dirk@470
   132
+ (void)cleanUp
andreas@391
   133
{
dirk@761
   134
    // This triggers setting HOME und GPGHOME in the adapter.
dirk@761
   135
    // Important for tests which do a cleanup on test start.
dz@781
   136
    [PEPObjCAdapter homeURL];
dirk@761
   137
andreas@391
   138
    [PEPSession cleanup];
andreas@391
   139
andreas@391
   140
    for (id path in [self pEpWorkFiles]) {
dirk@470
   141
        [self delFilePath:path];
andreas@391
   142
    }
andreas@391
   143
}
andreas@391
   144
andreas@391
   145
#pragma mark - PRIVATE
andreas@391
   146
dirk@470
   147
+ (void)delFilePath:(NSString *)filePath
andreas@391
   148
{
andreas@391
   149
    NSFileManager *fileManager = [NSFileManager defaultManager];
andreas@391
   150
    NSError *error = nil;
dirk@470
   151
    if ([fileManager fileExistsAtPath:filePath]) {
dirk@470
   152
        BOOL success = [fileManager removeItemAtPath:filePath error:&error];
andreas@391
   153
        if (!success) {
andreas@391
   154
            NSLog(@"Error: %@", [error localizedDescription]);
andreas@391
   155
        }
andreas@391
   156
    }
andreas@391
   157
}
andreas@391
   158
andreas@391
   159
+ (void)undelFileWithPath:(NSString *)path backup:(NSString *)backup;
andreas@391
   160
{
andreas@391
   161
    NSParameterAssert(backup);
andreas@391
   162
    NSFileManager *fileManager = [NSFileManager defaultManager];
andreas@391
   163
    NSString* bpath = [path stringByAppendingString:backup];
andreas@391
   164
    BOOL fileExists = [fileManager fileExistsAtPath:bpath];
andreas@391
   165
    if (fileExists) {
andreas@391
   166
        NSError *error = nil;
andreas@391
   167
        BOOL success = [fileManager moveItemAtPath:bpath toPath:path error:&error];
andreas@391
   168
        if (!success) {
andreas@391
   169
            NSLog(@"Error: %@", [error localizedDescription]);
andreas@391
   170
        }
andreas@391
   171
    }
andreas@391
   172
}
andreas@391
   173
andreas@391
   174
@end