pEpObjCTests/PEPMessageUtilTest.m
author Dirk Zimmermann <dz@pep.security>
Wed, 20 Mar 2019 16:40:35 +0100
branchIOS-1480
changeset 902 32760267cd59
parent 867 7dc412058f5c
permissions -rw-r--r--
IOS-1480 Avoid framework imports.
     1 //
     2 //  PEPMessageUtilTest.m
     3 //  pEpObjCAdapterTests
     4 //
     5 //  Created by Andreas Buff on 26.01.18.
     6 //  Copyright © 2018 p≡p. All rights reserved.
     7 //
     8 
     9 #import <XCTest/XCTest.h>
    10 
    11 #import "PEPMessageUtil.h"
    12 #import "PEPObjCAdapterFramework.h"
    13 
    14 @interface PEPMessageUtilTest : XCTestCase
    15 @property pEp_identity *pepIdentitySomeOne;
    16 @property NSDictionary *dictSomeOne;
    17 @property pEp_identity *pepIdentityMe;
    18 @property NSDictionary *dictMe;
    19 @end
    20 
    21 @implementation PEPMessageUtilTest
    22 
    23 - (void)setUp
    24 {
    25     // Someones (not "Me") identity as struct and dict.
    26     self.pepIdentitySomeOne = new_identity("some@some.com",
    27                                            "SOMEONES-FPR_IS_PRETTY_SHORT",
    28                                            "SOMEONES-USER-ID",
    29                                            "Bob 12345");
    30     self.pepIdentitySomeOne->comm_type = PEP_ct_to_be_checked_confirmed;
    31     self.pepIdentitySomeOne->me = false;
    32     self.dictSomeOne = @{kPepAddress:@"some@some.com",
    33                          kPepFingerprint:@"SOMEONES-FPR_IS_PRETTY_SHORT",
    34                          kPepUserID:@"SOMEONES-USER-ID",
    35                          kPepUsername:@"Bob 12345",
    36                          kPepCommType:[NSNumber numberWithInt: PEP_ct_to_be_checked_confirmed],
    37                          kPepIsOwn:[NSNumber numberWithBool: NO]};
    38 
    39     // "Me" identity as struct and dict.
    40     self.pepIdentityMe = new_identity("me@me.com",
    41                                            "MY-FPR_IS_PRETTY_SHORT",
    42                                            "MY-USER-ID",
    43                                            "Me 12345");
    44     self.pepIdentityMe->comm_type = PEP_ct_confirmed;
    45     self.pepIdentityMe->me = true;
    46     self.dictMe = @{kPepAddress:@"me@me.com",
    47                          kPepFingerprint:@"MY-FPR_IS_PRETTY_SHORT",
    48                          kPepUserID:@"MY-USER-ID",
    49                          kPepUsername:@ "Me 12345",
    50                          kPepCommType:[NSNumber numberWithInt: PEP_ct_confirmed],
    51                          kPepIsOwn:[NSNumber numberWithBool: YES]};
    52 }
    53 
    54 #pragma mark - PEP_identityDictToStruct
    55 
    56 - (void)testIdentityDictToStruct_someone
    57 {
    58     pEp_identity *testee = PEP_identityDictToStruct(self.dictSomeOne);
    59     [self assertEqualIdentityStructs:testee second:self.pepIdentitySomeOne shouldFail:NO];
    60 }
    61 
    62 - (void)testIdentityDictToStruct_me
    63 {
    64     pEp_identity *testee= PEP_identityDictToStruct(self.dictMe);
    65     [self assertEqualIdentityStructs:testee second:self.pepIdentityMe shouldFail:NO];
    66 }
    67 
    68 #pragma mark - PEP_identityDictFromStruct
    69 
    70 - (void)testIdentityDictFromStruct_someone
    71 {
    72     NSDictionary *testee = PEP_identityDictFromStruct(self.pepIdentitySomeOne);
    73     [self assertEqualIdentityDicts:testee second:self.dictSomeOne shouldFail:NO];
    74 }
    75 
    76 - (void)testIdentityDictFromStruct_me
    77 {
    78     NSDictionary *testee = PEP_identityDictFromStruct(self.pepIdentityMe);
    79     [self assertEqualIdentityDicts:testee second:self.dictMe shouldFail:NO];
    80 }
    81 
    82 
    83 
    84 #pragma mark - HELPER
    85 
    86 - (void)assertEqualIdentityStructs:(pEp_identity *)first
    87                             second:(pEp_identity *)second
    88                         shouldFail:(BOOL)shouldFail
    89 {
    90     XCTAssert(shouldFail
    91               ? strcmp(first->address, second->address) != 0
    92               : strcmp(first->address, second->address) == 0);
    93     XCTAssert(shouldFail
    94               ? strcmp(first->fpr, second->fpr) != 0
    95               :  strcmp(first->fpr, second->fpr) == 0);
    96     XCTAssert(shouldFail
    97               ? strcmp(first->user_id, second->user_id) != 0
    98               : strcmp(first->user_id, second->user_id) == 0);
    99     XCTAssert(shouldFail
   100               ? strcmp(first->username, second->username) != 0
   101               :  strcmp(first->username, second->username) == 0);
   102     XCTAssert(shouldFail
   103               ? first->comm_type != second->comm_type
   104               :  first->comm_type == second->comm_type);
   105     XCTAssert(shouldFail
   106               ? first->me != second->me
   107               : first->me == second->me);
   108 }
   109 
   110 - (void)assertEqualIdentityDicts:(NSDictionary *)first
   111                           second:(NSDictionary *)second
   112                       shouldFail:(BOOL)shouldFail
   113 {
   114     if (shouldFail) {
   115         XCTAssertNotEqualObjects(first[kPepAddress], second[kPepAddress]);
   116         XCTAssertNotEqualObjects(first[kPepFingerprint], second[kPepFingerprint]);
   117         XCTAssertNotEqualObjects(first[kPepUserID], second[kPepUserID]);
   118         XCTAssertNotEqualObjects(first[kPepUsername], second[kPepUsername]);
   119         XCTAssertNotEqualObjects(first[kPepCommType], second[kPepCommType]);
   120         XCTAssertNotEqualObjects(first[kPepIsOwn], second[kPepIsOwn]);
   121     } else {
   122         XCTAssertEqualObjects(first[kPepAddress], second[kPepAddress]);
   123         XCTAssertEqualObjects(first[kPepFingerprint], second[kPepFingerprint]);
   124         XCTAssertEqualObjects(first[kPepUserID], second[kPepUserID]);
   125         XCTAssertEqualObjects(first[kPepUsername], second[kPepUsername]);
   126         XCTAssertEqualObjects(first[kPepCommType], second[kPepCommType]);
   127         XCTAssertEqualObjects(first[kPepIsOwn], second[kPepIsOwn]);
   128     }
   129 }
   130 
   131 #pragma mark test the helpers
   132 
   133 - (void)testdiffernetIdentityStructsFail
   134 {
   135     [self assertEqualIdentityStructs:self.pepIdentityMe second:self.pepIdentitySomeOne shouldFail:YES];
   136 }
   137 
   138 - (void)testEqualIdentityStructsSucceed
   139 {
   140     [self assertEqualIdentityStructs:self.pepIdentityMe second:self.pepIdentityMe shouldFail:NO];
   141 }
   142 
   143 - (void)testdiffernetIdentityDicsFail
   144 {
   145     [self assertEqualIdentityDicts:self.dictMe second:self.dictSomeOne shouldFail:YES];
   146 }
   147 
   148 - (void)testEqualIdentityDictsSucceed
   149 {
   150     [self assertEqualIdentityDicts:self.dictMe second:self.dictMe shouldFail:NO];
   151 }
   152 
   153 @end