Skip to content

Commit

Permalink
adding check for devices on network (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion committed Jun 27, 2024
1 parent 9ac6680 commit 2260628
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Sources/SublimationBonjour/NWTXTRecord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,9 @@
}
return value
}

internal func getKeys() -> any Sequence<String> {
self.dictionary.keys

Check warning on line 42 in Sources/SublimationBonjour/NWTXTRecord.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SublimationBonjour/NWTXTRecord.swift#L41-L42

Added lines #L41 - L42 were not covered by tests
}
}
#endif
18 changes: 18 additions & 0 deletions Sources/SublimationBonjour/SublimationKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ extension SublimationKey {
}
return [prefix, value.description].joined(separator: "_")
}

internal static func isValid(_ string: String) -> Bool {
if SublimationKeyValues(rawValue: string) != nil {
return true
}
if string.hasPrefix(SublimationKeyValues.address.rawValue),
string.count > SublimationKeyValues.address.rawValue.count + 1 {

Check notice on line 62 in Sources/SublimationBonjour/SublimationKey.swift

View check run for this annotation

codefactor.io / CodeFactor

Sources/SublimationBonjour/SublimationKey.swift#L62

Code should be indented using one tab or 2 spaces. (indentation_width)
let indexString = string.suffix(
from: string.index(
string.startIndex,
offsetBy: SublimationKeyValues.address.rawValue.count + 1
)
)

return (Int(indexString) ?? .min) >= 0
}
return false
}
}

extension SublimationKeyValues {
Expand Down
14 changes: 13 additions & 1 deletion Sources/SublimationBonjour/TXTRecord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ internal protocol TXTRecord {
var count: Int { get }
init(_ dictionary: [String: String])
func getStringEntry(for key: String) -> String?
func getKeys() -> any Sequence<String>
}

extension TXTRecord {
Expand Down Expand Up @@ -89,6 +90,9 @@ extension TXTRecord {
defaultPort: defaultPort,
defaultTLS: defaultTLS
)
guard let configuration else {
return []
}
logger?.log { $0.debug("Parsing \(configuration.count) Addresses") }
return (0 ..< configuration.count).compactMap { index -> URL? in
let host: String? = self.getEntry(for: .address(index)).value
Expand Down Expand Up @@ -117,7 +121,7 @@ extension TXTRecord {
logger: LoggingActor?,
defaultPort: Int,
defaultTLS: Bool
) -> URL.Configuration {
) -> URL.Configuration? {
var offset = 0

let portEntry = self.getEntry(for: .port, of: Int.self)
Expand All @@ -138,6 +142,14 @@ extension TXTRecord {
logger?.log { $0.warning("Port Entry is invalid: \(invalidTLSEntryString)") }
}

let isValid = self.getKeys().allSatisfy { key in
SublimationKey.isValid(key)
}

guard isValid else {
return nil
}

return self.urlConfiguration(at: offset, port: port, isTLS: isTLS, logger: logger)
}
}
4 changes: 4 additions & 0 deletions Tests/SublimationBonjourTests/MockTXTRecord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import Foundation
import XCTest

internal struct MockTXTRecord: TXTRecord {
func getKeys() -> any Sequence<String> {

Check notice on line 35 in Tests/SublimationBonjourTests/MockTXTRecord.swift

View check run for this annotation

codefactor.io / CodeFactor

Tests/SublimationBonjourTests/MockTXTRecord.swift#L35

All declarations should specify Access Control Level keywords explicitly. (explicit_acl)

Check notice on line 35 in Tests/SublimationBonjourTests/MockTXTRecord.swift

View check run for this annotation

codefactor.io / CodeFactor

Tests/SublimationBonjourTests/MockTXTRecord.swift#L35

An 'other_method' should not be placed amongst the type content(s) 'instance_property'. (type_contents_order)
dictionary.keys
}

internal let dictionary: [String: String]

internal var count: Int {
Expand Down
8 changes: 7 additions & 1 deletion Tests/SublimationBonjourTests/TXTRecordTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ import Foundation
import XCTest

internal class TXTRecordTests: XCTestCase {
internal func testBadRecord() {
let record = MockTXTRecord(["Serial Number": UUID().uuidString])
let urls = record.urls(defaultPort: 0, defaultTLS: false, logger: nil)

XCTAssert(urls.isEmpty)
}

internal func testInit() {
let expectation = expectation(description: "Filter")

Expand Down Expand Up @@ -111,4 +118,3 @@ internal class TXTRecordTests: XCTestCase {
XCTAssertEqual(urls, expectedURLs)
}
}

0 comments on commit 2260628

Please sign in to comment.