pEpForiOSTests/PEPUtilTests.swift
author Xavier Algarra <xavier@pep-project.org>
Tue, 06 Jun 2017 07:49:23 +0200
changeset 2248 75013a586bfd
parent 802 20df8c8709e7
permissions -rw-r--r--
IOS-137 add multiple accounts option with the new login
dirk@387
     1
//
dirk@387
     2
//  PEPUtilTests.swift
dirk@387
     3
//  pEpForiOS
dirk@387
     4
//
dirk@387
     5
//  Created by Dirk Zimmermann on 23/06/16.
dirk@387
     6
//  Copyright © 2016 p≡p Security S.A. All rights reserved.
dirk@387
     7
//
dirk@387
     8
dirk@387
     9
import XCTest
dirk@387
    10
dirk@387
    11
import pEpForiOS
dirk@387
    12
dirk@387
    13
class PEPUtilTests: XCTestCase {
dirk@448
    14
    /**
dirk@448
    15
     Those keys in contacts should not interfere with equality tests.
dirk@448
    16
     */
dirk@527
    17
    let keysNotToCompare = [kPepUsername, kPepIsMe, kPepInReplyTo, kPepReferences]
dirk@448
    18
dirk@387
    19
    var persistentSetup: PersistentSetup!
dirk@387
    20
dirk@387
    21
    override func setUp() {
dirk@387
    22
        super.setUp()
dirk@387
    23
        persistentSetup = PersistentSetup()
dirk@387
    24
    }
dirk@387
    25
dirk@387
    26
    override func tearDown() {
dirk@387
    27
        super.tearDown()
dirk@387
    28
    }
dirk@387
    29
dirk@387
    30
    func testPepContact() {
dirk@449
    31
        let c1 = persistentSetup.model.insertOrUpdateContactEmail(
dirk@448
    32
            "some@some.com", name: "Whatever")
dirk@448
    33
        c1.addressBookID = 1
dirk@387
    34
dirk@387
    35
        let pepC1 = PEPUtil.pepContact(c1)
dirk@387
    36
dirk@387
    37
        XCTAssertEqual(pepC1[kPepAddress] as? String, c1.email)
dirk@387
    38
        XCTAssertEqual(pepC1[kPepUsername] as? String, c1.name)
dirk@431
    39
        XCTAssertNotNil(pepC1[kPepUserID])
dirk@790
    40
        XCTAssertEqual(pepC1[kPepUserID] as? String, String(describing: c1.addressBookID!))
dirk@450
    41
dirk@450
    42
        let c2 = AddressbookContact.init(email: "some@email.com")
dirk@450
    43
        let pepC2 = PEPUtil.pepContact(c2)
dirk@450
    44
        XCTAssertEqual(pepC2[kPepAddress] as? String, c2.email)
dirk@450
    45
        XCTAssertEqual(pepC2[kPepUsername] as? String, "some")
dirk@450
    46
        XCTAssertNil(pepC2[kPepUserID])
dirk@450
    47
dirk@450
    48
        let c3 = AddressbookContact.init(email: "some_iaeuiae@email.com")
dirk@450
    49
        let pepC3 = PEPUtil.pepContact(c3)
dirk@450
    50
        XCTAssertEqual(pepC3[kPepAddress] as? String, c3.email)
dirk@450
    51
        XCTAssertEqual(pepC3[kPepUsername] as? String, "some_iaeuiae")
dirk@450
    52
        XCTAssertNil(pepC3[kPepUserID])
dirk@450
    53
}
dirk@387
    54
dirk@387
    55
    func testPepAttachment() {
dirk@784
    56
        let data = "Just some plaintext".data(using: String.Encoding.utf8)!
dirk@460
    57
        let a1 = persistentSetup.model.insertAttachmentWithContentType(
dirk@387
    58
            "text/plain", filename: "excel.txt",
dirk@387
    59
            data: data)
dirk@387
    60
dirk@387
    61
        let pepA1 = PEPUtil.pepAttachment(a1)
dirk@387
    62
dirk@387
    63
        XCTAssertEqual(pepA1[kPepMimeFilename] as? String, a1.filename)
dirk@387
    64
        XCTAssertEqual(pepA1[kPepMimeType] as? String, a1.contentType)
dirk@790
    65
        XCTAssertEqual(pepA1[kPepMimeData] as? NSData, a1.data)
dirk@387
    66
    }
dirk@387
    67
dirk@387
    68
    func testPepMail() {
dirk@449
    69
        let c1 = persistentSetup.model.insertOrUpdateContactEmail(
dirk@448
    70
            "some@some.com", name: "Whatever")
dirk@431
    71
        c1.addressBookID = 1
dirk@387
    72
dirk@449
    73
        let c2 = persistentSetup.model.insertOrUpdateContactEmail(
dirk@448
    74
            "some@some2.com", name: "Whatever2")
dirk@431
    75
        c2.addressBookID = 2
dirk@387
    76
dirk@784
    77
        let data1 = "Just some plaintext".data(using: String.Encoding.utf8)!
dirk@387
    78
        let a1 = persistentSetup.model.insertAttachmentWithContentType(
dirk@387
    79
            "text/plain", filename: "excel.txt",
dirk@387
    80
            data: data1)
dirk@387
    81
dirk@784
    82
        let data2 = "Just some plaintext2".data(using: String.Encoding.utf8)!
dirk@387
    83
        let a2 = persistentSetup.model.insertAttachmentWithContentType(
dirk@387
    84
            "text/plain", filename: "excel2.txt",
dirk@387
    85
            data: data2)
dirk@387
    86
dirk@790
    87
        let message = persistentSetup.model.insertNewMessage()
dirk@387
    88
        message.subject = "Some subject"
dirk@387
    89
        message.longMessage = "Long message"
dirk@387
    90
        message.longMessageFormatted = "Long HTML"
dirk@387
    91
dirk@790
    92
        message.addToObject(value: c1)
dirk@790
    93
        message.addCcObject(value: c2)
dirk@387
    94
dirk@790
    95
        message.addAttachmentsObject(value: a1)
dirk@790
    96
        message.addAttachmentsObject(value: a2)
dirk@387
    97
dirk@403
    98
        let pepMail = PEPUtil.pepMail(message, outgoing: true)
dirk@403
    99
        XCTAssertEqual(pepMail[kPepOutgoing] as? Bool, true)
dirk@387
   100
dirk@793
   101
        XCTAssertEqual(pepMail[kPepTo]?.objectAt(0) as? NSDictionary,
dirk@790
   102
                       PEPUtil.pepContact(c1) as NSDictionary)
dirk@793
   103
        XCTAssertEqual(pepMail[kPepCC]?.objectAt(0) as? NSDictionary,
dirk@790
   104
                       PEPUtil.pepContact(c2) as NSDictionary)
dirk@387
   105
dirk@793
   106
        XCTAssertEqual(pepMail[kPepAttachments]?.objectAt(0) as? NSDictionary,
dirk@387
   107
                       PEPUtil.pepAttachment(a1))
dirk@793
   108
        XCTAssertEqual(pepMail[kPepAttachments]?.objectAt(1) as? NSDictionary,
dirk@387
   109
                       PEPUtil.pepAttachment(a2))
dirk@387
   110
dirk@387
   111
        XCTAssertEqual(pepMail[kPepShortMessage] as? String, message.subject)
dirk@387
   112
        XCTAssertEqual(pepMail[kPepLongMessage] as? String, message.longMessage)
dirk@387
   113
        XCTAssertEqual(pepMail[kPepLongMessageFormatted] as? String, message.longMessageFormatted)
dirk@387
   114
    }
dirk@407
   115
dirk@407
   116
    func testPantomimeMailFromPep() {
dirk@448
   117
        let receiverToEmail = "unittest.ios.1@peptest.ch"
dirk@448
   118
        let receiverCCEmail = "unittest.ios.2@peptest.ch"
dirk@448
   119
        let receiverBCCEmail = "unittest.ios.3@peptest.ch"
dirk@407
   120
dirk@407
   121
        let pepMailOrig = NSMutableDictionary()
dirk@448
   122
        pepMailOrig[kPepFrom] = PEPUtil.pepContactFromEmail("unittest.ios.4@peptest.ch",
dirk@448
   123
                                                            name: "Unit 004")
dirk@452
   124
        pepMailOrig[kPepTo] = [PEPUtil.pepContactFromEmail(receiverToEmail,
dirk@790
   125
            name: "receiverTo")]
dirk@452
   126
        pepMailOrig[kPepCC] = [PEPUtil.pepContactFromEmail(receiverCCEmail,
dirk@790
   127
                               name: "receiverCC")]
dirk@452
   128
        pepMailOrig[kPepBCC] = [PEPUtil.pepContactFromEmail(receiverBCCEmail,
dirk@790
   129
                                name: "receiverBCC")]
dirk@407
   130
        pepMailOrig[kPepShortMessage] = "Subject"
dirk@407
   131
        pepMailOrig[kPepLongMessage] = "Some Text"
dirk@407
   132
        pepMailOrig[kPepLongMessageFormatted] = "<b>Some HTML</b>"
dirk@407
   133
        pepMailOrig[kPepID] = "<message001@peptest.ch>"
dirk@407
   134
        pepMailOrig[kPepOutgoing] = true
dirk@407
   135
dirk@790
   136
        let pantMail = PEPUtil.pantomimeMailFromPep(pepMailOrig as NSDictionary as! PEPMail)
dirk@407
   137
dirk@407
   138
        XCTAssertEqual(pepMailOrig[kPepID] as? String, pantMail.messageID())
dirk@407
   139
        XCTAssertEqual((pepMailOrig[kPepTo] as! [PEPContact])[0][kPepAddress] as? String,
dirk@407
   140
                       receiverToEmail)
dirk@407
   141
dirk@407
   142
        XCTAssertEqual(pantMail.recipients().count, 3)
dirk@407
   143
dirk@407
   144
        let to = pantMail.recipients()[0] as? CWInternetAddress
dirk@407
   145
        XCTAssertNotNil(to)
dirk@407
   146
        XCTAssertEqual(to!.address(), receiverToEmail)
dirk@407
   147
dirk@407
   148
        let cc = pantMail.recipients()[1] as? CWInternetAddress
dirk@407
   149
        XCTAssertNotNil(cc)
dirk@407
   150
        XCTAssertEqual(cc!.address(), receiverCCEmail)
dirk@407
   151
dirk@407
   152
        let bcc = pantMail.recipients()[2] as? CWInternetAddress
dirk@407
   153
        XCTAssertNotNil(bcc)
dirk@407
   154
        XCTAssertEqual(bcc!.address(), receiverBCCEmail)
dirk@407
   155
dirk@407
   156
        let from = pantMail.from()
dirk@407
   157
        XCTAssertNotNil(from)
dirk@790
   158
        XCTAssertEqual(from!.address(), (pepMailOrig[kPepFrom] as! NSDictionary)[kPepAddress]
dirk@790
   159
            as? String)
dirk@407
   160
dirk@407
   161
        XCTAssertEqual(pantMail.subject(), pepMailOrig[kPepShortMessage] as? String)
dirk@407
   162
dirk@407
   163
        XCTAssertEqual(pantMail.contentType(), Constants.contentTypeMultipartAlternative)
dirk@407
   164
        let content = pantMail.content() as? CWMIMEMultipart
dirk@407
   165
        XCTAssertNotNil(content)
dirk@407
   166
        XCTAssertEqual(content?.count(), 2)
dirk@407
   167
dirk@784
   168
        let partText = content?.part(at: 0)
dirk@407
   169
        XCTAssertNotNil(partText)
dirk@784
   170
        let contentTextData = partText?.content() as? Data
dirk@407
   171
        XCTAssertNotNil(contentTextData)
dirk@784
   172
        let contentText = String.init(data: contentTextData!, encoding: String.Encoding.utf8)
dirk@407
   173
        XCTAssertEqual(contentText, pepMailOrig[kPepLongMessage] as? String)
dirk@407
   174
dirk@784
   175
        let partHtml = content?.part(at: 1)
dirk@407
   176
        XCTAssertNotNil(partHtml)
dirk@784
   177
        let contentHtmlData = partHtml?.content() as? Data
dirk@407
   178
        XCTAssertNotNil(contentHtmlData)
dirk@784
   179
        let contentHtml = String.init(data: contentHtmlData!, encoding: String.Encoding.utf8)
dirk@407
   180
        XCTAssertEqual(contentHtml, pepMailOrig[kPepLongMessageFormatted] as? String)
dirk@407
   181
    }
dirk@407
   182
dirk@407
   183
    func testPepToPantomimeToPepWithoutAttachments() {
dirk@523
   184
        let messageID2 = "messageID2"
dirk@523
   185
        let message2Subject = "hah!"
dirk@527
   186
        let inReplyTo = "inReplyTo"
dirk@523
   187
        let messageIDs = ["messageID1", messageID2, "messageID3"]
dirk@523
   188
dirk@527
   189
        var referenced = messageIDs
dirk@527
   190
        referenced.append(inReplyTo)
dirk@527
   191
dirk@790
   192
        guard let folder = persistentSetup.model.insertOrUpdateFolderName(
dirk@577
   193
            ImapSync.defaultImapInboxName, folderSeparator: nil,
dirk@790
   194
            accountEmail: persistentSetup.accountEmail) else {
dirk@790
   195
                XCTAssertTrue(false)
dirk@790
   196
                return
dirk@790
   197
        }
dirk@407
   198
dirk@523
   199
        let msg = persistentSetup.model.insertNewMessage()
dirk@790
   200
        msg.folder = folder
dirk@523
   201
        msg.messageID = messageID2
dirk@523
   202
        msg.subject = message2Subject
dirk@523
   203
dirk@407
   204
        // Create pEp mail dict
dirk@407
   205
        let pepMailOrig = NSMutableDictionary()
dirk@448
   206
        pepMailOrig[kPepFrom] = PEPUtil.pepContactFromEmail("unittest.ios.4@peptest.ch",
dirk@795
   207
                                                            name: "unit 4")
dirk@452
   208
        pepMailOrig[kPepTo] = [PEPUtil.pepContactFromEmail("unittest.ios.2@peptest.ch",
dirk@452
   209
            name: "unit 2")]
dirk@452
   210
        pepMailOrig[kPepCC] = [PEPUtil.pepContactFromEmail("unittest.ios.3@peptest.ch",
dirk@452
   211
            name: "unit 3")]
dirk@452
   212
        pepMailOrig[kPepBCC] = [PEPUtil.pepContactFromEmail("unittest.ios.4@peptest.ch",
dirk@452
   213
            name: "unit 4")]
dirk@407
   214
        pepMailOrig[kPepShortMessage] = "Subject"
dirk@407
   215
        pepMailOrig[kPepLongMessage] = "Some Text"
dirk@407
   216
        pepMailOrig[kPepLongMessageFormatted] = "<b>Some HTML</b>"
dirk@407
   217
        pepMailOrig[kPepID] = "<message001@peptest.ch>"
dirk@407
   218
        pepMailOrig[kPepOutgoing] = true
dirk@527
   219
        pepMailOrig[kPepInReplyTo] = [inReplyTo]
dirk@523
   220
        pepMailOrig[kPepReferences] = messageIDs
dirk@407
   221
dirk@407
   222
        // Convert to pantomime
dirk@790
   223
        let pantMail = PEPUtil.pantomimeMailFromPep(pepMailOrig as NSDictionary as! PEPMail)
dirk@407
   224
        pantMail.setFolder(CWIMAPFolder.init(name: ImapSync.defaultImapInboxName))
dirk@407
   225
dirk@407
   226
        XCTAssertNotNil(pantMail.from())
dirk@527
   227
        XCTAssertEqual(pantMail.allReferences() as! [String], referenced)
dirk@407
   228
dirk@407
   229
        // Convert to model
dirk@407
   230
        let message = persistentSetup.model.insertOrUpdatePantomimeMail(
dirk@492
   231
            pantMail, accountEmail: persistentSetup.connectionInfo.email,
dirk@492
   232
            forceParseAttachments: true)
dirk@407
   233
dirk@407
   234
        // Check model
dirk@407
   235
        XCTAssertNotNil(message)
dirk@407
   236
        XCTAssertNotNil(message?.from)
dirk@407
   237
        XCTAssertNotNil(message?.messageID)
dirk@492
   238
        XCTAssertEqual(message?.longMessage, pepMailOrig[kPepLongMessage] as? String)
dirk@492
   239
        XCTAssertEqual(message?.longMessageFormatted, pepMailOrig[kPepLongMessageFormatted]
dirk@492
   240
            as? String)
dirk@675
   241
        XCTAssertEqual(message?.references.count, referenced.count)
dirk@523
   242
        if let m = message {
dirk@523
   243
            var counter = 0
dirk@790
   244
            for theRef in m.references {
dirk@802
   245
                guard let ref = theRef as? CdMessageReference else {
dirk@790
   246
                    XCTAssertTrue(false)
dirk@790
   247
                    continue
dirk@790
   248
                }
dirk@675
   249
                XCTAssertEqual(ref.messageID, referenced[counter])
dirk@675
   250
                if counter == 1 {
dirk@790
   251
                    XCTAssertNotNil(ref.message)
dirk@523
   252
                }
dirk@675
   253
                counter = counter + 1
dirk@523
   254
            }
dirk@523
   255
        }
dirk@407
   256
dirk@675
   257
        XCTAssertEqual(message?.to.count, 1)
dirk@407
   258
        let tosOpt = message?.to
dirk@407
   259
        XCTAssertNotNil(tosOpt)
dirk@407
   260
dirk@675
   261
        XCTAssertEqual(message?.cc.count, 1)
dirk@407
   262
        let ccsOpt = message?.cc
dirk@407
   263
        XCTAssertNotNil(ccsOpt)
dirk@407
   264
dirk@675
   265
        XCTAssertEqual(message?.bcc.count, 1)
dirk@407
   266
        let bccsOpt = message?.bcc
dirk@407
   267
        XCTAssertNotNil(bccsOpt)
dirk@407
   268
dirk@407
   269
        // Convert back to pEp
dirk@407
   270
        if let m = message {
dirk@407
   271
            var pepMail = PEPUtil.pepMail(m)
dirk@527
   272
            XCTAssertNil(pepMail[kPepInReplyTo])
dirk@527
   273
            XCTAssertEqual(pepMail[kPepReferences] as! [String], referenced)
dirk@407
   274
            let attachments = pepMail[kPepAttachments] as? NSArray
dirk@407
   275
            XCTAssertTrue(attachments == nil || attachments?.count == 0)
dirk@407
   276
            pepMail[kPepAttachments] = nil
dirk@448
   277
            let pepMail2 = TestUtil.removeUnneededKeysForComparison(
dirk@448
   278
                keysNotToCompare, fromMail: pepMail)
dirk@448
   279
            let pepMailOrig2 = TestUtil.removeUnneededKeysForComparison(
dirk@790
   280
                keysNotToCompare, fromMail: pepMailOrig as NSDictionary as! PEPMail)
dirk@790
   281
            TestUtil.diffDictionaries(pepMail2 as NSDictionary, dict2: pepMailOrig2 as NSDictionary)
dirk@790
   282
            XCTAssertEqual(pepMail2 as NSDictionary, pepMailOrig2 as NSDictionary)
dirk@407
   283
        }
dirk@407
   284
    }
dirk@407
   285
dirk@407
   286
    /**
dirk@407
   287
     Same code as `testPepToPantomimeToPepWithoutAttachments`, but with some attachments.
dirk@407
   288
     */
dirk@407
   289
    func testPepToPantomimeToPepWithAttachments() {
dirk@790
   290
        let _ = persistentSetup.model.insertOrUpdateFolderName(
dirk@577
   291
            ImapSync.defaultImapInboxName, folderSeparator: nil,
dirk@577
   292
            accountEmail: persistentSetup.accountEmail)
dirk@407
   293
dirk@407
   294
        // Some mail constants for later comparison
dirk@407
   295
        let subject = "Subject"
dirk@407
   296
        let longMessage = "Some text"
dirk@407
   297
        let longMessageFormatted = "<b>Some HTML</b>"
dirk@407
   298
dirk@407
   299
        // Create pEp mail dict
dirk@407
   300
        let pepMailOrig = NSMutableDictionary()
dirk@448
   301
        pepMailOrig[kPepFrom] = PEPUtil.pepContactFromEmail("unittest.ios.4@peptest.ch",
dirk@452
   302
                                                            name: "unit 4")
dirk@452
   303
        pepMailOrig[kPepTo] = [PEPUtil.pepContactFromEmail("unittest.ios.1@peptest.ch",
dirk@452
   304
            name: "unit 1")]
dirk@452
   305
        pepMailOrig[kPepCC] = [PEPUtil.pepContactFromEmail("unittest.ios.2@peptest.ch",
dirk@452
   306
            name: "unit 2")]
dirk@452
   307
        pepMailOrig[kPepBCC] = [PEPUtil.pepContactFromEmail("unittest.ios.3@peptest.ch",
dirk@452
   308
            name: "unit 3")]
dirk@407
   309
        pepMailOrig[kPepShortMessage] = subject
dirk@407
   310
        pepMailOrig[kPepLongMessage] = longMessage
dirk@407
   311
        pepMailOrig[kPepLongMessageFormatted] = longMessageFormatted
dirk@407
   312
        pepMailOrig[kPepID] = "<message001@peptest.ch>"
dirk@407
   313
        pepMailOrig[kPepOutgoing] = true
dirk@407
   314
dirk@407
   315
        // Add Attachments
dirk@407
   316
dirk@407
   317
        var attachments: [NSMutableDictionary] = []
dirk@407
   318
dirk@407
   319
        let attachmentFilename1 = "some1.html"
dirk@407
   320
        let attachment1 = NSMutableDictionary()
dirk@407
   321
        attachment1[kPepMimeFilename] = attachmentFilename1
dirk@407
   322
        attachment1[kPepMimeType] = Constants.contentTypeHtml
dirk@407
   323
        attachment1[kPepMimeData] = (pepMailOrig[kPepLongMessageFormatted] as! String)
dirk@784
   324
            .data(using: String.Encoding.utf8)
dirk@407
   325
        attachments.append(attachment1)
dirk@407
   326
dirk@407
   327
        let attachmentFilename2 = "some2.html"
dirk@407
   328
        let attachment2 = NSMutableDictionary()
dirk@407
   329
        attachment2[kPepMimeFilename] = attachmentFilename2
dirk@407
   330
        attachment2[kPepMimeType] = Constants.contentTypeHtml
dirk@784
   331
        attachment2[kPepMimeData] = "<h1>Title only!</h1>".data(using: String.Encoding.utf8)
dirk@407
   332
        attachments.append(attachment2)
dirk@407
   333
dirk@407
   334
        pepMailOrig[kPepAttachments] = attachments
dirk@407
   335
dirk@407
   336
        // Convert to pantomime
dirk@790
   337
        let pantMail = PEPUtil.pantomimeMailFromPep(pepMailOrig as NSDictionary as! PEPMail)
dirk@407
   338
        pantMail.setFolder(CWIMAPFolder.init(name: ImapSync.defaultImapInboxName))
dirk@407
   339
dirk@407
   340
        // Check pantomime
dirk@407
   341
dirk@407
   342
        XCTAssertNotNil(pantMail.from())
dirk@407
   343
        XCTAssertEqual(pantMail.contentType(), Constants.contentTypeMultipartMixed)
dirk@407
   344
dirk@407
   345
        let partMain = pantMail.content() as? CWMIMEMultipart
dirk@407
   346
        XCTAssertNotNil(partMain)
dirk@407
   347
        XCTAssertEqual(partMain?.count(), 3)
dirk@407
   348
dirk@784
   349
        let partAlt = partMain?.part(at: 0)
dirk@407
   350
        XCTAssertEqual(partAlt?.contentType(), Constants.contentTypeMultipartAlternative)
dirk@407
   351
dirk@784
   352
        let partAttachment1 = partMain?.part(at: 1)
dirk@407
   353
        XCTAssertEqual(partAttachment1?.contentType(), Constants.contentTypeHtml)
dirk@407
   354
        XCTAssertEqual(partAttachment1?.filename(), attachmentFilename1)
dirk@407
   355
        XCTAssertNotNil(partAttachment1?.content())
dirk@407
   356
dirk@784
   357
        let partAttachment2 = partMain?.part(at: 2)
dirk@407
   358
        XCTAssertEqual(partAttachment2?.contentType(), Constants.contentTypeHtml)
dirk@407
   359
        XCTAssertEqual(partAttachment2?.filename(), attachmentFilename2)
dirk@407
   360
        XCTAssertNotNil(partAttachment2?.content())
dirk@407
   361
dirk@407
   362
        // Convert to model
dirk@407
   363
        let message = persistentSetup.model.insertOrUpdatePantomimeMail(
dirk@492
   364
            pantMail, accountEmail: persistentSetup.connectionInfo.email,
dirk@492
   365
            forceParseAttachments: true)
dirk@407
   366
dirk@407
   367
        // Check model
dirk@407
   368
        XCTAssertNotNil(message)
dirk@407
   369
        XCTAssertNotNil(message?.from)
dirk@407
   370
        XCTAssertNotNil(message?.messageID)
dirk@407
   371
dirk@407
   372
        XCTAssertEqual(message?.subject, subject)
dirk@407
   373
        XCTAssertEqual(message?.longMessage, longMessage)
dirk@407
   374
        XCTAssertEqual(message?.longMessageFormatted, longMessageFormatted)
dirk@407
   375
dirk@675
   376
        XCTAssertEqual(message?.to.count, 1)
dirk@407
   377
        let tosOpt = message?.to
dirk@407
   378
        XCTAssertNotNil(tosOpt)
dirk@407
   379
dirk@675
   380
        XCTAssertEqual(message?.cc.count, 1)
dirk@407
   381
        let ccsOpt = message?.cc
dirk@407
   382
        XCTAssertNotNil(ccsOpt)
dirk@407
   383
dirk@675
   384
        XCTAssertEqual(message?.bcc.count, 1)
dirk@407
   385
        let bccsOpt = message?.bcc
dirk@407
   386
        XCTAssertNotNil(bccsOpt)
dirk@407
   387
dirk@407
   388
        XCTAssertEqual(message?.attachments.count, 2)
dirk@407
   389
dirk@800
   390
        let modelAttachment1 = message?.attachments.object(at: 0) as? CdAttachment
dirk@407
   391
        XCTAssertNotNil(modelAttachment1)
dirk@407
   392
        XCTAssertEqual(modelAttachment1?.filename, attachmentFilename1)
dirk@407
   393
        XCTAssertEqual(modelAttachment1?.contentType, Constants.contentTypeHtml)
dirk@407
   394
dirk@800
   395
        let modelAttachment2 = message?.attachments.object(at: 1) as? CdAttachment
dirk@407
   396
        XCTAssertNotNil(modelAttachment2)
dirk@407
   397
        XCTAssertEqual(modelAttachment2?.filename, attachmentFilename2)
dirk@407
   398
        XCTAssertEqual(modelAttachment2?.contentType, Constants.contentTypeHtml)
dirk@407
   399
dirk@407
   400
        XCTAssertEqual(modelAttachment1!.filename, attachmentFilename1)
dirk@407
   401
        XCTAssertEqual(modelAttachment2!.filename, attachmentFilename2)
dirk@407
   402
dirk@448
   403
        XCTAssertNotNil(message)
dirk@448
   404
dirk@407
   405
        // Convert back to pEp
dirk@407
   406
        if let m = message {
dirk@407
   407
            var pepMail = PEPUtil.pepMail(m)
dirk@407
   408
            let attachments = pepMail[kPepAttachments] as? NSArray
dirk@407
   409
            XCTAssertEqual(attachments?.count, 2)
dirk@448
   410
            let pepMail2 = TestUtil.removeUnneededKeysForComparison(
dirk@790
   411
                keysNotToCompare, fromMail: pepMail as NSDictionary as! PEPMail)
dirk@448
   412
            let pepMailOrig2 = TestUtil.removeUnneededKeysForComparison(
dirk@790
   413
                keysNotToCompare, fromMail: pepMailOrig as NSDictionary as! PEPMail)
dirk@790
   414
            XCTAssertEqual(pepMail2 as NSDictionary, pepMailOrig2 as NSDictionary)
dirk@407
   415
        }
dirk@407
   416
    }
dirk@421
   417
dirk@421
   418
    func testColorRatingForContact() {
dirk@431
   419
        let unknownContact = AddressbookContact.init(email: "unknownuser@peptest.ch")
dirk@421
   420
        XCTAssertEqual(PEPUtil.colorRatingForContact(unknownContact), PEP_rating_undefined)
dirk@421
   421
dirk@421
   422
        // Create myself
dirk@784
   423
        let backgroundQueue = OperationQueue.init()
dirk@784
   424
        let expMyselfFinished = expectation(description: "expMyselfFinished")
dirk@790
   425
        guard let account = persistentSetup.model.accountByEmail(
dirk@790
   426
            persistentSetup.accountEmail) else {
dirk@790
   427
                XCTAssertTrue(false)
dirk@790
   428
                return
dirk@790
   429
        }
dirk@421
   430
        var identityMyself: NSDictionary? = nil
dirk@790
   431
        PEPUtil.myselfFromAccount(account, queue: backgroundQueue) { identity in
dirk@421
   432
            expMyselfFinished.fulfill()
dirk@421
   433
            identityMyself = identity
dirk@730
   434
        }
dirk@784
   435
        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@421
   436
            XCTAssertNil(error)
dirk@421
   437
            XCTAssertNotNil(identityMyself)
dirk@421
   438
            XCTAssertNotNil(identityMyself?[kPepFingerprint])
dirk@421
   439
        })
