-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix a division-by-zero crash in the VPN code (#823)
Task/Issue URL: https://app.asana.com/0/1203137811378537/1207299387361586/f iOS PR: duckduckgo/iOS#2862 macOS PR: duckduckgo/macos-browser#2785 What kind of version bump will this require?: Patch ## Description Fixes a crash caused by a division by zero.
- Loading branch information
1 parent
7d5ddc9
commit ada5f68
Showing
2 changed files
with
79 additions
and
6 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
65 changes: 65 additions & 0 deletions
65
Tests/NetworkProtectionTests/NetworkProtectionConnectionBandwidthAnalyzerTests.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,65 @@ | ||
// | ||
// NetworkProtectionConnectionBandwidthAnalyzerTests.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// 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 XCTest | ||
@testable import NetworkProtection | ||
@testable import NetworkProtectionTestUtils | ||
|
||
final class NetworkProtectionConnectionBandwidthAnalyzerTests: XCTestCase { | ||
|
||
func testBytesPerSecondCalculationIsCorrect() { | ||
let deltaSeconds = TimeInterval(5) | ||
let oldDate = Date() | ||
let newDate = oldDate.addingTimeInterval(deltaSeconds) | ||
let oldRx = UInt64(1000) | ||
let newRx = UInt64(2000) | ||
let oldTx = UInt64(2000) | ||
let newTx = UInt64(6000) | ||
let expectedRx = Double(newRx - oldRx) / deltaSeconds | ||
let expectedTx = Double(newTx - oldTx) / deltaSeconds | ||
|
||
let oldSnapshot = NetworkProtectionConnectionBandwidthAnalyzer.Snapshot(rxBytes: oldRx, txBytes: oldTx, date: oldDate) | ||
let newSnapshot = NetworkProtectionConnectionBandwidthAnalyzer.Snapshot(rxBytes: newRx, txBytes: newTx, date: newDate) | ||
|
||
let (rx, tx) = NetworkProtectionConnectionBandwidthAnalyzer.bytesPerSecond(newer: newSnapshot, older: oldSnapshot) | ||
|
||
XCTAssertEqual(rx, expectedRx) | ||
XCTAssertEqual(tx, expectedTx) | ||
} | ||
|
||
func testBytesPerSecondNoTimeDelta() { | ||
let deltaSeconds = TimeInterval(0) | ||
let oldDate = Date() | ||
let newDate = oldDate.addingTimeInterval(deltaSeconds) | ||
let oldRx = UInt64(1000) | ||
let newRx = UInt64(2000) | ||
let oldTx = UInt64(2000) | ||
let newTx = UInt64(6000) | ||
let expectedRx = Double(0) | ||
let expectedTx = Double(0) | ||
|
||
let oldSnapshot = NetworkProtectionConnectionBandwidthAnalyzer.Snapshot(rxBytes: oldRx, txBytes: oldTx, date: oldDate) | ||
let newSnapshot = NetworkProtectionConnectionBandwidthAnalyzer.Snapshot(rxBytes: newRx, txBytes: newTx, date: newDate) | ||
|
||
let (rx, tx) = NetworkProtectionConnectionBandwidthAnalyzer.bytesPerSecond(newer: newSnapshot, older: oldSnapshot) | ||
|
||
XCTAssertEqual(rx, expectedRx) | ||
XCTAssertEqual(tx, expectedTx) | ||
} | ||
} |