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