Search in sources :

Example 16 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method minidumpFilePathNull.

@Test
public void minidumpFilePathNull() throws Exception {
    /* Set up mock for the crash. */
    final com.microsoft.appcenter.crashes.ingestion.models.Exception exception = mock(com.microsoft.appcenter.crashes.ingestion.models.Exception.class);
    DefaultLogSerializer defaultLogSerializer = mock(DefaultLogSerializer.class);
    mock(ErrorAttachmentLog.class);
    mockStatic(ErrorLogHelper.class);
    mockStatic(ErrorAttachmentLog.class);
    ErrorReport errorReport = new ErrorReport();
    Device device = new Device();
    device.setWrapperSdkName(WRAPPER_SDK_NAME_NDK);
    errorReport.setDevice(device);
    when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), anyString())).thenReturn(errorReport);
    whenNew(DefaultLogSerializer.class).withAnyArguments().thenReturn(defaultLogSerializer);
    whenNew(com.microsoft.appcenter.crashes.ingestion.models.Exception.class).withAnyArguments().thenReturn(exception);
    when(exception.getMinidumpFilePath()).thenReturn(null);
    when(exception.getType()).thenReturn(MINIDUMP_FILE);
    when(exception.getMessage()).thenReturn("Error message");
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class), mock(File.class) });
    when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]);
    when(FileManager.read(any(File.class))).thenReturn("");
    String jsonCrash = "{}";
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString(), anyString())).thenAnswer(new Answer<ManagedErrorLog>() {

        @Override
        public ManagedErrorLog answer(InvocationOnMock invocation) {
            ManagedErrorLog log = mock(ManagedErrorLog.class);
            when(log.getId()).thenReturn(UUID.randomUUID());
            when(log.getException()).thenReturn(exception);
            return log;
        }
    });
    when(logSerializer.serializeLog(any(Log.class))).thenReturn(jsonCrash);
    when(SharedPreferencesManager.getBoolean(CRASHES_ENABLED_KEY, true)).thenReturn(true);
    ErrorAttachmentLog errorAttachmentLog = mock(ErrorAttachmentLog.class);
    whenNew(ErrorAttachmentLog.class).withAnyArguments().thenReturn(errorAttachmentLog);
    /* Start crashes. */
    Crashes crashes = Crashes.getInstance();
    crashes.setLogSerializer(logSerializer);
    crashes.onStarting(mAppCenterHandler);
    crashes.onStarted(mock(Context.class), mock(Channel.class), "secret-app-mock", null, true);
    /*
         * Verify that attachmentWithBinary doesn't get called if minidump is missing.
         * This scenario used to crash before, so if the test succeeds that also tests the crash is fixed.
         */
    verifyStatic(never());
    attachmentWithBinary(new byte[] { anyByte() }, anyString(), anyString());
}
Also used : SessionContext(com.microsoft.appcenter.utils.context.SessionContext) Context(android.content.Context) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) HandledErrorLog(com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) Device(com.microsoft.appcenter.ingestion.models.Device) Channel(com.microsoft.appcenter.channel.Channel) Matchers.anyString(org.mockito.Matchers.anyString) Log.getStackTraceString(android.util.Log.getStackTraceString) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) File(java.io.File) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 17 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method queuePendingCrashesShouldProcess.

@Test
public void queuePendingCrashesShouldProcess() throws JSONException {
    /* Setup mock. */
    Context mockContext = mock(Context.class);
    Channel mockChannel = mock(Channel.class);
    ErrorReport report = new ErrorReport();
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
    when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]);
    when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), anyString())).thenReturn(report);
    when(FileManager.read(any(File.class))).thenReturn("");
    CrashesListener mockListener = mock(CrashesListener.class);
    when(mockListener.shouldProcess(report)).thenReturn(true);
    when(mockListener.shouldAwaitUserConfirmation()).thenReturn(false);
    ErrorAttachmentLog mockAttachment = mock(ErrorAttachmentLog.class);
    when(mockAttachment.getId()).thenReturn(UUID.randomUUID());
    when(mockAttachment.getErrorId()).thenReturn(UUID.randomUUID());
    when(mockAttachment.getContentType()).thenReturn("");
    when(mockAttachment.getFileName()).thenReturn("");
    when(mockAttachment.getData()).thenReturn(new byte[0]);
    when(mockAttachment.isValid()).thenReturn(true);
    ErrorAttachmentLog mockEmptyAttachment = mock(ErrorAttachmentLog.class);
    final int skipAttachmentLogsCount = 2;
    List<ErrorAttachmentLog> errorAttachmentLogList = Arrays.asList(mockAttachment, mockAttachment, mockEmptyAttachment, null);
    when(mockListener.getErrorAttachments(report)).thenReturn(errorAttachmentLogList);
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString(), anyString())).thenReturn(mErrorLog);
    Crashes crashes = Crashes.getInstance();
    crashes.setLogSerializer(logSerializer);
    crashes.setInstanceListener(mockListener);
    crashes.onStarting(mAppCenterHandler);
    crashes.onStarted(mockContext, mockChannel, "", null, true);
    /* Test. */
    verify(mockListener).shouldProcess(report);
    verify(mockListener).shouldAwaitUserConfirmation();
    verify(mockListener).getErrorAttachments(report);
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object log) {
            return log.equals(mErrorLog);
        }
    }), eq(crashes.getGroupName()), eq(CRITICAL));
    verify(mockChannel, times(errorAttachmentLogList.size() - skipAttachmentLogsCount)).enqueue(mockAttachment, crashes.getGroupName(), DEFAULTS);
}
Also used : SessionContext(com.microsoft.appcenter.utils.context.SessionContext) Context(android.content.Context) Channel(com.microsoft.appcenter.channel.Channel) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) ArgumentMatcher(org.mockito.ArgumentMatcher) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) File(java.io.File) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 18 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method discardHugeErrorAttachments.

