• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package android.tzdata.mts;
17 
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.fail;
20 
21 import android.icu.util.VersionInfo;
22 import android.os.Build;
23 import android.util.TimeUtils;
24 
25 import org.junit.Test;
26 
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.nio.charset.StandardCharsets;
31 
32 /**
33  * Tests concerning version information associated with, or affected by, the time zone data module.
34  *
35  * <p>Generally we don't want to assert anything too specific here (like exact version), since that
36  * would mean more to update every tzdb release. Also, if the module being tested contains an old
37  * version then why wouldn't the tests be just as old too?
38  */
39 public class TimeZoneVersionTest {
40 
41     private static final File TIME_ZONE_MODULE_VERSION_FILE =
42             new File("/apex/com.android.tzdata/etc/tz/tz_version");
43 
44     @Test
timeZoneModuleIsCompatibleWithThisRelease()45     public void timeZoneModuleIsCompatibleWithThisRelease() throws Exception {
46         String majorVersion = readMajorFormatVersionFromModuleVersionFile();
47         if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
48             assertEquals("003", majorVersion);
49         } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R) {
50             assertEquals("004", majorVersion);
51         } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.S) {
52             assertEquals("005", majorVersion);
53         } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.S_V2) {
54             // S_V2 is 5.x, as the format version did not change from S.
55             assertEquals("005", majorVersion);
56         } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.TIRAMISU) {
57             assertEquals("006", majorVersion);
58         } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
59             // TODO Hack for master, which will have the same API level as U until the next
60             // release API is finalized.
61             if (VersionInfo.ICU_VERSION.getMajor() > 72) {
62                 // V is expected to be *.x.
63                 assertEquals("008", majorVersion);
64             } else {
65                 assertEquals("007", majorVersion);
66             }
67         } else {
68             // If this fails, a new API level has likely been finalized and can be made
69             // an explicit case. Keep this clause and add an explicit "else if" above.
70             // Consider removing any checks for pre-release devices too if they're not
71             // needed for now.
72             fail("Unhandled SDK_INT version:" + Build.VERSION.SDK_INT);
73         }
74     }
75 
76     /**
77      * Confirms that tzdb version information available via published APIs is consistent.
78      */
79     @Test
tzdbVersionIsConsistentAcrossApis()80     public void tzdbVersionIsConsistentAcrossApis() throws Exception {
81         String tzModuleTzdbVersion = readTzDbVersionFromModuleVersionFile();
82 
83         String icu4jTzVersion = android.icu.util.TimeZone.getTZDataVersion();
84         assertEquals(tzModuleTzdbVersion, icu4jTzVersion);
85 
86         assertEquals(tzModuleTzdbVersion, TimeUtils.getTimeZoneDatabaseVersion());
87     }
88 
89     /**
90      * Reads up to {@code maxBytes} bytes from the specified file. The returned array can be
91      * shorter than {@code maxBytes} if the file is shorter.
92      */
readBytes(File file, int maxBytes)93     private static byte[] readBytes(File file, int maxBytes) throws IOException {
94         if (maxBytes <= 0) {
95             throw new IllegalArgumentException("maxBytes ==" + maxBytes);
96         }
97 
98         try (FileInputStream in = new FileInputStream(file)) {
99             byte[] max = new byte[maxBytes];
100             int bytesRead = in.read(max, 0, maxBytes);
101             byte[] toReturn = new byte[bytesRead];
102             System.arraycopy(max, 0, toReturn, 0, bytesRead);
103             return toReturn;
104         }
105     }
106 
readTzDbVersionFromModuleVersionFile()107     private static String readTzDbVersionFromModuleVersionFile() throws IOException {
108         byte[] versionBytes = readBytes(TIME_ZONE_MODULE_VERSION_FILE, 13);
109         assertEquals(13, versionBytes.length);
110 
111         String versionString = new String(versionBytes, StandardCharsets.US_ASCII);
112         // Format is: xxx.yyy|zzzzz|...., we want zzzzz
113         String[] dataSetVersionComponents = versionString.split("\\|");
114         return dataSetVersionComponents[1];
115     }
116 
readMajorFormatVersionFromModuleVersionFile()117     private static String readMajorFormatVersionFromModuleVersionFile() throws IOException {
118         byte[] versionBytes = readBytes(TIME_ZONE_MODULE_VERSION_FILE, 7);
119         assertEquals(7, versionBytes.length);
120 
121         String versionString = new String(versionBytes, StandardCharsets.US_ASCII);
122         // Format is: xxx.yyy|zzzz|.... we want xxx
123         String[] dataSetVersionComponents = versionString.split("\\.");
124         return dataSetVersionComponents[0];
125     }
126 }
127