1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/i18n/icu_util.h"
6
7 #include "base/files/file_util.h"
8 #include "base/logging.h"
9 #include "base/strings/string_util.h"
10 #include "base/test/icu_test_util.h"
11 #include "build/build_config.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/icu/source/i18n/unicode/timezone.h"
14
15 namespace base {
16 namespace i18n {
17
18 namespace {
19
20 // Directory path to the tzdata configuration files, used in tests only.
21 const char kTestTzDataDirPath[] = "/pkg/base/test/data/tzdata/icu/44/le";
22
23 // File path to the text file containing the expected ICU library revision, for
24 // example "2019c". This file is available in production.
25 const char kTZDataRevisionFilePath[] = "/config/tzdata/icu/revision.txt";
26
27 } // namespace
28
29 class TimeZoneDataTest : public testing::Test {
30 protected:
TearDown()31 void TearDown() override {
32 ResetGlobalsForTesting();
33
34 // ICU must be set back up in case e.g. a log statement that formats times
35 // uses it.
36 test::InitializeICUForTesting();
37 }
38
GetActualRevision(std::string * icu_version)39 void GetActualRevision(std::string* icu_version) {
40 UErrorCode err = U_ZERO_ERROR;
41 *icu_version = icu::TimeZone::getTZDataVersion(err);
42 ASSERT_EQ(U_ZERO_ERROR, err) << u_errorName(err);
43 }
44 };
45
46 // Loads a file revision.txt from the actual underlying filesystem, which
47 // contains the tzdata version we expect to be able to load. It then attempts
48 // to load this configuration from the default path and compares the version it
49 // obtained from the load with the expected version, failing on version
50 // mismatch.
TEST_F(TimeZoneDataTest,CompareSystemRevisionWithExpected)51 TEST_F(TimeZoneDataTest, CompareSystemRevisionWithExpected) {
52 ASSERT_TRUE(base::PathExists(base::FilePath(kTZDataRevisionFilePath)));
53 ResetGlobalsForTesting();
54
55 ASSERT_TRUE(InitializeICU());
56 std::string expected;
57 ASSERT_TRUE(base::ReadFileToString(base::FilePath(kTZDataRevisionFilePath),
58 &expected))
59 << "Could not read from path: " << kTZDataRevisionFilePath;
60 std::string actual;
61 GetActualRevision(&actual);
62 EXPECT_EQ(expected, actual);
63 }
64
65 // Verifies that the current version of the ICU library in use can load ICU
66 // data in a specific version format (in this case 44). Designed to fail if
67 // the ICU library version used in Chromium drifts from version 44 so much that
68 // the library is no longer able to load the old tzdata. If the test fails,
69 // this could be a sign that all platforms Chromium runs on need to upgrade the
70 // ICU library versions.
TEST_F(TimeZoneDataTest,TestLoadingTimeZoneDataFromKnownConfigs)71 TEST_F(TimeZoneDataTest, TestLoadingTimeZoneDataFromKnownConfigs) {
72 ASSERT_TRUE(base::DirectoryExists(base::FilePath(kTestTzDataDirPath)));
73 ResetGlobalsForTesting();
74 SetIcuTimeZoneDataDirForTesting(kTestTzDataDirPath);
75
76 ASSERT_TRUE(InitializeICU());
77 std::string actual;
78 GetActualRevision(&actual);
79 EXPECT_EQ("2019a", actual) << "If ICU no longer supports this tzdata "
80 "version, tzdata version needs to be upgraded";
81 }
82
83 using TimeZoneDataDeathTest = TimeZoneDataTest;
84
TEST_F(TimeZoneDataDeathTest,CrashesWithNonexistentPath)85 TEST_F(TimeZoneDataDeathTest, CrashesWithNonexistentPath) {
86 ResetGlobalsForTesting();
87 SetIcuTimeZoneDataDirForTesting("/some/nonexistent/path");
88
89 EXPECT_DEATH(InitializeICU(),
90 "Could not open directory: '/some/nonexistent/path'");
91 }
92
93 } // namespace i18n
94 } // namespace base
95