@Test
public void discardHugeErrorAttachments() throws JSONException {
    /* Prepare a big (too big) attachment and a small one. */
    ArrayList<ErrorAttachmentLog> errorAttachmentLogs = new ArrayList<>(2);
    ErrorAttachmentLog binaryAttachment = attachmentWithBinary(new byte[7 * 1024 * 1024 + 1], "earth.png", "image/png");
    errorAttachmentLogs.add(binaryAttachment);
    ErrorAttachmentLog textAttachment = ErrorAttachmentLog.attachmentWithText("hello", "log.txt");
    errorAttachmentLogs.add(textAttachment);
    /* Set up callbacks. */
    CrashesListener listener = mock(CrashesListener.class);
    when(listener.shouldProcess(any(ErrorReport.class))).thenReturn(true);
    when(listener.getErrorAttachments(any(ErrorReport.class))).thenReturn(errorAttachmentLogs);
    /* Mock a crash log to process. */
    com.microsoft.appcenter.crashes.ingestion.models.Exception mockException = new com.microsoft.appcenter.crashes.ingestion.models.Exception();
    mockException.setType("type");
    mockException.setMessage("message");
    ManagedErrorLog log = mock(ManagedErrorLog.class);
    when(log.getId()).thenReturn(UUID.randomUUID());
    when(log.getException()).thenReturn(mockException);
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString(), anyString())).thenReturn(log);
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
    when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]);
    when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), anyString())).thenReturn(new ErrorReport());
    when(FileManager.read(any(File.class))).thenReturn("");
    /* Mock starting crashes so that attachments are processed. */
    Crashes crashes = Crashes.getInstance();
    crashes.setInstanceListener(listener);
    crashes.setLogSerializer(logSerializer);
    crashes.onStarting(mAppCenterHandler);
    Channel channel = mock(Channel.class);
    crashes.onStarted(mock(Context.class), channel, "", null, true);
    /* Check we send only the text attachment as the binary is too big. */
    verify(channel).enqueue(textAttachment, crashes.getGroupName(), NORMAL);
    verify(channel, never()).enqueue(eq(binaryAttachment), anyString(), anyInt());
}
Also used : SessionContext(com.microsoft.appcenter.utils.context.SessionContext) Context(android.content.Context) Channel(com.microsoft.appcenter.channel.Channel) ArrayList(java.util.ArrayList) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) JSONException(org.json.JSONException) IOException(java.io.IOException) TestCrashException(com.microsoft.appcenter.crashes.model.TestCrashException) ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) File(java.io.File) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 19 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method queuePendingCrashesShouldNotProcess.

@Test
public void queuePendingCrashesShouldNotProcess() throws JSONException {
    Context mockContext = mock(Context.class);
    Channel mockChannel = mock(Channel.class);
    ErrorReport report = new ErrorReport();
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
    when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]);
    when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), anyString())).thenReturn(report);
    when(FileManager.read(any(File.class))).thenReturn("");
    CrashesListener mockListener = mock(CrashesListener.class);
    when(mockListener.shouldProcess(report)).thenReturn(false);
    Crashes crashes = Crashes.getInstance();
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString(), anyString())).thenReturn(mErrorLog);
    crashes.setLogSerializer(logSerializer);
    crashes.setInstanceListener(mockListener);
    crashes.onStarting(mAppCenterHandler);
    crashes.onStarted(mockContext, mockChannel, "", null, true);
    verify(mockListener).shouldProcess(report);
    verify(mockListener, never()).shouldAwaitUserConfirmation();
    verify(mockListener, never()).getErrorAttachments(report);
    verify(mockChannel, never()).enqueue(any(Log.class), eq(crashes.getGroupName()), anyInt());
}
Also used : SessionContext(com.microsoft.appcenter.utils.context.SessionContext) Context(android.content.Context) ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) HandledErrorLog(com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) Channel(com.microsoft.appcenter.channel.Channel) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) File(java.io.File) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 20 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method minidumpStoredWithOldSDK.

