Files
purchases-ios/Tests/UnitTests/SubscriberAttributes/SubscriberAttributeTests.swift
T
NachoSoto d4ecc7b298 DispatchTimeInterval & Date: avoid 32-bit overflows, fix watchOS crashes (#2342)
Fixes #2338, [SDKONCALL-237], [SDK-2974].

`Int` is actually 32 bits in 32-bit platforms, like `watchOS` still is
(sort of).
This fixes 2 potential `Int` overflows: `Date.millisecondsSince1970` and
`DispatchTimeInterval` arithmetic using `nanoseconds`.

See also #2340.

[SDKONCALL-237]:
https://revenuecats.atlassian.net/browse/SDKONCALL-237?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
2023-03-16 13:24:16 -07:00

86 lines
3.1 KiB
Swift

//
// Created by RevenueCat on 2/26/20.
// Copyright (c) 2020 Purchases. All rights reserved.
//
import Nimble
import XCTest
@testable import RevenueCat
class SubscriberAttributeTests: TestCase {
func testInitWithKeyValueSetsRightValues() {
let now = Date()
let dateProvider = MockDateProvider(stubbedNow: now)
let subscriberAttribute = SubscriberAttribute(withKey: "a key",
value: "a value",
dateProvider: dateProvider)
expect(subscriberAttribute.key) == "a key"
expect(subscriberAttribute.value) == "a value"
expect(subscriberAttribute.setTime) == now
expect(subscriberAttribute.isSynced) == false
}
func testAsDictionaryReturnsCorrectFormat() throws {
let subscriberAttribute = Self.mockAttribute
let receivedDictionary = subscriberAttribute.asDictionary()
expect(receivedDictionary.keys).to(haveCount(4))
expect(receivedDictionary["key"] as? String) == subscriberAttribute.key
expect(receivedDictionary["value"] as? String) == subscriberAttribute.value
expect(receivedDictionary["setTime"] as? Date) == subscriberAttribute.setTime
expect((receivedDictionary["isSynced"] as? NSNumber)?.boolValue) == subscriberAttribute.isSynced
}
func testAsBackendDictionaryReturnsCorrectFormat() throws {
let receivedDictionary = Self.mockAttribute.asBackendDictionary()
expect(receivedDictionary.keys).to(haveCount(2))
expect(receivedDictionary["value"] as? String) == Self.mockAttribute.value
expect((receivedDictionary["updated_at_ms"] as? NSNumber)?.uint64Value)
== Self.mockAttribute.setTime.millisecondsSince1970
}
func testInitWithDictionarySetsRightValues() throws {
let key = "some key"
let value = "some value"
let setTime = NSDate()
let isSynced = true
let subscriberDict: [String: NSObject] = [
"key": key as NSString,
"value": value as NSString,
"setTime": setTime,
"isSynced": NSNumber(value: isSynced)
]
let subscriberAttribute = try XCTUnwrap(SubscriberAttribute(dictionary: subscriberDict))
expect(subscriberAttribute.key) == key
expect(subscriberAttribute.value) == value
expect(subscriberAttribute.setTime as NSDate) == setTime
expect(subscriberAttribute.isSynced) == isSynced
}
func testEncodeAndDecode() throws {
let subscriberAttribute = Self.mockAttribute
expect(SubscriberAttribute(dictionary: subscriberAttribute.asDictionary())) == subscriberAttribute
}
}
private extension SubscriberAttributeTests {
static let mockAttribute: SubscriberAttribute = {
let key = "some key"
let value = "some value"
let now = Date(timeIntervalSince1970: 2_000_000_000) // 2033-05-18 03:33:20Z
let dateProvider = MockDateProvider(stubbedNow: now)
return .init(withKey: key, value: value, dateProvider: dateProvider)
}()
}