| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.crash; | 5 package org.chromium.chrome.browser.crash; |
| 6 | 6 |
| 7 import android.test.suitebuilder.annotation.SmallTest; | 7 import android.test.suitebuilder.annotation.SmallTest; |
| 8 | 8 |
| 9 import org.chromium.base.annotations.SuppressFBWarnings; | 9 import org.chromium.base.annotations.SuppressFBWarnings; |
| 10 import org.chromium.base.test.util.Feature; | 10 import org.chromium.base.test.util.Feature; |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 return null; | 110 return null; |
| 111 } | 111 } |
| 112 } | 112 } |
| 113 | 113 |
| 114 private static class MockCrashReportingPermissionManager | 114 private static class MockCrashReportingPermissionManager |
| 115 implements CrashReportingPermissionManager { | 115 implements CrashReportingPermissionManager { |
| 116 private final boolean mIsPermitted; | 116 private final boolean mIsPermitted; |
| 117 private final boolean mIsUserPermitted; | 117 private final boolean mIsUserPermitted; |
| 118 private final boolean mIsCommandLineDisabled; | 118 private final boolean mIsCommandLineDisabled; |
| 119 private final boolean mIsLimited; | 119 private final boolean mIsLimited; |
| 120 private final boolean mIsEnabledForTests; |
| 120 | 121 |
| 121 MockCrashReportingPermissionManager(boolean isPermitted, | 122 MockCrashReportingPermissionManager(boolean isPermitted, boolean isUserP
ermitted, |
| 122 boolean isUserPermitted, boolean isCommandLineDisabled, boolean
isLimited) { | 123 boolean isCommandLineDisabled, boolean isLimited, boolean isEnab
ledForTests) { |
| 123 mIsPermitted = isPermitted; | 124 mIsPermitted = isPermitted; |
| 124 mIsUserPermitted = isUserPermitted; | 125 mIsUserPermitted = isUserPermitted; |
| 125 mIsCommandLineDisabled = isCommandLineDisabled; | 126 mIsCommandLineDisabled = isCommandLineDisabled; |
| 126 mIsLimited = isLimited; | 127 mIsLimited = isLimited; |
| 128 mIsEnabledForTests = isEnabledForTests; |
| 127 } | 129 } |
| 128 | 130 |
| 129 @Override | 131 @Override |
| 130 public boolean isUploadPermitted() { | 132 public boolean isUploadPermitted() { |
| 131 return mIsPermitted; | 133 return mIsPermitted; |
| 132 } | 134 } |
| 133 | 135 |
| 134 @Override | 136 @Override |
| 135 public boolean isUploadUserPermitted() { | 137 public boolean isUploadUserPermitted() { |
| 136 return mIsUserPermitted; | 138 return mIsUserPermitted; |
| 137 } | 139 } |
| 138 | 140 |
| 139 @Override | 141 @Override |
| 140 public boolean isUploadCommandLineDisabled() { | 142 public boolean isUploadCommandLineDisabled() { |
| 141 return mIsCommandLineDisabled; | 143 return mIsCommandLineDisabled; |
| 142 } | 144 } |
| 143 | 145 |
| 144 @Override | 146 @Override |
| 145 public boolean isUploadLimited() { | 147 public boolean isUploadLimited() { |
| 146 return mIsLimited; | 148 return mIsLimited; |
| 147 } | 149 } |
| 150 |
| 151 @Override |
| 152 public boolean isUploadEnabledForTests() { |
| 153 return mIsEnabledForTests; |
| 154 } |
| 148 } | 155 } |
| 149 | 156 |
| 150 /** | 157 /** |
| 151 * This class calls |getInstrumentation| which cannot be done in a static co
ntext. | 158 * This class calls |getInstrumentation| which cannot be done in a static co
ntext. |
| 152 */ | 159 */ |
| 153 private class MockMinidumpUploadCallable extends MinidumpUploadCallable { | 160 private class MockMinidumpUploadCallable extends MinidumpUploadCallable { |
| 154 MockMinidumpUploadCallable( | 161 MockMinidumpUploadCallable( |
| 155 HttpURLConnectionFactory httpURLConnectionFactory, | 162 HttpURLConnectionFactory httpURLConnectionFactory, |
| 156 CrashReportingPermissionManager permManager) { | 163 CrashReportingPermissionManager permManager) { |
| 157 super(mTestUpload, mUploadLog, httpURLConnectionFactory, permManager
); | 164 super(mTestUpload, mUploadLog, httpURLConnectionFactory, permManager
); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 181 @Override | 188 @Override |
| 182 protected void tearDown() throws Exception { | 189 protected void tearDown() throws Exception { |
| 183 if (mTestUpload.exists()) mTestUpload.delete(); | 190 if (mTestUpload.exists()) mTestUpload.delete(); |
| 184 super.tearDown(); | 191 super.tearDown(); |
| 185 } | 192 } |
| 186 | 193 |
| 187 @SmallTest | 194 @SmallTest |
| 188 @Feature({"Android-AppBase"}) | 195 @Feature({"Android-AppBase"}) |
| 189 public void testCallWhenCurrentlyPermitted() throws Exception { | 196 public void testCallWhenCurrentlyPermitted() throws Exception { |
| 190 CrashReportingPermissionManager testPermManager = | 197 CrashReportingPermissionManager testPermManager = |
| 191 new MockCrashReportingPermissionManager(true, true, false, false
); | 198 new MockCrashReportingPermissionManager(true, true, false, false
, false); |
| 192 | 199 |
| 193 HttpURLConnectionFactory httpURLConnectionFactory = new TestHttpURLConne
ctionFactory(); | 200 HttpURLConnectionFactory httpURLConnectionFactory = new TestHttpURLConne
ctionFactory(); |
| 194 | 201 |
| 195 MinidumpUploadCallable minidumpUploadCallable = | 202 MinidumpUploadCallable minidumpUploadCallable = |
| 196 new MockMinidumpUploadCallable(httpURLConnectionFactory, testPer
mManager); | 203 new MockMinidumpUploadCallable(httpURLConnectionFactory, testPer
mManager); |
| 197 assertEquals(MinidumpUploadCallable.UPLOAD_SUCCESS, | 204 assertEquals(MinidumpUploadCallable.UPLOAD_SUCCESS, |
| 198 minidumpUploadCallable.call().intValue()); | 205 minidumpUploadCallable.call().intValue()); |
| 199 assertTrue(mExpectedFileAfterUpload.exists()); | 206 assertTrue(mExpectedFileAfterUpload.exists()); |
| 200 assertValidUploadLogEntry(); | 207 assertValidUploadLogEntry(); |
| 201 } | 208 } |
| 202 | 209 |
| 203 @SmallTest | 210 @SmallTest |
| 204 @Feature({"Android-AppBase"}) | 211 @Feature({"Android-AppBase"}) |
| 205 public void testCallNotPermittedByUser() throws Exception { | 212 public void testCallNotPermittedByUser() throws Exception { |
| 206 CrashReportingPermissionManager testPermManager = | 213 CrashReportingPermissionManager testPermManager = |
| 207 new MockCrashReportingPermissionManager(false, false, false, fal
se); | 214 new MockCrashReportingPermissionManager(false, false, false, fal
se, false); |
| 208 | 215 |
| 209 HttpURLConnectionFactory httpURLConnectionFactory = new FailHttpURLConne
ctionFactory(); | 216 HttpURLConnectionFactory httpURLConnectionFactory = new FailHttpURLConne
ctionFactory(); |
| 210 | 217 |
| 211 MinidumpUploadCallable minidumpUploadCallable = | 218 MinidumpUploadCallable minidumpUploadCallable = |
| 212 new MockMinidumpUploadCallable(httpURLConnectionFactory, testPer
mManager); | 219 new MockMinidumpUploadCallable(httpURLConnectionFactory, testPer
mManager); |
| 213 assertEquals(MinidumpUploadCallable.UPLOAD_USER_DISABLED, | 220 assertEquals(MinidumpUploadCallable.UPLOAD_USER_DISABLED, |
| 214 minidumpUploadCallable.call().intValue()); | 221 minidumpUploadCallable.call().intValue()); |
| 215 assertTrue(mExpectedFileAfterUpload.exists()); | 222 assertTrue(mExpectedFileAfterUpload.exists()); |
| 216 } | 223 } |
| 217 | 224 |
| 218 @SmallTest | 225 @SmallTest |
| 219 @Feature({"Android-AppBase"}) | 226 @Feature({"Android-AppBase"}) |
| 220 public void testCallNotPermittedByCommandLine() throws Exception { | 227 public void testCallNotPermittedByCommandLine() throws Exception { |
| 221 CrashReportingPermissionManager testPermManager = | 228 CrashReportingPermissionManager testPermManager = |
| 222 new MockCrashReportingPermissionManager(true, true, true, false)
; | 229 new MockCrashReportingPermissionManager(true, true, true, false,
false); |
| 223 | 230 |
| 224 HttpURLConnectionFactory httpURLConnectionFactory = new FailHttpURLConne
ctionFactory(); | 231 HttpURLConnectionFactory httpURLConnectionFactory = new FailHttpURLConne
ctionFactory(); |
| 225 | 232 |
| 226 MinidumpUploadCallable minidumpUploadCallable = | 233 MinidumpUploadCallable minidumpUploadCallable = |
| 227 new MockMinidumpUploadCallable(httpURLConnectionFactory, testPer
mManager); | 234 new MockMinidumpUploadCallable(httpURLConnectionFactory, testPer
mManager); |
| 228 assertEquals(MinidumpUploadCallable.UPLOAD_COMMANDLINE_DISABLED, | 235 assertEquals(MinidumpUploadCallable.UPLOAD_COMMANDLINE_DISABLED, |
| 229 minidumpUploadCallable.call().intValue()); | 236 minidumpUploadCallable.call().intValue()); |
| 230 assertFalse(mExpectedFileAfterUpload.exists()); | 237 assertFalse(mExpectedFileAfterUpload.exists()); |
| 231 } | 238 } |
| 232 | 239 |
| 233 @SmallTest | 240 @SmallTest |
| 234 @Feature({"Android-AppBase"}) | 241 @Feature({"Android-AppBase"}) |
| 235 public void testCallPermittedButNotUnderCurrentCircumstances() throws Except
ion { | 242 public void testCallPermittedButNotUnderCurrentCircumstances() throws Except
ion { |
| 236 CrashReportingPermissionManager testPermManager = | 243 CrashReportingPermissionManager testPermManager = |
| 237 new MockCrashReportingPermissionManager(false, true, false, fals
e); | 244 new MockCrashReportingPermissionManager(false, true, false, fals
e, false); |
| 238 | 245 |
| 239 HttpURLConnectionFactory httpURLConnectionFactory = new FailHttpURLConne
ctionFactory(); | 246 HttpURLConnectionFactory httpURLConnectionFactory = new FailHttpURLConne
ctionFactory(); |
| 240 | 247 |
| 241 MinidumpUploadCallable minidumpUploadCallable = | 248 MinidumpUploadCallable minidumpUploadCallable = |
| 242 new MockMinidumpUploadCallable(httpURLConnectionFactory, testPer
mManager); | 249 new MockMinidumpUploadCallable(httpURLConnectionFactory, testPer
mManager); |
| 243 assertEquals(MinidumpUploadCallable.UPLOAD_FAILURE, | 250 assertEquals(MinidumpUploadCallable.UPLOAD_FAILURE, |
| 244 minidumpUploadCallable.call().intValue()); | 251 minidumpUploadCallable.call().intValue()); |
| 245 assertFalse(mExpectedFileAfterUpload.exists()); | 252 assertFalse(mExpectedFileAfterUpload.exists()); |
| 246 } | 253 } |
| 247 | 254 |
| 248 @SmallTest | 255 @SmallTest |
| 249 @Feature({"Android-AppBase"}) | 256 @Feature({"Android-AppBase"}) |
| 250 public void testCrashUploadConstrainted() throws Exception { | 257 public void testCrashUploadConstrainted() throws Exception { |
| 251 CrashReportingPermissionManager testPermManager = | 258 CrashReportingPermissionManager testPermManager = |
| 252 new MockCrashReportingPermissionManager(true, true, false, true)
; | 259 new MockCrashReportingPermissionManager(true, true, false, true,
false); |
| 253 | 260 |
| 254 HttpURLConnectionFactory httpURLConnectionFactory = new TestHttpURLConne
ctionFactory(); | 261 HttpURLConnectionFactory httpURLConnectionFactory = new TestHttpURLConne
ctionFactory(); |
| 255 | 262 |
| 256 MinidumpUploadCallable minidumpUploadCallable = | 263 MinidumpUploadCallable minidumpUploadCallable = |
| 257 new MockMinidumpUploadCallable(httpURLConnectionFactory, testPer
mManager); | 264 new MockMinidumpUploadCallable(httpURLConnectionFactory, testPer
mManager); |
| 258 assertEquals(MinidumpUploadCallable.UPLOAD_FAILURE, | 265 assertEquals(MinidumpUploadCallable.UPLOAD_FAILURE, |
| 259 minidumpUploadCallable.call().intValue()); | 266 minidumpUploadCallable.call().intValue()); |
| 260 assertFalse(mExpectedFileAfterUpload.exists()); | 267 assertFalse(mExpectedFileAfterUpload.exists()); |
| 261 } | 268 } |
| 262 | 269 |
| 270 @SmallTest |
| 271 @Feature({"Android-AppBase"}) |
| 272 public void testCrashUploadEnabledForTestsDespiteConstraints() throws Except
ion { |
| 273 CrashReportingPermissionManager testPermManager = |
| 274 new MockCrashReportingPermissionManager(false, false, false, tru
e, true); |
| 275 |
| 276 HttpURLConnectionFactory httpURLConnectionFactory = new TestHttpURLConne
ctionFactory(); |
| 277 |
| 278 MinidumpUploadCallable minidumpUploadCallable = |
| 279 new MockMinidumpUploadCallable(httpURLConnectionFactory, testPer
mManager); |
| 280 assertEquals(MinidumpUploadCallable.UPLOAD_SUCCESS, |
| 281 minidumpUploadCallable.call().intValue()); |
| 282 assertTrue(mExpectedFileAfterUpload.exists()); |
| 283 assertValidUploadLogEntry(); |
| 284 } |
| 285 |
| 263 private void extendUploadFile(int numBytes) throws FileNotFoundException, IO
Exception { | 286 private void extendUploadFile(int numBytes) throws FileNotFoundException, IO
Exception { |
| 264 FileOutputStream stream = null; | 287 FileOutputStream stream = null; |
| 265 try { | 288 try { |
| 266 stream = new FileOutputStream(mTestUpload, true); | 289 stream = new FileOutputStream(mTestUpload, true); |
| 267 byte[] buf = new byte[numBytes]; | 290 byte[] buf = new byte[numBytes]; |
| 268 stream.write(buf); | 291 stream.write(buf); |
| 269 stream.flush(); | 292 stream.flush(); |
| 270 } finally { | 293 } finally { |
| 271 if (stream != null) stream.close(); | 294 if (stream != null) stream.close(); |
| 272 } | 295 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 290 | 313 |
| 291 // Sanity check on the time stamp (within an hour). | 314 // Sanity check on the time stamp (within an hour). |
| 292 // Chances are the write and the check should have less than 1 second in
between. | 315 // Chances are the write and the check should have less than 1 second in
between. |
| 293 assertTrue(time <= now); | 316 assertTrue(time <= now); |
| 294 assertTrue(time > now - 60 * 60); | 317 assertTrue(time > now - 60 * 60); |
| 295 | 318 |
| 296 String id = lastEntry.substring(seperator + 1, lastEntry.length()); | 319 String id = lastEntry.substring(seperator + 1, lastEntry.length()); |
| 297 assertEquals(id, CRASH_ID); | 320 assertEquals(id, CRASH_ID); |
| 298 } | 321 } |
| 299 } | 322 } |
| OLD | NEW |