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