1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17package android.app; 18 19import org.junit.Test; 20import org.junit.runner.RunWith; 21 22import android.app.ApplicationErrorReport.CrashInfo; 23import android.support.test.filters.SmallTest; 24import android.support.test.runner.AndroidJUnit4; 25 26import static org.junit.Assert.assertEquals; 27import static org.junit.Assert.assertTrue; 28 29@RunWith(AndroidJUnit4.class) 30@SmallTest 31public class ApplicationErrorReportTest { 32 33 @Test 34 public void testHugeStacktraceLeadsToReasonableReport() { 35 Throwable deepStackTrace = deepStackTrace(); 36 CrashInfo crashInfo = new CrashInfo(deepStackTrace); 37 38 assertTrue("stack trace is longer than 50'000 characters", 39 crashInfo.stackTrace.length() < 50000); 40 } 41 42 @Test 43 public void testHugeExceptionMessageLeadsToReasonableReport() { 44 StringBuilder msg = new StringBuilder(); 45 for (int i = 0; i < 1000000; i++) { 46 msg.append('x'); 47 } 48 49 CrashInfo crashInfo = new CrashInfo(new Throwable(msg.toString())); 50 51 assertTrue("message is longer than 50'000 characters", 52 crashInfo.exceptionMessage.length() < 50000); 53 } 54 55 @Test 56 public void testTruncationKeepsStartAndEndIntact() { 57 StringBuilder msg = new StringBuilder("start"); 58 for (int i = 0; i < 1000000; i++) { 59 msg.append('x'); 60 } 61 msg.append("end"); 62 63 CrashInfo crashInfo = new CrashInfo(new Throwable(msg.toString())); 64 65 String exceptionMessage = crashInfo.exceptionMessage; 66 assertEquals("start", exceptionMessage.substring(0, "start".length())); 67 assertEquals("end", exceptionMessage.substring(exceptionMessage.length() - "end".length())); 68 } 69 70 /** 71 * @return a Throwable with a very long stack trace. 72 */ 73 private Throwable deepStackTrace() { 74 return stackTraceGenerator__aaaaaaaaa_aaaaaaaaa_aaaaaaaaa_aaaaaaaaa_aaaaaaaaa(1000); 75 } 76 77 private Throwable stackTraceGenerator__aaaaaaaaa_aaaaaaaaa_aaaaaaaaa_aaaaaaaaa_aaaaaaaaa( 78 int d) { 79 if (d > 0) { 80 return stackTraceGenerator__aaaaaaaaa_aaaaaaaaa_aaaaaaaaa_aaaaaaaaa_aaaaaaaaa(d - 1); 81 } else { 82 return new Throwable("here"); 83 } 84 } 85} 86