121 lines
3.8 KiB
Swift
121 lines
3.8 KiB
Swift
import Foundation
|
|
|
|
import Testing
|
|
|
|
@testable import MessageUtils
|
|
|
|
@Test func serializeMessage() async throws {
|
|
// Write your test here and use APIs like `#expect(...)` to check expected conditions.
|
|
let msg = SignedMessage(
|
|
version: 1,
|
|
messageType: .KEEPALIVE,
|
|
devType: .EDGE_SENSOR,
|
|
RESERVED: 0,
|
|
signType: .P521,
|
|
timestamp: Date(),
|
|
devID: 1,
|
|
location: Location(x: 10, y: 20, z: 1),
|
|
fields: [],
|
|
signature: Array(repeating: 255, count: 132)
|
|
)
|
|
|
|
let data = try serializeV1(msg: msg)
|
|
|
|
}
|
|
@Test func serializeMessageWithField() async throws {
|
|
// Write your test here and use APIs like `#expect(...)` to check expected conditions.
|
|
let msg = Message(
|
|
version: 1,
|
|
messageType: .KEEPALIVE,
|
|
devType: .EDGE_SENSOR,
|
|
RESERVED: 0,
|
|
signType: .P521,
|
|
timestamp: Date(),
|
|
devID: 1,
|
|
location: Location(x: 10, y: 20, z: 1),
|
|
fields: [
|
|
Field(key: Array("valueOfLife".utf8), value: Array("42".utf8)),
|
|
Field(key: Array("valueOfLife".utf8), value: Array("42".utf8)),
|
|
]
|
|
)
|
|
|
|
let data = try serializeV1(msg: msg)
|
|
|
|
}
|
|
@Test func serializeDeserializeMessage() async throws {
|
|
let keyPath = "./Private/privateKey.pem"
|
|
let key = try pem2key(filePath: keyPath)
|
|
let publicKey = key.publicKey
|
|
|
|
// Write your test here and use APIs like `#expect(...)` to check expected conditions.
|
|
let msg = Message(
|
|
version: 1,
|
|
messageType: .KEEPALIVE,
|
|
devType: .EDGE_SENSOR,
|
|
RESERVED: 0,
|
|
signType: .P521,
|
|
timestamp: Date(),
|
|
devID: 1,
|
|
location: Location(x: 10, y: 20, z: 1),
|
|
fields: [
|
|
Field(key: Array("valueOfLife".utf8), value: Array("42".utf8)),
|
|
Field(key: Array("valueOfLife".utf8), value: Array("42".utf8)),
|
|
Field(key: Array("69".utf8), value: Array("Nice".utf8)),
|
|
Field(key: Array("valueOfLife".utf8), value: Array("42".utf8)),
|
|
Field(key: Array("Live Love".utf8), value: Array("Laugh".utf8)),
|
|
]
|
|
)
|
|
|
|
print(msg.toString())
|
|
|
|
var data = serializeV1(msg: msg)
|
|
|
|
let bytes = data.map { value in
|
|
return value
|
|
}
|
|
|
|
let actualBytes = countBytes(msg: msg)
|
|
|
|
print("Number of Bytes for the message: \(bytes.count)")
|
|
print("Number of Computed Bytes: \(actualBytes)")
|
|
|
|
let signature = try signMessage(msgData: data, signType: msg.signType, key: key)
|
|
data.append(contentsOf: signature)
|
|
|
|
let msg2 = try deserializeV1(serializedData: data)
|
|
let authenticMessage = try verifyMessageSignature(message: msg2, key: publicKey)
|
|
|
|
#expect(authenticMessage, "If this is not true, then we are doing some deserialization errors")
|
|
print(msg2.toString())
|
|
|
|
}
|
|
@Test func serializeMessageForLaterUse() async throws {
|
|
// Write your test here and use APIs like `#expect(...)` to check expected conditions.
|
|
|
|
let keyPath = "./Private/privateKey.pem"
|
|
let key = try pem2key(filePath: keyPath)
|
|
|
|
let msg = Message(
|
|
version: 1,
|
|
messageType: .KEEPALIVE,
|
|
devType: .EDGE_SENSOR,
|
|
RESERVED: 0,
|
|
signType: .P521,
|
|
timestamp: Date(),
|
|
devID: 1,
|
|
location: Location(x: 10, y: 20, z: 1),
|
|
fields: [
|
|
Field(key: Array("valueOfLife".utf8), value: Array("42".utf8)),
|
|
Field(key: Array("Live Love".utf8), value: Array("Laugh".utf8)),
|
|
]
|
|
)
|
|
|
|
var data = try serializeV1(msg: msg)
|
|
|
|
let signature = try signMessage(msgData: data, signType: msg.signType, key: key)
|
|
data.append(Data(signature))
|
|
|
|
try data.write(to: URL(filePath: "./Private/signedMessage/Message.bin"))
|
|
|
|
}
|