Skip to content

Commit 7038593

Browse files
committed
allow simulated dates
1 parent c17e0db commit 7038593

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Requires macOS 12.0 and higher.
3535
- macOS device is 14.4.1: Required OS: 14.5 - Target macOS 14.4.1 requiredInstallationDate of 2024-06-03 00:00:00 +0000
3636
- macOS device is 14.5: Required OS: 14.5 - Fully updated
3737
- Addresses [612](https://github.com/macadmins/nudge/issues/612)
38+
- To ease testing, you can now pass `-simulate-date` as an argument to override the built-in date check.
39+
- Ex: `-simulate-date "2024-07-25T00:00:00Z"`
3840

3941
### Changed
4042
- The `Actively Exploited` logic internally within Nudge and the UI on the left sidebar will show `True` if any previous updates missing on the device had active exploits.
@@ -44,7 +46,7 @@ Requires macOS 12.0 and higher.
4446
- Addresses [610](https://github.com/macadmins/nudge/issues/610) and [613](https://github.com/macadmins/nudge/issues/613)
4547
- When `showRequiredDate` is set to `True` and the admin is using the default values for `requiredInstallationDisplayFormat`, Nudge will attempt to understand the current locale and display the menu item appropriately.
4648
- Addresses [615](https://github.com/macadmins/nudge/issues/615)
47-
- Banned shortcut keys - including the ability to quit the application - are now allowed when passing `-simulate-os-version` or `-simulate-hardware-id`
49+
- Banned shortcut keys - including the ability to quit the application - are now allowed when passing `-simulate-os-version` or `-simulate-hardware-id` or `-simulate-date`
4850

4951
### Fixed
5052
- Several components in the Github Actions were triggering deprecation warnings. These have been addressed by updating to the latest version of these components

Nudge/UI/Main.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
155155
if nudgePrimaryState.shouldExit {
156156
return .terminateNow
157157
} else {
158-
if (CommandLineUtilities().simulateOSVersion() != nil) || (CommandLineUtilities().simulateHardwareID() != nil) {
158+
if (CommandLineUtilities().simulateOSVersion() != nil) || (CommandLineUtilities().simulateHardwareID() != nil) || (CommandLineUtilities().simulateDate() != nil) {
159159
LogManager.warning("Attempt to exit Nudge was allowed due to simulation arguments.", logger: uiLog)
160160
return .terminateNow
161161
}
@@ -538,7 +538,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
538538
}
539539

540540
private func detectBannedShortcutKeys(with event: NSEvent) -> Bool {
541-
if (CommandLineUtilities().simulateOSVersion() != nil) || (CommandLineUtilities().simulateHardwareID() != nil) { return false }
541+
if (CommandLineUtilities().simulateOSVersion() != nil) || (CommandLineUtilities().simulateHardwareID() != nil) || (CommandLineUtilities().simulateDate() != nil) { return false }
542542
guard NSApplication.shared.isActive else { return false }
543543
switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) {
544544
// Disable CMD + H - Hides Nudge

Nudge/Utilities/Logger.swift

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class LogState {
6161
var hasLoggedRequireMajorUgprade = false
6262
var hasLoggedScreenshotIconMode = false
6363
var hasLoggedSimpleMode = false
64+
var hasLoggedSimulatedDate = false
6465
var hasLoggedUnitTestingMode = false
6566
}
6667

Nudge/Utilities/Utils.swift

+25-1
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ struct CommandLineUtilities {
431431
return argumentPassed
432432
}
433433

434+
func simulateDate() -> String? {
435+
return valueForArgument("-simulate-date")
436+
}
437+
434438
func simulateHardwareID() -> String? {
435439
return valueForArgument("-simulate-hardware-id")
436440
}
@@ -592,11 +596,31 @@ struct DateManager {
592596
}
593597

594598
func getCurrentDate() -> Date {
595-
switch Calendar.current.identifier {
599+
let dateFormatterISO8601 = ISO8601DateFormatter()
600+
601+
if (CommandLineUtilities().simulateDate() != nil) {
602+
// Try to parse the provided ISO8601 string
603+
if let date = dateFormatterISO8601.date(from: CommandLineUtilities().simulateDate()!) {
604+
if !nudgeLogState.hasLoggedSimulatedDate {
605+
LogManager.notice("Simulated Date set via -simulated-date, returning \(CommandLineUtilities().simulateDate()!)", logger: uiLog)
606+
nudgeLogState.hasLoggedSimulatedDate = true
607+
}
608+
return date
609+
} else {
610+
if !nudgeLogState.hasLoggedSimulatedDate {
611+
LogManager.error("Failed to parse -simulated-date, returning current date.", logger: uiLog)
612+
nudgeLogState.hasLoggedSimulatedDate = true
613+
}
614+
return Date()
615+
}
616+
} else {
617+
// If no string is provided, return the current date based on calendar
618+
switch Calendar.current.identifier {
596619
case .buddhist, .japanese, .gregorian, .coptic, .ethiopicAmeteMihret, .hebrew, .iso8601, .indian, .islamic, .islamicCivil, .islamicTabular, .islamicUmmAlQura, .persian:
597620
return dateFormatterISO8601.date(from: dateFormatterISO8601.string(from: Date())) ?? Date()
598621
default:
599622
return Date()
623+
}
600624
}
601625
}
602626

0 commit comments

Comments
 (0)