OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.crash; |
| 6 |
| 7 import android.content.SharedPreferences; |
| 8 import android.preference.PreferenceManager; |
| 9 import android.test.suitebuilder.annotation.SmallTest; |
| 10 |
| 11 import org.chromium.base.annotations.SuppressFBWarnings; |
| 12 import org.chromium.base.test.util.Feature; |
| 13 import org.chromium.chrome.browser.preferences.privacy.CrashReportingPermissionM
anager; |
| 14 import org.chromium.chrome.browser.util.HttpURLConnectionFactory; |
| 15 |
| 16 import java.io.BufferedReader; |
| 17 import java.io.ByteArrayInputStream; |
| 18 import java.io.ByteArrayOutputStream; |
| 19 import java.io.File; |
| 20 import java.io.FileOutputStream; |
| 21 import java.io.FileReader; |
| 22 import java.io.IOException; |
| 23 import java.io.InputStream; |
| 24 import java.io.OutputStream; |
| 25 import java.net.HttpURLConnection; |
| 26 import java.net.URL; |
| 27 |
| 28 /** |
| 29 * Unittests for {@link MinidumpUploadCallable}. |
| 30 */ |
| 31 public class MinidumpUploadCallableTest extends CrashTestCase { |
| 32 private static final String BOUNDARY = "TESTBOUNDARY"; |
| 33 private static final String CRASH_ID = "IMACRASHID"; |
| 34 private static final int CURRENT_DAY = 14; |
| 35 private static final String LOG_FILE_NAME = "chromium_renderer-123_log.dmp22
4"; |
| 36 private File mTestUpload; |
| 37 private File mUploadLog; |
| 38 private File mExpectedFileAfterUpload; |
| 39 |
| 40 private static class TestHttpURLConnection extends HttpURLConnection { |
| 41 private static final String EXPECTED_CONTENT_TYPE_VALUE = |
| 42 String.format(MinidumpUploadCallable.CONTENT_TYPE_TMPL, BOUNDARY
); |
| 43 |
| 44 /** |
| 45 * The value of the "Content-Type" property if the property has been set
. |
| 46 */ |
| 47 private String mContentTypePropertyValue = ""; |
| 48 |
| 49 TestHttpURLConnection(URL url) { |
| 50 super(url); |
| 51 assertEquals(MinidumpUploadCallable.CRASH_URL_STRING, url.toString()
); |
| 52 } |
| 53 |
| 54 @Override |
| 55 public void disconnect() { |
| 56 // Check that the "Content-Type" property has been set and the prope
rty's value. |
| 57 assertEquals(EXPECTED_CONTENT_TYPE_VALUE, mContentTypePropertyValue)
; |
| 58 } |
| 59 |
| 60 @Override |
| 61 public InputStream getInputStream() { |
| 62 return new ByteArrayInputStream(CRASH_ID.getBytes()); |
| 63 } |
| 64 |
| 65 @Override |
| 66 public OutputStream getOutputStream() { |
| 67 return new ByteArrayOutputStream(); |
| 68 } |
| 69 |
| 70 @Override |
| 71 public int getResponseCode() { |
| 72 return 200; |
| 73 } |
| 74 |
| 75 @Override |
| 76 public String getResponseMessage() { |
| 77 return null; |
| 78 } |
| 79 |
| 80 @Override |
| 81 public boolean usingProxy() { |
| 82 return false; |
| 83 } |
| 84 |
| 85 @Override |
| 86 public void connect() { |
| 87 } |
| 88 |
| 89 @Override |
| 90 public void setRequestProperty(String key, String value) { |
| 91 if (key.equals("Content-Type")) { |
| 92 mContentTypePropertyValue = value; |
| 93 } |
| 94 } |
| 95 } |
| 96 |
| 97 private static class TestHttpURLConnectionFactory implements HttpURLConnecti
onFactory { |
| 98 @Override |
| 99 public HttpURLConnection createHttpURLConnection(String url) { |
| 100 try { |
| 101 return new TestHttpURLConnection(new URL(url)); |
| 102 } catch (IOException e) { |
| 103 return null; |
| 104 } |
| 105 } |
| 106 } |
| 107 |
| 108 private static class FailHttpURLConnectionFactory implements HttpURLConnecti
onFactory { |
| 109 @Override |
| 110 public HttpURLConnection createHttpURLConnection(String url) { |
| 111 fail(); |
| 112 return null; |
| 113 } |
| 114 } |
| 115 |
| 116 private static class MockCrashReportingPermissionManager |
| 117 implements CrashReportingPermissionManager { |
| 118 private final boolean mIsPermitted; |
| 119 private final boolean mIsLimitted; |
| 120 |
| 121 MockCrashReportingPermissionManager(boolean isPermitted, boolean isLimit
ted) { |
| 122 mIsPermitted = isPermitted; |
| 123 mIsLimitted = isLimitted; |
| 124 } |
| 125 |
| 126 @Override |
| 127 public boolean isUploadPermitted() { |
| 128 return mIsPermitted; |
| 129 } |
| 130 |
| 131 @Override |
| 132 public boolean isUploadLimited() { |
| 133 return mIsLimitted; |
| 134 } |
| 135 } |
| 136 |
| 137 /** |
| 138 * This class accesses member fields of |MinidumpUploadCallableTest| class a
nd calls |
| 139 * |getInstrumentation| which cannot be done in a static context. |
| 140 */ |
| 141 private class MockMinidumpUploadCallable extends MinidumpUploadCallable { |
| 142 MockMinidumpUploadCallable( |
| 143 HttpURLConnectionFactory httpURLConnectionFactory, |
| 144 CrashReportingPermissionManager permManager) { |
| 145 super(mTestUpload, mUploadLog, httpURLConnectionFactory, permManager
, |
| 146 PreferenceManager.getDefaultSharedPreferences( |
| 147 getInstrumentation().getTargetContext())); |
| 148 } |
| 149 |
| 150 @Override |
| 151 protected int getCurrentDay() { |
| 152 return CURRENT_DAY; |
| 153 } |
| 154 } |
| 155 |
| 156 private void createMinidumpFile() throws Exception { |
| 157 mTestUpload = new File(mCrashDir, LOG_FILE_NAME); |
| 158 setUpMinidumpFile(mTestUpload, BOUNDARY); |
| 159 } |
| 160 |
| 161 @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE") |
| 162 @Override |
| 163 protected void setUp() throws Exception { |
| 164 super.setUp(); |
| 165 mUploadLog = new File(mCacheDir, CrashFileManager.CRASH_DUMP_LOGFILE); |
| 166 // Delete all logs from previous runs if possible. |
| 167 mUploadLog.delete(); |
| 168 |
| 169 createMinidumpFile(); |
| 170 mExpectedFileAfterUpload = new File( |
| 171 mCrashDir, |
| 172 mTestUpload.getName().replaceFirst("\\.dmp", ".up")); |
| 173 } |
| 174 |
| 175 @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE") |
| 176 @Override |
| 177 protected void tearDown() throws Exception { |
| 178 if (mTestUpload.exists()) mTestUpload.delete(); |
| 179 super.tearDown(); |
| 180 } |
| 181 |
| 182 @SmallTest |
| 183 @Feature({"Android-AppBase"}) |
| 184 public void testCallWhenCurrentlyPermitted() throws Exception { |
| 185 CrashReportingPermissionManager testPermManager = |
| 186 new MockCrashReportingPermissionManager(true, false); |
| 187 |
| 188 HttpURLConnectionFactory httpURLConnectionFactory = new TestHttpURLConne
ctionFactory(); |
| 189 |
| 190 MinidumpUploadCallable minidumpUploadCallable = |
| 191 new MockMinidumpUploadCallable(httpURLConnectionFactory, testPer
mManager); |
| 192 assertTrue(minidumpUploadCallable.call()); |
| 193 assertTrue(mExpectedFileAfterUpload.exists()); |
| 194 assertValidUploadLogEntry(); |
| 195 } |
| 196 |
| 197 @SmallTest |
| 198 @Feature({"Android-AppBase"}) |
| 199 public void testCallPermittedButNotUnderCurrentCircumstances() throws Except
ion { |
| 200 CrashReportingPermissionManager testPermManager = |
| 201 new MockCrashReportingPermissionManager(false, false); |
| 202 |
| 203 HttpURLConnectionFactory httpURLConnectionFactory = new FailHttpURLConne
ctionFactory(); |
| 204 |
| 205 MinidumpUploadCallable minidumpUploadCallable = |
| 206 new MockMinidumpUploadCallable(httpURLConnectionFactory, testPer
mManager); |
| 207 assertFalse(minidumpUploadCallable.call()); |
| 208 assertFalse(mExpectedFileAfterUpload.exists()); |
| 209 } |
| 210 |
| 211 @SmallTest |
| 212 @Feature({"Android-AppBase"}) |
| 213 public void testCrashUploadSizeConstraints() throws Exception { |
| 214 CrashReportingPermissionManager testPermManager = |
| 215 new MockCrashReportingPermissionManager(true, true); |
| 216 |
| 217 HttpURLConnectionFactory httpURLConnectionFactory = new TestHttpURLConne
ctionFactory(); |
| 218 |
| 219 FileOutputStream stream = null; |
| 220 try { |
| 221 stream = new FileOutputStream(mTestUpload, true); |
| 222 byte[] buf = new byte[MinidumpUploadCallable.LOG_SIZE_LIMIT_BYTES]; |
| 223 stream.write(buf); |
| 224 stream.flush(); |
| 225 } finally { |
| 226 if (stream != null) stream.close(); |
| 227 } |
| 228 |
| 229 MinidumpUploadCallable minidumpUploadCallable = |
| 230 new MockMinidumpUploadCallable(httpURLConnectionFactory, testPer
mManager); |
| 231 setUpCrashPreferences(CURRENT_DAY, MinidumpUploadCallable.LOG_UPLOAD_LIM
IT_PER_DAY - 1); |
| 232 assertFalse(minidumpUploadCallable.call()); |
| 233 assertFalse(mExpectedFileAfterUpload.exists()); |
| 234 } |
| 235 |
| 236 @SmallTest |
| 237 @Feature({"Android-AppBase"}) |
| 238 public void testCrashUploadSizeNotLimited() throws Exception { |
| 239 CrashReportingPermissionManager testPermManager = |
| 240 new MockCrashReportingPermissionManager(true, false); |
| 241 |
| 242 HttpURLConnectionFactory httpURLConnectionFactory = new TestHttpURLConne
ctionFactory(); |
| 243 |
| 244 FileOutputStream stream = null; |
| 245 try { |
| 246 stream = new FileOutputStream(mTestUpload, true); |
| 247 byte[] buf = new byte[MinidumpUploadCallable.LOG_SIZE_LIMIT_BYTES]; |
| 248 stream.write(buf); |
| 249 stream.flush(); |
| 250 } finally { |
| 251 if (stream != null) stream.close(); |
| 252 } |
| 253 |
| 254 MinidumpUploadCallable minidumpUploadCallable = |
| 255 new MockMinidumpUploadCallable(httpURLConnectionFactory, testPer
mManager); |
| 256 setUpCrashPreferences(CURRENT_DAY, MinidumpUploadCallable.LOG_UPLOAD_LIM
IT_PER_DAY - 1); |
| 257 assertTrue(minidumpUploadCallable.call()); |
| 258 assertTrue(mExpectedFileAfterUpload.exists()); |
| 259 } |
| 260 |
| 261 @SmallTest |
| 262 @Feature({"Android-AppBase"}) |
| 263 public void testCrashUploadFrequencyConstraints() throws Exception { |
| 264 CrashReportingPermissionManager testPermManager = |
| 265 new MockCrashReportingPermissionManager(true, true); |
| 266 |
| 267 HttpURLConnectionFactory httpURLConnectionFactory = new TestHttpURLConne
ctionFactory(); |
| 268 |
| 269 MinidumpUploadCallable minidumpUploadCallable = |
| 270 new MockMinidumpUploadCallable(httpURLConnectionFactory, testPer
mManager); |
| 271 setUpCrashPreferences(CURRENT_DAY, MinidumpUploadCallable.LOG_UPLOAD_LIM
IT_PER_DAY); |
| 272 assertFalse(minidumpUploadCallable.call()); |
| 273 assertFalse(mExpectedFileAfterUpload.exists()); |
| 274 |
| 275 setUpCrashPreferences(CURRENT_DAY, MinidumpUploadCallable.LOG_UPLOAD_LIM
IT_PER_DAY - 1); |
| 276 assertTrue(minidumpUploadCallable.call()); |
| 277 assertTrue(mExpectedFileAfterUpload.exists()); |
| 278 |
| 279 createMinidumpFile(); |
| 280 |
| 281 setUpCrashPreferences(CURRENT_DAY - 1, MinidumpUploadCallable.LOG_UPLOAD
_LIMIT_PER_DAY); |
| 282 assertTrue(minidumpUploadCallable.call()); |
| 283 assertTrue(mExpectedFileAfterUpload.exists()); |
| 284 } |
| 285 |
| 286 private void setUpCrashPreferences(int lastDay, int count) { |
| 287 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences( |
| 288 getInstrumentation().getTargetContext()); |
| 289 pref.edit() |
| 290 .putInt(MinidumpUploadCallable.PREF_LAST_UPLOAD_DAY, lastDay) |
| 291 .putInt(MinidumpUploadCallable.PREF_UPLOAD_COUNT, count) |
| 292 .apply(); |
| 293 } |
| 294 |
| 295 private void assertValidUploadLogEntry() throws IOException { |
| 296 File logfile = new File(mCacheDir, CrashFileManager.CRASH_DUMP_LOGFILE); |
| 297 BufferedReader input = new BufferedReader(new FileReader(logfile)); |
| 298 String line = null; |
| 299 String lastEntry = null; |
| 300 while ((line = input.readLine()) != null) { |
| 301 lastEntry = line; |
| 302 } |
| 303 input.close(); |
| 304 |
| 305 assertNotNull("We do not have a single entry in uploads.log", lastEntry)
; |
| 306 int seperator = lastEntry.indexOf(','); |
| 307 |
| 308 long time = Long.parseLong(lastEntry.substring(0, seperator)); |
| 309 long now = System.currentTimeMillis() / 1000; // Timestamp was in second
s. |
| 310 |
| 311 // Sanity check on the time stamp (within an hour). |
| 312 // Chances are the write and the check should have less than 1 second in
between. |
| 313 assertTrue(time <= now); |
| 314 assertTrue(time > now - 60 * 60); |
| 315 |
| 316 String id = lastEntry.substring(seperator + 1, lastEntry.length()); |
| 317 assertEquals(id, CRASH_ID); |
| 318 } |
| 319 } |
OLD | NEW |