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 // TODO Hack for master, which will have the same API level as S until the next release 53 // API is finalized. 54 if (VersionInfo.ICU_VERSION.getMajor() > 68) { 55 // T is expected to be 6.x. 56 assertEquals("006", majorVersion); 57 } else { 58 // S is 5.x. 59 assertEquals("005", majorVersion); 60 } 61 } else { 62 // If this fails, a new API level has likely been finalized and can be made 63 // an explicit case. Keep this clause and add an explicit "else if" above. 64 // Consider removing any checks for pre-release devices too if they're not 65 // needed for now. 66 fail("Unhandled SDK_INT version:" + Build.VERSION.SDK_INT); 67 } 68 } 69 70 /** 71 * Confirms that tzdb version information available via published APIs is consistent. 72 */ 73 @Test tzdbVersionIsConsistentAcrossApis()74 public void tzdbVersionIsConsistentAcrossApis() throws Exception { 75 String tzModuleTzdbVersion = readTzDbVersionFromModuleVersionFile(); 76 77 String icu4jTzVersion = android.icu.util.TimeZone.getTZDataVersion(); 78 assertEquals(tzModuleTzdbVersion, icu4jTzVersion); 79 80 assertEquals(tzModuleTzdbVersion, TimeUtils.getTimeZoneDatabaseVersion()); 81 } 82 83 /** 84 * Reads up to {@code maxBytes} bytes from the specified file. The returned array can be 85 * shorter than {@code maxBytes} if the file is shorter. 86 */ readBytes(File file, int maxBytes)87 private static byte[] readBytes(File file, int maxBytes) throws IOException { 88 if (maxBytes <= 0) { 89 throw new IllegalArgumentException("maxBytes ==" + maxBytes); 90 } 91 92 try (FileInputStream in = new FileInputStream(file)) { 93 byte[] max = new byte[maxBytes]; 94 int bytesRead = in.read(max, 0, maxBytes); 95 byte[] toReturn = new byte[bytesRead]; 96 System.arraycopy(max, 0, toReturn, 0, bytesRead); 97 return toReturn; 98 } 99 } 100 readTzDbVersionFromModuleVersionFile()101 private static String readTzDbVersionFromModuleVersionFile() throws IOException { 102 byte[] versionBytes = readBytes(TIME_ZONE_MODULE_VERSION_FILE, 13); 103 assertEquals(13, versionBytes.length); 104 105 String versionString = new String(versionBytes, StandardCharsets.US_ASCII); 106 // Format is: xxx.yyy|zzzzz|...., we want zzzzz 107 String[] dataSetVersionComponents = versionString.split("\\|"); 108 return dataSetVersionComponents[1]; 109 } 110 readMajorFormatVersionFromModuleVersionFile()111 private static String readMajorFormatVersionFromModuleVersionFile() throws IOException { 112 byte[] versionBytes = readBytes(TIME_ZONE_MODULE_VERSION_FILE, 7); 113 assertEquals(7, versionBytes.length); 114 115 String versionString = new String(versionBytes, StandardCharsets.US_ASCII); 116 // Format is: xxx.yyy|zzzz|.... we want xxx 117 String[] dataSetVersionComponents = versionString.split("\\."); 118 return dataSetVersionComponents[0]; 119 } 120 } 121