dirk@421
   440
dirk@421
   441
        // Check rating for myself, should be at least yellow
dirk@421
   442
        let contact = PEPUtil.insertPepContact(
dirk@421
   443
            identityMyself?.mutableCopy() as! PEPContact, intoModel: persistentSetup.model)
dirk@421
   444
        let rating = PEPUtil.colorRatingForContact(contact)
dirk@421
   445
        XCTAssertGreaterThanOrEqual(rating.rawValue, PEP_rating_reliable.rawValue)
dirk@421
   446
    }
dirk@460
   447
dirk@460
   448
    func testTrustwords() {
dirk@784
   449
        XCTAssertEqual("0".compare("000"), ComparisonResult.orderedAscending)
dirk@784
   450
        XCTAssertEqual("111".compare("000"), ComparisonResult.orderedDescending)
dirk@460
   451
dirk@460
   452
        let fpr1 = "DB4713183660A12ABAFA7714EBE90D44146F62F4"
dirk@460
   453
        let fpr2 = "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
dirk@460
   454
dirk@460
   455
        let session = PEPSession.init()
dirk@460
   456
dirk@460
   457
        XCTAssertEqual(
dirk@460
   458
            PEPUtil.shortTrustwordsForFpr(fpr1, language: "en", session: session),
dirk@460
   459
            "BAPTISMAL BERTRAND DIVERSITY SCOTSWOMAN TRANSDUCER")
dirk@460
   460
        XCTAssertEqual(
dirk@460
   461
            PEPUtil.shortTrustwordsForFpr(fpr2, language: "en", session: session),
dirk@460
   462
            "FROZE EDGEWISE HOTHEADED DERREK BRITNI")
dirk@784
   463
        XCTAssertEqual(fpr1.compare(fpr2), ComparisonResult.orderedDescending)
dirk@460
   464
dirk@790
   465
        let dict1: PEPMail = [kPepFingerprint: fpr1 as AnyObject, kPepUserID: "1" as AnyObject,
dirk@790
   466
                              kPepAddress: "email1" as AnyObject, kPepUsername: "1" as AnyObject]
dirk@790
   467
        let dict2: PEPMail = [kPepFingerprint: fpr2 as AnyObject, kPepUserID: "2" as AnyObject,
dirk@790
   468
                              kPepAddress: "email2" as AnyObject, kPepUsername: "2" as AnyObject]
dirk@460
   469
        let words = PEPUtil.trustwordsForIdentity1(dict1, identity2: dict2,
dirk@460
   470
                                                   language: "en", session: session)
dirk@460
   471
        XCTAssertEqual(words,
dirk@460
   472
                       "FROZE EDGEWISE HOTHEADED DERREK BRITNI BAPTISMAL BERTRAND DIVERSITY SCOTSWOMAN TRANSDUCER")
dirk@460
   473
    }
