| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.android_webview.test.crash; | 5 package org.chromium.android_webview.test.crash; |
| 6 | 6 |
| 7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; | 7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; |
| 8 | 8 |
| 9 import android.content.Context; | |
| 10 import android.os.ParcelFileDescriptor; | 9 import android.os.ParcelFileDescriptor; |
| 11 import android.support.test.filters.MediumTest; | 10 import android.support.test.filters.MediumTest; |
| 12 import android.webkit.ValueCallback; | 11 import android.webkit.ValueCallback; |
| 13 | 12 |
| 14 import org.chromium.android_webview.PlatformServiceBridge; | 13 import org.chromium.android_webview.PlatformServiceBridge; |
| 15 import org.chromium.android_webview.crash.AwMinidumpUploaderDelegate; | 14 import org.chromium.android_webview.crash.AwMinidumpUploaderDelegate; |
| 16 import org.chromium.android_webview.crash.CrashReceiverService; | 15 import org.chromium.android_webview.crash.CrashReceiverService; |
| 17 import org.chromium.base.FileUtils; | 16 import org.chromium.base.FileUtils; |
| 18 import org.chromium.base.ThreadUtils; | 17 import org.chromium.base.ThreadUtils; |
| 19 import org.chromium.components.minidump_uploader.CrashFileManager; | 18 import org.chromium.components.minidump_uploader.CrashFileManager; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 44 * Instrumentation tests for MinidumpUploader. | 43 * Instrumentation tests for MinidumpUploader. |
| 45 */ | 44 */ |
| 46 public class MinidumpUploaderTest extends CrashTestCase { | 45 public class MinidumpUploaderTest extends CrashTestCase { |
| 47 private static final String TAG = "MinidumpUploaderTest"; | 46 private static final String TAG = "MinidumpUploaderTest"; |
| 48 private static final String BOUNDARY = "TESTBOUNDARY"; | 47 private static final String BOUNDARY = "TESTBOUNDARY"; |
| 49 | 48 |
| 50 private static final long TIME_OUT_MILLIS = 3000; | 49 private static final long TIME_OUT_MILLIS = 3000; |
| 51 | 50 |
| 52 @Override | 51 @Override |
| 53 protected File getExistingCacheDir() { | 52 protected File getExistingCacheDir() { |
| 54 return CrashReceiverService.createWebViewCrashDir(getInstrumentation().g
etTargetContext()); | 53 return CrashReceiverService.getOrCreateWebViewCrashDir(); |
| 55 } | 54 } |
| 56 | 55 |
| 57 private static class TestPlatformServiceBridge extends PlatformServiceBridge
{ | 56 private static class TestPlatformServiceBridge extends PlatformServiceBridge
{ |
| 58 private final boolean mEnabled; | 57 private final boolean mEnabled; |
| 59 | 58 |
| 60 public TestPlatformServiceBridge(boolean enabled) { | 59 public TestPlatformServiceBridge(boolean enabled) { |
| 61 mEnabled = enabled; | 60 mEnabled = enabled; |
| 62 } | 61 } |
| 63 | 62 |
| 64 @Override | 63 @Override |
| 65 public boolean canUseGms() { | 64 public boolean canUseGms() { |
| 66 return true; | 65 return true; |
| 67 } | 66 } |
| 68 | 67 |
| 69 @Override | 68 @Override |
| 70 public void queryMetricsSetting(ValueCallback<Boolean> callback) { | 69 public void queryMetricsSetting(ValueCallback<Boolean> callback) { |
| 71 ThreadUtils.assertOnUiThread(); | 70 ThreadUtils.assertOnUiThread(); |
| 72 callback.onReceiveValue(mEnabled); | 71 callback.onReceiveValue(mEnabled); |
| 73 } | 72 } |
| 74 } | 73 } |
| 75 | 74 |
| 76 private class TestMinidumpUploaderImpl extends MinidumpUploaderImpl { | 75 private class TestMinidumpUploaderImpl extends MinidumpUploaderImpl { |
| 77 private final CrashReportingPermissionManager mPermissionManager; | 76 private final CrashReportingPermissionManager mPermissionManager; |
| 78 | 77 |
| 79 TestMinidumpUploaderImpl(CrashReportingPermissionManager permissionManag
er) { | 78 TestMinidumpUploaderImpl(CrashReportingPermissionManager permissionManag
er) { |
| 80 super(new AwMinidumpUploaderDelegate(getInstrumentation().getTargetC
ontext())); | 79 super(new AwMinidumpUploaderDelegate()); |
| 81 mPermissionManager = permissionManager; | 80 mPermissionManager = permissionManager; |
| 82 } | 81 } |
| 83 | 82 |
| 84 TestMinidumpUploaderImpl(MinidumpUploaderDelegate delegate, | 83 TestMinidumpUploaderImpl(MinidumpUploaderDelegate delegate, |
| 85 CrashReportingPermissionManager permissionManager) { | 84 CrashReportingPermissionManager permissionManager) { |
| 86 super(delegate); | 85 super(delegate); |
| 87 mPermissionManager = permissionManager; | 86 mPermissionManager = permissionManager; |
| 88 } | 87 } |
| 89 | 88 |
| 90 @Override | 89 @Override |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 assertTrue(uploadsFinishedLatch.await( | 190 assertTrue(uploadsFinishedLatch.await( |
| 192 scaleTimeout(TIME_OUT_MILLIS), TimeUnit.MILLISECONDS)); | 191 scaleTimeout(TIME_OUT_MILLIS), TimeUnit.MILLISECONDS)); |
| 193 } catch (InterruptedException e) { | 192 } catch (InterruptedException e) { |
| 194 throw new RuntimeException(e); | 193 throw new RuntimeException(e); |
| 195 } | 194 } |
| 196 } | 195 } |
| 197 | 196 |
| 198 /** | 197 /** |
| 199 * Ensure MinidumpUploaderImpl doesn't crash even if the WebView Crash dir d
oesn't exist (could | 198 * Ensure MinidumpUploaderImpl doesn't crash even if the WebView Crash dir d
oesn't exist (could |
| 200 * happen e.g. if a Job persists across WebView-updates? | 199 * happen e.g. if a Job persists across WebView-updates? |
| 200 * |
| 201 * MinidumpUploaderImpl should automatically recreate the directory. |
| 201 */ | 202 */ |
| 202 @MediumTest | 203 @MediumTest |
| 203 public void testUploadingWithoutCrashDir() throws IOException { | 204 public void testUploadingWithoutCrashDir() throws IOException { |
| 204 File webviewCrashDir = getExistingCacheDir(); | 205 File webviewCrashDir = getExistingCacheDir(); |
| 205 // Delete the WebView crash directory to ensure MinidumpUploader doesn't
crash without it. | 206 // Delete the WebView crash directory to ensure MinidumpUploader doesn't
crash without it. |
| 206 FileUtils.recursivelyDeleteFile(webviewCrashDir); | 207 FileUtils.recursivelyDeleteFile(webviewCrashDir); |
| 207 assertFalse(webviewCrashDir.exists()); | 208 assertFalse(webviewCrashDir.exists()); |
| 208 | 209 |
| 209 PlatformServiceBridge.injectInstance(new TestPlatformServiceBridge(true)
); | 210 PlatformServiceBridge.injectInstance(new TestPlatformServiceBridge(true)
); |
| 210 final CrashReportingPermissionManager permManager = | 211 final CrashReportingPermissionManager permManager = |
| (...skipping 14 matching lines...) Expand all Loading... |
| 225 assertFalse(firstFile.exists()); | 226 assertFalse(firstFile.exists()); |
| 226 assertTrue(expectedFirstUploadFile.exists()); | 227 assertTrue(expectedFirstUploadFile.exists()); |
| 227 assertFalse(secondFile.exists()); | 228 assertFalse(secondFile.exists()); |
| 228 assertTrue(expectedSecondUploadFile.exists()); | 229 assertTrue(expectedSecondUploadFile.exists()); |
| 229 } | 230 } |
| 230 | 231 |
| 231 private interface MinidumpUploadCallableCreator { | 232 private interface MinidumpUploadCallableCreator { |
| 232 MinidumpUploadCallable createCallable(File minidumpFile, File logfile); | 233 MinidumpUploadCallable createCallable(File minidumpFile, File logfile); |
| 233 } | 234 } |
| 234 | 235 |
| 235 private MinidumpUploaderImpl createCallableListMinidumpUploader(Context cont
ext, | 236 private MinidumpUploaderImpl createCallableListMinidumpUploader( |
| 236 final List<MinidumpUploadCallableCreator> callables, final boolean u
serPermitted) { | 237 final List<MinidumpUploadCallableCreator> callables, final boolean u
serPermitted) { |
| 237 PlatformServiceBridge.injectInstance(new TestPlatformServiceBridge(userP
ermitted)); | 238 PlatformServiceBridge.injectInstance(new TestPlatformServiceBridge(userP
ermitted)); |
| 238 return new TestMinidumpUploaderImpl(null) { | 239 return new TestMinidumpUploaderImpl(null) { |
| 239 private int mIndex = 0; | 240 private int mIndex = 0; |
| 240 | 241 |
| 241 @Override | 242 @Override |
| 242 public MinidumpUploadCallable createMinidumpUploadCallable( | 243 public MinidumpUploadCallable createMinidumpUploadCallable( |
| 243 File minidumpFile, File logfile) { | 244 File minidumpFile, File logfile) { |
| 244 if (mIndex >= callables.size()) { | 245 if (mIndex >= callables.size()) { |
| 245 fail("Should not create callable number " + mIndex); | 246 fail("Should not create callable number " + mIndex); |
| 246 } | 247 } |
| 247 return callables.get(mIndex++).createCallable(minidumpFile, logf
ile); | 248 return callables.get(mIndex++).createCallable(minidumpFile, logf
ile); |
| 248 } | 249 } |
| 249 }; | 250 }; |
| 250 } | 251 } |
| 251 | 252 |
| 252 @MediumTest | 253 @MediumTest |
| 253 public void testFailingThenPassingUpload() throws IOException { | 254 public void testFailingThenPassingUpload() throws IOException { |
| 254 final CrashReportingPermissionManager permManager = | 255 final CrashReportingPermissionManager permManager = |
| 255 new MockCrashReportingPermissionManager() { | 256 new MockCrashReportingPermissionManager() { |
| 256 { mIsEnabledForTests = true; } | 257 { mIsEnabledForTests = true; } |
| 257 }; | 258 }; |
| 258 List<MinidumpUploadCallableCreator> callables = new ArrayList(); | 259 List<MinidumpUploadCallableCreator> callables = new ArrayList<>(); |
| 259 callables.add(new MinidumpUploadCallableCreator() { | 260 callables.add(new MinidumpUploadCallableCreator() { |
| 260 @Override | 261 @Override |
| 261 public MinidumpUploadCallable createCallable(File minidumpFile, File
logfile) { | 262 public MinidumpUploadCallable createCallable(File minidumpFile, File
logfile) { |
| 262 return new MinidumpUploadCallable( | 263 return new MinidumpUploadCallable( |
| 263 minidumpFile, logfile, new FailingHttpUrlConnectionFacto
ry(), permManager); | 264 minidumpFile, logfile, new FailingHttpUrlConnectionFacto
ry(), permManager); |
| 264 } | 265 } |
| 265 }); | 266 }); |
| 266 callables.add(new MinidumpUploadCallableCreator() { | 267 callables.add(new MinidumpUploadCallableCreator() { |
| 267 @Override | 268 @Override |
| 268 public MinidumpUploadCallable createCallable(File minidumpFile, File
logfile) { | 269 public MinidumpUploadCallable createCallable(File minidumpFile, File
logfile) { |
| 269 return new MinidumpUploadCallable(minidumpFile, logfile, | 270 return new MinidumpUploadCallable(minidumpFile, logfile, |
| 270 new MinidumpUploadCallableTest.TestHttpURLConnectionFact
ory(), permManager); | 271 new MinidumpUploadCallableTest.TestHttpURLConnectionFact
ory(), permManager); |
| 271 } | 272 } |
| 272 }); | 273 }); |
| 273 MinidumpUploader minidumpUploader = | 274 MinidumpUploader minidumpUploader = createCallableListMinidumpUploader( |
| 274 createCallableListMinidumpUploader(getInstrumentation().getTarge
tContext(), | 275 callables, permManager.isUsageAndCrashReportingPermittedByUser()
); |
| 275 callables, permManager.isUsageAndCrashReportingPermitted
ByUser()); | |
| 276 | 276 |
| 277 File firstFile = createMinidumpFileInCrashDir("firstFile.dmp0"); | 277 File firstFile = createMinidumpFileInCrashDir("firstFile.dmp0"); |
| 278 File secondFile = createMinidumpFileInCrashDir("secondFile.dmp0"); | 278 File secondFile = createMinidumpFileInCrashDir("secondFile.dmp0"); |
| 279 | 279 |
| 280 uploadMinidumpsSync(minidumpUploader, true /* expectReschedule */); | 280 uploadMinidumpsSync(minidumpUploader, true /* expectReschedule */); |
| 281 assertFalse(firstFile.exists()); | 281 assertFalse(firstFile.exists()); |
| 282 assertFalse(secondFile.exists()); | 282 assertFalse(secondFile.exists()); |
| 283 File expectedSecondFile = null; | 283 File expectedSecondFile; |
| 284 // Not sure which minidump will fail and which will succeed, so just ens
ure one was uploaded | 284 // Not sure which minidump will fail and which will succeed, so just ens
ure one was uploaded |
| 285 // and the other one failed. | 285 // and the other one failed. |
| 286 if (new File(mCrashDir, firstFile.getName() + ".try1").exists()) { | 286 if (new File(mCrashDir, firstFile.getName() + ".try1").exists()) { |
| 287 expectedSecondFile = new File(mCrashDir, secondFile.getName().replac
e(".dmp", ".up")); | 287 expectedSecondFile = new File(mCrashDir, secondFile.getName().replac
e(".dmp", ".up")); |
| 288 } else { | 288 } else { |
| 289 File uploadedFirstFile = | 289 File uploadedFirstFile = |
| 290 new File(mCrashDir, firstFile.getName().replace(".dmp", ".up
")); | 290 new File(mCrashDir, firstFile.getName().replace(".dmp", ".up
")); |
| 291 assertTrue(uploadedFirstFile.exists()); | 291 assertTrue(uploadedFirstFile.exists()); |
| 292 expectedSecondFile = new File(mCrashDir, secondFile.getName() + ".tr
y1"); | 292 expectedSecondFile = new File(mCrashDir, secondFile.getName() + ".tr
y1"); |
| 293 } | 293 } |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 assertTrue(firstFile.exists()); | 409 assertTrue(firstFile.exists()); |
| 410 assertFalse(expectedFirstUploadFile.exists()); | 410 assertFalse(expectedFirstUploadFile.exists()); |
| 411 assertFalse(expectedFirstRetryFile.exists()); | 411 assertFalse(expectedFirstRetryFile.exists()); |
| 412 } | 412 } |
| 413 } | 413 } |
| 414 | 414 |
| 415 /** | 415 /** |
| 416 * Ensures that the minidump copying works together with the minidump upload
ing. | 416 * Ensures that the minidump copying works together with the minidump upload
ing. |
| 417 */ | 417 */ |
| 418 @MediumTest | 418 @MediumTest |
| 419 public void testCopyAndUploadWebViewMinidump() throws FileNotFoundException,
IOException { | 419 public void testCopyAndUploadWebViewMinidump() throws IOException { |
| 420 final CrashFileManager fileManager = new CrashFileManager( | 420 final CrashFileManager fileManager = |
| 421 CrashReceiverService.getWebViewCrashDir(getInstrumentation().get
TargetContext())); | 421 new CrashFileManager(CrashReceiverService.getWebViewCrashDir()); |
| 422 // Note that these minidump files are set up directly in the cache dir -
not in the WebView | 422 // Note that these minidump files are set up directly in the cache dir -
not in the WebView |
| 423 // crash dir. This is to ensure the CrashFileManager doesn't see these m
inidumps without us | 423 // crash dir. This is to ensure the CrashFileManager doesn't see these m
inidumps without us |
| 424 // first copying them. | 424 // first copying them. |
| 425 File minidumpToCopy = | 425 File minidumpToCopy = new File(getExistingCacheDir(), "toCopy.dmp"); |
| 426 new File(getInstrumentation().getTargetContext().getCacheDir(),
"toCopy.dmp"); | |
| 427 setUpMinidumpFile(minidumpToCopy, BOUNDARY, "browser"); | 426 setUpMinidumpFile(minidumpToCopy, BOUNDARY, "browser"); |
| 428 final String expectedFileContent = readEntireFile(minidumpToCopy); | 427 final String expectedFileContent = readEntireFile(minidumpToCopy); |
| 429 | 428 |
| 430 File[] uploadedFiles = copyAndUploadMinidumpsSync( | 429 File[] uploadedFiles = copyAndUploadMinidumpsSync( |
| 431 fileManager, new File[][] {{minidumpToCopy}}, new int[] {0}); | 430 fileManager, new File[][] {{minidumpToCopy}}, new int[] {0}); |
| 432 | 431 |
| 433 // CrashReceiverService will rename the minidumps to some globally uniqu
e file name | 432 // CrashReceiverService will rename the minidumps to some globally uniqu
e file name |
| 434 // meaning that we have to check the contents of the minidump rather tha
n the file | 433 // meaning that we have to check the contents of the minidump rather tha
n the file |
| 435 // name. | 434 // name. |
| 436 try { | 435 try { |
| 437 assertEquals(expectedFileContent, readEntireFile(uploadedFiles[0])); | 436 assertEquals(expectedFileContent, readEntireFile(uploadedFiles[0])); |
| 438 } catch (IOException e) { | 437 } catch (IOException e) { |
| 439 throw new RuntimeException(e); | 438 throw new RuntimeException(e); |
| 440 } | 439 } |
| 441 File webviewTmpDir = | 440 File webviewTmpDir = CrashReceiverService.getWebViewTmpCrashDir(); |
| 442 CrashReceiverService.getWebViewTmpCrashDir(getInstrumentation().
getTargetContext()); | |
| 443 assertEquals(0, webviewTmpDir.listFiles().length); | 441 assertEquals(0, webviewTmpDir.listFiles().length); |
| 444 } | 442 } |
| 445 | 443 |
| 446 /** | 444 /** |
| 447 * Ensure that when PlatformServiceBridge returns true we do upload minidump
s. | 445 * Ensure that when PlatformServiceBridge returns true we do upload minidump
s. |
| 448 */ | 446 */ |
| 449 @MediumTest | 447 @MediumTest |
| 450 public void testPlatformServicesBridgeIsUsedUserConsent() throws IOException
{ | 448 public void testPlatformServicesBridgeIsUsedUserConsent() throws IOException
{ |
| 451 testPlatformServicesBridgeIsUsed(true); | 449 testPlatformServicesBridgeIsUsed(true); |
| 452 } | 450 } |
| 453 | 451 |
| 454 /** | 452 /** |
| 455 * Ensure that when PlatformServiceBridge returns false we do not upload min
idumps. | 453 * Ensure that when PlatformServiceBridge returns false we do not upload min
idumps. |
| 456 */ | 454 */ |
| 457 @MediumTest | 455 @MediumTest |
| 458 public void testPlatformServicesBridgeIsUsedNoUserConsent() throws IOExcepti
on { | 456 public void testPlatformServicesBridgeIsUsedNoUserConsent() throws IOExcepti
on { |
| 459 testPlatformServicesBridgeIsUsed(false); | 457 testPlatformServicesBridgeIsUsed(false); |
| 460 } | 458 } |
| 461 | 459 |
| 462 /** | 460 /** |
| 463 * MinidumpUploaderDelegate sub-class that uses MinidumpUploaderDelegate's i
mplementation of | 461 * MinidumpUploaderDelegate sub-class that uses MinidumpUploaderDelegate's i
mplementation of |
| 464 * CrashReportingPermissionManager.isUsageAndCrashReportingPermittedByUser()
. | 462 * CrashReportingPermissionManager.isUsageAndCrashReportingPermittedByUser()
. |
| 465 */ | 463 */ |
| 466 private static class WebViewUserConsentMinidumpUploaderDelegate | 464 private static class WebViewUserConsentMinidumpUploaderDelegate |
| 467 extends AwMinidumpUploaderDelegate { | 465 extends AwMinidumpUploaderDelegate { |
| 468 private final boolean mUserConsent; | 466 private final boolean mUserConsent; |
| 469 WebViewUserConsentMinidumpUploaderDelegate(Context context, boolean user
Consent) { | 467 WebViewUserConsentMinidumpUploaderDelegate(boolean userConsent) { |
| 470 super(context); | 468 super(); |
| 471 mUserConsent = userConsent; | 469 mUserConsent = userConsent; |
| 472 } | 470 } |
| 473 @Override | 471 @Override |
| 474 public CrashReportingPermissionManager createCrashReportingPermissionMan
ager() { | 472 public CrashReportingPermissionManager createCrashReportingPermissionMan
ager() { |
| 475 final CrashReportingPermissionManager realPermissionManager = | 473 final CrashReportingPermissionManager realPermissionManager = |
| 476 super.createCrashReportingPermissionManager(); | 474 super.createCrashReportingPermissionManager(); |
| 477 return new MockCrashReportingPermissionManager() { | 475 return new MockCrashReportingPermissionManager() { |
| 478 { | 476 { |
| 479 // This setup ensures we depend on | 477 // This setup ensures we depend on |
| 480 // isUsageAndCrashReportingPermittedByUser(). | 478 // isUsageAndCrashReportingPermittedByUser(). |
| 481 mIsInSample = true; | 479 mIsInSample = true; |
| 482 mIsNetworkAvailable = true; | 480 mIsNetworkAvailable = true; |
| 483 mIsEnabledForTests = false; | 481 mIsEnabledForTests = false; |
| 484 } | 482 } |
| 485 @Override | 483 @Override |
| 486 public boolean isUsageAndCrashReportingPermittedByUser() { | 484 public boolean isUsageAndCrashReportingPermittedByUser() { |
| 487 // Ensure that we use the real implementation of | 485 // Ensure that we use the real implementation of |
| 488 // isUsageAndCrashReportingPermittedByUser. | 486 // isUsageAndCrashReportingPermittedByUser. |
| 489 boolean userPermitted = | 487 boolean userPermitted = |
| 490 realPermissionManager.isUsageAndCrashReportingPermit
tedByUser(); | 488 realPermissionManager.isUsageAndCrashReportingPermit
tedByUser(); |
| 491 assertEquals(mUserConsent, userPermitted); | 489 assertEquals(mUserConsent, userPermitted); |
| 492 return userPermitted; | 490 return userPermitted; |
| 493 } | 491 } |
| 494 }; | 492 }; |
| 495 } | 493 } |
| 496 } | 494 } |
| 497 | 495 |
| 498 private void testPlatformServicesBridgeIsUsed(final boolean userConsent) thr
ows IOException { | 496 private void testPlatformServicesBridgeIsUsed(final boolean userConsent) thr
ows IOException { |
| 499 PlatformServiceBridge.injectInstance(new TestPlatformServiceBridge(userC
onsent)); | 497 PlatformServiceBridge.injectInstance(new TestPlatformServiceBridge(userC
onsent)); |
| 500 MinidumpUploaderDelegate delegate = new WebViewUserConsentMinidumpUpload
erDelegate( | 498 MinidumpUploaderDelegate delegate = |
| 501 getInstrumentation().getTargetContext(), userConsent); | 499 new WebViewUserConsentMinidumpUploaderDelegate(userConsent); |
| 502 MinidumpUploader minidumpUploader = new TestMinidumpUploaderImpl( | 500 MinidumpUploader minidumpUploader = new TestMinidumpUploaderImpl( |
| 503 delegate, delegate.createCrashReportingPermissionManager()); | 501 delegate, delegate.createCrashReportingPermissionManager()); |
| 504 | 502 |
| 505 File firstFile = createMinidumpFileInCrashDir("1_abc.dmp0"); | 503 File firstFile = createMinidumpFileInCrashDir("1_abc.dmp0"); |
| 506 File secondFile = createMinidumpFileInCrashDir("12_abcd.dmp0"); | 504 File secondFile = createMinidumpFileInCrashDir("12_abcd.dmp0"); |
| 507 File expectedFirstFile = new File( | 505 File expectedFirstFile = new File( |
| 508 mCrashDir, firstFile.getName().replace(".dmp", userConsent ? ".u
p" : ".skipped")); | 506 mCrashDir, firstFile.getName().replace(".dmp", userConsent ? ".u
p" : ".skipped")); |
| 509 File expectedSecondFile = new File( | 507 File expectedSecondFile = new File( |
| 510 mCrashDir, secondFile.getName().replace(".dmp", userConsent ? ".
up" : ".skipped")); | 508 mCrashDir, secondFile.getName().replace(".dmp", userConsent ? ".
up" : ".skipped")); |
| 511 | 509 |
| 512 uploadMinidumpsSync(minidumpUploader, false /* expectReschedule */); | 510 uploadMinidumpsSync(minidumpUploader, false /* expectReschedule */); |
| 513 | 511 |
| 514 assertFalse(firstFile.exists()); | 512 assertFalse(firstFile.exists()); |
| 515 assertTrue(expectedFirstFile.exists()); | 513 assertTrue(expectedFirstFile.exists()); |
| 516 assertFalse(secondFile.exists()); | 514 assertFalse(secondFile.exists()); |
| 517 assertTrue(expectedSecondFile.exists()); | 515 assertTrue(expectedSecondFile.exists()); |
| 518 } | 516 } |
| 519 | 517 |
| 520 private static String readEntireFile(File file) throws IOException { | 518 private static String readEntireFile(File file) throws IOException { |
| 521 FileInputStream fileInputStream = new FileInputStream(file); | 519 try (FileInputStream fileInputStream = new FileInputStream(file)) { |
| 522 try { | |
| 523 byte[] data = new byte[(int) file.length()]; | 520 byte[] data = new byte[(int) file.length()]; |
| 524 fileInputStream.read(data); | 521 fileInputStream.read(data); |
| 525 return new String(data); | 522 return new String(data); |
| 526 } finally { | |
| 527 fileInputStream.close(); | |
| 528 } | 523 } |
| 529 } | 524 } |
| 530 | 525 |
| 531 /** | 526 /** |
| 532 * Ensure that the minidump copying doesn't trigger when we pass it invalid
file descriptors. | 527 * Ensure that the minidump copying doesn't trigger when we pass it invalid
file descriptors. |
| 533 */ | 528 */ |
| 534 @MediumTest | 529 @MediumTest |
| 535 public void testCopyingAbortsForInvalidFds() throws FileNotFoundException, I
OException { | 530 public void testCopyingAbortsForInvalidFds() throws IOException { |
| 531 assertFalse(CrashReceiverService.copyMinidumps(0 /* uid */, null)); |
| 536 assertFalse(CrashReceiverService.copyMinidumps( | 532 assertFalse(CrashReceiverService.copyMinidumps( |
| 537 getInstrumentation().getTargetContext(), 0 /* uid */, null)); | |
| 538 assertFalse(CrashReceiverService.copyMinidumps(getInstrumentation().getT
argetContext(), | |
| 539 0 /* uid */, new ParcelFileDescriptor[] {null, null})); | 533 0 /* uid */, new ParcelFileDescriptor[] {null, null})); |
| 540 assertFalse(CrashReceiverService.copyMinidumps( | 534 assertFalse(CrashReceiverService.copyMinidumps(0 /* uid */, new ParcelFi
leDescriptor[0])); |
| 541 getInstrumentation().getTargetContext(), 0 /* uid */, new Parcel
FileDescriptor[0])); | |
| 542 } | 535 } |
| 543 | 536 |
| 544 /** | 537 /** |
| 545 * Ensure deleting temporary files used when copying minidumps works correct
ly. | 538 * Ensure deleting temporary files used when copying minidumps works correct
ly. |
| 546 */ | 539 */ |
| 547 @MediumTest | 540 @MediumTest |
| 548 public void testDeleteFilesInDir() throws IOException { | 541 public void testDeleteFilesInDir() throws IOException { |
| 549 Context context = getInstrumentation().getTargetContext(); | 542 File webviewTmpDir = CrashReceiverService.getWebViewTmpCrashDir(); |
| 550 File webviewTmpDir = CrashReceiverService.getWebViewTmpCrashDir(context)
; | |
| 551 if (!webviewTmpDir.isDirectory()) { | 543 if (!webviewTmpDir.isDirectory()) { |
| 552 assertTrue(webviewTmpDir.mkdir()); | 544 assertTrue(webviewTmpDir.mkdir()); |
| 553 } | 545 } |
| 554 File testFile1 = new File(webviewTmpDir, "testFile1"); | 546 File testFile1 = new File(webviewTmpDir, "testFile1"); |
| 555 File testFile2 = new File(webviewTmpDir, "testFile2"); | 547 File testFile2 = new File(webviewTmpDir, "testFile2"); |
| 556 assertTrue(testFile1.createNewFile()); | 548 assertTrue(testFile1.createNewFile()); |
| 557 assertTrue(testFile2.createNewFile()); | 549 assertTrue(testFile2.createNewFile()); |
| 558 assertTrue(testFile1.exists()); | 550 assertTrue(testFile1.exists()); |
| 559 assertTrue(testFile2.exists()); | 551 assertTrue(testFile2.exists()); |
| 560 CrashReceiverService.deleteFilesInWebViewTmpDirIfExists(context); | 552 CrashReceiverService.deleteFilesInWebViewTmpDirIfExists(); |
| 561 assertFalse(testFile1.exists()); | 553 assertFalse(testFile1.exists()); |
| 562 assertFalse(testFile2.exists()); | 554 assertFalse(testFile2.exists()); |
| 563 } | 555 } |
| 564 | 556 |
| 565 /** | 557 /** |
| 566 * Ensure we can copy and upload several batches of files (i.e. emulate seve
ral copying-calls in | 558 * Ensure we can copy and upload several batches of files (i.e. emulate seve
ral copying-calls in |
| 567 * a row without the copying-service being destroyed in between). | 559 * a row without the copying-service being destroyed in between). |
| 568 */ | 560 */ |
| 569 @MediumTest | 561 @MediumTest |
| 570 public void testCopyAndUploadSeveralMinidumpBatches() throws IOException { | 562 public void testCopyAndUploadSeveralMinidumpBatches() throws IOException { |
| 571 final CrashFileManager fileManager = new CrashFileManager( | 563 final CrashFileManager fileManager = |
| 572 CrashReceiverService.getWebViewCrashDir(getInstrumentation().get
TargetContext())); | 564 new CrashFileManager(CrashReceiverService.getWebViewCrashDir()); |
| 573 // Note that these minidump files are set up directly in the cache dir -
not in the WebView | 565 // Note that these minidump files are set up directly in the cache dir -
not in the WebView |
| 574 // crash dir. This is to ensure the CrashFileManager doesn't see these m
inidumps without us | 566 // crash dir. This is to ensure the CrashFileManager doesn't see these m
inidumps without us |
| 575 // first copying them. | 567 // first copying them. |
| 576 File firstMinidumpToCopy = | 568 File firstMinidumpToCopy = new File(getExistingCacheDir(), "firstToCopy.
dmp"); |
| 577 new File(getInstrumentation().getTargetContext().getCacheDir(),
"firstToCopy.dmp"); | 569 File secondMinidumpToCopy = new File(getExistingCacheDir(), "secondToCop
y.dmp"); |
| 578 File secondMinidumpToCopy = | |
| 579 new File(getInstrumentation().getTargetContext().getCacheDir(),
"secondToCopy.dmp"); | |
| 580 setUpMinidumpFile(firstMinidumpToCopy, BOUNDARY, "browser"); | 570 setUpMinidumpFile(firstMinidumpToCopy, BOUNDARY, "browser"); |
| 581 setUpMinidumpFile(secondMinidumpToCopy, BOUNDARY, "renderer"); | 571 setUpMinidumpFile(secondMinidumpToCopy, BOUNDARY, "renderer"); |
| 582 final String expectedFirstFileContent = readEntireFile(firstMinidumpToCo
py); | 572 final String expectedFirstFileContent = readEntireFile(firstMinidumpToCo
py); |
| 583 final String expectedSecondFileContent = readEntireFile(secondMinidumpTo
Copy); | 573 final String expectedSecondFileContent = readEntireFile(secondMinidumpTo
Copy); |
| 584 | 574 |
| 585 File[] uploadedFiles = copyAndUploadMinidumpsSync(fileManager, | 575 File[] uploadedFiles = copyAndUploadMinidumpsSync(fileManager, |
| 586 new File[][] {{firstMinidumpToCopy}, {secondMinidumpToCopy}}, ne
w int[] {0, 0}); | 576 new File[][] {{firstMinidumpToCopy}, {secondMinidumpToCopy}}, ne
w int[] {0, 0}); |
| 587 | 577 |
| 588 // CrashReceiverService will rename the minidumps to some globally uniqu
e file name | 578 // CrashReceiverService will rename the minidumps to some globally uniqu
e file name |
| 589 // meaning that we have to check the contents of the minidump rather tha
n the file | 579 // meaning that we have to check the contents of the minidump rather tha
n the file |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 626 for (int n = 0; n < minidumps.length; n++) { | 616 for (int n = 0; n < minidumps.length; n++) { |
| 627 File[] currentMinidumps = minidumps[n]; | 617 File[] currentMinidumps = minidumps[n]; |
| 628 numMinidumps += currentMinidumps.length; | 618 numMinidumps += currentMinidumps.length; |
| 629 fileDescriptors[n] = new ParcelFileDescriptor[currentMinidumps.lengt
h]; | 619 fileDescriptors[n] = new ParcelFileDescriptor[currentMinidumps.lengt
h]; |
| 630 for (int m = 0; m < currentMinidumps.length; m++) { | 620 for (int m = 0; m < currentMinidumps.length; m++) { |
| 631 fileDescriptors[n][m] = ParcelFileDescriptor.open( | 621 fileDescriptors[n][m] = ParcelFileDescriptor.open( |
| 632 currentMinidumps[m], ParcelFileDescriptor.MODE_READ_ONLY
); | 622 currentMinidumps[m], ParcelFileDescriptor.MODE_READ_ONLY
); |
| 633 assertTrue(currentMinidumps[m].delete()); | 623 assertTrue(currentMinidumps[m].delete()); |
| 634 } | 624 } |
| 635 crashReceiverService.performMinidumpCopyingSerially( | 625 crashReceiverService.performMinidumpCopyingSerially( |
| 636 getInstrumentation().getTargetContext(), uids[n] /* uid */,
fileDescriptors[n], | 626 uids[n] /* uid */, fileDescriptors[n], false /* scheduleUplo
ads */); |
| 637 false /* scheduleUploads */); | |
| 638 } | 627 } |
| 639 | 628 |
| 640 PlatformServiceBridge.injectInstance(new TestPlatformServiceBridge(true)
); | 629 PlatformServiceBridge.injectInstance(new TestPlatformServiceBridge(true)
); |
| 641 final CrashReportingPermissionManager permManager = | 630 final CrashReportingPermissionManager permManager = |
| 642 new MockCrashReportingPermissionManager() { | 631 new MockCrashReportingPermissionManager() { |
| 643 { mIsEnabledForTests = true; } | 632 { mIsEnabledForTests = true; } |
| 644 }; | 633 }; |
| 645 MinidumpUploader minidumpUploader = new TestMinidumpUploaderImpl(permMan
ager); | 634 MinidumpUploader minidumpUploader = new TestMinidumpUploaderImpl(permMan
ager); |
| 646 | 635 |
| 647 uploadMinidumpsSync(minidumpUploader, false /* expectReschedule */); | 636 uploadMinidumpsSync(minidumpUploader, false /* expectReschedule */); |
| 648 // Ensure there are no minidumps left to upload. | 637 // Ensure there are no minidumps left to upload. |
| 649 File[] nonUploadedMinidumps = | 638 File[] nonUploadedMinidumps = |
| 650 fileManager.getAllMinidumpFiles(MinidumpUploaderImpl.MAX_UPLOAD_
TRIES_ALLOWED); | 639 fileManager.getAllMinidumpFiles(MinidumpUploaderImpl.MAX_UPLOAD_
TRIES_ALLOWED); |
| 651 assertEquals(0, nonUploadedMinidumps.length); | 640 assertEquals(0, nonUploadedMinidumps.length); |
| 652 | 641 |
| 653 File[] uploadedFiles = fileManager.getAllUploadedFiles(); | 642 File[] uploadedFiles = fileManager.getAllUploadedFiles(); |
| 654 assertEquals(numMinidumps, uploadedFiles.length); | 643 assertEquals(numMinidumps, uploadedFiles.length); |
| 655 return uploadedFiles; | 644 return uploadedFiles; |
| 656 } | 645 } |
| 657 | 646 |
| 658 private File createMinidumpFileInCrashDir(String name) throws IOException { | 647 private File createMinidumpFileInCrashDir(String name) throws IOException { |
| 659 File minidumpFile = new File(mCrashDir, name); | 648 File minidumpFile = new File(mCrashDir, name); |
| 660 setUpMinidumpFile(minidumpFile, BOUNDARY); | 649 setUpMinidumpFile(minidumpFile, BOUNDARY); |
| 661 return minidumpFile; | 650 return minidumpFile; |
| 662 } | 651 } |
| 663 } | 652 } |
| OLD | NEW |