file_timestamps¶
- Status:
● Tested
- Category:
File System
- Function Name:
file_timestamps
Tests file timestamps with various comparison types and formats.
This test function provides comprehensive timestamp validation for files, supporting multiple timestamp types, comparison operations, and flexible date/time formats. It’s crucial for forensic analysis and system behavior validation.
Parameters¶
Parameter |
Type |
Description |
|---|---|---|
|
string |
Required. The file path to examine. Supports glob patterns for dynamic path resolution. |
|
string |
Optional. Type of timestamp to check. Default: “modified”. Options: “modified”, “accessed”, “created”. |
|
string |
Optional. How to compare timestamps. Default: “equals”. Options: “equals”, “before”, “after”, “between”, “within_last”. |
|
string/number |
Optional. Expected timestamp for “equals”, “before”, “after” comparisons. |
|
string |
Optional. strptime format for parsing timestamp strings. |
|
integer |
Optional. Tolerance in seconds for “equals” comparison. |
|
string/number |
Optional. Start time for “between” comparison. |
|
string/number |
Optional. End time for “between” comparison. |
|
string |
Optional. Duration string for “within_last” comparison (e.g., “1h”, “30m”, “2d”). |
Usage Examples¶
File Modification Time¶
tests:
- name: verify_recent_modification
function: file_timestamps
parameter:
dst: "/var/log/application.log"
timestamp_type: "modified"
comparison_type: "within_last"
within_duration: "1h"
description: "Verify log file was modified within the last hour"
Exact Timestamp Verification¶
tests:
- name: verify_creation_time
function: file_timestamps
parameter:
dst: "/tmp/generated_file.txt"
timestamp_type: "created"
comparison_type: "equals"
expected_time: "2024-01-15 14:30:00"
time_format: "%Y-%m-%d %H:%M:%S"
tolerance_seconds: 30
description: "Verify file was created at expected time (±30s)"
Time Range Validation¶
tests:
- name: verify_access_window
function: file_timestamps
parameter:
dst: "/home/user/document.pdf"
timestamp_type: "accessed"
comparison_type: "between"
start_time: "2024-01-15 09:00:00"
end_time: "2024-01-15 17:00:00"
time_format: "%Y-%m-%d %H:%M:%S"
description: "Verify file was accessed during business hours"
Timestamp Types¶
Type |
Description |
Platform Notes |
|---|---|---|
|
File modification time |
Available on all platforms |
|
File access time |
May not be updated on all filesystems |
|
File creation time |
Windows: true creation time; Unix: change time or birth time |
Comparison Types¶
Type |
Parameters Required |
Description |
|---|---|---|
|
|
Timestamp equals expected time (within tolerance) |
|
|
Timestamp is before expected time |
|
|
Timestamp is after expected time |
|
|
Timestamp is between start and end times |
|
|
Timestamp is within specified duration from now |
Time Format Support¶
- Unix Timestamps
Numeric timestamps (seconds since epoch):
expected_time: 1705334400 # Unix timestamp
- ISO Format
Standard ISO date/time formats:
expected_time: "2024-01-15T14:30:00" expected_time: "2024-01-15T14:30:00Z"
- Custom Formats
Use
time_formatfor custom parsing:expected_time: "15/01/2024 2:30 PM" time_format: "%d/%m/%Y %I:%M %p"
- Common Formats
Built-in support for common formats:
%Y-%m-%d %H:%M:%S- “2024-01-15 14:30:00”%Y-%m-%d- “2024-01-15”%m/%d/%Y %H:%M:%S- “01/15/2024 14:30:00”
Duration Formats¶
For within_last comparisons, use these duration formats:
Unit |
Format |
Examples |
|---|---|---|
Seconds |
|
|
Minutes |
|
|
Hours |
|
|
Days |
|
|
Common Use Cases¶
- Log File Monitoring
Verify log files are being updated regularly
- File Creation Tracking
Confirm files are created at expected times during processes
- Access Pattern Analysis
Monitor when files are accessed for security analysis
- Backup Verification
Ensure backup files have expected timestamps
- Process Timing Validation
Verify automated processes complete within expected timeframes
- Forensic Analysis
Analyze file timeline for investigation purposes
Return Values¶
- Success
Returns success when timestamp comparison passes
- Failure
Returns failure when timestamp comparison fails, showing expected vs actual times
- Execution Error
Returns execution error when:
File cannot be found or accessed
Invalid timestamp format or comparison type
Permission denied accessing file metadata
Path resolution fails
Example Results¶
# Success case
result: success
details:
- "modified timestamp is within last 1h"
# Failure case
result: failed
details:
- "modified timestamp mismatch. Expected: 2024-01-15 14:30:00, Got: 2024-01-15 15:45:00"
# Execution error case
result: execution_error
error: "ValueError: Cannot parse timestamp: invalid_date"
context: "Timestamp parsing/comparison error"