| 1 |
#!/usr/bin/env python3 |
| 2 |
"""Tests for the Cocoa time implementation.""" |
| 3 |
|
| 4 |
import decimal |
| 5 |
import unittest |
| 6 |
|
| 7 |
from dfdatetime import cocoa_time |
| 8 |
|
| 9 |
|
| 10 |
class CocoaTimeEpochTest(unittest.TestCase): |
| 11 |
"""Tests for the Cocoa time epoch.""" |
| 12 |
|
| 13 |
def testInitialize(self): |
| 14 |
"""Tests the __init__ function.""" |
| 15 |
cocoa_time_epoch = cocoa_time.CocoaTimeEpoch() |
| 16 |
self.assertIsNotNone(cocoa_time_epoch) |
| 17 |
|
| 18 |
|
| 19 |
class CocoaTimeTest(unittest.TestCase): |
| 20 |
"""Tests for the Cocoa timestamp.""" |
| 21 |
|
| 22 |
# pylint: disable=protected-access |
| 23 |
|
| 24 |
def testProperties(self): |
| 25 |
"""Tests the properties.""" |
| 26 |
cocoa_time_object = cocoa_time.CocoaTime(timestamp=395011845.0) |
| 27 |
self.assertEqual(cocoa_time_object.timestamp, 395011845.0) |
| 28 |
|
| 29 |
cocoa_time_object = cocoa_time.CocoaTime() |
| 30 |
self.assertIsNone(cocoa_time_object.timestamp) |
| 31 |
|
| 32 |
def testGetNormalizedTimestamp(self): |
| 33 |
"""Tests the _GetNormalizedTimestamp function.""" |
| 34 |
cocoa_time_object = cocoa_time.CocoaTime(timestamp=395011845.0) |
| 35 |
|
| 36 |
normalized_timestamp = cocoa_time_object._GetNormalizedTimestamp() |
| 37 |
self.assertEqual(normalized_timestamp, decimal.Decimal("1373319045.0")) |
| 38 |
|
| 39 |
cocoa_time_object = cocoa_time.CocoaTime( |
| 40 |
time_zone_offset=60, timestamp=395011845.0 |
| 41 |
) |
| 42 |
normalized_timestamp = cocoa_time_object._GetNormalizedTimestamp() |
| 43 |
self.assertEqual(normalized_timestamp, decimal.Decimal("1373315445.0")) |
| 44 |
|
| 45 |
cocoa_time_object = cocoa_time.CocoaTime(timestamp=395011845.0) |
| 46 |
cocoa_time_object.time_zone_offset = 60 |
| 47 |
|
| 48 |
normalized_timestamp = cocoa_time_object._GetNormalizedTimestamp() |
| 49 |
self.assertEqual(normalized_timestamp, decimal.Decimal("1373315445.0")) |
| 50 |
|
| 51 |
cocoa_time_object = cocoa_time.CocoaTime() |
| 52 |
|
| 53 |
normalized_timestamp = cocoa_time_object._GetNormalizedTimestamp() |
| 54 |
self.assertIsNone(normalized_timestamp) |
| 55 |
|
| 56 |
def testCopyFromDateTimeString(self): |
| 57 |
"""Tests the CopyFromDateTimeString function.""" |
| 58 |
cocoa_time_object = cocoa_time.CocoaTime() |
| 59 |
|
| 60 |
cocoa_time_object.CopyFromDateTimeString("2013-07-08") |
| 61 |
self.assertEqual(cocoa_time_object._timestamp, 394934400.0) |
| 62 |
self.assertEqual(cocoa_time_object._time_zone_offset, None) |
| 63 |
|
| 64 |
cocoa_time_object.CopyFromDateTimeString("2013-07-08 21:30:45") |
| 65 |
self.assertEqual(cocoa_time_object._timestamp, 395011845.0) |
| 66 |
self.assertEqual(cocoa_time_object._time_zone_offset, None) |
| 67 |
|
| 68 |
cocoa_time_object.CopyFromDateTimeString("2013-07-08 21:30:45.546875") |
| 69 |
self.assertEqual(cocoa_time_object._timestamp, 395011845.546875) |
| 70 |
self.assertEqual(cocoa_time_object._time_zone_offset, None) |
| 71 |
|
| 72 |
cocoa_time_object.CopyFromDateTimeString("2013-07-08 21:30:45.546875-01:00") |
| 73 |
self.assertEqual(cocoa_time_object._timestamp, 395011845.546875) |
| 74 |
self.assertEqual(cocoa_time_object._time_zone_offset, -60) |
| 75 |
|
| 76 |
cocoa_time_object.CopyFromDateTimeString("2013-07-08 21:30:45.546875+01:00") |
| 77 |
self.assertEqual(cocoa_time_object._timestamp, 395011845.546875) |
| 78 |
self.assertEqual(cocoa_time_object._time_zone_offset, 60) |
| 79 |
|
| 80 |
cocoa_time_object.CopyFromDateTimeString("2001-01-02 00:00:00") |
| 81 |
self.assertEqual(cocoa_time_object._timestamp, 86400.0) |
| 82 |
self.assertEqual(cocoa_time_object._time_zone_offset, None) |
| 83 |
|
| 84 |
def testCopyToDateTimeString(self): |
| 85 |
"""Tests the CopyToDateTimeString function.""" |
| 86 |
cocoa_time_object = cocoa_time.CocoaTime(timestamp=395011845.546875) |
| 87 |
|
| 88 |
date_time_string = cocoa_time_object.CopyToDateTimeString() |
| 89 |
self.assertEqual(date_time_string, "2013-07-08 21:30:45.546875") |
| 90 |
|
| 91 |
epoch_year = cocoa_time_object._EPOCH.year |
| 92 |
cocoa_time_object._EPOCH.year = -1 |
| 93 |
|
| 94 |
with self.assertRaises(ValueError): |
| 95 |
cocoa_time_object.CopyToDateTimeString() |
| 96 |
|
| 97 |
cocoa_time_object._EPOCH.year = epoch_year |
| 98 |
|
| 99 |
cocoa_time_object = cocoa_time.CocoaTime() |
| 100 |
|
| 101 |
date_time_string = cocoa_time_object.CopyToDateTimeString() |
| 102 |
self.assertIsNone(date_time_string) |
| 103 |
|
| 104 |
def testCopyToDateTimeStringISO8601(self): |
| 105 |
"""Tests the CopyToDateTimeStringISO8601 function.""" |
| 106 |
cocoa_time_object = cocoa_time.CocoaTime(timestamp=395011845.546875) |
| 107 |
|
| 108 |
date_time_string = cocoa_time_object.CopyToDateTimeStringISO8601() |
| 109 |
self.assertEqual(date_time_string, "2013-07-08T21:30:45.546875+00:00") |
| 110 |
|
| 111 |
def testCopyToSerializableDict(self): |
| 112 |
"""Test the CopyToSerializableDict function.""" |
| 113 |
cocoa_time_object = cocoa_time.CocoaTime(timestamp=395011845.546875) |
| 114 |
|
| 115 |
expected_serializable_dict = { |
| 116 |
"__class_name__": "CocoaTime", |
| 117 |
"__type__": "DateTimeValues", |
| 118 |
"timestamp": 395011845.546875, |
| 119 |
} |
| 120 |
serializable_dict = cocoa_time_object.CopyToSerializableDict() |
| 121 |
self.assertEqual(serializable_dict, expected_serializable_dict) |
| 122 |
|
| 123 |
def testGetDate(self): |
| 124 |
"""Tests the GetDate function.""" |
| 125 |
cocoa_time_object = cocoa_time.CocoaTime(timestamp=395011845.546875) |
| 126 |
|
| 127 |
date_tuple = cocoa_time_object.GetDate() |
| 128 |
self.assertEqual(date_tuple, (2013, 7, 8)) |
| 129 |
|
| 130 |
cocoa_time_object = cocoa_time.CocoaTime() |
| 131 |
|
| 132 |
date_tuple = cocoa_time_object.GetDate() |
| 133 |
self.assertEqual(date_tuple, (None, None, None)) |
| 134 |
|
| 135 |
def testGetDateWithTimeOfDay(self): |
| 136 |
"""Tests the GetDateWithTimeOfDay function.""" |
| 137 |
cocoa_time_object = cocoa_time.CocoaTime(timestamp=395011845.546875) |
| 138 |
|
| 139 |
date_with_time_of_day_tuple = cocoa_time_object.GetDateWithTimeOfDay() |
| 140 |
self.assertEqual(date_with_time_of_day_tuple, (2013, 7, 8, 21, 30, 45)) |
| 141 |
|
| 142 |
cocoa_time_object = cocoa_time.CocoaTime() |
| 143 |
|
| 144 |
date_with_time_of_day_tuple = cocoa_time_object.GetDateWithTimeOfDay() |
| 145 |
self.assertEqual( |
| 146 |
date_with_time_of_day_tuple, (None, None, None, None, None, None) |
| 147 |
) |
| 148 |
|
| 149 |
# TODO: remove this method when there is no more need for it in Plaso. |
| 150 |
def testGetPlasoTimestamp(self): |
| 151 |
"""Tests the GetPlasoTimestamp function.""" |
| 152 |
cocoa_time_object = cocoa_time.CocoaTime(timestamp=395011845.0) |
| 153 |
|
| 154 |
micro_posix_timestamp = cocoa_time_object.GetPlasoTimestamp() |
| 155 |
self.assertEqual(micro_posix_timestamp, 1373319045000000) |
| 156 |
|
| 157 |
cocoa_time_object = cocoa_time.CocoaTime() |
| 158 |
|
| 159 |
micro_posix_timestamp = cocoa_time_object.GetPlasoTimestamp() |
| 160 |
self.assertIsNone(micro_posix_timestamp) |
| 161 |
|
| 162 |
def testGetTimeOfDay(self): |
| 163 |
"""Tests the GetTimeOfDay function.""" |
| 164 |
cocoa_time_object = cocoa_time.CocoaTime(timestamp=395011845.546875) |
| 165 |
|
| 166 |
time_of_day_tuple = cocoa_time_object.GetTimeOfDay() |
| 167 |
self.assertEqual(time_of_day_tuple, (21, 30, 45)) |
| 168 |
|
| 169 |
cocoa_time_object = cocoa_time.CocoaTime() |
| 170 |
|
| 171 |
time_of_day_tuple = cocoa_time_object.GetTimeOfDay() |
| 172 |
self.assertEqual(time_of_day_tuple, (None, None, None)) |
| 173 |
|
| 174 |
|
| 175 |
if __name__ == "__main__": |
| 176 |
unittest.main() |