Christian Risi 711ad7b5c8 V0.6.9 Arroyo Toad
Added support for P256 Curve and suppressed support for P521 Curve that will be reimplemented later
2024-12-07 17:58:38 +00:00

123 lines
4.0 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: .P256,
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/privateKey256.pem"
let key = try pem2_P256key(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: .P256,
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/privateKey256.pem"
let key = try pem2_P256key(filePath: keyPath)
let msg = Message(
version: 1,
messageType: .KEEPALIVE,
devType: .EDGE_SENSOR,
RESERVED: 0,
signType: .P256,
timestamp: Date(),
devID: 120,
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)),
Field(key: Array("Covfefe".utf8), value: Array("1.20f".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"))
try key.publicKey.pemRepresentation.write(to: URL(filePath: "./Private/signedMessage/public-key256.pem"), atomically: true, encoding: String.Encoding.utf8)
}