1/* 2 * Copyright (C) 2013 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 17package libcore.util; 18 19import java.io.File; 20import java.io.FileOutputStream; 21import java.io.RandomAccessFile; 22import java.util.TimeZone; 23 24public class ZoneInfoDBTest extends junit.framework.TestCase { 25 private static final String CURRENT_VERSION = ZoneInfoDB.getInstance().getVersion(); 26 27 // Any new file in /data... 28 private static final String TZDATA_IN_DATA = System.getenv("ANDROID_DATA") + "/misc/zoneinfo/tzdata"; 29 // ...overrides any existing file in /system. 30 private static final String TZDATA_IN_ROOT = System.getenv("ANDROID_ROOT") + "/usr/share/zoneinfo/tzdata"; 31 32 // An empty override file should fall back to the default file. 33 public void testEmptyOverrideFile() throws Exception { 34 ZoneInfoDB.TzData data = new ZoneInfoDB.TzData(makeEmptyFile(), TZDATA_IN_DATA, TZDATA_IN_ROOT); 35 assertEquals(CURRENT_VERSION, data.getVersion()); 36 assertEquals(TimeZone.getAvailableIDs().length, data.getAvailableIDs().length); 37 } 38 39 // A corrupt override file should fall back to the default file. 40 public void testCorruptOverrideFile() throws Exception { 41 ZoneInfoDB.TzData data = new ZoneInfoDB.TzData(makeCorruptFile(), TZDATA_IN_DATA, TZDATA_IN_ROOT); 42 assertEquals(CURRENT_VERSION, data.getVersion()); 43 assertEquals(TimeZone.getAvailableIDs().length, data.getAvailableIDs().length); 44 } 45 46 // Given no tzdata files we can use, we should fall back to built-in "GMT". 47 public void testNoGoodFile() throws Exception { 48 ZoneInfoDB.TzData data = new ZoneInfoDB.TzData(makeEmptyFile()); 49 assertEquals("missing", data.getVersion()); 50 assertEquals(1, data.getAvailableIDs().length); 51 assertEquals("GMT", data.getAvailableIDs()[0]); 52 } 53 54 // Given a valid override file, we should find ourselves using that. 55 public void testGoodOverrideFile() throws Exception { 56 // We copy /system/usr/share/zoneinfo/tzdata because we know that always exists. 57 RandomAccessFile in = new RandomAccessFile(TZDATA_IN_ROOT, "r"); 58 byte[] content = new byte[(int) in.length()]; 59 in.readFully(content); 60 // Bump the version number to one long past where humans will be extinct. 61 content[6] = '9'; 62 content[7] = '9'; 63 content[8] = '9'; 64 content[9] = '9'; 65 content[10] = 'z'; 66 in.close(); 67 68 String goodFile = makeTemporaryFile(content); 69 try { 70 ZoneInfoDB.TzData data = new ZoneInfoDB.TzData(goodFile, TZDATA_IN_DATA, TZDATA_IN_ROOT); 71 assertEquals("9999z", data.getVersion()); 72 assertEquals(TimeZone.getAvailableIDs().length, data.getAvailableIDs().length); 73 } finally { 74 new File(goodFile).delete(); 75 } 76 } 77 78 private static String makeCorruptFile() throws Exception { 79 return makeTemporaryFile("invalid content".getBytes()); 80 } 81 82 private static String makeEmptyFile() throws Exception { 83 return makeTemporaryFile(new byte[0]); 84 } 85 86 private static String makeTemporaryFile(byte[] content) throws Exception { 87 File f = File.createTempFile("temp-", ".txt"); 88 FileOutputStream fos = new FileOutputStream(f); 89 fos.write(content); 90 fos.close(); 91 return f.getPath(); 92 } 93} 94