1 /*
2  * Copyright (C) 2007 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 
17 package tests.support;
18 
19 import java.io.ByteArrayOutputStream;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.IOException;
23 import java.io.UnsupportedEncodingException;
24 import java.util.Date;
25 
26 /**
27  * Represents test data used by the Request API tests
28  */
29 public class Support_TestWebData {
30   public final static byte[] test1 = utfBytes();
31   public final static byte[] test2 = newBinaryFile(8192);
32 
33   // Although the tests report the content length of this file as being large, a file is created
34   // that is much shorter. Actually creating a large file is not practical during tests. This
35   // file should only be used for checking header information.
36   private static final long TEST_3_CONTENT_LENGTH = ((long) Integer.MAX_VALUE) + 1;
37   public final static byte[] test3 = newBinaryFile(3);
38 
utfBytes()39   private static byte[] utfBytes() {
40     try {
41       return "<html>\n<body>\n<h1>Hello World!</h1>\n</body>\n</html>\n".getBytes("UTF-8");
42     } catch (UnsupportedEncodingException ex) {
43       throw new AssertionError();
44     }
45   }
46 
newBinaryFile(int byteCount)47   private static byte[] newBinaryFile(int byteCount) {
48     byte[] result = new byte[byteCount];
49     for (int i = 0; i < result.length; ++i) {
50       result[i] = (byte) i;
51     }
52     return result;
53   }
54 
55   // string for test request post body
56   public final static String postContent = "user=111";
57 
58   // Array of all test data
59   public final static byte[][] tests = {
60     test1,
61     test2,
62     test3,
63   };
64 
65   /**
66    * List of static test cases for use with test server
67    */
68   public static Support_TestWebData[] testParams = {
69     new Support_TestWebData(test1.length, 14000000, "test1", "text/html", false, 0),
70     new Support_TestWebData(test2.length, 14000002, "test2", "unknown/unknown", false,
71             new Date().getTime() + 100000),
72     new Support_TestWebData(TEST_3_CONTENT_LENGTH, 14000004, "test3", "unknown/unknown", false,
73             new Date().getTime() + 100000),
74   };
75 
76   /**
77    * List of response strings for use by the test server
78    */
79   public static String[] testServerResponse = {
80     "Redirecting 301",
81     "Redirecting 302",
82     "Redirecting 303",
83     "Redirecting 307"
84   };
85 
86   // Redirection indices into testServerResponse
87   public final static int REDIRECT_301 = 0;
88   public final static int REDIRECT_302 = 1;
89   public final static int REDIRECT_303 = 2;
90   public final static int REDIRECT_307 = 3;
91 
92   /**
93    * Creates a data package with information used by the server when responding
94    * to requests
95    */
Support_TestWebData(long length, int lastModified, String name, String type, boolean isDir, long expDate)96   Support_TestWebData(long length, int lastModified, String name, String type, boolean isDir, long expDate) {
97     testLength = length;
98     testLastModified = lastModified;
99     testName = name;
100     testType = type;
101     testDir = isDir;
102     testExp = expDate;
103   }
104 
105   /**
106    * Creates a data package with information used by the server when responding
107    * to requests
108    */
Support_TestWebData(String path, String type)109   private Support_TestWebData(String path, String type) {
110     File file = new File(path);
111     testLength = file.length();
112     testLastModified = file.lastModified();
113     testName = file.getName();
114     testType = type;
115     testDir = file.isDirectory();
116     ByteArrayOutputStream out = new ByteArrayOutputStream();
117     FileInputStream in = null;
118     try {
119         in = new FileInputStream(file);
120         while (in.available() > 0) {
121             out.write(in.read());
122         }
123         in.close();
124         out.flush();
125         test0Data = out.toByteArray();
126         out.close();
127         test0DataAvailable = true;
128         return;
129     } catch (Exception e) {
130         // ignore
131         e.printStackTrace();
132     } finally {
133         try {
134             if (in != null) {
135                 in.close();
136             }
137             if (out != null) {
138                 out.close();
139             }
140         } catch (IOException e) {
141             // ignore
142         }
143     }
144   }
145 
initDynamicTestWebData(String path, String type)146   public static void initDynamicTestWebData(String path, String type) {
147       test0Params = new Support_TestWebData(path, type);
148   }
149 
150   // Length of test entity body
151   public long testLength;
152 
153   // Last modified date value (milliseconds)
154   public long testLastModified;
155 
156   // Test identification name
157   public String testName;
158 
159   // The MIME type to assume for this test
160   public String testType;
161 
162   // The expiration date
163   public long testExp;
164 
165   // Indicates if this is a directory or not
166   public boolean testDir;
167 
168   // Indicate if test0 data has bin initialized
169   public static boolean test0DataAvailable = false;
170 
171   // test0 data
172   public static byte[] test0Data;
173 
174   // test0 parameters
175   public static Support_TestWebData test0Params;
176 }
177