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