Statistics
| Branch: | Tag: | Revision:

chatsecureios / ChatSecureTests / OTROMEMOTestModule.swift @ db493e51

History | View | Annotate | Download (4.3 KB)

1
//
2
//  OTROMEMOTestModule.swift
3
//  ChatSecure
4
//
5
//  Created by David Chiles on 10/7/16.
6
//  Copyright © 2016 Chris Ballinger. All rights reserved.
7
//
8

    
9
import XCTest
10
import XMPPFramework
11

    
12
protocol OTROMEMOTestModuleProtocol: class {
13
    func username() -> String
14
    func receiveKeyData(_ keyData: [OMEMOKeyData], iv: Data, fromJID: XMPPJID, senderDeviceId: UInt32, payload: Data?, elementId: String?)
15
    func bundle() -> OMEMOBundle
16
}
17

    
18
class OTROMEMOTestModule: OMEMOModule {
19

    
20
    weak var otherUser:OTROMEMOTestModuleProtocol?
21
    var thisUser:TestUser!
22
    
23
    override var xmppStream:XMPPStream {
24
        get {
25
            let stream = XMPPStream()
26
            stream.myJID = XMPPJID(string:self.thisUser.account.username)
27
            return stream
28
        }
29
    }
30
    
31
    /** Manually called after all the otherUser and thisUser are setup */
32
    override func xmppStreamDidAuthenticate(_ sender: XMPPStream) {
33
        let bundle = otherUser?.bundle()
34
        XCTAssertNotNil(bundle)
35
        let device = bundle?.deviceId
36
        XCTAssertNotNil(device)
37
        let otherUserStr = otherUser!.username()
38
        let otherJID = XMPPJID(string:otherUserStr)!
39
        let ourJID = XMPPJID(string:thisUser.account.username)!
40
        //After authentication fake receiving devices from other buddy
41
        self.omemoStorage.storeDeviceIds([NSNumber(value: device! as UInt32)], for: otherJID)
42
        self.omemoStorage.storeDeviceIds([NSNumber(value: (thisUser.signalOMEMOCoordinator.fetchMyBundle()?.deviceId)!)], for: ourJID)
43
    }
44
    
45
    /** When fetching a bundle for device and jid we return that device given to us in the other user struct*/
46
    override func fetchBundle(forDeviceId deviceId: UInt32, jid: XMPPJID, elementId: String?) {
47
        self.moduleQueue.async { 
48
            if self.otherUser?.bundle().deviceId == deviceId {
49
                let multicastDelegate = self.value(forKey: "multicastDelegate")!
50
                //Empty responses so not nil and have correct elementID.
51
                let response = XMPPIQ(type: "get", to: nil, elementID: elementId, child: nil)
52
                let outgoing = XMPPIQ(type: "get", to: nil, elementID: elementId, child: nil)
53
                (multicastDelegate as AnyObject).omemo!(self, fetchedBundle: self.otherUser!.bundle(), from: jid, responseIq: response, outgoingIq: outgoing)
54
                
55
            }
56
        }
57
    }
58
    
59
    /** When we send key data we automtically route that data to the other user to decrypto*/
60
    override func sendKeyData(_ keyData: [OMEMOKeyData], iv: Data, to toJID: XMPPJID, payload: Data?, elementId: String?) {
61
        
62
        self.otherUser?.receiveKeyData(keyData, iv: iv, fromJID: XMPPJID(string:thisUser.account.username)!, senderDeviceId:(thisUser.signalOMEMOCoordinator.fetchMyBundle()?.deviceId)!, payload: payload, elementId: elementId)
63
        //self.otherUser.signalOMEMOCoordinator.omemo(self, receivedKeyData: keyData, iv: iv, senderDeviceId: (thisUser.signalOMEMOCoordinator.fetchMyBundle()?.deviceId)!, fromJID: XMPPJID.jidWithString(thisUser.account.username), payload: payload, message: dummyMessage)
64
    }
65
    
66
    override func removeDeviceIds(_ deviceIds: [NSNumber], elementId: String?) {
67
        self.moduleQueue.async {
68
            let multicastDelegate = self.value(forKey: "multicastDelegate")!
69
            let element = XMPPIQ(type: "result", to: self.xmppStream.myJID, elementID: elementId)
70
            (multicastDelegate as AnyObject).omemo!(self, deviceListUpdate: [NSNumber](), from:self.xmppStream.myJID!, incomingElement:element)
71
        }
72
    }
73
}
74

    
75
extension OTROMEMOTestModule: OTROMEMOTestModuleProtocol {
76
    func username() -> String {
77
        return self.thisUser.account.username
78
    }
79
    
80
    func receiveKeyData(_ keyData: [OMEMOKeyData], iv: Data, fromJID: XMPPJID, senderDeviceId:UInt32, payload: Data?, elementId: String?) {
81
        let dummyMessage = XMPPMessage(type: "chat", elementID: "1234")
82
        dummyMessage.addAttribute(withName: "from", stringValue: fromJID.full)
83
        dummyMessage.addAttribute(withName: "to", stringValue: username())
84
        self.thisUser.signalOMEMOCoordinator.omemo(self, receivedKeyData: keyData, iv: iv, senderDeviceId: senderDeviceId, from: fromJID, payload: payload, message: dummyMessage)
85
    }
86
    
87
    func bundle() -> OMEMOBundle {
88
        return self.thisUser.signalOMEMOCoordinator.fetchMyBundle()!
89
    }
90
}