| 1 |
#!/usr/bin/env python3 |
| 2 |
"""Tests for the Delphi TDateTime implementation.""" |
| 3 |
|
| 4 |
import decimal |
| 5 |
import unittest |
| 6 |
|
| 7 |
from dfdatetime import delphi_date_time |
| 8 |
|
| 9 |
|
| 10 |
class DelphiDateTimeEpochTest(unittest.TestCase): |
| 11 |
"""Tests for the Delphi TDateTime epoch.""" |
| 12 |
|
| 13 |
def testInitialize(self): |
| 14 |
"""Tests the __init__ function.""" |
| 15 |
delphi_date_time_epoch = delphi_date_time.DelphiDateTimeEpoch() |
| 16 |
self.assertIsNotNone(delphi_date_time_epoch) |
| 17 |
|
| 18 |
|
| 19 |
class DelphiDateTimeInvalidYear(delphi_date_time.DelphiDateTime): |
| 20 |
"""Delphi TDateTime timestamp for testing invalid year.""" |
| 21 |
|
| 22 |
def _CopyDateTimeFromString(self, time_string): |
| 23 |
"""Copies a date and time from a string. |
| 24 |
|
| 25 |
Args: |
| 26 |
time_string (str): date and time value formatted as: |
| 27 |
YYYY-MM-DD hh:mm:ss.######[+-]##:## |
| 28 |
|
| 29 |
Where # are numeric digits ranging from 0 to 9 and the seconds |
| 30 |
fraction can be either 3 or 6 digits. The time of day, seconds |
| 31 |
fraction and time zone offset are optional. The default time zone |
| 32 |
is UTC. |
| 33 |
|
| 34 |
Returns: |
| 35 |
dict[str, int]: date and time values, such as year, month, day of month, |
| 36 |
hours, minutes, seconds, microseconds. |
| 37 |
|
| 38 |
Raises: |
| 39 |
ValueError: if the time string is invalid or not supported. |
| 40 |
""" |
| 41 |
return { |
| 42 |
"year": 10000, |
| 43 |
"month": 1, |
| 44 |
"day_of_month": 2, |
| 45 |
"hours": 0, |
| 46 |
"minutes": 0, |
| 47 |
"seconds": 0, |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
class DelphiDateTimeTest(unittest.TestCase): |
| 52 |
"""Tests for the Delphi TDateTime timestamp.""" |
| 53 |
|
| 54 |
# pylint: disable=protected-access |
| 55 |
|
| 56 |
def testProperties(self): |
| 57 |
"""Tests the properties.""" |
| 58 |
delphi_date_time_object = delphi_date_time.DelphiDateTime( |
| 59 |
timestamp=41443.8263953 |
| 60 |
) |
| 61 |
self.assertEqual(delphi_date_time_object.timestamp, 41443.8263953) |
| 62 |
|
| 63 |
delphi_date_time_object = delphi_date_time.DelphiDateTime() |
| 64 |
self.assertIsNone(delphi_date_time_object.timestamp) |
| 65 |
|
| 66 |
def testGetNormalizedTimestamp(self): |
| 67 |
"""Tests the _GetNormalizedTimestamp function.""" |
| 68 |
delphi_date_time_object = delphi_date_time.DelphiDateTime( |
| 69 |
timestamp=41443.8263953 |
| 70 |
) |
| 71 |
expected_normalized_timestamp = decimal.Decimal("1371585000.553919887170195579") |
| 72 |
normalized_timestamp = delphi_date_time_object._GetNormalizedTimestamp() |
| 73 |
self.assertEqual(normalized_timestamp, expected_normalized_timestamp) |
| 74 |
|
| 75 |
delphi_date_time_object = delphi_date_time.DelphiDateTime( |
| 76 |
time_zone_offset=60, timestamp=41443.8263953 |
| 77 |
) |
| 78 |
expected_normalized_timestamp = decimal.Decimal("1371581400.553919887170195579") |
| 79 |
normalized_timestamp = delphi_date_time_object._GetNormalizedTimestamp() |
| 80 |
self.assertEqual(normalized_timestamp, expected_normalized_timestamp) |
| 81 |
|
| 82 |
delphi_date_time_object = delphi_date_time.DelphiDateTime( |
| 83 |
timestamp=41443.8263953 |
| 84 |
) |
| 85 |
delphi_date_time_object.time_zone_offset = 60 |
| 86 |
|
| 87 |
expected_normalized_timestamp = decimal.Decimal("1371581400.553919887170195579") |
| 88 |
normalized_timestamp = delphi_date_time_object._GetNormalizedTimestamp() |
| 89 |
self.assertEqual(normalized_timestamp, expected_normalized_timestamp) |
| 90 |
|
| 91 |
delphi_date_time_object = delphi_date_time.DelphiDateTime() |
| 92 |
|
| 93 |
normalized_timestamp = delphi_date_time_object._GetNormalizedTimestamp() |
| 94 |
self.assertIsNone(normalized_timestamp) |
| 95 |
|
| 96 |
def testCopyFromDateTimeString(self): |
| 97 |
"""Tests the CopyFromDateTimeString function.""" |
| 98 |
delphi_date_time_object = delphi_date_time.DelphiDateTime() |
| 99 |
|
| 100 |
delphi_date_time_object.CopyFromDateTimeString("2013-06-18") |
| 101 |
self.assertEqual(delphi_date_time_object._timestamp, 41443.0) |
| 102 |
self.assertEqual(delphi_date_time_object._time_zone_offset, None) |
| 103 |
|
| 104 |
delphi_date_time_object.CopyFromDateTimeString("2013-06-18 19:50:00") |
| 105 |
self.assertEqual(delphi_date_time_object._timestamp, 41443.82638888889) |
| 106 |
self.assertEqual(delphi_date_time_object._time_zone_offset, None) |
| 107 |
|
| 108 |
delphi_date_time_object.CopyFromDateTimeString("2013-06-18 19:50:00.546875") |
| 109 |
self.assertEqual(delphi_date_time_object._timestamp, 41443.826395218464) |
| 110 |
self.assertEqual(delphi_date_time_object._time_zone_offset, None) |
| 111 |
|
| 112 |
delphi_date_time_object.CopyFromDateTimeString( |
| 113 |
"2013-06-18 19:50:00.546875-01:00" |
| 114 |
) |
| 115 |
self.assertEqual(delphi_date_time_object._timestamp, 41443.826395218464) |
| 116 |
self.assertEqual(delphi_date_time_object._time_zone_offset, -60) |
| 117 |
|
| 118 |
delphi_date_time_object.CopyFromDateTimeString( |
| 119 |
"2013-06-18 19:50:00.546875+01:00" |
| 120 |
) |
| 121 |
self.assertEqual(delphi_date_time_object._timestamp, 41443.826395218464) |
| 122 |
self.assertEqual(delphi_date_time_object._time_zone_offset, 60) |
| 123 |
|
| 124 |
delphi_date_time_object.CopyFromDateTimeString("1899-12-31 00:00:00") |
| 125 |
self.assertEqual(delphi_date_time_object._timestamp, 1.0) |
| 126 |
self.assertEqual(delphi_date_time_object._time_zone_offset, None) |
| 127 |
|
| 128 |
delphi_date_time_object = DelphiDateTimeInvalidYear() |
| 129 |
|
| 130 |
with self.assertRaises(ValueError): |
| 131 |
delphi_date_time_object.CopyFromDateTimeString("9999-01-02 00:00:00") |
| 132 |
|
| 133 |
def testCopyToDateTimeString(self): |
| 134 |
"""Tests the CopyToDateTimeString function.""" |
| 135 |
delphi_date_time_object = delphi_date_time.DelphiDateTime( |
| 136 |
timestamp=41443.8263953 |
| 137 |
) |
| 138 |
date_time_string = delphi_date_time_object.CopyToDateTimeString() |
| 139 |
self.assertEqual(date_time_string, "2013-06-18 19:50:00.553919") |
| 140 |
|
| 141 |
delphi_date_time_object = delphi_date_time.DelphiDateTime() |
| 142 |
|
| 143 |
date_time_string = delphi_date_time_object.CopyToDateTimeString() |
| 144 |
self.assertIsNone(date_time_string) |
| 145 |
|
| 146 |
def testCopyToDateTimeStringISO8601(self): |
| 147 |
"""Tests the CopyToDateTimeStringISO8601 function.""" |
| 148 |
delphi_date_time_object = delphi_date_time.DelphiDateTime( |
| 149 |
timestamp=41443.8263953 |
| 150 |
) |
| 151 |
date_time_string = delphi_date_time_object.CopyToDateTimeStringISO8601() |
| 152 |
self.assertEqual(date_time_string, "2013-06-18T19:50:00.553919+00:00") |
| 153 |
|
| 154 |
delphi_date_time_object = delphi_date_time.DelphiDateTime(timestamp=8.0e174) |
| 155 |
date_time_string = delphi_date_time_object.CopyToDateTimeStringISO8601() |
| 156 |
self.assertIsNone(date_time_string) |
| 157 |
|
| 158 |
def testCopyToSerializableDict(self): |
| 159 |
"""Test the CopyToSerializableDict function.""" |
| 160 |
delphi_date_time_object = delphi_date_time.DelphiDateTime( |
| 161 |
timestamp=41443.8263953 |
| 162 |
) |
| 163 |
expected_serializable_dict = { |
| 164 |
"__class_name__": "DelphiDateTime", |
| 165 |
"__type__": "DateTimeValues", |
| 166 |
"timestamp": 41443.8263953, |
| 167 |
} |
| 168 |
serializable_dict = delphi_date_time_object.CopyToSerializableDict() |
| 169 |
self.assertEqual(serializable_dict, expected_serializable_dict) |
| 170 |
|
| 171 |
def testGetDate(self): |
| 172 |
"""Tests the GetDate function.""" |
| 173 |
delphi_date_time_object = delphi_date_time.DelphiDateTime( |
| 174 |
timestamp=41443.8263953 |
| 175 |
) |
| 176 |
date_tuple = delphi_date_time_object.GetDate() |
| 177 |
self.assertEqual(date_tuple, (2013, 6, 18)) |
| 178 |
|
| 179 |
delphi_date_time_object = delphi_date_time.DelphiDateTime() |
| 180 |
|
| 181 |
date_tuple = delphi_date_time_object.GetDate() |
| 182 |
self.assertEqual(date_tuple, (None, None, None)) |
| 183 |
|
| 184 |
def testGetDateWithTimeOfDay(self): |
| 185 |
"""Tests the GetDateWithTimeOfDay function.""" |
| 186 |
delphi_date_time_object = delphi_date_time.DelphiDateTime( |
| 187 |
timestamp=41443.8263953 |
| 188 |
) |
| 189 |
date_with_time_of_day_tuple = delphi_date_time_object.GetDateWithTimeOfDay() |
| 190 |
self.assertEqual(date_with_time_of_day_tuple, (2013, 6, 18, 19, 50, 0)) |
| 191 |
|
| 192 |
delphi_date_time_object = delphi_date_time.DelphiDateTime() |
| 193 |
|
| 194 |
date_with_time_of_day_tuple = delphi_date_time_object.GetDateWithTimeOfDay() |
| 195 |
self.assertEqual( |
| 196 |
date_with_time_of_day_tuple, (None, None, None, None, None, None) |
| 197 |
) |
| 198 |
|
| 199 |
# TODO: remove this method when there is no more need for it in Plaso. |
| 200 |
def testGetPlasoTimestamp(self): |
| 201 |
"""Tests the GetPlasoTimestamp function.""" |
| 202 |
delphi_date_time_object = delphi_date_time.DelphiDateTime( |
| 203 |
timestamp=41443.8263953 |
| 204 |
) |
| 205 |
micro_posix_timestamp = delphi_date_time_object.GetPlasoTimestamp() |
| 206 |
self.assertEqual(micro_posix_timestamp, 1371585000553920) |
| 207 |
|
| 208 |
delphi_date_time_object = delphi_date_time.DelphiDateTime() |
| 209 |
|
| 210 |
micro_posix_timestamp = delphi_date_time_object.GetPlasoTimestamp() |
| 211 |
self.assertIsNone(micro_posix_timestamp) |
| 212 |
|
| 213 |
delphi_date_time_object = delphi_date_time.DelphiDateTime(timestamp=8.0e174) |
| 214 |
|
| 215 |
with self.assertRaises(ValueError): |
| 216 |
delphi_date_time_object.GetPlasoTimestamp() |
| 217 |
|
| 218 |
def testGetTimeOfDay(self): |
| 219 |
"""Tests the GetTimeOfDay function.""" |
| 220 |
delphi_date_time_object = delphi_date_time.DelphiDateTime( |
| 221 |
timestamp=41443.8263953 |
| 222 |
) |
| 223 |
time_of_day_tuple = delphi_date_time_object.GetTimeOfDay() |
| 224 |
self.assertEqual(time_of_day_tuple, (19, 50, 0)) |
| 225 |
|
| 226 |
delphi_date_time_object = delphi_date_time.DelphiDateTime() |
| 227 |
|
| 228 |
time_of_day_tuple = delphi_date_time_object.GetTimeOfDay() |
| 229 |
self.assertEqual(time_of_day_tuple, (None, None, None)) |
| 230 |
|
| 231 |
|
| 232 |
if __name__ == "__main__": |
| 233 |
unittest.main() |