1b90da8218274400c8710211d467ed28f23bc28baUrs Grob/* 2b90da8218274400c8710211d467ed28f23bc28baUrs Grob * Copyright (C) 2007 The Android Open Source Project 3b90da8218274400c8710211d467ed28f23bc28baUrs Grob * 4b90da8218274400c8710211d467ed28f23bc28baUrs Grob * Licensed under the Apache License, Version 2.0 (the "License"); 5b90da8218274400c8710211d467ed28f23bc28baUrs Grob * you may not use this file except in compliance with the License. 6b90da8218274400c8710211d467ed28f23bc28baUrs Grob * You may obtain a copy of the License at 7b90da8218274400c8710211d467ed28f23bc28baUrs Grob * 8b90da8218274400c8710211d467ed28f23bc28baUrs Grob * http://www.apache.org/licenses/LICENSE-2.0 9b90da8218274400c8710211d467ed28f23bc28baUrs Grob * 10b90da8218274400c8710211d467ed28f23bc28baUrs Grob * Unless required by applicable law or agreed to in writing, software 11b90da8218274400c8710211d467ed28f23bc28baUrs Grob * distributed under the License is distributed on an "AS IS" BASIS, 12b90da8218274400c8710211d467ed28f23bc28baUrs Grob * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b90da8218274400c8710211d467ed28f23bc28baUrs Grob * See the License for the specific language governing permissions and 14b90da8218274400c8710211d467ed28f23bc28baUrs Grob * limitations under the License. 15b90da8218274400c8710211d467ed28f23bc28baUrs Grob */ 16b90da8218274400c8710211d467ed28f23bc28baUrs Grob 17b90da8218274400c8710211d467ed28f23bc28baUrs Grobpackage tests.support; 18b90da8218274400c8710211d467ed28f23bc28baUrs Grob 19b90da8218274400c8710211d467ed28f23bc28baUrs Grobimport java.io.ByteArrayOutputStream; 20b90da8218274400c8710211d467ed28f23bc28baUrs Grobimport java.io.File; 21b90da8218274400c8710211d467ed28f23bc28baUrs Grobimport java.io.FileInputStream; 22b90da8218274400c8710211d467ed28f23bc28baUrs Grobimport java.io.FileNotFoundException; 23b90da8218274400c8710211d467ed28f23bc28baUrs Grobimport java.io.IOException; 24e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughesimport java.io.UnsupportedEncodingException; 25b90da8218274400c8710211d467ed28f23bc28baUrs Grobimport java.util.Date; 26b90da8218274400c8710211d467ed28f23bc28baUrs Grob 27b90da8218274400c8710211d467ed28f23bc28baUrs Grob/** 28b90da8218274400c8710211d467ed28f23bc28baUrs Grob * Represents test data used by the Request API tests 29b90da8218274400c8710211d467ed28f23bc28baUrs Grob */ 30b90da8218274400c8710211d467ed28f23bc28baUrs Grobpublic class Support_TestWebData { 31e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes public final static byte[] test1 = utfBytes(); 32e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes public final static byte[] test2 = newBinaryFile(8192); 33f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 34e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes private static byte[] utfBytes() { 35e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes try { 36e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes return "<html>\n<body>\n<h1>Hello World!</h1>\n</body>\n</html>\n".getBytes("UTF-8"); 37e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes } catch (UnsupportedEncodingException ex) { 38e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes throw new AssertionError(); 39e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes } 40e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes } 41f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 42e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes private static byte[] newBinaryFile(int byteCount) { 43e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes byte[] result = new byte[byteCount]; 44e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes for (int i = 0; i < result.length; ++i) { 45e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes result[i] = (byte) i; 46e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes } 47e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes return result; 48e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes } 49f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 50b90da8218274400c8710211d467ed28f23bc28baUrs Grob // string for test request post body 51b90da8218274400c8710211d467ed28f23bc28baUrs Grob public final static String postContent = "user=111"; 52f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 53b90da8218274400c8710211d467ed28f23bc28baUrs Grob // Array of all test data 54b90da8218274400c8710211d467ed28f23bc28baUrs Grob public final static byte[][] tests = { 55b90da8218274400c8710211d467ed28f23bc28baUrs Grob test1, 56b90da8218274400c8710211d467ed28f23bc28baUrs Grob test2 57b90da8218274400c8710211d467ed28f23bc28baUrs Grob }; 58b90da8218274400c8710211d467ed28f23bc28baUrs Grob 59b90da8218274400c8710211d467ed28f23bc28baUrs Grob /** 60b90da8218274400c8710211d467ed28f23bc28baUrs Grob * List of static test cases for use with test server 61b90da8218274400c8710211d467ed28f23bc28baUrs Grob */ 62b90da8218274400c8710211d467ed28f23bc28baUrs Grob public static Support_TestWebData[] testParams = { 63e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes new Support_TestWebData(test1.length, 14000000, "test1", "text/html", false, 0), 64e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes new Support_TestWebData(test2.length, 14000002, "test2", "unknown/unknown", false, 65b90da8218274400c8710211d467ed28f23bc28baUrs Grob new Date().getTime() + 100000) 66b90da8218274400c8710211d467ed28f23bc28baUrs Grob }; 67b90da8218274400c8710211d467ed28f23bc28baUrs Grob 68b90da8218274400c8710211d467ed28f23bc28baUrs Grob /** 69b90da8218274400c8710211d467ed28f23bc28baUrs Grob * List of response strings for use by the test server 70b90da8218274400c8710211d467ed28f23bc28baUrs Grob */ 71b90da8218274400c8710211d467ed28f23bc28baUrs Grob public static String[] testServerResponse = { 72b90da8218274400c8710211d467ed28f23bc28baUrs Grob "Redirecting 301", 73b90da8218274400c8710211d467ed28f23bc28baUrs Grob "Redirecting 302", 74b90da8218274400c8710211d467ed28f23bc28baUrs Grob "Redirecting 303", 75b90da8218274400c8710211d467ed28f23bc28baUrs Grob "Redirecting 307" 76b90da8218274400c8710211d467ed28f23bc28baUrs Grob }; 77b90da8218274400c8710211d467ed28f23bc28baUrs Grob 78b90da8218274400c8710211d467ed28f23bc28baUrs Grob // Redirection indices into testServerResponse 79b90da8218274400c8710211d467ed28f23bc28baUrs Grob public final static int REDIRECT_301 = 0; 80b90da8218274400c8710211d467ed28f23bc28baUrs Grob public final static int REDIRECT_302 = 1; 81b90da8218274400c8710211d467ed28f23bc28baUrs Grob public final static int REDIRECT_303 = 2; 82b90da8218274400c8710211d467ed28f23bc28baUrs Grob public final static int REDIRECT_307 = 3; 83b90da8218274400c8710211d467ed28f23bc28baUrs Grob 84b90da8218274400c8710211d467ed28f23bc28baUrs Grob /** 85b90da8218274400c8710211d467ed28f23bc28baUrs Grob * Creates a data package with information used by the server when responding 86b90da8218274400c8710211d467ed28f23bc28baUrs Grob * to requests 87b90da8218274400c8710211d467ed28f23bc28baUrs Grob */ 88b90da8218274400c8710211d467ed28f23bc28baUrs Grob Support_TestWebData(int length, int lastModified, String name, String type, boolean isDir, long expDate) { 89b90da8218274400c8710211d467ed28f23bc28baUrs Grob testLength = length; 90b90da8218274400c8710211d467ed28f23bc28baUrs Grob testLastModified = lastModified; 91b90da8218274400c8710211d467ed28f23bc28baUrs Grob testName = name; 92b90da8218274400c8710211d467ed28f23bc28baUrs Grob testType = type; 93b90da8218274400c8710211d467ed28f23bc28baUrs Grob testDir = isDir; 94b90da8218274400c8710211d467ed28f23bc28baUrs Grob testExp = expDate; 95b90da8218274400c8710211d467ed28f23bc28baUrs Grob } 96b90da8218274400c8710211d467ed28f23bc28baUrs Grob 97b90da8218274400c8710211d467ed28f23bc28baUrs Grob /** 98b90da8218274400c8710211d467ed28f23bc28baUrs Grob * Creates a data package with information used by the server when responding 99b90da8218274400c8710211d467ed28f23bc28baUrs Grob * to requests 100b90da8218274400c8710211d467ed28f23bc28baUrs Grob */ 101b90da8218274400c8710211d467ed28f23bc28baUrs Grob private Support_TestWebData(String path, String type) { 102b90da8218274400c8710211d467ed28f23bc28baUrs Grob File file = new File(path); 103b90da8218274400c8710211d467ed28f23bc28baUrs Grob testLength = file.length(); 104b90da8218274400c8710211d467ed28f23bc28baUrs Grob testLastModified = file.lastModified(); 105b90da8218274400c8710211d467ed28f23bc28baUrs Grob testName = file.getName(); 106b90da8218274400c8710211d467ed28f23bc28baUrs Grob testType = type; 107b90da8218274400c8710211d467ed28f23bc28baUrs Grob testDir = file.isDirectory(); 108b90da8218274400c8710211d467ed28f23bc28baUrs Grob ByteArrayOutputStream out = new ByteArrayOutputStream(); 109b90da8218274400c8710211d467ed28f23bc28baUrs Grob FileInputStream in = null; 110b90da8218274400c8710211d467ed28f23bc28baUrs Grob try { 111b90da8218274400c8710211d467ed28f23bc28baUrs Grob in = new FileInputStream(file); 112b90da8218274400c8710211d467ed28f23bc28baUrs Grob while (in.available() > 0) { 113b90da8218274400c8710211d467ed28f23bc28baUrs Grob out.write(in.read()); 114b90da8218274400c8710211d467ed28f23bc28baUrs Grob } 115b90da8218274400c8710211d467ed28f23bc28baUrs Grob in.close(); 116b90da8218274400c8710211d467ed28f23bc28baUrs Grob out.flush(); 117b90da8218274400c8710211d467ed28f23bc28baUrs Grob test0Data = out.toByteArray(); 118b90da8218274400c8710211d467ed28f23bc28baUrs Grob out.close(); 119b90da8218274400c8710211d467ed28f23bc28baUrs Grob test0DataAvailable = true; 120b90da8218274400c8710211d467ed28f23bc28baUrs Grob return; 121b90da8218274400c8710211d467ed28f23bc28baUrs Grob } catch (Exception e) { 122b90da8218274400c8710211d467ed28f23bc28baUrs Grob // ignore 123b90da8218274400c8710211d467ed28f23bc28baUrs Grob e.printStackTrace(); 124b90da8218274400c8710211d467ed28f23bc28baUrs Grob } finally { 125b90da8218274400c8710211d467ed28f23bc28baUrs Grob try { 126b90da8218274400c8710211d467ed28f23bc28baUrs Grob if (in != null) { 127b90da8218274400c8710211d467ed28f23bc28baUrs Grob in.close(); 128b90da8218274400c8710211d467ed28f23bc28baUrs Grob } 129b90da8218274400c8710211d467ed28f23bc28baUrs Grob if (out != null) { 130b90da8218274400c8710211d467ed28f23bc28baUrs Grob out.close(); 131b90da8218274400c8710211d467ed28f23bc28baUrs Grob } 132b90da8218274400c8710211d467ed28f23bc28baUrs Grob } catch (IOException e) { 133b90da8218274400c8710211d467ed28f23bc28baUrs Grob // ignore 134b90da8218274400c8710211d467ed28f23bc28baUrs Grob } 135b90da8218274400c8710211d467ed28f23bc28baUrs Grob } 136b90da8218274400c8710211d467ed28f23bc28baUrs Grob } 137b90da8218274400c8710211d467ed28f23bc28baUrs Grob 138b90da8218274400c8710211d467ed28f23bc28baUrs Grob public static void initDynamicTestWebData(String path, String type) { 139b90da8218274400c8710211d467ed28f23bc28baUrs Grob test0Params = new Support_TestWebData(path, type); 140b90da8218274400c8710211d467ed28f23bc28baUrs Grob } 141b90da8218274400c8710211d467ed28f23bc28baUrs Grob 142b90da8218274400c8710211d467ed28f23bc28baUrs Grob // Length of test entity body 143b90da8218274400c8710211d467ed28f23bc28baUrs Grob public long testLength; 144b90da8218274400c8710211d467ed28f23bc28baUrs Grob 145b90da8218274400c8710211d467ed28f23bc28baUrs Grob // Last modified date value (milliseconds) 146b90da8218274400c8710211d467ed28f23bc28baUrs Grob public long testLastModified; 147b90da8218274400c8710211d467ed28f23bc28baUrs Grob 148b90da8218274400c8710211d467ed28f23bc28baUrs Grob // Test identification name 149b90da8218274400c8710211d467ed28f23bc28baUrs Grob public String testName; 150b90da8218274400c8710211d467ed28f23bc28baUrs Grob 151b90da8218274400c8710211d467ed28f23bc28baUrs Grob // The MIME type to assume for this test 152b90da8218274400c8710211d467ed28f23bc28baUrs Grob public String testType; 153b90da8218274400c8710211d467ed28f23bc28baUrs Grob 154b90da8218274400c8710211d467ed28f23bc28baUrs Grob // The expiration date 155b90da8218274400c8710211d467ed28f23bc28baUrs Grob public long testExp; 156b90da8218274400c8710211d467ed28f23bc28baUrs Grob 157b90da8218274400c8710211d467ed28f23bc28baUrs Grob // Indicates if this is a directory or not 158b90da8218274400c8710211d467ed28f23bc28baUrs Grob public boolean testDir; 159b90da8218274400c8710211d467ed28f23bc28baUrs Grob 160b90da8218274400c8710211d467ed28f23bc28baUrs Grob // Indicate if test0 data has bin initialized 161b90da8218274400c8710211d467ed28f23bc28baUrs Grob public static boolean test0DataAvailable = false; 162b90da8218274400c8710211d467ed28f23bc28baUrs Grob 163b90da8218274400c8710211d467ed28f23bc28baUrs Grob // test0 data 164b90da8218274400c8710211d467ed28f23bc28baUrs Grob public static byte[] test0Data; 165b90da8218274400c8710211d467ed28f23bc28baUrs Grob 166b90da8218274400c8710211d467ed28f23bc28baUrs Grob // test0 parameters 167b90da8218274400c8710211d467ed28f23bc28baUrs Grob public static Support_TestWebData test0Params; 168b90da8218274400c8710211d467ed28f23bc28baUrs Grob} 169