From bf71a4ec34cf51dc760dd7b2ad4e5c1f1316aef6 Mon Sep 17 00:00:00 2001 From: halo Date: Sat, 24 Feb 2024 13:32:10 +0100 Subject: [PATCH] Bring back Advisor unit tests --- LinkLiarTests/AdvisorTests.swift | 63 ++++++++++++++++---------------- LinkTools/Config/Arbiter.swift | 6 ++- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/LinkLiarTests/AdvisorTests.swift b/LinkLiarTests/AdvisorTests.swift index 24316a2..f9cbf59 100644 --- a/LinkLiarTests/AdvisorTests.swift +++ b/LinkLiarTests/AdvisorTests.swift @@ -5,35 +5,36 @@ import XCTest @testable import LinkLiar class AdvisorTests: XCTestCase { -// -// func testRandom() { -// let dictionary = [ -// "e1:e1:e1:e1:e1:e1": -// ["action": "random"] -// ] -// let config = Config.Reader(dictionary) -// let interface = Interface("e1:e1:e1:e1:e1:e1") -// interface.overrideSoftMacInTests = MAC("e2:e2:e2:e2:e2:e2") -// let arbiter = Config.Arbiter(config: config, hardMAC: interface.hardMAC) -// arbiter.overrideAddressInTests = MAC("aa:aa:aa:aa:aa:aa") -// let advisor = Advisor(interface: interface, arbiter: arbiter) -// -// let expectedAddress = MAC("aa:aa:aa:aa:aa:aa") -// XCTAssertEqual(expectedAddress, advisor.addressForStandard) -// } -// -// func testRandomWhenAlreadyRandom() { -// let dictionary = [ -// "e1:e1:e1:e1:e1:e1": -// ["action": "random"] -// ] -// let config = Config.Reader(dictionary) -// let interface = Interface("e1:e1:e1:e1:e1:e1") -// interface.overrideSoftMacInTests = MAC("00:03:93:e2:e2:e2") -// let arbiter = Config.Arbiter(config: config, hardMAC: interface.hardMAC) -// arbiter.overrideAddressInTests = MAC("aa:aa:aa:aa:aa:aa") -// let advisor = Advisor(interface: interface, arbiter: arbiter) -// -// XCTAssertNil(advisor.addressForStandard) -// } + func testRandom() { + let dictionary = [ + "e1:e1:e1:e1:e1:e1": + ["action": "random"] + ] + let config = Config.Reader(dictionary) + let interface = Interface(bsd: BSD("en0")!, + hardMAC: MAC("e1:e1:e1:e1:e1:e1")!, + stubSoftMAC: MAC("e2:e2:e2:e2:e2:e2"))! + let arbiter = Config.Arbiter(config: config, hardMAC: interface.hardMAC) + arbiter.stubAddressInTests = MAC("aa:aa:aa:aa:aa:aa") + let advisor = Advisor(interface: interface, arbiter: arbiter) + + let expectedAddress = MAC("aa:aa:aa:aa:aa:aa") + XCTAssertEqual(expectedAddress, advisor.address) + } + + func testRandomWhenAlreadyRandom() { + let dictionary = [ + "e1:e1:e1:e1:e1:e1": + ["action": "random"] + ] + let config = Config.Reader(dictionary) + let interface = Interface(bsd: BSD("en0")!, + hardMAC: MAC("e1:e1:e1:e1:e1:e1")!, + stubSoftMAC: MAC("00:03:93:e2:e2:e2"))! + let arbiter = Config.Arbiter(config: config, hardMAC: interface.hardMAC) + arbiter.stubAddressInTests = MAC("aa:aa:aa:aa:aa:aa") + let advisor = Advisor(interface: interface, arbiter: arbiter) + + XCTAssertNil(advisor.address) + } } diff --git a/LinkTools/Config/Arbiter.swift b/LinkTools/Config/Arbiter.swift index 0d2a9e9..1b8ceac 100644 --- a/LinkTools/Config/Arbiter.swift +++ b/LinkTools/Config/Arbiter.swift @@ -2,6 +2,8 @@ // SPDX-License-Identifier: MIT extension Config { + /// Determines the priority of configuration values. + /// E.g. an interface specific value overrides general defaults. class Arbiter { // MARK: Class Methods @@ -62,12 +64,12 @@ extension Config { config.policy(hardMAC).exceptionAddress } - var overrideAddressInTests: MAC? + var stubAddressInTests: MAC? // MARK: Instance Methods func randomAddress() -> MAC { - if let override = overrideAddressInTests { + if let override = stubAddressInTests { Log.debug("Using static random address in tests") return override }