-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
10 changed files
with
137 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
Tests/UnitTests/FoundationExtensions/DispatchTimeIntervalExtensionsTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// | ||
// Copyright RevenueCat Inc. All Rights Reserved. | ||
// | ||
// Licensed under the MIT License (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://opensource.org/licenses/MIT | ||
// | ||
// DispatchTimeIntervalExtensionsTests.swift | ||
// | ||
// Created by Nacho Soto on 3/16/23. | ||
|
||
import Nimble | ||
import XCTest | ||
|
||
@testable import RevenueCat | ||
|
||
// swiftlint:disable identifier_name | ||
|
||
class DispatchTimeIntervalExtensionsTests: TestCase { | ||
|
||
func testIsLessThan() { | ||
let a: DispatchTimeInterval = .seconds(1) | ||
let b: DispatchTimeInterval = .seconds(2) | ||
|
||
expect(a) < b | ||
} | ||
|
||
func testIsGreaterThan() { | ||
let a: DispatchTimeInterval = .seconds(2) | ||
let b: DispatchTimeInterval = .seconds(1) | ||
|
||
expect(a) > b | ||
} | ||
|
||
func testSecondsToMilliseconds() { | ||
expect(DispatchTimeInterval.seconds(5).milliseconds) == 5_000 | ||
} | ||
|
||
func testSecondsToNanoseconds() { | ||
expect(DispatchTimeInterval.seconds(5).nanoseconds) == 5_000_000_000 | ||
} | ||
|
||
func testAddingSeconds() { | ||
let a: DispatchTimeInterval = .seconds(2) | ||
let b: DispatchTimeInterval = .seconds(1) | ||
|
||
expect(a + b) == .seconds(3) | ||
} | ||
|
||
func testAddingMilliseconds() { | ||
let a: DispatchTimeInterval = .milliseconds(2) | ||
let b: DispatchTimeInterval = .milliseconds(1) | ||
|
||
expect(a + b) == .milliseconds(3) | ||
} | ||
|
||
func testAddingNanoseconds() { | ||
let a: DispatchTimeInterval = .nanoseconds(2_000_000) | ||
let b: DispatchTimeInterval = .nanoseconds(1_000_000) | ||
|
||
expect(a + b) == .milliseconds(3) | ||
} | ||
|
||
func testSubstractingSeconds() { | ||
let a: DispatchTimeInterval = .seconds(3) | ||
let b: DispatchTimeInterval = .seconds(1) | ||
|
||
expect(a - b) == .seconds(2) | ||
} | ||
|
||
func testSubstractingMilliseconds() { | ||
let a: DispatchTimeInterval = .milliseconds(3) | ||
let b: DispatchTimeInterval = .milliseconds(1) | ||
|
||
expect(a - b) == .milliseconds(2) | ||
} | ||
|
||
func testSubstractingNanoseconds() { | ||
let a: DispatchTimeInterval = .nanoseconds(3_000_000) | ||
let b: DispatchTimeInterval = .nanoseconds(1_000_000) | ||
|
||
expect(a - b) == .milliseconds(2) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters