167 lines
4.5 KiB
Swift
167 lines
4.5 KiB
Swift
import Testing
|
|
import RandomCpp
|
|
import Foundation
|
|
import Crypto
|
|
|
|
@testable import IoT_Simulator_Core
|
|
|
|
@Test func idealSensor() async throws {
|
|
|
|
let env = PhysicalEnvironment("Delta")
|
|
let truth = PhysicalData(.Temperature, 22)
|
|
|
|
env.setPhysicalData(DataType.Temperature, truth)
|
|
|
|
let sensor : Sensor = Sensor(id: 1, sensorType: .Temperature)
|
|
let data = 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)
|
|
|
|
env.setPhysicalData(DataType.Temperature, truth)
|
|
|
|
let sensor : Sensor = Sensor(id: 1, sensorType: .Temperature, faulty: true)
|
|
let data = 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)
|
|
|
|
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 = 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)
|
|
|
|
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 = 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)
|
|
|
|
env.setPhysicalData(DataType.Temperature, truth)
|
|
|
|
let signKeyPath = "./Private/privateKey.pem"
|
|
|
|
let privateKey = try pem2key(filePath: signKeyPath)
|
|
|
|
let dev: EdgeDevice = EdgeDevice(
|
|
deviceID: "EDG-001",
|
|
dataType: .Temperature,
|
|
disconnected: false,
|
|
location: Location3D(20, 10, 0),
|
|
dutyCicle: 100098,
|
|
sensors: [
|
|
0: Sensor(id: 0, sensorType: DataType.Temperature),
|
|
1: Sensor(id: 0, sensorType: DataType.Temperature),
|
|
2: Sensor(id: 0, sensorType: DataType.Temperature, faulty: true)
|
|
],
|
|
privateKey: privateKey
|
|
)
|
|
|
|
let message = dev.work(envrionment: env)
|
|
message.signMessage(key: dev.privateKey)
|
|
|
|
|
|
#expect(message != nil, "If this is nil, I don't knwo what's going on")
|
|
#expect(message.signature != nil, "If signature is nil, something is wrong")
|
|
|
|
print(message.description)
|
|
print(message.signature)
|
|
|
|
#expect(
|
|
try verifySignature(
|
|
signature:message.signature,
|
|
string: message.toDataCompatibleString(),
|
|
key: dev.privateKey.publicKey
|
|
),
|
|
"Let's see that signatures match"
|
|
)
|
|
}
|
|
|
|
@Test func edgeDeviceMean() async throws {
|
|
|
|
let env = PhysicalEnvironment("Delta")
|
|
let truth = PhysicalData(.Temperature, 22)
|
|
|
|
env.setPhysicalData(DataType.Temperature, truth)
|
|
|
|
let signKeyPath = "./Private/privateKey.pem"
|
|
|
|
let privateKey = try pem2key(filePath: signKeyPath)
|
|
|
|
let dev: EdgeDevice = EdgeDevice(
|
|
deviceID: "EDG-001",
|
|
dataType: .Temperature,
|
|
disconnected: false,
|
|
location: Location3D(20, 10, 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: privateKey
|
|
)
|
|
|
|
let message = dev.work(envrionment: env)
|
|
message.signMessage(key: dev.privateKey)
|
|
|
|
|
|
#expect(message != nil, "If this is nil, I don't knwo what's going on")
|
|
#expect(message.signature != nil, "If signature is nil, something is wrong")
|
|
|
|
print(message.description)
|
|
print(message.signature)
|
|
|
|
#expect(
|
|
try verifySignature(
|
|
signature:message.signature,
|
|
string: message.toDataCompatibleString(),
|
|
key: dev.privateKey.publicKey
|
|
),
|
|
"Let's see that signatures match"
|
|
)
|
|
}
|
|
|
|
|