|
| 1 | +/* |
| 2 | + * Nextcloud Android SingleSignOn Library |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 6 | + */ |
| 7 | +package com.nextcloud.android.sso.api |
| 8 | + |
| 9 | +import org.junit.Assert.assertEquals |
| 10 | +import org.junit.Assert.assertFalse |
| 11 | +import org.junit.Assert.assertNotNull |
| 12 | +import org.junit.Assert.assertNull |
| 13 | +import org.junit.Assert.assertTrue |
| 14 | +import org.junit.Test |
| 15 | +import java.io.ByteArrayInputStream |
| 16 | +import java.io.ByteArrayOutputStream |
| 17 | +import java.io.ObjectOutputStream |
| 18 | +import java.nio.charset.StandardCharsets |
| 19 | + |
| 20 | +class AidlNetworkRequestDeserializationTest { |
| 21 | + |
| 22 | + /** |
| 23 | + * Exception class only used to be renamed in the serialized stream, simulating an exception |
| 24 | + * type that exists in the Files app but not in the client app (e.g. |
| 25 | + * `com.owncloud.android.lib.common.network.CertificateCombinedException`). |
| 26 | + */ |
| 27 | + class OnlyInFilesAppException(message: String) : Exception(message) |
| 28 | + |
| 29 | + @Test |
| 30 | + fun deserializeObjectV2ExceptionClassMissingInClientAppReturnsReadableException() { |
| 31 | + val data = serialize(OnlyInFilesAppException("certificate expired"), "[]") |
| 32 | + |
| 33 | + // Rename the class inside the serialized stream to one that does not exist on this |
| 34 | + // side of the IPC channel (same length, so the stream structure stays intact) |
| 35 | + val tampered = replaceBytes(data, "OnlyInFilesAppException", "OnlyInF1lesAppException") |
| 36 | + |
| 37 | + val response = AidlNetworkRequest.deserializeObjectV2(ByteArrayInputStream(tampered)) |
| 38 | + |
| 39 | + val exception = response.exception() |
| 40 | + assertNotNull(exception) |
| 41 | + assertFalse(exception is ClassNotFoundException) |
| 42 | + assertNotNull(exception!!.message) |
| 43 | + assertTrue( |
| 44 | + "message should contain the original exception type", |
| 45 | + exception.message!!.contains("OnlyInF1lesAppException") |
| 46 | + ) |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun deserializeObjectV2NoExceptionParsesHeaders() { |
| 51 | + val data = serialize(null, "[{\"name\":\"Content-Type\",\"value\":\"application/json\"}]") |
| 52 | + |
| 53 | + val response = AidlNetworkRequest.deserializeObjectV2(ByteArrayInputStream(data)) |
| 54 | + |
| 55 | + assertNull(response.exception()) |
| 56 | + assertEquals(1, response.headers().size) |
| 57 | + assertEquals("Content-Type", response.headers()[0].name) |
| 58 | + assertEquals("application/json", response.headers()[0].value) |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun deserializeObjectV2KnownExceptionReturnsItUnchanged() { |
| 63 | + val data = serialize(IllegalStateException("CE_1"), "[]") |
| 64 | + |
| 65 | + val response = AidlNetworkRequest.deserializeObjectV2(ByteArrayInputStream(data)) |
| 66 | + |
| 67 | + assertTrue(response.exception() is IllegalStateException) |
| 68 | + assertEquals("CE_1", response.exception()?.message) |
| 69 | + } |
| 70 | + |
| 71 | + /** Mirrors `InputStreamBinder#serializeObjectToInputStreamV2` in the Files app. */ |
| 72 | + private fun serialize(exception: Exception?, headers: String): ByteArray { |
| 73 | + val baos = ByteArrayOutputStream() |
| 74 | + ObjectOutputStream(baos).use { oos -> |
| 75 | + oos.writeObject(exception) |
| 76 | + oos.writeObject(headers) |
| 77 | + } |
| 78 | + return baos.toByteArray() |
| 79 | + } |
| 80 | + |
| 81 | + private fun replaceBytes(data: ByteArray, search: String, replace: String): ByteArray { |
| 82 | + val searchBytes = search.toByteArray(StandardCharsets.US_ASCII) |
| 83 | + val replaceBytes = replace.toByteArray(StandardCharsets.US_ASCII) |
| 84 | + assertEquals(searchBytes.size, replaceBytes.size) |
| 85 | + |
| 86 | + val result = data.clone() |
| 87 | + var i = 0 |
| 88 | + while (i <= result.size - searchBytes.size) { |
| 89 | + var match = true |
| 90 | + for (j in searchBytes.indices) { |
| 91 | + if (result[i + j] != searchBytes[j]) { |
| 92 | + match = false |
| 93 | + break |
| 94 | + } |
| 95 | + } |
| 96 | + if (match) { |
| 97 | + System.arraycopy(replaceBytes, 0, result, i, replaceBytes.size) |
| 98 | + } |
| 99 | + i++ |
| 100 | + } |
| 101 | + return result |
| 102 | + } |
| 103 | +} |
0 commit comments