dirk@467
   474
dirk@481
   475
    func testEncryptedReceivers() {
dirk@467
   476
        let session = PEPSession.init()
dirk@467
   477
dirk@467
   478
        let (origIdentity, _, _, _, receiver4) =
dirk@467
   479
            TestUtil.setupSomeIdentities(session)
dirk@467
   480
        let identity = origIdentity.mutableCopy() as! NSMutableDictionary
dirk@467
   481
        session.mySelf(identity)
dirk@467
   482
        XCTAssertNotNil(identity[kPepFingerprint])
dirk@467
   483
dirk@467
   484
        // Import public key for receiver4
dirk@467
   485
        TestUtil.importKeyByFileName(
dirk@467
   486
            session, fileName: "5A90_3590_0E48_AB85_F3DB__045E_4623_C5D1_EAB6_643E.asc")
dirk@467
   487
dirk@481
   488
        // This might fail the first time the test is run
dirk@481
   489
        // (see ENGINE-41)
dirk@481
   490
        XCTAssertTrue(session.isEncryptedPEPContact(
dirk@790
   491
            identity as NSDictionary as! PEPContact, from: identity as NSDictionary as! PEPContact))
dirk@467
   492
dirk@481
   493
        XCTAssertTrue(session.isEncryptedPEPContact(
dirk@790
   494
            receiver4 as PEPContact, from: identity as NSDictionary as! PEPContact))
dirk@467
   495
    }
