Files
purchases-ios/Tests/UnitTests/FoundationExtensions/DateExtensionsTests.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

31 lines
903 B
Swift

//
// Created by RevenueCat on 3/4/20.
// Copyright (c) 2020 Purchases. All rights reserved.
//
import Nimble
import XCTest
@testable import RevenueCat
class DateExtensionsTests: TestCase {
func testMillisecondsSince1970ConvertsCorrectlyWithCurrentTime() {
let date = Date()
expect(date.millisecondsSince1970) == UInt64(date.timeIntervalSince1970 * 1000)
}
func testMillisecondsSince1970ConvertsCorrectlyWithFixedTime() {
let secondsSince1970: TimeInterval = 1619555571.0
let millisecondsSince1970UInt64: UInt64 = 1619555571000
let date = Date(timeIntervalSince1970: secondsSince1970)
expect(date.millisecondsSince1970) == millisecondsSince1970UInt64
}
func testMillisecondsSince1970() {
let date = Date()
expect(Date(millisecondsSince1970: date.millisecondsSince1970)).to(beCloseTo(date, within: 0.01))
}
}