@Test
public void minidumpStoredWithOldSDK() throws Exception {
    /* Set up mock for the crash. */
    final com.microsoft.appcenter.crashes.ingestion.models.Exception exception = mock(com.microsoft.appcenter.crashes.ingestion.models.Exception.class);
    DefaultLogSerializer defaultLogSerializer = mock(DefaultLogSerializer.class);
    mock(ErrorAttachmentLog.class);
    mockStatic(ErrorLogHelper.class);
    mockStatic(ErrorAttachmentLog.class);
    ErrorReport errorReport = new ErrorReport();
    Device device = new Device();
    device.setWrapperSdkName(WRAPPER_SDK_NAME_NDK);
    errorReport.setDevice(device);
    when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), anyString())).thenReturn(errorReport);
    whenNew(DefaultLogSerializer.class).withAnyArguments().thenReturn(defaultLogSerializer);
    whenNew(com.microsoft.appcenter.crashes.ingestion.models.Exception.class).withAnyArguments().thenReturn(exception);
    when(exception.getStackTrace()).thenReturn("some minidump");
    when(exception.getType()).thenReturn(MINIDUMP_FILE);
    when(exception.getMessage()).thenReturn("message");
    /* This mocks we already processed minidump to convert to pending regular crash report as that would be the case if migrating data from older SDK. */
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
    when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]);
    when(FileManager.read(any(File.class))).thenReturn("");
    String jsonCrash = "{}";
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString(), anyString())).thenAnswer(new Answer<ManagedErrorLog>() {

        @Override
        public ManagedErrorLog answer(InvocationOnMock invocation) {
            ManagedErrorLog log = mock(ManagedErrorLog.class);
            when(log.getId()).thenReturn(UUID.randomUUID());
            when(log.getException()).thenReturn(exception);
            return log;
        }
    });
    when(logSerializer.serializeLog(any(Log.class))).thenReturn(jsonCrash);
    when(SharedPreferencesManager.getBoolean(CRASHES_ENABLED_KEY, true)).thenReturn(true);
    ErrorAttachmentLog errorAttachmentLog = mock(ErrorAttachmentLog.class);
    whenNew(ErrorAttachmentLog.class).withAnyArguments().thenReturn(errorAttachmentLog);
    /* Start crashes. */
    Crashes crashes = Crashes.getInstance();
    crashes.setLogSerializer(logSerializer);
    crashes.onStarting(mAppCenterHandler);
    crashes.onStarted(mock(Context.class), mock(Channel.class), "secret-app-mock", null, true);
    /* Verify that attachmentWithBinary does get sent. */
    verifyStatic();
    attachmentWithBinary(new byte[] { anyByte() }, anyString(), anyString());
    /* Verify temporary field erased. */
    verify(exception, times(1)).setStackTrace(null);
}
Also used : SessionContext(com.microsoft.appcenter.utils.context.SessionContext) Context(android.content.Context) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) HandledErrorLog(com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) Device(com.microsoft.appcenter.ingestion.models.Device) Channel(com.microsoft.appcenter.channel.Channel) Matchers.anyString(org.mockito.Matchers.anyString) Log.getStackTraceString(android.util.Log.getStackTraceString) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) File(java.io.File) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

ErrorReport (com.microsoft.appcenter.crashes.model.ErrorReport)54 Test (org.junit.Test)40 File (java.io.File)36 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)31 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)31 Context (android.content.Context)26 Channel (com.microsoft.appcenter.channel.Channel)25 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)22 UUID (java.util.UUID)21 ErrorAttachmentLog (com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog)18 Device (com.microsoft.appcenter.ingestion.models.Device)12 DefaultLogSerializer (com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer)12 SessionContext (com.microsoft.appcenter.utils.context.SessionContext)12 SessionContext (com.microsoft.appcenter.SessionContext)11 Log (com.microsoft.appcenter.ingestion.models.Log)11 HandledErrorLog (com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog)10 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)10 JSONException (org.json.JSONException)10 InvocationOnMock (org.mockito.invocation.InvocationOnMock)10 TestCrashException (com.microsoft.appcenter.crashes.model.TestCrashException)9