dirk@467
   496
dirk@610
   497
    /**
dirk@610
   498
     This needs access to the addressbook, otherwise it will just fail.
dirk@610
   499
     */
dirk@467
   500
    func testInsertPepContact() {
dirk@790
   501
        var addressBookContact: IContact?
dirk@467
   502
        let ab = AddressBook.init()
dirk@467
   503
        let context = persistentSetup.coreDataUtil.privateContext()
dirk@467
   504
dirk@467
   505
        let testBlock = {
dirk@467
   506
            let contacts = ab.allContacts()
dirk@610
   507
            XCTAssertGreaterThan(contacts.count, 0)
dirk@467
   508
            if let first = contacts.first {
dirk@467
   509
                addressBookContact = first
dirk@467
   510
            }
dirk@467
   511
dirk@784
   512
            let expAddressBookTransfered = self.expectation(
dirk@784
   513
                description: "expAddressBookTransfered")
dirk@481
   514
dirk@467
   515
            MiscUtil.transferAddressBook(context, blockFinished: { contacts in
dirk@467
   516
                XCTAssertGreaterThan(contacts.count, 0)
dirk@467
   517
                expAddressBookTransfered.fulfill()
dirk@467
   518
            })
dirk@481
   519
dirk@784
   520
            self.waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@467
   521
                XCTAssertNil(error)
dirk@467
   522
            })
