2020-07-01 10:58:49 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Proton Technologies AG
|
|
|
|
* This file is part of Proton Technologies AG and ProtonCore.
|
|
|
|
*
|
|
|
|
* ProtonCore is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* ProtonCore is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with ProtonCore. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package me.proton.core.network.data
|
|
|
|
|
|
|
|
import io.mockk.MockKAnnotations
|
2020-10-28 18:32:21 +00:00
|
|
|
import io.mockk.coEvery
|
2020-07-01 10:58:49 +00:00
|
|
|
import io.mockk.every
|
|
|
|
import io.mockk.impl.annotations.MockK
|
2020-07-01 12:49:06 +00:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
2020-07-01 10:58:49 +00:00
|
|
|
import kotlinx.coroutines.runBlocking
|
2020-07-01 12:49:06 +00:00
|
|
|
import kotlinx.coroutines.test.TestCoroutineDispatcher
|
2020-07-01 10:58:49 +00:00
|
|
|
import me.proton.core.network.data.di.ApiFactory
|
|
|
|
import me.proton.core.network.data.util.MockApiClient
|
2020-07-07 11:47:26 +00:00
|
|
|
import me.proton.core.network.data.util.MockLogger
|
2020-07-03 11:24:18 +00:00
|
|
|
import me.proton.core.network.data.util.MockNetworkPrefs
|
2020-09-25 09:28:47 +00:00
|
|
|
import me.proton.core.network.data.util.MockSession
|
|
|
|
import me.proton.core.network.data.util.MockSessionListener
|
2020-07-01 10:58:49 +00:00
|
|
|
import me.proton.core.network.data.util.TestRetrofitApi
|
2020-07-15 17:10:52 +00:00
|
|
|
import me.proton.core.network.data.util.TestTLSHelper
|
2020-07-01 10:58:49 +00:00
|
|
|
import me.proton.core.network.data.util.prepareResponse
|
|
|
|
import me.proton.core.network.domain.ApiManager
|
|
|
|
import me.proton.core.network.domain.ApiResult
|
|
|
|
import me.proton.core.network.domain.NetworkManager
|
2020-07-03 11:24:18 +00:00
|
|
|
import me.proton.core.network.domain.NetworkPrefs
|
2020-09-25 09:28:47 +00:00
|
|
|
import me.proton.core.network.domain.session.Session
|
|
|
|
import me.proton.core.network.domain.session.SessionListener
|
|
|
|
import me.proton.core.network.domain.session.SessionProvider
|
2020-07-15 17:10:52 +00:00
|
|
|
import okhttp3.OkHttpClient
|
2020-07-01 10:58:49 +00:00
|
|
|
import okhttp3.mockwebserver.MockResponse
|
|
|
|
import okhttp3.mockwebserver.MockWebServer
|
2020-07-15 17:10:52 +00:00
|
|
|
import org.junit.runner.RunWith
|
|
|
|
import org.robolectric.RobolectricTestRunner
|
2020-07-01 10:58:49 +00:00
|
|
|
import retrofit2.converter.scalars.ScalarsConverterFactory
|
|
|
|
import java.net.HttpURLConnection
|
2020-09-25 09:28:47 +00:00
|
|
|
import kotlin.test.AfterTest
|
|
|
|
import kotlin.test.BeforeTest
|
|
|
|
import kotlin.test.Test
|
|
|
|
import kotlin.test.assertEquals
|
|
|
|
import kotlin.test.assertTrue
|
2020-07-01 10:58:49 +00:00
|
|
|
|
|
|
|
// Can't use runBlockingTest with MockWebServer. See:
|
|
|
|
// https://github.com/square/retrofit/issues/3330
|
|
|
|
// https://github.com/Kotlin/kotlinx.coroutines/issues/1204
|
2020-07-15 17:10:52 +00:00
|
|
|
@RunWith(RobolectricTestRunner::class)
|
2020-07-01 10:58:49 +00:00
|
|
|
internal class ProtonApiBackendTests {
|
|
|
|
|
2020-07-15 17:10:52 +00:00
|
|
|
val scope = CoroutineScope(TestCoroutineDispatcher())
|
|
|
|
|
|
|
|
private val testTlsHelper = TestTLSHelper()
|
2020-09-25 09:28:47 +00:00
|
|
|
private lateinit var apiFactory: ApiFactory
|
|
|
|
private lateinit var webServer: MockWebServer
|
2020-07-15 17:10:52 +00:00
|
|
|
|
2020-09-25 09:28:47 +00:00
|
|
|
private lateinit var backend: ProtonApiBackend<TestRetrofitApi>
|
|
|
|
|
|
|
|
private lateinit var session: Session
|
|
|
|
|
|
|
|
@MockK
|
|
|
|
private lateinit var sessionProvider: SessionProvider
|
|
|
|
private var sessionListener: SessionListener = MockSessionListener(
|
|
|
|
onTokenRefreshed = { session -> this.session = session }
|
|
|
|
)
|
|
|
|
|
|
|
|
private lateinit var logger: MockLogger
|
|
|
|
private lateinit var client: MockApiClient
|
2020-07-01 10:58:49 +00:00
|
|
|
|
|
|
|
private var isNetworkAvailable = true
|
|
|
|
|
|
|
|
@MockK
|
|
|
|
lateinit var networkManager: NetworkManager
|
|
|
|
|
2020-07-03 11:24:18 +00:00
|
|
|
private lateinit var prefs: NetworkPrefs
|
|
|
|
|
2020-07-01 10:58:49 +00:00
|
|
|
@BeforeTest
|
|
|
|
fun before() {
|
|
|
|
MockKAnnotations.init(this)
|
2020-07-15 17:10:52 +00:00
|
|
|
logger = MockLogger()
|
|
|
|
client = MockApiClient()
|
2020-07-03 11:24:18 +00:00
|
|
|
prefs = MockNetworkPrefs()
|
2020-07-01 10:58:49 +00:00
|
|
|
|
2020-09-25 09:28:47 +00:00
|
|
|
session = MockSession.getDefault()
|
2020-10-28 18:32:21 +00:00
|
|
|
coEvery { sessionProvider.getSessionId(any()) } returns session.sessionId
|
|
|
|
coEvery { sessionProvider.getSession(any()) } returns session
|
2020-07-01 10:58:49 +00:00
|
|
|
|
2020-09-25 09:28:47 +00:00
|
|
|
apiFactory = ApiFactory(
|
|
|
|
"https://example.com/",
|
|
|
|
client,
|
|
|
|
logger,
|
|
|
|
networkManager,
|
|
|
|
prefs,
|
|
|
|
sessionProvider,
|
|
|
|
sessionListener,
|
|
|
|
scope
|
|
|
|
)
|
|
|
|
|
|
|
|
every { networkManager.isConnectedToNetwork() } returns isNetworkAvailable
|
2020-07-01 10:58:49 +00:00
|
|
|
isNetworkAvailable = true
|
2020-09-25 09:28:47 +00:00
|
|
|
|
2020-07-15 17:10:52 +00:00
|
|
|
webServer = testTlsHelper.createMockServer()
|
|
|
|
|
|
|
|
backend = createBackend {
|
|
|
|
testTlsHelper.initPinning(it, TestTLSHelper.TEST_PINS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun createBackend(pinningInit: (OkHttpClient.Builder) -> Unit) =
|
|
|
|
ProtonApiBackend(
|
2020-07-01 10:58:49 +00:00
|
|
|
webServer.url("/").toString(),
|
|
|
|
client,
|
2020-07-07 11:47:26 +00:00
|
|
|
logger,
|
2020-09-25 09:28:47 +00:00
|
|
|
session.sessionId,
|
|
|
|
sessionProvider,
|
2020-07-01 10:58:49 +00:00
|
|
|
apiFactory.baseOkHttpClient,
|
|
|
|
listOf(
|
|
|
|
ScalarsConverterFactory.create(),
|
|
|
|
apiFactory.jsonConverter
|
|
|
|
),
|
|
|
|
TestRetrofitApi::class,
|
|
|
|
networkManager,
|
2020-07-15 17:10:52 +00:00
|
|
|
pinningInit
|
2020-07-01 10:58:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@AfterTest
|
|
|
|
fun after() {
|
|
|
|
webServer.shutdown()
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `test ok call`() = runBlocking {
|
|
|
|
webServer.prepareResponse(
|
|
|
|
HttpURLConnection.HTTP_OK,
|
2020-09-25 09:28:47 +00:00
|
|
|
"""{ "Number": 5, "String": "foo" }"""
|
|
|
|
)
|
2020-07-01 10:58:49 +00:00
|
|
|
|
|
|
|
val result = backend(ApiManager.Call(0) { test() })
|
|
|
|
assertTrue(result is ApiResult.Success)
|
|
|
|
|
|
|
|
val data = result.valueOrNull
|
|
|
|
assertEquals(5, data.number)
|
|
|
|
assertEquals("foo", data.string)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `test http error`() = runBlocking {
|
|
|
|
webServer.prepareResponse(404)
|
|
|
|
|
|
|
|
val result = backend(ApiManager.Call(0) { test() })
|
|
|
|
assertTrue(result is ApiResult.Error.Http)
|
|
|
|
assertEquals(404, result.httpCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `test too many requests`() = runBlocking {
|
|
|
|
val response = MockResponse()
|
|
|
|
.setResponseCode(429)
|
|
|
|
.setHeader("Retry-After", "5")
|
|
|
|
webServer.enqueue(response)
|
|
|
|
|
|
|
|
val result = backend(ApiManager.Call(0) { test() })
|
|
|
|
assertTrue(result is ApiResult.Error.TooManyRequest)
|
|
|
|
assertEquals(429, result.httpCode)
|
|
|
|
assertEquals(5, result.retryAfterSeconds)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `test proton error`() = runBlocking {
|
|
|
|
webServer.prepareResponse(
|
|
|
|
401,
|
2020-09-25 09:28:47 +00:00
|
|
|
"""{ "Code": 10, "Error": "darn!" }"""
|
|
|
|
)
|
2020-07-01 10:58:49 +00:00
|
|
|
|
|
|
|
val result = backend(ApiManager.Call(0) { test() })
|
2020-07-01 12:39:58 +00:00
|
|
|
assertTrue(result is ApiResult.Error.Http)
|
2020-07-01 10:58:49 +00:00
|
|
|
|
2020-07-01 12:39:58 +00:00
|
|
|
assertEquals(10, result.proton?.code)
|
2020-07-01 10:58:49 +00:00
|
|
|
assertEquals(401, result.httpCode)
|
2020-07-01 12:39:58 +00:00
|
|
|
assertEquals("darn!", result.proton?.error)
|
2020-07-01 10:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `test Accept header override`() = runBlocking {
|
|
|
|
webServer.prepareResponse(HttpURLConnection.HTTP_OK, "plain")
|
|
|
|
|
|
|
|
val result = backend(ApiManager.Call(0) { testPlain() })
|
|
|
|
assertEquals("text/plain", webServer.takeRequest().headers["Accept"])
|
|
|
|
assertTrue(result is ApiResult.Success)
|
|
|
|
|
|
|
|
assertEquals("plain", result.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `test extra field ignored`() = runBlocking {
|
|
|
|
webServer.prepareResponse(
|
|
|
|
HttpURLConnection.HTTP_OK,
|
2020-09-25 09:28:47 +00:00
|
|
|
"""{ "Number": 5, "String": "foo", "Extra": "bar" }"""
|
|
|
|
)
|
2020-07-01 10:58:49 +00:00
|
|
|
|
|
|
|
val result = backend(ApiManager.Call(0) { test() })
|
|
|
|
assertTrue(result is ApiResult.Success)
|
|
|
|
|
|
|
|
val data = result.valueOrNull
|
|
|
|
assertEquals(5, data.number)
|
|
|
|
assertEquals("foo", data.string)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `test missing field`() = runBlocking {
|
|
|
|
webServer.prepareResponse(
|
|
|
|
HttpURLConnection.HTTP_OK,
|
2020-09-25 09:28:47 +00:00
|
|
|
"""{ "NumberTypo": 5, "String": "foo" }"""
|
|
|
|
)
|
2020-07-01 10:58:49 +00:00
|
|
|
|
|
|
|
val result = backend(ApiManager.Call(0) { test() })
|
|
|
|
assertTrue(result is ApiResult.Error.Parse)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `test default val`() = runBlocking {
|
|
|
|
webServer.prepareResponse(
|
|
|
|
HttpURLConnection.HTTP_OK,
|
2020-09-25 09:28:47 +00:00
|
|
|
"""{ "Number": 5, "String": "foo" }"""
|
|
|
|
)
|
2020-07-01 10:58:49 +00:00
|
|
|
|
|
|
|
val result = backend(ApiManager.Call(0) { test() })
|
|
|
|
assertEquals(true, result.valueOrNull?.bool)
|
|
|
|
}
|
2020-07-07 11:56:18 +00:00
|
|
|
|
|
|
|
@Test
|
2020-10-15 13:50:03 +00:00
|
|
|
fun `can deserialize false from 0`() = runBlocking {
|
2020-07-07 11:56:18 +00:00
|
|
|
webServer.prepareResponse(
|
|
|
|
HttpURLConnection.HTTP_OK,
|
2020-09-25 09:28:47 +00:00
|
|
|
"""{ "Number": 5, "String": "foo", Bool: 0 }"""
|
|
|
|
)
|
2020-07-07 11:56:18 +00:00
|
|
|
|
|
|
|
val result = backend(ApiManager.Call(0) { test() })
|
|
|
|
assertEquals(false, result.valueOrNull?.bool)
|
|
|
|
}
|
2020-07-15 17:10:52 +00:00
|
|
|
|
2020-10-15 13:50:03 +00:00
|
|
|
@Test
|
|
|
|
fun `can deserialize true from 1`() = runBlocking {
|
|
|
|
webServer.prepareResponse(
|
|
|
|
HttpURLConnection.HTTP_OK,
|
|
|
|
"""{ "Number": 5, "String": "foo", Bool: 1 }""")
|
|
|
|
|
|
|
|
val result = backend(ApiManager.Call(0) { test() })
|
|
|
|
assertEquals(true, result.valueOrNull?.bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `can deserialize true from 5`() = runBlocking {
|
|
|
|
webServer.prepareResponse(
|
|
|
|
HttpURLConnection.HTTP_OK,
|
|
|
|
"""{ "Number": 5, "String": "foo", Bool: 5 }""")
|
|
|
|
|
|
|
|
val result = backend(ApiManager.Call(0) { test() })
|
|
|
|
assertEquals(true, result.valueOrNull?.bool)
|
|
|
|
}
|
|
|
|
|
2020-07-15 17:10:52 +00:00
|
|
|
@Test
|
|
|
|
fun `test pinning error`() = runBlocking {
|
|
|
|
val badBackend = createBackend {
|
|
|
|
testTlsHelper.initPinning(it, TestTLSHelper.BAD_PINS)
|
|
|
|
}
|
|
|
|
|
|
|
|
webServer.prepareResponse(
|
|
|
|
HttpURLConnection.HTTP_OK,
|
2020-09-25 09:28:47 +00:00
|
|
|
"""{ "Number": 5, "String": "foo" }"""
|
|
|
|
)
|
2020-07-15 17:10:52 +00:00
|
|
|
|
|
|
|
val result = badBackend(ApiManager.Call(0) { test() })
|
|
|
|
assertTrue(result is ApiResult.Error.Certificate)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `test spki leaf pinning ok`() = runBlocking {
|
|
|
|
val altBackend = createBackend { builder ->
|
|
|
|
testTlsHelper.setupSPKIleafPinning(builder, TestTLSHelper.TEST_PINS.toList().map {
|
|
|
|
it.removePrefix("sha256/")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
webServer.prepareResponse(
|
|
|
|
HttpURLConnection.HTTP_OK,
|
2020-09-25 09:28:47 +00:00
|
|
|
"""{ "Number": 5, "String": "foo" }"""
|
|
|
|
)
|
2020-07-15 17:10:52 +00:00
|
|
|
|
|
|
|
val result = altBackend(ApiManager.Call(0) { test() })
|
|
|
|
assertTrue(result is ApiResult.Success)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `test spki leaf pinning error`() = runBlocking {
|
|
|
|
val badAltBackend = createBackend { builder ->
|
|
|
|
testTlsHelper.setupSPKIleafPinning(builder, TestTLSHelper.BAD_PINS.toList().map {
|
|
|
|
it.removePrefix("sha256/")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
webServer.prepareResponse(
|
|
|
|
HttpURLConnection.HTTP_OK,
|
2020-09-25 09:28:47 +00:00
|
|
|
"""{ "Number": 5, "String": "foo" }"""
|
|
|
|
)
|
2020-07-15 17:10:52 +00:00
|
|
|
|
|
|
|
val result = badAltBackend(ApiManager.Call(0) { test() })
|
|
|
|
assertTrue(result is ApiResult.Error.Certificate)
|
|
|
|
}
|
2020-07-01 10:58:49 +00:00
|
|
|
}
|