|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.firebase.crashlytics.internal.common; |
| 16 | + |
| 17 | +import static org.junit.Assert.assertArrayEquals; |
| 18 | + |
| 19 | +import android.content.Context; |
| 20 | +import com.google.firebase.crashlytics.internal.CrashlyticsTestCase; |
| 21 | +import com.google.firebase.crashlytics.internal.model.CrashlyticsReport; |
| 22 | +import java.io.ByteArrayOutputStream; |
| 23 | +import java.io.File; |
| 24 | +import java.io.FileOutputStream; |
| 25 | +import java.io.IOException; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +public class FileBackedNativeSessionFileTest extends CrashlyticsTestCase { |
| 29 | + byte[] testContents = {0, 2, 20, 10}; |
| 30 | + byte[] emptyContents = {}; |
| 31 | + File testFile; |
| 32 | + File emptyFile; |
| 33 | + File missingFile; |
| 34 | + |
| 35 | + @Override |
| 36 | + protected void setUp() throws Exception { |
| 37 | + super.setUp(); |
| 38 | + final Context context = getContext(); |
| 39 | + testFile = new File(context.getFilesDir(), "testFile"); |
| 40 | + try (FileOutputStream fout = new FileOutputStream(testFile); |
| 41 | + ByteArrayOutputStream stream = new ByteArrayOutputStream()) { |
| 42 | + stream.write(testContents); |
| 43 | + stream.writeTo(fout); |
| 44 | + } |
| 45 | + emptyFile = new File(context.getFilesDir(), "emptyFile"); |
| 46 | + emptyFile.createNewFile(); |
| 47 | + missingFile = new File(context.getFilesDir(), "missingFile"); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + protected void tearDown() throws Exception { |
| 52 | + super.tearDown(); |
| 53 | + testFile.delete(); |
| 54 | + emptyFile.delete(); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testAsStream_convertsToStream() throws IOException { |
| 59 | + FileBackedNativeSessionFile nativeSessionFile = |
| 60 | + new FileBackedNativeSessionFile("file_name", "file", testFile); |
| 61 | + byte[] readBytes = new byte[4]; |
| 62 | + nativeSessionFile.getStream().read(readBytes); |
| 63 | + assertArrayEquals(testContents, readBytes); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testAsStreamWhenEmpty_returnsEmpty() throws IOException { |
| 68 | + FileBackedNativeSessionFile nativeSessionFile = |
| 69 | + new FileBackedNativeSessionFile("file_name", "file", emptyFile); |
| 70 | + byte[] readBytes = new byte[0]; |
| 71 | + nativeSessionFile.getStream().read(readBytes); |
| 72 | + assertArrayEquals(emptyContents, readBytes); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testAsStreamWhenMissing_returnsNull() { |
| 77 | + FileBackedNativeSessionFile nativeSessionFile = |
| 78 | + new FileBackedNativeSessionFile("file_name", "file", missingFile); |
| 79 | + assertNull(nativeSessionFile.getStream()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testAsFilePayload_convertsToFilePayload() { |
| 84 | + FileBackedNativeSessionFile nativeSessionFile = |
| 85 | + new FileBackedNativeSessionFile("file_name", "file", testFile); |
| 86 | + CrashlyticsReport.FilesPayload.File filesPayload = nativeSessionFile.asFilePayload(); |
| 87 | + assertNotNull(filesPayload); |
| 88 | + assertArrayEquals(testContents, filesPayload.getContents()); |
| 89 | + assertEquals("file_name", filesPayload.getFilename()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testAsFilePayloadWhenEmpty_returnsEmptyPayload() { |
| 94 | + FileBackedNativeSessionFile nativeSessionFile = |
| 95 | + new FileBackedNativeSessionFile("file_name", "file", emptyFile); |
| 96 | + CrashlyticsReport.FilesPayload.File filesPayload = nativeSessionFile.asFilePayload(); |
| 97 | + assertNotNull(filesPayload); |
| 98 | + assertArrayEquals(emptyContents, filesPayload.getContents()); |
| 99 | + assertEquals("file_name", filesPayload.getFilename()); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testAsFilePayloadWhenMissing_convertsToNull() { |
| 104 | + FileBackedNativeSessionFile nativeSessionFile = |
| 105 | + new FileBackedNativeSessionFile("file_name", "file", missingFile); |
| 106 | + assertNull(nativeSessionFile.asFilePayload()); |
| 107 | + } |
| 108 | +} |
0 commit comments