2014-10-03 14:55:54 -04:00
|
|
|
|
// Copyright 2014 The Crashpad Authors. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
2014-10-17 13:41:45 -04:00
|
|
|
|
#include "snapshot/mac/system_snapshot_mac.h"
|
2014-10-03 14:55:54 -04:00
|
|
|
|
|
2017-02-07 13:59:18 -05:00
|
|
|
|
#include <stdlib.h>
|
2014-10-03 14:55:54 -04:00
|
|
|
|
#include <sys/time.h>
|
2014-10-03 18:40:09 -04:00
|
|
|
|
#include <time.h>
|
2014-10-03 14:55:54 -04:00
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
2017-02-07 13:59:18 -05:00
|
|
|
|
#include "base/strings/stringprintf.h"
|
2014-10-03 14:55:54 -04:00
|
|
|
|
#include "build/build_config.h"
|
|
|
|
|
#include "gtest/gtest.h"
|
2014-10-17 13:41:45 -04:00
|
|
|
|
#include "snapshot/mac/process_reader.h"
|
test: Move util/test to its own top-level directory, test.
After 9e79ea1da719, it no longer makes sense for crashpad_util_test_lib
to “hide” in util/util_test.gyp. All of util/test is moved to its own
top-level directory, test, which all other test code is allowed to
depend on. test, too, is allowed to depend on all other non-test code.
In a future change, when crashpad_util_test_lib gains a dependency on
crashpad_client, it won’t look so weird for something in util (even
though it’s in util/test) to depend on something in client, because the
thing that needs to depend on client will live in test, not util.
BUG=crashpad:33
R=scottmg@chromium.org
Review URL: https://codereview.chromium.org/1051533002
2015-03-31 17:44:14 -04:00
|
|
|
|
#include "test/errors.h"
|
2014-10-03 14:55:54 -04:00
|
|
|
|
#include "util/mac/mac_util.h"
|
|
|
|
|
|
2014-10-07 17:28:50 -04:00
|
|
|
|
namespace crashpad {
|
|
|
|
|
namespace test {
|
2014-10-03 14:55:54 -04:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
// SystemSnapshotMac objects would be cumbersome to construct in each test that
|
|
|
|
|
// requires one, because of the repetitive and mechanical work necessary to set
|
|
|
|
|
// up a ProcessReader and timeval, along with the checks to verify that these
|
|
|
|
|
// operations succeed. This test fixture class handles the initialization work
|
|
|
|
|
// so that individual tests don’t have to.
|
|
|
|
|
class SystemSnapshotMacTest : public testing::Test {
|
|
|
|
|
public:
|
|
|
|
|
SystemSnapshotMacTest()
|
|
|
|
|
: Test(),
|
|
|
|
|
process_reader_(),
|
|
|
|
|
snapshot_time_(),
|
|
|
|
|
system_snapshot_() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const internal::SystemSnapshotMac& system_snapshot() const {
|
|
|
|
|
return system_snapshot_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// testing::Test:
|
2014-10-14 11:11:57 -04:00
|
|
|
|
void SetUp() override {
|
2014-10-03 14:55:54 -04:00
|
|
|
|
ASSERT_TRUE(process_reader_.Initialize(mach_task_self()));
|
2014-10-14 11:10:45 -04:00
|
|
|
|
ASSERT_EQ(0, gettimeofday(&snapshot_time_, nullptr))
|
2014-10-03 14:55:54 -04:00
|
|
|
|
<< ErrnoMessage("gettimeofday");
|
|
|
|
|
system_snapshot_.Initialize(&process_reader_, &snapshot_time_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
ProcessReader process_reader_;
|
|
|
|
|
timeval snapshot_time_;
|
|
|
|
|
internal::SystemSnapshotMac system_snapshot_;
|
|
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(SystemSnapshotMacTest);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TEST_F(SystemSnapshotMacTest, GetCPUArchitecture) {
|
|
|
|
|
CPUArchitecture cpu_architecture = system_snapshot().GetCPUArchitecture();
|
|
|
|
|
|
|
|
|
|
#if defined(ARCH_CPU_X86)
|
|
|
|
|
EXPECT_EQ(kCPUArchitectureX86, cpu_architecture);
|
|
|
|
|
#elif defined(ARCH_CPU_X86_64)
|
|
|
|
|
EXPECT_EQ(kCPUArchitectureX86_64, cpu_architecture);
|
|
|
|
|
#else
|
|
|
|
|
#error port to your architecture
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SystemSnapshotMacTest, CPUCount) {
|
|
|
|
|
EXPECT_GE(system_snapshot().CPUCount(), 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SystemSnapshotMacTest, CPUVendor) {
|
|
|
|
|
std::string cpu_vendor = system_snapshot().CPUVendor();
|
|
|
|
|
|
|
|
|
|
#if defined(ARCH_CPU_X86_FAMILY)
|
|
|
|
|
// Apple has only shipped Intel x86-family CPUs, but here’s a small nod to the
|
|
|
|
|
// “Hackintosh” crowd.
|
|
|
|
|
if (cpu_vendor != "GenuineIntel" && cpu_vendor != "AuthenticAMD") {
|
2014-10-03 18:40:09 -04:00
|
|
|
|
FAIL() << "cpu_vendor " << cpu_vendor;
|
2014-10-03 14:55:54 -04:00
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
#error port to your architecture
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(ARCH_CPU_X86_FAMILY)
|
|
|
|
|
|
|
|
|
|
TEST_F(SystemSnapshotMacTest, CPUX86SupportsDAZ) {
|
|
|
|
|
// All x86-family CPUs that Apple is known to have shipped should support DAZ.
|
|
|
|
|
EXPECT_TRUE(system_snapshot().CPUX86SupportsDAZ());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
TEST_F(SystemSnapshotMacTest, GetOperatingSystem) {
|
|
|
|
|
EXPECT_EQ(SystemSnapshot::kOperatingSystemMacOSX,
|
|
|
|
|
system_snapshot().GetOperatingSystem());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SystemSnapshotMacTest, OSVersion) {
|
|
|
|
|
int major;
|
|
|
|
|
int minor;
|
|
|
|
|
int bugfix;
|
|
|
|
|
std::string build;
|
|
|
|
|
system_snapshot().OSVersion(&major, &minor, &bugfix, &build);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(10, major);
|
|
|
|
|
EXPECT_EQ(MacOSXMinorVersion(), minor);
|
|
|
|
|
EXPECT_FALSE(build.empty());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SystemSnapshotMacTest, OSVersionFull) {
|
|
|
|
|
EXPECT_FALSE(system_snapshot().OSVersionFull().empty());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SystemSnapshotMacTest, MachineDescription) {
|
|
|
|
|
EXPECT_FALSE(system_snapshot().MachineDescription().empty());
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-07 13:59:18 -05:00
|
|
|
|
class ScopedSetTZ {
|
|
|
|
|
public:
|
|
|
|
|
ScopedSetTZ(const std::string& tz) {
|
|
|
|
|
const char* old_tz = getenv(kTZ);
|
|
|
|
|
old_tz_set_ = old_tz;
|
|
|
|
|
if (old_tz_set_) {
|
|
|
|
|
old_tz_.assign(old_tz);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(0, setenv(kTZ, tz.c_str(), 1)) << ErrnoMessage("setenv");
|
|
|
|
|
tzset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~ScopedSetTZ() {
|
|
|
|
|
if (old_tz_set_) {
|
|
|
|
|
EXPECT_EQ(0, setenv(kTZ, old_tz_.c_str(), 1)) << ErrnoMessage("setenv");
|
|
|
|
|
} else {
|
|
|
|
|
EXPECT_EQ(0, unsetenv(kTZ)) << ErrnoMessage("unsetenv");
|
|
|
|
|
}
|
|
|
|
|
tzset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::string old_tz_;
|
|
|
|
|
bool old_tz_set_;
|
|
|
|
|
|
|
|
|
|
static constexpr char kTZ[] = "TZ";
|
|
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ScopedSetTZ);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constexpr char ScopedSetTZ::kTZ[];
|
|
|
|
|
|
2014-10-03 18:40:09 -04:00
|
|
|
|
TEST_F(SystemSnapshotMacTest, TimeZone) {
|
|
|
|
|
SystemSnapshot::DaylightSavingTimeStatus dst_status;
|
|
|
|
|
int standard_offset_seconds;
|
|
|
|
|
int daylight_offset_seconds;
|
|
|
|
|
std::string standard_name;
|
|
|
|
|
std::string daylight_name;
|
|
|
|
|
|
|
|
|
|
system_snapshot().TimeZone(&dst_status,
|
|
|
|
|
&standard_offset_seconds,
|
|
|
|
|
&daylight_offset_seconds,
|
|
|
|
|
&standard_name,
|
|
|
|
|
&daylight_name);
|
|
|
|
|
|
|
|
|
|
// |standard_offset_seconds| gives seconds east of UTC, and |timezone| gives
|
|
|
|
|
// seconds west of UTC.
|
|
|
|
|
EXPECT_EQ(-timezone, standard_offset_seconds);
|
|
|
|
|
|
|
|
|
|
// In contemporary usage, most time zones have an integer hour offset from
|
|
|
|
|
// UTC, although several are at a half-hour offset, and two are at 15-minute
|
|
|
|
|
// offsets. Throughout history, other variations existed. See
|
|
|
|
|
// http://www.timeanddate.com/time/time-zones-interesting.html.
|
|
|
|
|
EXPECT_EQ(0, standard_offset_seconds % (15 * 60))
|
|
|
|
|
<< "standard_offset_seconds " << standard_offset_seconds;
|
|
|
|
|
|
|
|
|
|
if (dst_status == SystemSnapshot::kDoesNotObserveDaylightSavingTime) {
|
|
|
|
|
EXPECT_EQ(standard_offset_seconds, daylight_offset_seconds);
|
|
|
|
|
EXPECT_EQ(standard_name, daylight_name);
|
|
|
|
|
} else {
|
|
|
|
|
EXPECT_EQ(0, daylight_offset_seconds % (15 * 60))
|
|
|
|
|
<< "daylight_offset_seconds " << daylight_offset_seconds;
|
|
|
|
|
|
|
|
|
|
// In contemporary usage, dst_delta_seconds will almost always be one hour,
|
|
|
|
|
// except for Lord Howe Island, Australia, which uses a 30-minute
|
|
|
|
|
// delta. Throughout history, other variations existed. See
|
|
|
|
|
// http://www.timeanddate.com/time/dst/#brief.
|
|
|
|
|
int dst_delta_seconds = daylight_offset_seconds - standard_offset_seconds;
|
|
|
|
|
if (dst_delta_seconds != 60 * 60 && dst_delta_seconds != 30 * 60) {
|
|
|
|
|
FAIL() << "dst_delta_seconds " << dst_delta_seconds;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EXPECT_NE(standard_name, daylight_name);
|
|
|
|
|
}
|
2017-02-07 13:59:18 -05:00
|
|
|
|
|
|
|
|
|
// Test a variety of time zones. Some of these observe daylight saving time,
|
|
|
|
|
// some don’t. Some used to but no longer do. Some have uncommon UTC offsets.
|
2017-03-28 13:03:36 -04:00
|
|
|
|
// standard_name and daylight_name can be nullptr where no name exists to
|
|
|
|
|
// verify, as may happen when some versions of the timezone database carry
|
|
|
|
|
// invented names and others do not.
|
2017-02-07 13:59:18 -05:00
|
|
|
|
const struct {
|
|
|
|
|
const char* tz;
|
|
|
|
|
bool observes_dst;
|
|
|
|
|
float standard_offset_hours;
|
|
|
|
|
float daylight_offset_hours;
|
|
|
|
|
const char* standard_name;
|
|
|
|
|
const char* daylight_name;
|
|
|
|
|
} kTestTimeZones[] = {
|
|
|
|
|
{"America/Anchorage", true, -9, -8, "AKST", "AKDT"},
|
|
|
|
|
{"America/Chicago", true, -6, -5, "CST", "CDT"},
|
|
|
|
|
{"America/Denver", true, -7, -6, "MST", "MDT"},
|
|
|
|
|
{"America/Halifax", true, -4, -3, "AST", "ADT"},
|
|
|
|
|
{"America/Los_Angeles", true, -8, -7, "PST", "PDT"},
|
|
|
|
|
{"America/New_York", true, -5, -4, "EST", "EDT"},
|
|
|
|
|
{"America/Phoenix", false, -7, -7, "MST", "MST"},
|
|
|
|
|
{"Asia/Karachi", false, 5, 5, "PKT", "PKT"},
|
|
|
|
|
{"Asia/Kolkata", false, 5.5, 5.5, "IST", "IST"},
|
|
|
|
|
{"Asia/Shanghai", false, 8, 8, "CST", "CST"},
|
|
|
|
|
{"Asia/Tokyo", false, 9, 9, "JST", "JST"},
|
|
|
|
|
{"Australia/Adelaide", true, 9.5, 10.5, "ACST", "ACDT"},
|
|
|
|
|
{"Australia/Brisbane", false, 10, 10, "AEST", "AEST"},
|
|
|
|
|
{"Australia/Darwin", false, 9.5, 9.5, "ACST", "ACST"},
|
2017-03-28 13:03:36 -04:00
|
|
|
|
{"Australia/Eucla", false, 8.75, 8.75, nullptr, nullptr},
|
|
|
|
|
{"Australia/Lord_Howe", true, 10.5, 11, nullptr, nullptr},
|
2017-02-07 13:59:18 -05:00
|
|
|
|
{"Australia/Perth", false, 8, 8, "AWST", "AWST"},
|
|
|
|
|
{"Australia/Sydney", true, 10, 11, "AEST", "AEDT"},
|
|
|
|
|
{"Europe/Bucharest", true, 2, 3, "EET", "EEST"},
|
|
|
|
|
{"Europe/London", true, 0, 1, "GMT", "BST"},
|
|
|
|
|
{"Europe/Moscow", false, 3, 3, "MSK", "MSK"},
|
|
|
|
|
{"Europe/Paris", true, 1, 2, "CET", "CEST"},
|
|
|
|
|
{"Europe/Reykjavik", false, 0, 0, "UTC", "UTC"},
|
|
|
|
|
{"Pacific/Auckland", true, 12, 13, "NZST", "NZDT"},
|
|
|
|
|
{"Pacific/Honolulu", false, -10, -10, "HST", "HST"},
|
|
|
|
|
{"UTC", false, 0, 0, "UTC", "UTC"},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (size_t index = 0; index < arraysize(kTestTimeZones); ++index) {
|
|
|
|
|
const auto& test_time_zone = kTestTimeZones[index];
|
|
|
|
|
const char* tz = test_time_zone.tz;
|
|
|
|
|
SCOPED_TRACE(base::StringPrintf("index %zu, tz %s", index, tz));
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
ScopedSetTZ set_tz(tz);
|
|
|
|
|
system_snapshot().TimeZone(&dst_status,
|
|
|
|
|
&standard_offset_seconds,
|
|
|
|
|
&daylight_offset_seconds,
|
|
|
|
|
&standard_name,
|
|
|
|
|
&daylight_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(test_time_zone.observes_dst,
|
|
|
|
|
dst_status != SystemSnapshot::kDoesNotObserveDaylightSavingTime);
|
|
|
|
|
EXPECT_EQ(test_time_zone.standard_offset_hours * 60 * 60,
|
|
|
|
|
standard_offset_seconds);
|
|
|
|
|
EXPECT_EQ(test_time_zone.daylight_offset_hours * 60 * 60,
|
|
|
|
|
daylight_offset_seconds);
|
2017-03-28 13:03:36 -04:00
|
|
|
|
if (test_time_zone.standard_name) {
|
|
|
|
|
EXPECT_EQ(test_time_zone.standard_name, standard_name);
|
|
|
|
|
}
|
|
|
|
|
if (test_time_zone.daylight_name) {
|
|
|
|
|
EXPECT_EQ(test_time_zone.daylight_name, daylight_name);
|
|
|
|
|
}
|
2017-02-07 13:59:18 -05:00
|
|
|
|
}
|
2014-10-03 18:40:09 -04:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-03 14:55:54 -04:00
|
|
|
|
} // namespace
|
2014-10-07 17:28:50 -04:00
|
|
|
|
} // namespace test
|
|
|
|
|
} // namespace crashpad
|