162 lines
4.7 KiB
Swift
162 lines
4.7 KiB
Swift
import Testing
|
|
import RandomCpp
|
|
import Foundation
|
|
import Crypto
|
|
import MessageUtils
|
|
|
|
@testable import IoT_Simulator_Core
|
|
|
|
@Test func idealSensor() async throws {
|
|
|
|
let env = PhysicalEnvironment("Delta")
|
|
let truth = PhysicalData(.Temperature, 22)
|
|
|
|
await env.setPhysicalData(DataType.Temperature, truth)
|
|
|
|
let sensor : IdealSensor = IdealSensor(id: 1, sensorType: .Temperature)
|
|
let data = await sensor.read(env)
|
|
|
|
|
|
#expect(data.value == truth.value, "If values match, we are cool")
|
|
|
|
|
|
}
|
|
|
|
@Test func faultyIdealSensor() async throws {
|
|
|
|
let env = PhysicalEnvironment("Delta")
|
|
let truth = PhysicalData(.Temperature, 22)
|
|
|
|
await env.setPhysicalData(DataType.Temperature, truth)
|
|
|
|
let sensor : IdealSensor = IdealSensor(id: 1, sensorType: .Temperature, faulty: true)
|
|
let data = await sensor.read(env)
|
|
|
|
|
|
#expect(data.value != truth.value, "If these match, something is not working")
|
|
|
|
|
|
}
|
|
|
|
@Test func realSensor() async throws {
|
|
|
|
let env = PhysicalEnvironment("Delta")
|
|
let truth = PhysicalData(.Temperature, 22)
|
|
|
|
await env.setPhysicalData(DataType.Temperature, truth)
|
|
|
|
let sensor : Sensor = RealSensor(sensorID: 1, sensorType: .Temperature, faulty: false, meanNoise: 0.5, stdNoise: 0.25, quantizationBits: 3)
|
|
let data = await sensor.read(env)
|
|
|
|
|
|
#expect(data.value - truth.value < 10, "If these match, we are cool")
|
|
|
|
print(data.value)
|
|
|
|
|
|
}
|
|
|
|
@Test func faultyRealSensor() async throws {
|
|
|
|
let env = PhysicalEnvironment("Delta")
|
|
let truth = PhysicalData(.Temperature, 22)
|
|
|
|
await env.setPhysicalData(DataType.Temperature, truth)
|
|
|
|
let sensor : Sensor = RealSensor(sensorID: 1, sensorType: .Temperature, faulty: true, meanNoise: 0.5, stdNoise: 0.25, quantizationBits: 3)
|
|
let data = await sensor.read(env)
|
|
|
|
|
|
#expect(data.value - truth.value > 10, "If these match, something is not working")
|
|
|
|
print(data.value)
|
|
|
|
|
|
}
|
|
|
|
@Test func edgeDevice() async throws {
|
|
|
|
let env = PhysicalEnvironment("Delta")
|
|
let truth = PhysicalData(.Temperature, 22)
|
|
|
|
await env.setPhysicalData(DataType.Temperature, truth)
|
|
|
|
let signKeyPath = "./Private/privateKey.pem"
|
|
|
|
let privateKey = try pem2_P256key(filePath: signKeyPath)
|
|
|
|
let dev: EdgeDevice = EdgeDevice(
|
|
deviceID: 1,
|
|
dataType: .Temperature,
|
|
disconnected: false,
|
|
location: Location(x: 20, y: 10, z: 0),
|
|
dutyCicle: 100098,
|
|
sensors: [
|
|
0: IdealSensor(id: 0, sensorType: DataType.Temperature),
|
|
1: IdealSensor(id: 0, sensorType: DataType.Temperature),
|
|
2: IdealSensor(id: 0, sensorType: DataType.Temperature, faulty: true)
|
|
],
|
|
privateKey: try pem2_P256key(filePath: signKeyPath)
|
|
)
|
|
|
|
let message = try await dev.work(envrionment: env)
|
|
|
|
let signedMessage = try deserializeV1(serializedData: message)
|
|
|
|
print(signedMessage.toString())
|
|
print(signedMessage.signature)
|
|
|
|
#expect(signedMessage != nil, "If this is nil, I don't knwo what's going on")
|
|
#expect(signedMessage.signature != nil, "If signature is nil, something is wrong")
|
|
|
|
|
|
#expect(
|
|
try verifyMessageSignature(message: signedMessage, key: privateKey.publicKey) ,
|
|
"Let's see that signatures match"
|
|
)
|
|
}
|
|
|
|
@Test func edgeDeviceMean() async throws {
|
|
|
|
let env = PhysicalEnvironment("Delta")
|
|
let truth = PhysicalData(.Temperature, 22)
|
|
|
|
await env.setPhysicalData(DataType.Temperature, truth)
|
|
|
|
let signKeyPath = "./Private/privateKey.pem"
|
|
|
|
let privateKey = try pem2_P256key(filePath: signKeyPath)
|
|
|
|
let dev: EdgeDevice = EdgeDevice(
|
|
deviceID: 1,
|
|
dataType: .Temperature,
|
|
disconnected: false,
|
|
location: Location(x: 20, y: 10, z: 0),
|
|
dutyCicle: 100098,
|
|
sensors: [
|
|
0: RealSensor(sensorID: 0, sensorType: .Temperature, faulty: false, meanNoise: 1, stdNoise: 3, quantizationBits: 3),
|
|
1: RealSensor(sensorID: 1, sensorType: .Temperature, faulty: false, meanNoise: 1, stdNoise: 3, quantizationBits: 3),
|
|
2: RealSensor(sensorID: 2, sensorType: .Temperature, faulty: false, meanNoise: 1, stdNoise: 3, quantizationBits: 3),
|
|
],
|
|
privateKey: try pem2_P256key(filePath: signKeyPath)
|
|
)
|
|
|
|
let message = try await dev.work(envrionment: env)
|
|
|
|
let signedMessage = try deserializeV1(serializedData: message)
|
|
|
|
print(signedMessage.toString())
|
|
print(signedMessage.signature)
|
|
|
|
#expect(signedMessage != nil, "If this is nil, I don't knwo what's going on")
|
|
#expect(signedMessage.signature != nil, "If signature is nil, something is wrong")
|
|
|
|
|
|
#expect(
|
|
try verifyMessageSignature(message: signedMessage, key: privateKey.publicKey) ,
|
|
"Let's see that signatures match"
|
|
)
|
|
}
|
|
|
|
|