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 
20 import android.icu.util.VersionInfo;
21 import android.os.Build;
22 import android.util.TimeUtils;
23 
24 import org.junit.Test;
25 
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.nio.charset.StandardCharsets;
30 
31 /**
32  * Tests concerning version information associated with, or affected by, the time zone data module.
33  *
34  * <p>Generally we don't want to assert anything too specific here (like exact version), since that
35  * would mean more to update every tzdb release. Also, if the module being tested contains an old
36  * version then why wouldn't the tests be just as old too?
37  */
38 public class TimeZoneVersionTest {
39 
40     private static final File TIME_ZONE_MODULE_VERSION_FILE =
41             new File("/apex/com.android.tzdata/etc/tz/tz_version");
42 
43     @Test
timeZoneModuleIsCompatibleWithThisRelease()44     public void timeZoneModuleIsCompatibleWithThisRelease() throws Exception {
45         String majorVersion = readMajorFormatVersionFromModuleVersionFile();
46         if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
47 
48             // TODO Hack for master: This ain't Q. Remove this after R devices are behaving
49             //  properly.
50             if (VersionInfo.ICU_VERSION.getMajor() > 63) {
51                 // R is 4.x.
52                 assertEquals("004", majorVersion);
53             } else {
54                 // Q is 3.x.
55                 assertEquals("003", majorVersion);
56             }
57         } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R) {
58             // R is 4.x.
59             assertEquals("004", majorVersion);
60         }
61     }
62 
63     /**
64      * Confirms that tzdb version information available via published APIs is consistent.
65      */
66     @Test
tzdbVersionIsConsistentAcrossApis()67     public void tzdbVersionIsConsistentAcrossApis() throws Exception {
68         String tzModuleTzdbVersion = readTzDbVersionFromModuleVersionFile();
69 
70         String icu4jTzVersion = android.icu.util.TimeZone.getTZDataVersion();
71         assertEquals(tzModuleTzdbVersion, icu4jTzVersion);
72 
73         assertEquals(tzModuleTzdbVersion, TimeUtils.getTimeZoneDatabaseVersion());
74     }
75 
76     /**
77      * Reads up to {@code maxBytes} bytes from the specified file. The returned array can be
78      * shorter than {@code maxBytes} if the file is shorter.
79      */
readBytes(File file, int maxBytes)80     private static byte[] readBytes(File file, int maxBytes) throws IOException {
81         if (maxBytes <= 0) {
82             throw new IllegalArgumentException("maxBytes ==" + maxBytes);
83         }
84 
85         try (FileInputStream in = new FileInputStream(file)) {
86             byte[] max = new byte[maxBytes];
87             int bytesRead = in.read(max, 0, maxBytes);
88             byte[] toReturn = new byte[bytesRead];
89             System.arraycopy(max, 0, toReturn, 0, bytesRead);
90             return toReturn;
91         }
92     }
93 
readTzDbVersionFromModuleVersionFile()94     private static String readTzDbVersionFromModuleVersionFile() throws IOException {
95         byte[] versionBytes = readBytes(TIME_ZONE_MODULE_VERSION_FILE, 13);
96         assertEquals(13, versionBytes.length);
97 
98         String versionString = new String(versionBytes, StandardCharsets.US_ASCII);
99         // Format is: xxx.yyy|zzzzz|...., we want zzzzz
100         String[] dataSetVersionComponents = versionString.split("\\|");
101         return dataSetVersionComponents[1];
102     }
103 
readMajorFormatVersionFromModuleVersionFile()104     private static String readMajorFormatVersionFromModuleVersionFile() throws IOException {
105         byte[] versionBytes = readBytes(TIME_ZONE_MODULE_VERSION_FILE, 7);
106         assertEquals(7, versionBytes.length);
107 
108         String versionString = new String(versionBytes, StandardCharsets.US_ASCII);
109         // Format is: xxx.yyy|zzzz|.... we want xxx
110         String[] dataSetVersionComponents = versionString.split("\\.");
111         return dataSetVersionComponents[0];
112     }
113 }
114