dirk@467
   523
        }
dirk@467
   524
dirk@467
   525
        TestUtil.runAddressBookTest(testBlock, addressBook: ab, testCase: self,
dirk@679
   526
                                    waitTime: TestUtil.waitTime)
dirk@467
   527
        XCTAssertNotNil(addressBookContact)
dirk@467
   528
dirk@481
   529
        if let abContact = addressBookContact {
dirk@481
   530
            let pepContact = NSMutableDictionary()
dirk@481
   531
            pepContact[kPepAddress] = abContact.email
dirk@790
   532
            let contact = PEPUtil.insertPepContact(
dirk@790
   533
                pepContact as NSDictionary as! PEPContact, intoModel: persistentSetup.model)
dirk@481
   534
            XCTAssertNotNil(contact.pepUserID)
dirk@481
   535
            XCTAssertNotNil(contact.addressBookID)
dirk@467
   536
dirk@481
   537
            if let abID = contact.addressBookID {
dirk@790
   538
                XCTAssertEqual(contact.pepUserID, String(describing: abID))
dirk@481
   539
            }
dirk@467
   540
        }
dirk@467
   541
    }
dirk@510
   542
dirk@510
   543
    func testFingerprintForContact() {
dirk@510
   544
        let session = PEPSession.init()
dirk@510
   545
dirk@510
   546
        let (_, _, _, _, receiver4) =
dirk@510
   547
            TestUtil.setupSomeIdentities(session)
dirk@510
   548
dirk@510
   549
        // Import public key for receiver4
dirk@510
   550
        TestUtil.importKeyByFileName(
dirk@510
   551
            session, fileName: "5A90_3590_0E48_AB85_F3DB__045E_4623_C5D1_EAB6_643E.asc")
dirk@510
   552
dirk@510
   553
        let contact = PEPUtil.insertPepContact(receiver4, intoModel: persistentSetup.model)
dirk@510
   554
dirk@510
   555
        XCTAssertNotNil(PEPUtil.fingprprintForContact(contact, session: session))
dirk@510
   556
        XCTAssertNotNil(PEPUtil.fingprprintForPepContact(receiver4, session: session))
dirk@510
   557
    }
dirk@547
   558
dirk@547
   559
    /**
dirk@547
   560
     Just test that there is no crash calling trust functions.
dirk@547
   561
     */
dirk@547
   562
    func testTrustPersonalKey() {
dirk@547
   563
        let session = PEPSession.init()
dirk@547
   564
dirk@547
   565
        let (_, _, _, _, receiver4) =
dirk@547
   566
            TestUtil.setupSomeIdentities(session)
dirk@547
   567
dirk@547
   568
        // Import public key for receiver4
dirk@547
   569
        TestUtil.importKeyByFileName(
dirk@547
   570
            session, fileName: "5A90_3590_0E48_AB85_F3DB__045E_4623_C5D1_EAB6_643E.asc")
dirk@547
   571
dirk@547
   572
        let contact = PEPUtil.insertPepContact(receiver4, intoModel: persistentSetup.model)
dirk@547
   573
dirk@547
   574
        PEPUtil.trustContact(contact)
dirk@547
   575
        PEPUtil.resetTrustForContact(contact)
dirk@547
   576
        PEPUtil.mistrustContact(contact)
dirk@550
   577
        PEPUtil.resetTrustForContact(contact)
dirk@547
   578
    }
dirk@784
   579
}