pEpObjCTests/TestUtils/PEPTestUtils.m
author Dirk Zimmermann <dirk@pep-project.org>
Tue, 24 Apr 2018 13:29:19 +0200
branchIOSAD-92
changeset 594 a9cae3a6f78d
parent 581 0954976b8da9
child 754 78dbef38ae98
permissions -rw-r--r--
IOSAD-92 PEPIdentity using hashBasedOnKeys etc.
     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 #import "PEPAttachment.h"
    19 
    20 /**
    21  For now, safer to use that, until the engine copes with our own.
    22  Should mimick the value of PEP_OWN_USERID.
    23  */
    24 NSString * const ownUserId = @"pEp_own_userId";
    25 
    26 @implementation PEPTestUtils
    27 
    28 + (PEPIdentity *)foreignPepIdentityWithAddress:(NSString *)address userName:(NSString *)username;
    29 {
    30     return [[PEPIdentity alloc] initWithAddress:address
    31                                          userID:@"UNIT-TEST-USER-ID-FOREIGN-IDENTITY"
    32                                        userName:username
    33                                           isOwn:NO fingerPrint:nil];
    34 }
    35 
    36 + (PEPIdentity *)ownPepIdentityWithAddress:(NSString *)address userName:(NSString *)username;
    37 {
    38     return [[PEPIdentity alloc] initWithAddress:address
    39                                          userID:ownUserId
    40                                        userName:username
    41                                           isOwn:YES fingerPrint:nil];
    42 }
    43 
    44 + (NSString *)loadResourceByName:(NSString *)name;
    45 {
    46     NSURL *url = [[NSBundle bundleForClass:[self class]] URLForResource:name withExtension:nil];
    47     return [NSString stringWithContentsOfURL:url usedEncoding:nil error:nil];
    48 }
    49 
    50 + (NSString *)loadStringFromFileName:(NSString *)fileName;
    51 {
    52     NSString *txtFilePath = [[[NSBundle bundleForClass:[self class]] resourcePath]
    53                              stringByAppendingPathComponent:fileName];
    54     NSString *txtFileContents = [NSString stringWithContentsOfFile:txtFilePath
    55                                                           encoding:NSUTF8StringEncoding error:NULL];
    56     return txtFileContents;
    57 }
    58 
    59 + (NSDictionary *)unarchiveDictionary:(NSString *)fileName
    60 {
    61     NSString *filePath = [[[NSBundle bundleForClass:[self class]]
    62                            resourcePath] stringByAppendingPathComponent:fileName];
    63     NSMutableData *data = [NSMutableData dataWithContentsOfFile:filePath];
    64     NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    65     NSDictionary *dict = [unarchiver decodeObject];
    66     [unarchiver finishDecoding];
    67     return dict;
    68 }
    69 
    70 /**
    71  Converts a given message dict to a version with correct attachments, using PEPAttachment.
    72  Using unarchiveDirectory for a message object will yield the old attachment format,
    73  which was just an array of dictionaries. Now the correct way is to use PEPAttachments.
    74  */
    75 + (void)migrateUnarchivedMessageDictionary:(NSMutableDictionary *)message
    76 {
    77     NSMutableArray *attachments = [NSMutableArray new];
    78     for (NSDictionary *attachmentDict in [message objectForKey:kPepAttachments]) {
    79         PEPAttachment *attachment = [[PEPAttachment alloc]
    80                                      initWithData:[attachmentDict objectForKey:@"data"]];
    81         attachment.filename = [attachmentDict objectForKey:@"filename"];
    82         attachment.mimeType = [attachmentDict objectForKey:@"mimeType"];
    83         [attachments addObject:attachment];
    84     }
    85     [message setValue:[NSArray arrayWithArray:attachments] forKey:kPepAttachments];
    86 }
    87 
    88 + (BOOL)importBundledKey:(NSString *)item session:(PEPSession *)session
    89 {
    90     if (!session) {
    91         session = [PEPSession new];
    92     }
    93 
    94     NSString *txtFileContents = [self loadStringFromFileName:item];
    95     if (!txtFileContents) {
    96         return NO;
    97     } else {
    98         return [session importKey:txtFileContents error:nil];
    99     }
   100 }
   101 
   102 + (PEPMessage * _Nonnull) mailFrom:(PEPIdentity * _Nullable) fromIdent
   103                            toIdent: (PEPIdentity * _Nullable) toIdent
   104                       shortMessage:(NSString *)shortMessage
   105                        longMessage: (NSString *)longMessage
   106                           outgoing:(BOOL) outgoing;
   107 {
   108     PEPMessage *message = [PEPMessage new];
   109     message.from = fromIdent;
   110     message.to = @[toIdent];
   111     message.direction = outgoing ? PEP_dir_outgoing:PEP_dir_incoming;
   112     message.shortMessage = shortMessage;
   113     message.longMessage = longMessage;
   114     return message;
   115 }
   116 
   117 + (NSArray *)pEpWorkFiles;
   118 {
   119     // Only files whose content is affected by tests.
   120     NSString *home = [[[NSProcessInfo processInfo]environment]objectForKey:@"HOME"];
   121     NSString *gpgHome = [home stringByAppendingPathComponent:@".gnupg"];
   122     return @[[home stringByAppendingPathComponent:@".pEp_management.db"],
   123              [home stringByAppendingPathComponent:@".pEp_management.db-shm"],
   124              [home stringByAppendingPathComponent:@".pEp_management.db-wal"],
   125              [gpgHome stringByAppendingPathComponent:@"pubring.gpg"],
   126              [gpgHome stringByAppendingPathComponent:@"secring.gpg"]];
   127 
   128 }
   129 
   130 + (void)cleanUp
   131 {
   132     [PEPSession cleanup];
   133 
   134     for (id path in [self pEpWorkFiles]) {
   135         [self delFilePath:path];
   136     }
   137 }
   138 
   139 #pragma mark - PRIVATE
   140 
   141 + (void)delFilePath:(NSString *)filePath
   142 {
   143     NSFileManager *fileManager = [NSFileManager defaultManager];
   144     NSError *error = nil;
   145     if ([fileManager fileExistsAtPath:filePath]) {
   146         BOOL success = [fileManager removeItemAtPath:filePath error:&error];
   147         if (!success) {
   148             NSLog(@"Error: %@", [error localizedDescription]);
   149         }
   150     }
   151 }
   152 
   153 + (void)undelFileWithPath:(NSString *)path backup:(NSString *)backup;
   154 {
   155     NSParameterAssert(backup);
   156     NSFileManager *fileManager = [NSFileManager defaultManager];
   157     NSString* bpath = [path stringByAppendingString:backup];
   158     BOOL fileExists = [fileManager fileExistsAtPath:bpath];
   159     if (fileExists) {
   160         NSError *error = nil;
   161         BOOL success = [fileManager moveItemAtPath:bpath toPath:path error:&error];
   162         if (!success) {
   163             NSLog(@"Error: %@", [error localizedDescription]);
   164         }
   165     }
   166 }
   167 
   168 @end