Skip to content

Commit

Permalink
Fixes #225 - Use a properly defined user agent
Browse files Browse the repository at this point in the history
* Set a proper user agent
* Add fallback when UA comes out nil
* Remove unused Bundle extension
* Return nil when asciification fails
* Add whitespace handling and further emoji test case
* Explicitly check if string is already in ASCII
* Rename factory to builder
* Use allSatisfy instead of reduce
* Use key path syntax
  • Loading branch information
Johennes authored Oct 28, 2022
1 parent ba4064c commit 09b2f93
Show file tree
Hide file tree
Showing 12 changed files with 306 additions and 57 deletions.
157 changes: 101 additions & 56 deletions ElementX.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
"version" : "1.5.0"
}
},
{
"identity" : "devicekit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/devicekit/DeviceKit",
"state" : {
"revision" : "20e0991f3975916ab0f6d58db84d8bc64f883537",
"version" : "4.7.0"
}
},
{
"identity" : "dtcoretext",
"kind" : "remoteSourceControl",
Expand Down
18 changes: 18 additions & 0 deletions ElementX/Sources/Other/Extensions/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,22 @@ extension String {
}
return abs(hash)
}

var isASCII: Bool {
allSatisfy(\.isASCII)
}

func asciified() -> String? {
guard !isASCII else {
return self
}
guard !canBeConverted(to: .ascii) else {
return nil
}
let mutableString = NSMutableString(string: self)
guard CFStringTransform(mutableString, nil, "Any-Latin; Latin-ASCII; [:^ASCII:] Remove" as CFString, false) else {
return nil
}
return mutableString.trimmingCharacters(in: .whitespaces)
}
}
71 changes: 71 additions & 0 deletions ElementX/Sources/Other/UserAgentBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation
import UIKit

#if !os(OSX)
import DeviceKit
#endif

final class UserAgentBuilder {
class func makeASCIIUserAgent() -> String? {
makeUserAgent()?.asciified()
}

private class func makeUserAgent() -> String? {
let clientName = ElementInfoPlist.cfBundleDisplayName
let clientVersion = ElementInfoPlist.cfBundleShortVersionString

#if os(iOS)
return String(
format: "%@/%@ (%@; iOS %@; Scale/%0.2f)",
clientName,
clientVersion,
Device.current.safeDescription,
UIDevice.current.systemVersion,
UIScreen.main.scale
)
#elseif os(tvOS)
return String(
format: "%@/%@ (%@; tvOS %@; Scale/%0.2f)",
clientName,
clientVersion,
Device.current.safeDescription,
UIDevice.current.systemVersion,
UIScreen.main.scale
)
#elseif os(watchOS)
return String(
format: "%@/%@ (%@; watchOS %@; Scale/%0.2f)",
clientName,
clientVersion,
Device.current.safeDescription,
WKInterfaceDevice.current.systemVersion,
WKInterfaceDevice.currentDevice.screenScale
)
#elseif os(OSX)
return String(
format: "%@/%@ (Mac; Mac OS X %@)",
clientName,
clientVersion,
NSProcessInfo.processInfo.operatingSystemVersionString
)
#else
return nil
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ class UserSessionStore: UserSessionStoreProtocol {
let builder = ClientBuilder()
.basePath(path: baseDirectory.path)
.username(username: credentials.userID)

.userAgent(userAgent: UserAgentBuilder.makeASCIIUserAgent() ?? "unknown")

do {
let client: Client = try await Task.dispatch(on: .global()) {
let client = try builder.build()
Expand Down
1 change: 1 addition & 0 deletions ElementX/SupportingFiles/target.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ targets:
- package: DesignKit
- package: AnalyticsEvents
- package: AppAuth
- package: DeviceKit
- package: DTCoreText
- package: KeychainAccess
- package: Kingfisher
Expand Down
2 changes: 2 additions & 0 deletions IntegrationTests/SupportingFiles/target.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ targets:
- target: ElementX
- package: MatrixRustSDK
linkType: static
- package: DeviceKit
linkType: static
- package: DTCoreText
linkType: static
- package: KeychainAccess
Expand Down
2 changes: 2 additions & 0 deletions UITests/SupportingFiles/target.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ targets:
linkType: static
- package: AppAuth
linkType: static
- package: DeviceKit
linkType: static
- package: DTCoreText
linkType: static
- package: KeychainAccess
Expand Down
61 changes: 61 additions & 0 deletions UnitTests/Sources/StringTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import XCTest

@testable import ElementX

class StringTests: XCTestCase {
func testEmptyIsAscii() {
XCTAssertTrue("".isASCII)
}

func testSpaceIsAscii() {
XCTAssertTrue("".isASCII)
}

func testJohnnyIsAscii() {
XCTAssertTrue("johnny".isASCII)
}

func testJöhnnyIsNotAscii() {
XCTAssertFalse("jöhnny".isASCII)
}

func testJ🅾️hnnyIsNotAscii() {
XCTAssertFalse("j🅾️hnny".isASCII)
}

func testAsciiStaysAscii() {
XCTAssertEqual("johnny".asciified(), "johnny")
}

func testÖBecomesO() {
XCTAssertEqual("jöhnny".asciified(), "johnny")
}

func testÅBecomesA() {
XCTAssertEqual("jåhnny".asciified(), "jahnny")
}

func test1️⃣2️⃣3️⃣Becomes123() {
XCTAssertEqual("1️⃣2️⃣3️⃣".asciified(), "123")
}

func testStripsTheHeartbreakHotel() {
XCTAssertEqual("Heartbreak Hotel 🏩".asciified(), "Heartbreak Hotel")
}
}
35 changes: 35 additions & 0 deletions UnitTests/Sources/UserAgentBuilderTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import XCTest

@testable import ElementX

class UserAgentBuilderTests: XCTestCase {
func testIsNotNil() {
XCTAssertNotNil(UserAgentBuilder.makeASCIIUserAgent())
}

func testContainsClientName() {
let userAgent = UserAgentBuilder.makeASCIIUserAgent()
XCTAssert(userAgent?.contains(ElementInfoPlist.cfBundleDisplayName) == true, "\(userAgent ?? "nil") does not contain client name")
}

func testContainsClientVersion() {
let userAgent = UserAgentBuilder.makeASCIIUserAgent()
XCTAssert(userAgent?.contains(ElementInfoPlist.cfBundleShortVersionString) == true, "\(userAgent ?? "nil") does not contain client version")
}
}
1 change: 1 addition & 0 deletions changelog.d/225.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Set a proper user agent
3 changes: 3 additions & 0 deletions project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ packages:
AppAuth:
url: https://github.com/openid/AppAuth-iOS
majorVersion: 1.5.0
DeviceKit:
url: https://github.com/devicekit/DeviceKit
majorVersion: 4.7.0
DTCoreText:
url: https://github.com/Cocoanetics/DTCoreText
majorVersion: 1.6.26
Expand Down

0 comments on commit 09b2f93

Please sign in to comment.