pEpObjCTests/TestUtils/PEPTestUtils.m
author Dirk Zimmermann <dirk@pep-project.org>
Thu, 22 Mar 2018 15:38:12 +0100
changeset 551 6b7914602bc6
parent 488 72a4e45d0398
child 579 1ddf35bfa4f3
permissions -rw-r--r--
IOSAD-72 importKey error handling
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
andreas@391
    13
#import "NSDictionary+Extension.h"
andreas@391
    14
#import "PEPIdentity.h"
andreas@391
    15
#import "PEPInternalSession.h"
andreas@391
    16
#import "PEPMessage.h"
andreas@391
    17
#import "PEPSession.h"
andreas@391
    18
andreas@391
    19
/**
andreas@391
    20
 For now, safer to use that, until the engine copes with our own.
andreas@391
    21
 Should mimick the value of PEP_OWN_USERID.
andreas@391
    22
 */
andreas@391
    23
NSString * const ownUserId = @"pEp_own_userId";
andreas@391
    24
andreas@391
    25
@implementation PEPTestUtils
andreas@391
    26
andreas@411
    27
+ (PEPIdentity *)foreignPepIdentityWithAddress:(NSString *)address userName:(NSString *)username;
andreas@411
    28
{
andreas@411
    29
    return [[PEPIdentity alloc] initWithAddress:address
andreas@411
    30
                                         userID:@"UNIT-TEST-USER-ID-FOREIGN-IDENTITY"
andreas@411
    31
                                       userName:username
andreas@411
    32
                                          isOwn:NO fingerPrint:nil];
andreas@411
    33
}
andreas@411
    34
andreas@411
    35
+ (PEPIdentity *)ownPepIdentityWithAddress:(NSString *)address userName:(NSString *)username;
andreas@411
    36
{
andreas@411
    37
    return [[PEPIdentity alloc] initWithAddress:address
andreas@411
    38
                                         userID:ownUserId
andreas@411
    39
                                       userName:username
andreas@411
    40
                                          isOwn:YES fingerPrint:nil];
andreas@411
    41
}
andreas@411
    42
andreas@392
    43
+ (NSString *)loadResourceByName:(NSString *)name;
andreas@391
    44
{
andreas@392
    45
    NSURL *url = [[NSBundle bundleForClass:[self class]] URLForResource:name withExtension:nil];
andreas@392
    46
    return [NSString stringWithContentsOfURL:url usedEncoding:nil error:nil];
andreas@391
    47
}
andreas@391
    48
andreas@391
    49
+ (NSString *)loadStringFromFileName:(NSString *)fileName;
andreas@391
    50
{
andreas@391
    51
    NSString *txtFilePath = [[[NSBundle bundleForClass:[self class]] resourcePath]
andreas@391
    52
                             stringByAppendingPathComponent:fileName];
andreas@391
    53
    NSString *txtFileContents = [NSString stringWithContentsOfFile:txtFilePath
andreas@391
    54
                                                          encoding:NSUTF8StringEncoding error:NULL];
andreas@391
    55
    return txtFileContents;
andreas@391
    56
}
andreas@391
    57
andreas@391
    58
+ (NSDictionary *)unarchiveDictionary:(NSString *)fileName
andreas@391
    59
{
andreas@391
    60
    NSString *filePath = [[[NSBundle bundleForClass:[self class]]
andreas@391
    61
                           resourcePath] stringByAppendingPathComponent:fileName];
andreas@391
    62
    NSMutableData *data = [NSMutableData dataWithContentsOfFile:filePath];
andreas@391
    63
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
andreas@391
    64
    NSDictionary *dict = [unarchiver decodeObject];
andreas@391
    65
    [unarchiver finishDecoding];
andreas@391
    66
    return dict;
andreas@391
    67
}
andreas@391
    68
dirk@488
    69
+ (BOOL)importBundledKey:(NSString *)item session:(PEPSession *)session
andreas@392
    70
{
dirk@488
    71
    if (!session) {
dirk@488
    72
        session = [PEPSession new];
dirk@488
    73
    }
dirk@488
    74
andreas@392
    75
    NSString *txtFileContents = [self loadStringFromFileName:item];
dirk@488
    76
    if (!txtFileContents) {
dirk@488
    77
        return NO;
dirk@488
    78
    } else {
dirk@551
    79
        return [session importKey:txtFileContents error:nil];
dirk@488
    80
    }
andreas@392
    81
}
andreas@392
    82
