88 lines
2.1 KiB
Swift
88 lines
2.1 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 = 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: [],
|
|
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))
|
|
],
|
|
signature: Array(repeating: 255, count: 132)
|
|
)
|
|
|
|
let data = try serializeV1(msg: msg)
|
|
|
|
}
|
|
|
|
@Test func serializeDeserializeMessage() 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))
|
|
],
|
|
signature: Array(repeating: 255, count: 132)
|
|
)
|
|
|
|
print(msg.toString())
|
|
|
|
let data = try serializeV1(msg: msg)
|
|
|
|
let bytes = data.map { value in
|
|
return value
|
|
}
|
|
|
|
|
|
|
|
print(bytes.count % 8 == 0)
|
|
|
|
|
|
let msg2 = try derializeV1(serializedData: data)
|
|
print(msg2.toString())
|
|
|
|
|
|
}
|
|
|
|
|