OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Crashpad Authors. All rights reserved. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 | |
15 #include "snapshot/system_snapshot_mac.h" | |
16 | |
17 #include <sys/time.h> | |
18 #include <time.h> | |
19 | |
20 #include <string> | |
21 | |
22 #include "build/build_config.h" | |
23 #include "gtest/gtest.h" | |
24 #include "util/mac/mac_util.h" | |
25 #include "util/mac/process_reader.h" | |
26 #include "util/test/errors.h" | |
27 | |
28 namespace crashpad { | |
29 namespace test { | |
30 namespace { | |
31 | |
32 // SystemSnapshotMac objects would be cumbersome to construct in each test that | |
33 // requires one, because of the repetitive and mechanical work necessary to set | |
34 // up a ProcessReader and timeval, along with the checks to verify that these | |
35 // operations succeed. This test fixture class handles the initialization work | |
36 // so that individual tests don’t have to. | |
37 class SystemSnapshotMacTest : public testing::Test { | |
38 public: | |
39 SystemSnapshotMacTest() | |
40 : Test(), | |
41 process_reader_(), | |
42 snapshot_time_(), | |
43 system_snapshot_() { | |
44 } | |
45 | |
46 const internal::SystemSnapshotMac& system_snapshot() const { | |
47 return system_snapshot_; | |
48 } | |
49 | |
50 // testing::Test: | |
51 void SetUp() override { | |
52 ASSERT_TRUE(process_reader_.Initialize(mach_task_self())); | |
53 ASSERT_EQ(0, gettimeofday(&snapshot_time_, nullptr)) | |
54 << ErrnoMessage("gettimeofday"); | |
55 system_snapshot_.Initialize(&process_reader_, &snapshot_time_); | |
56 } | |
57 | |
58 private: | |
59 ProcessReader process_reader_; | |
60 timeval snapshot_time_; | |
61 internal::SystemSnapshotMac system_snapshot_; | |
62 | |
63 DISALLOW_COPY_AND_ASSIGN(SystemSnapshotMacTest); | |
64 }; | |
65 | |
66 TEST_F(SystemSnapshotMacTest, GetCPUArchitecture) { | |
67 CPUArchitecture cpu_architecture = system_snapshot().GetCPUArchitecture(); | |
68 | |
69 #if defined(ARCH_CPU_X86) | |
70 EXPECT_EQ(kCPUArchitectureX86, cpu_architecture); | |
71 #elif defined(ARCH_CPU_X86_64) | |
72 EXPECT_EQ(kCPUArchitectureX86_64, cpu_architecture); | |
73 #else | |
74 #error port to your architecture | |
75 #endif | |
76 } | |
77 | |
78 TEST_F(SystemSnapshotMacTest, CPUCount) { | |
79 EXPECT_GE(system_snapshot().CPUCount(), 1); | |
80 } | |
81 | |
82 TEST_F(SystemSnapshotMacTest, CPUVendor) { | |
83 std::string cpu_vendor = system_snapshot().CPUVendor(); | |
84 | |
85 #if defined(ARCH_CPU_X86_FAMILY) | |
86 // Apple has only shipped Intel x86-family CPUs, but here’s a small nod to the | |
87 // “Hackintosh” crowd. | |
88 if (cpu_vendor != "GenuineIntel" && cpu_vendor != "AuthenticAMD") { | |
89 FAIL() << "cpu_vendor " << cpu_vendor; | |
90 } | |
91 #else | |
92 #error port to your architecture | |
93 #endif | |
94 } | |
95 | |
96 #if defined(ARCH_CPU_X86_FAMILY) | |
97 | |
98 TEST_F(SystemSnapshotMacTest, CPUX86SupportsDAZ) { | |
99 // All x86-family CPUs that Apple is known to have shipped should support DAZ. | |
100 EXPECT_TRUE(system_snapshot().CPUX86SupportsDAZ()); | |
101 } | |
102 | |
103 #endif | |
104 | |
105 TEST_F(SystemSnapshotMacTest, GetOperatingSystem) { | |
106 EXPECT_EQ(SystemSnapshot::kOperatingSystemMacOSX, | |
107 system_snapshot().GetOperatingSystem()); | |
108 } | |
109 | |
110 TEST_F(SystemSnapshotMacTest, OSVersion) { | |
111 int major; | |
112 int minor; | |
113 int bugfix; | |
114 std::string build; | |
115 system_snapshot().OSVersion(&major, &minor, &bugfix, &build); | |
116 | |
117 EXPECT_EQ(10, major); | |
118 EXPECT_EQ(MacOSXMinorVersion(), minor); | |
119 EXPECT_FALSE(build.empty()); | |
120 } | |
121 | |
122 TEST_F(SystemSnapshotMacTest, OSVersionFull) { | |
123 EXPECT_FALSE(system_snapshot().OSVersionFull().empty()); | |
124 } | |
125 | |
126 TEST_F(SystemSnapshotMacTest, MachineDescription) { | |
127 EXPECT_FALSE(system_snapshot().MachineDescription().empty()); | |
128 } | |
129 | |
130 TEST_F(SystemSnapshotMacTest, TimeZone) { | |
131 SystemSnapshot::DaylightSavingTimeStatus dst_status; | |
132 int standard_offset_seconds; | |
133 int daylight_offset_seconds; | |
134 std::string standard_name; | |
135 std::string daylight_name; | |
136 | |
137 system_snapshot().TimeZone(&dst_status, | |
138 &standard_offset_seconds, | |
139 &daylight_offset_seconds, | |
140 &standard_name, | |
141 &daylight_name); | |
142 | |
143 // |standard_offset_seconds| gives seconds east of UTC, and |timezone| gives | |
144 // seconds west of UTC. | |
145 EXPECT_EQ(-timezone, standard_offset_seconds); | |
146 | |
147 // In contemporary usage, most time zones have an integer hour offset from | |
148 // UTC, although several are at a half-hour offset, and two are at 15-minute | |
149 // offsets. Throughout history, other variations existed. See | |
150 // http://www.timeanddate.com/time/time-zones-interesting.html. | |
151 EXPECT_EQ(0, standard_offset_seconds % (15 * 60)) | |
152 << "standard_offset_seconds " << standard_offset_seconds; | |
153 | |
154 if (dst_status == SystemSnapshot::kDoesNotObserveDaylightSavingTime) { | |
155 EXPECT_EQ(standard_offset_seconds, daylight_offset_seconds); | |
156 EXPECT_EQ(standard_name, daylight_name); | |
157 } else { | |
158 EXPECT_EQ(0, daylight_offset_seconds % (15 * 60)) | |
159 << "daylight_offset_seconds " << daylight_offset_seconds; | |
160 | |
161 // In contemporary usage, dst_delta_seconds will almost always be one hour, | |
162 // except for Lord Howe Island, Australia, which uses a 30-minute | |
163 // delta. Throughout history, other variations existed. See | |
164 // http://www.timeanddate.com/time/dst/#brief. | |
165 int dst_delta_seconds = daylight_offset_seconds - standard_offset_seconds; | |
166 if (dst_delta_seconds != 60 * 60 && dst_delta_seconds != 30 * 60) { | |
167 FAIL() << "dst_delta_seconds " << dst_delta_seconds; | |
168 } | |
169 | |
170 EXPECT_NE(standard_name, daylight_name); | |
171 } | |
172 } | |
173 | |
174 } // namespace | |
175 } // namespace test | |
176 } // namespace crashpad | |
OLD | NEW |