Skip to content

Commit

Permalink
Fixing deprecation issue: 'SecTrustGetCertificateAtIndex' was depreca…
Browse files Browse the repository at this point in the history
…ted in watchOS 8.0: renamed to 'SecTrustCopyCertificateChain(_:)'

Adding test for regex used for OpenHABStateDescription.numberPattern
Adding swift 5.7 regex for OpenHABRootViewController.uiCommandAction()
LocalizationsTests migrated to swift 5.7 regex
  • Loading branch information
timbms committed Jul 31, 2024
1 parent 5a021be commit b39efe1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public class OpenHABStateDescription {
self.options = options ?? []

// Remove transformation instructions (e.g. for 'MAP(foo.map):%s' keep only '%s')

let regexPattern = /^[A-Z]+(\(.*\))?:(.*)$/.ignoresCase()
if let tobeSearched {
if let firstMatch = tobeSearched.firstMatch(of: regexPattern){
if let firstMatch = tobeSearched.firstMatch(of: regexPattern) {
numberPattern = String(firstMatch.2)
} else {
numberPattern = tobeSearched
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,11 @@ public class ServerCertificateManager: ServerTrustManager, ServerTrustEvaluating
func getLeafCertificate(trust: SecTrust?) -> SecCertificate? {
// Returns the leaf certificate from a SecTrust object (that is always the
// certificate at index 0).
var result: SecCertificate?

if let trust {
if SecTrustGetCertificateCount(trust) > 0 {
result = SecTrustGetCertificateAtIndex(trust, 0)
return result
} else {
return nil
}
if let trust, SecTrustGetCertificateCount(trust) > 0, let certificates = SecTrustCopyCertificateChain(trust) as? [SecCertificate] {
certificates[0]
} else {
return nil
nil
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ final class OpenHABCoreGeneralTests: XCTestCase {
XCTAssertEqual(urlc, URL(string: "http://192.169.2.1/icon/switch?state=OFF&format=SVG"), "Check endpoint creation")
}

@available(iOS 16.0, *)
func testLabelValue() {
let widget = OpenHABWidget()
widget.label = "llldl [llsl]"
XCTAssertEqual(widget.labelValue, "llsl")
widget.label = "llllsl[kkks] llls"
XCTAssertEqual(widget.labelValue, "kkks")
}

func testOpenHABStateDescription() {
let openHABStateDescription = OpenHABStateDescription(minimum: 0.0, maximum: 1.0, step: 0.2, readOnly: true, options: nil, pattern: "MAP(foo.map):%s")
XCTAssertEqual(openHABStateDescription.numberPattern, "%s")
}
}
47 changes: 20 additions & 27 deletions openHAB/OpenHABRootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,34 +212,27 @@ class OpenHABRootViewController: UIViewController {

private func uiCommandAction(_ command: String) {
os_log("navigateCommandAction: %{PUBLIC}@", log: .notifications, type: .info, command)
let pattern = "^(/basicui/app\\?.*|/.*|.*)$"

do {
let regex = try NSRegularExpression(pattern: pattern, options: [])
let nsString = command as NSString
let results = regex.matches(in: command, options: [], range: NSRange(location: 0, length: nsString.length))

if let match = results.first {
let pathRange = match.range(at: 1)
let path = nsString.substring(with: pathRange)
os_log("navigateCommandAction path: %{PUBLIC}@", log: .notifications, type: .info, path)
if currentView != webViewController {
switchView(target: .webview)
}
if path.starts(with: "/basicui/app?") {
// TODO: this is a sitemap, we should use the native renderer
// temp hack right now to just use a webview
webViewController.loadWebView(force: true, path: path)
} else if path.starts(with: "/") {
// have the webview load this path itself
webViewController.loadWebView(force: true, path: path)
} else {
// have the mainUI handle the navigation
webViewController.navigateCommand(path)
}
let regexPattern = /^(\/basicui\/app\\?.*|\/.*|.*)$/
if let firstMatch = command.firstMatch(of: regexPattern) {
let path = String(firstMatch.1)
os_log("navigateCommandAction path: %{PUBLIC}@", log: .notifications, type: .info, path)
if currentView != webViewController {
switchView(target: .webview)
}
} catch {
os_log("Invalid regex: %{PUBLIC}@", log: .notifications, type: .error, error.localizedDescription)
if path.starts(with: "/basicui/app?") {
// TODO: this is a sitemap, we should use the native renderer
// temp hack right now to just use a webview
webViewController.loadWebView(force: true, path: path)
} else if path.starts(with: "/") {
// have the webview load this path itself
webViewController.loadWebView(force: true, path: path)
} else {
// have the mainUI handle the navigation
webViewController.navigateCommand(path)
}

} else {
os_log("Invalid regex: %{PUBLIC}@", log: .notifications, type: .error, command)
}
}

Expand Down
26 changes: 11 additions & 15 deletions openHABTestsSwift/LocalizationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,18 @@ class LocalizationTests: XCTestCase {
for language in LocalizationTests.localizations {
print("Testing language: '\(language)'.")
for tuple in LocalizationTests.localizedFormatStrings {
do {
guard let translation = tuple.key.localized(for: language)?.replacingOccurrences(of: "%%", with: "") else {
XCTFail("Failed to get translation for key '\(tuple.key)' in language '\(language)'.")
continue
}

XCTAssertNotEqual(translation, "__MISSING__", "Missing translation for key '\(tuple.key)' in language '\(language)'.")
let formatSpecifiersRegEx = try NSRegularExpression(pattern: "%(?:\\d+\\$)?[+-]?(?:[lh]{0,2})(?:[qLztj])?(?:[ 0]|'.{1})?\\d*(?:\\.\\d?)?[@dDiuUxXoOfeEgGcCsSpaAFn]")
let numberOfMatches = formatSpecifiersRegEx.numberOfMatches(in: translation, options: [], range: NSRange(location: 0, length: translation.utf16.count))
XCTAssertEqual(numberOfMatches, tuple.arguments.count, "Invalid number of format specifiers for key '\(tuple.key)' in language '\(language)'.")
} catch {
XCTFail("Failed to create regular expression for key '\(tuple.key)' in language '\(language)'.")
guard let translation = tuple.key.localized(for: language)?.replacingOccurrences(of: "%%", with: "") else {
XCTFail("Failed to get translation for key '\(tuple.key)' in language '\(language)'.")
continue
}
let translation = tuple.key.localizedWithFormat(for: language, arguments: tuple.arguments)
XCTAssertNotNil(translation, "Failed to get translation for key '\(tuple.key)' in language '\(language)'.")
print("Translation: \(tuple.key) = \(translation ?? "FAILED")")
XCTAssertNotEqual(translation, "__MISSING__", "Missing translation for key '\(tuple.key)' in language '\(language)'.")
let regex = /%(?:\d+\$)?[+-]?(?:[lh]{0,2})(?:[qLztj])?(?:[ 0]|'.{1})?\d*(?:\\.\d?)?[@dDiuUxXoOfeEgGcCsSpaAFn]/
let numberOfMatches = translation.matches(of: regex).count
XCTAssertEqual(numberOfMatches, tuple.arguments.count, "Invalid number of format specifiers for key '\(tuple.key)' in language '\(language)'.")

let translationResult = tuple.key.localizedWithFormat(for: language, arguments: tuple.arguments)
XCTAssertNotNil(translationResult, "Failed to get translation for key '\(tuple.key)' in language '\(language)'.")
print("Translation: \(tuple.key) = \(translation)")
}
}
}
Expand Down

0 comments on commit b39efe1

Please sign in to comment.