andreas@391
    83
+ (PEPMessage * _Nonnull) mailFrom:(PEPIdentity * _Nullable) fromIdent
andreas@391
    84
                           toIdent: (PEPIdentity * _Nullable) toIdent
andreas@391
    85
                      shortMessage:(NSString *)shortMessage
andreas@391
    86
                       longMessage: (NSString *)longMessage
andreas@391
    87
                          outgoing:(BOOL) outgoing;
andreas@391
    88
{
andreas@391
    89
    PEPMessage *message = [PEPMessage new];
andreas@391
    90
    message.from = fromIdent;
andreas@391
    91
    message.to = @[toIdent];
andreas@391
    92
    message.direction = outgoing ? PEP_dir_outgoing:PEP_dir_incoming;
andreas@391
    93
    message.shortMessage = shortMessage;
andreas@391
    94
    message.longMessage = longMessage;
andreas@391
    95
    return message;
andreas@391
    96
}
andreas@391
    97
andreas@391
    98
+ (NSArray *)pEpWorkFiles;
andreas@391
    99
{
andreas@391
   100
    // Only files whose content is affected by tests.
dirk@442
   101
    NSString *home = [[[NSProcessInfo processInfo]environment]objectForKey:@"HOME"];
dirk@442
   102
    NSString *gpgHome = [home stringByAppendingPathComponent:@".gnupg"];
andreas@391
   103
    return @[[home stringByAppendingPathComponent:@".pEp_management.db"],
andreas@391
   104
             [home stringByAppendingPathComponent:@".pEp_management.db-shm"],
andreas@391
   105
             [home stringByAppendingPathComponent:@".pEp_management.db-wal"],
andreas@391
   106
             [gpgHome stringByAppendingPathComponent:@"pubring.gpg"],
andreas@391
   107
             [gpgHome stringByAppendingPathComponent:@"secring.gpg"]];
andreas@391
   108
andreas@391
   109
}
andreas@391
   110
dirk@470
   111
+ (void)cleanUp
andreas@391
   112
{
andreas@391
   113
    [PEPSession cleanup];
andreas@391
   114
andreas@391
   115
    for (id path in [self pEpWorkFiles]) {
dirk@470
   116
        [self delFilePath:path];
andreas@391
   117
    }
andreas@391
   118
}
andreas@391
   119
andreas@391
   120
#pragma mark - PRIVATE
andreas@391
   121
dirk@470
   122
+ (void)delFilePath:(NSString *)filePath
andreas@391
   123
{
andreas@391
   124
    NSFileManager *fileManager = [NSFileManager defaultManager];
andreas@391
   125
    NSError *error = nil;
dirk@470
   126
    if ([fileManager fileExistsAtPath:filePath]) {
dirk@470
   127
        BOOL success = [fileManager removeItemAtPath:filePath error:&error];
andreas@391
   128
        if (!success) {
andreas@391
   129
            NSLog(@"Error: %@", [error localizedDescription]);
andreas@391
   130
        }
andreas@391
   131
    }
andreas@391
   132
}
andreas@391
   133
andreas@391
   134
+ (void)undelFileWithPath:(NSString *)path backup:(NSString *)backup;
andreas@391
   135
{
andreas@391
   136
    NSParameterAssert(backup);
andreas@391
   137
    NSFileManager *fileManager = [NSFileManager defaultManager];
andreas@391
   138
    NSString* bpath = [path stringByAppendingString:backup];
andreas@391
   139
    BOOL fileExists = [fileManager fileExistsAtPath:bpath];
andreas@391
   140
    if (fileExists) {
andreas@391
   141
        NSError *error = nil;
andreas@391
   142
        BOOL success = [fileManager moveItemAtPath:bpath toPath:path error:&error];
andreas@391
   143
        if (!success) {
andreas@391
   144
            NSLog(@"Error: %@", [error localizedDescription]);
andreas@391
   145
        }
andreas@391
   146
    }
andreas@391
   147
}
andreas@391
   148
andreas@391
   149
@end