Skip to content

Commit

Permalink
allow simulated dates
Browse files Browse the repository at this point in the history
  • Loading branch information
erikng committed Jul 25, 2024
1 parent c17e0db commit 7038593
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Requires macOS 12.0 and higher.
- macOS device is 14.4.1: Required OS: 14.5 - Target macOS 14.4.1 requiredInstallationDate of 2024-06-03 00:00:00 +0000
- macOS device is 14.5: Required OS: 14.5 - Fully updated
- Addresses [612](https://github.com/macadmins/nudge/issues/612)
- To ease testing, you can now pass `-simulate-date` as an argument to override the built-in date check.
- Ex: `-simulate-date "2024-07-25T00:00:00Z"`

### Changed
- 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.
Expand All @@ -44,7 +46,7 @@ Requires macOS 12.0 and higher.
- Addresses [610](https://github.com/macadmins/nudge/issues/610) and [613](https://github.com/macadmins/nudge/issues/613)
- 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.
- Addresses [615](https://github.com/macadmins/nudge/issues/615)
- Banned shortcut keys - including the ability to quit the application - are now allowed when passing `-simulate-os-version` or `-simulate-hardware-id`
- 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`

### Fixed
- Several components in the Github Actions were triggering deprecation warnings. These have been addressed by updating to the latest version of these components
Expand Down
4 changes: 2 additions & 2 deletions Nudge/UI/Main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
if nudgePrimaryState.shouldExit {
return .terminateNow
} else {
if (CommandLineUtilities().simulateOSVersion() != nil) || (CommandLineUtilities().simulateHardwareID() != nil) {
if (CommandLineUtilities().simulateOSVersion() != nil) || (CommandLineUtilities().simulateHardwareID() != nil) || (CommandLineUtilities().simulateDate() != nil) {
LogManager.warning("Attempt to exit Nudge was allowed due to simulation arguments.", logger: uiLog)
return .terminateNow
}
Expand Down Expand Up @@ -538,7 +538,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

private func detectBannedShortcutKeys(with event: NSEvent) -> Bool {
if (CommandLineUtilities().simulateOSVersion() != nil) || (CommandLineUtilities().simulateHardwareID() != nil) { return false }
if (CommandLineUtilities().simulateOSVersion() != nil) || (CommandLineUtilities().simulateHardwareID() != nil) || (CommandLineUtilities().simulateDate() != nil) { return false }
guard NSApplication.shared.isActive else { return false }
switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) {
// Disable CMD + H - Hides Nudge
Expand Down
1 change: 1 addition & 0 deletions Nudge/Utilities/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class LogState {
var hasLoggedRequireMajorUgprade = false
var hasLoggedScreenshotIconMode = false
var hasLoggedSimpleMode = false
var hasLoggedSimulatedDate = false
var hasLoggedUnitTestingMode = false
}

Expand Down
26 changes: 25 additions & 1 deletion Nudge/Utilities/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,10 @@ struct CommandLineUtilities {
return argumentPassed
}

func simulateDate() -> String? {
return valueForArgument("-simulate-date")
}

func simulateHardwareID() -> String? {
return valueForArgument("-simulate-hardware-id")
}
Expand Down Expand Up @@ -592,11 +596,31 @@ struct DateManager {
}

func getCurrentDate() -> Date {
switch Calendar.current.identifier {
let dateFormatterISO8601 = ISO8601DateFormatter()

if (CommandLineUtilities().simulateDate() != nil) {
// Try to parse the provided ISO8601 string
if let date = dateFormatterISO8601.date(from: CommandLineUtilities().simulateDate()!) {
if !nudgeLogState.hasLoggedSimulatedDate {
LogManager.notice("Simulated Date set via -simulated-date, returning \(CommandLineUtilities().simulateDate()!)", logger: uiLog)
nudgeLogState.hasLoggedSimulatedDate = true
}
return date
} else {
if !nudgeLogState.hasLoggedSimulatedDate {
LogManager.error("Failed to parse -simulated-date, returning current date.", logger: uiLog)
nudgeLogState.hasLoggedSimulatedDate = true
}
return Date()
}
} else {
// If no string is provided, return the current date based on calendar
switch Calendar.current.identifier {
case .buddhist, .japanese, .gregorian, .coptic, .ethiopicAmeteMihret, .hebrew, .iso8601, .indian, .islamic, .islamicCivil, .islamicTabular, .islamicUmmAlQura, .persian:
return dateFormatterISO8601.date(from: dateFormatterISO8601.string(from: Date())) ?? Date()
default:
return Date()
}
}
}

Expand Down

0 comments on commit 7038593

Please sign in to comment.