diff --git a/owncloudComLibrary/build.gradle b/owncloudComLibrary/build.gradle index 85232e93..5c6bc1a7 100644 --- a/owncloudComLibrary/build.gradle +++ b/owncloudComLibrary/build.gradle @@ -9,7 +9,7 @@ dependencies { api 'com.github.AppDevNext.Logcat:LogcatCore:2.2.2' // Moshi - implementation ("com.squareup.moshi:moshi-kotlin:$moshiVersion") { + implementation("com.squareup.moshi:moshi-kotlin:$moshiVersion") { exclude module: "kotlin-reflect" } kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshiVersion" diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/CookieJarImpl.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/CookieJarImpl.kt index 83ad8f5c..ae76424e 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/CookieJarImpl.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/CookieJarImpl.kt @@ -1,3 +1,26 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2021 ownCloud GmbH. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ package com.owncloud.android.lib.common.http import okhttp3.Cookie @@ -11,27 +34,27 @@ class CookieJarImpl( fun containsCookieWithName(cookies: List, name: String): Boolean { for (cookie: Cookie in cookies) { if (cookie.name == name) { - return true; + return true } } - return false; + return false } fun getUpdatedCookies(oldCookies: List, newCookies: List): List { - val updatedList = ArrayList(newCookies); + val updatedList = ArrayList(newCookies) for (oldCookie: Cookie in oldCookies) { if (!containsCookieWithName(updatedList, oldCookie.name)) { - updatedList.add(oldCookie); + updatedList.add(oldCookie) } } - return updatedList; + return updatedList } override fun saveFromResponse(url: HttpUrl, cookies: List) { // Avoid duplicated cookies but update val currentCookies: List = sCookieStore[url.host] ?: ArrayList() - val updatedCookies: List = getUpdatedCookies(currentCookies, cookies); - sCookieStore[url.host] = updatedCookies; + val updatedCookies: List = getUpdatedCookies(currentCookies, cookies) + sCookieStore[url.host] = updatedCookies } override fun loadForRequest(url: HttpUrl) = diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java index 7a904c93..046dc711 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java @@ -33,6 +33,7 @@ import okhttp3.CookieJar; import okhttp3.HttpUrl; import okhttp3.OkHttpClient; import okhttp3.Protocol; +import okhttp3.TlsVersion; import timber.log.Timber; import javax.net.ssl.SSLContext; @@ -41,7 +42,7 @@ import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; -import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.concurrent.TimeUnit; @@ -80,18 +81,18 @@ public class HttpClient { private static SSLContext getSslContext() throws NoSuchAlgorithmException { try { - return SSLContext.getInstance("TLSv1.3"); + return SSLContext.getInstance(TlsVersion.TLS_1_3.javaName()); } catch (NoSuchAlgorithmException tlsv13Exception) { try { Timber.w("TLSv1.3 is not supported in this device; falling through TLSv1.2"); - return SSLContext.getInstance("TLSv1.2"); + return SSLContext.getInstance(TlsVersion.TLS_1_2.javaName()); } catch (NoSuchAlgorithmException tlsv12Exception) { try { Timber.w("TLSv1.2 is not supported in this device; falling through TLSv1.1"); - return SSLContext.getInstance("TLSv1.1"); + return SSLContext.getInstance(TlsVersion.TLS_1_1.javaName()); } catch (NoSuchAlgorithmException tlsv11Exception) { Timber.w("TLSv1.1 is not supported in this device; falling through TLSv1.0"); - return SSLContext.getInstance("TLSv1"); + return SSLContext.getInstance(TlsVersion.TLS_1_0.javaName()); // should be available in any device; see reference of supported protocols in // http://developer.android.com/reference/javax/net/ssl/SSLSocket.html } @@ -110,7 +111,7 @@ public class HttpClient { CookieJar cookieJar) { return new OkHttpClient.Builder() .addNetworkInterceptor(getLogInterceptor()) - .protocols(Arrays.asList(Protocol.HTTP_1_1)) + .protocols(Collections.singletonList(Protocol.HTTP_1_1)) .readTimeout(HttpConstants.DEFAULT_DATA_TIMEOUT, TimeUnit.MILLISECONDS) .writeTimeout(HttpConstants.DEFAULT_DATA_TIMEOUT, TimeUnit.MILLISECONDS) .connectTimeout(HttpConstants.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS) @@ -121,14 +122,6 @@ public class HttpClient { .build(); } - public Context getContext() { - return sContext; - } - - public static void setContext(Context context) { - sContext = context; - } - public static LogInterceptor getLogInterceptor() { if (sLogInterceptor == null) { sLogInterceptor = new LogInterceptor(); @@ -140,6 +133,14 @@ public class HttpClient { return sCookieStore.get(httpUrl.host()); } + public Context getContext() { + return sContext; + } + + public static void setContext(Context context) { + sContext = context; + } + public void clearCookies() { sCookieStore.clear(); } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/HttpScheme.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/HttpScheme.kt index 48659324..ff3e3662 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/HttpScheme.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/HttpScheme.kt @@ -1,5 +1,5 @@ /* ownCloud Android Library is available under MIT license -* Copyright (C) 2020 ownCloud GmbH. +* Copyright (C) 2021 ownCloud GmbH. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/RemoteServerInfo.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/RemoteServerInfo.kt index 5938368d..7163fd4a 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/RemoteServerInfo.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/RemoteServerInfo.kt @@ -1,5 +1,5 @@ /* ownCloud Android Library is available under MIT license -* Copyright (C) 2020 ownCloud GmbH. +* Copyright (C) 2021 ownCloud GmbH. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt index 842c7d36..8706a185 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt @@ -1,5 +1,5 @@ /* ownCloud Android Library is available under MIT license -* Copyright (C) 2020 ownCloud GmbH. +* Copyright (C) 2021 ownCloud GmbH. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/owncloudComLibrary/src/test/java/com/owncloud/android/lib/CookieJarImplTest.kt b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/CookieJarImplTest.kt index f0e62a1f..7e1d7691 100644 --- a/owncloudComLibrary/src/test/java/com/owncloud/android/lib/CookieJarImplTest.kt +++ b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/CookieJarImplTest.kt @@ -1,47 +1,57 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2021 ownCloud GmbH. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ package com.owncloud.android.lib import com.owncloud.android.lib.common.http.CookieJarImpl -import junit.framework.Assert.assertEquals -import junit.framework.Assert.assertFalse -import junit.framework.Assert.assertTrue import okhttp3.Cookie import okhttp3.HttpUrl.Companion.toHttpUrl +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue import org.junit.Test class CookieJarImplTest { - private val oldCookies = ArrayList().apply { - add(COOKIE_A) - add(COOKIE_B_OLD) - } - - private val newCookies = ArrayList().apply { - add(COOKIE_B_NEW) - } - - private val updatedCookies = ArrayList().apply { - add(COOKIE_A) - add(COOKIE_B_NEW) - } - - private val cookieStore = HashMap>().apply { - put(SOME_HOST, oldCookies) - } + private val oldCookies = listOf(COOKIE_A, COOKIE_B_OLD) + private val newCookies = listOf(COOKIE_B_NEW) + private val updatedCookies = listOf(COOKIE_A, COOKIE_B_NEW) + private val cookieStore = hashMapOf(SOME_HOST to oldCookies) private val cookieJarImpl = CookieJarImpl(cookieStore) @Test - fun testContainsCookieWithNameReturnsTrue() { + fun `contains cookie with name - ok - true`() { assertTrue(cookieJarImpl.containsCookieWithName(oldCookies, COOKIE_B_OLD.name)) } @Test - fun testContainsCookieWithNameReturnsFalse() { + fun `contains cookie with name - ok - false`() { assertFalse(cookieJarImpl.containsCookieWithName(newCookies, COOKIE_A.name)) } @Test - fun testGetUpdatedCookies() { + fun `get updated cookies - ok`() { val generatedUpdatedCookies = cookieJarImpl.getUpdatedCookies(oldCookies, newCookies) assertEquals(2, generatedUpdatedCookies.size) assertEquals(updatedCookies[0], generatedUpdatedCookies[1]) @@ -49,7 +59,7 @@ class CookieJarImplTest { } @Test - fun testCookieStoreUpdateViaSaveFromResponse() { + fun `store cookie via saveFromResponse - ok`() { cookieJarImpl.saveFromResponse(SOME_URL, newCookies) val generatedUpdatedCookies = cookieStore[SOME_HOST] assertEquals(2, generatedUpdatedCookies?.size) @@ -58,7 +68,7 @@ class CookieJarImplTest { } @Test - fun testLoadForRequest() { + fun `load for request - ok`() { val cookies = cookieJarImpl.loadForRequest(SOME_URL) assertEquals(oldCookies[0], cookies[0]) assertEquals(oldCookies[1], cookies[1]) @@ -71,4 +81,4 @@ class CookieJarImplTest { val COOKIE_B_OLD = Cookie.parse(SOME_URL, "CookieB=CookieOldValueB")!! val COOKIE_B_NEW = Cookie.parse(SOME_URL, "CookieB=CookieNewValueB")!! } -} \ No newline at end of file +} diff --git a/owncloudComLibrary/src/test/java/com/owncloud/android/lib/GetRemoteStatusOperationTest.kt b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/GetRemoteStatusOperationTest.kt index 8cb93022..375e9aa5 100644 --- a/owncloudComLibrary/src/test/java/com/owncloud/android/lib/GetRemoteStatusOperationTest.kt +++ b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/GetRemoteStatusOperationTest.kt @@ -1,3 +1,26 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2021 ownCloud GmbH. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ package com.owncloud.android.lib import android.net.Uri @@ -59,10 +82,9 @@ class GetRemoteStatusOperationTest { @Test fun `build full https url - ok - no https with subdir`() { assertEquals( - Uri.parse(HTTPS_SOME_OWNCLOUD_WITH_SUBDIR), GetRemoteStatusOperation.buildFullHttpsUrl( - Uri.parse( - HTTPS_SOME_OWNCLOUD_WITH_SUBDIR - ) + Uri.parse(HTTPS_SOME_OWNCLOUD_WITH_SUBDIR), + GetRemoteStatusOperation.buildFullHttpsUrl( + Uri.parse(HTTPS_SOME_OWNCLOUD_WITH_SUBDIR) ) ) } @@ -70,10 +92,9 @@ class GetRemoteStatusOperationTest { @Test fun `build full https url - ok - no prefix with subdir`() { assertEquals( - Uri.parse(HTTPS_SOME_OWNCLOUD_WITH_SUBDIR), GetRemoteStatusOperation.buildFullHttpsUrl( - Uri.parse( - SOME_OWNCLOUD_WITH_SUBDIR - ) + Uri.parse(HTTPS_SOME_OWNCLOUD_WITH_SUBDIR), + GetRemoteStatusOperation.buildFullHttpsUrl( + Uri.parse(SOME_OWNCLOUD_WITH_SUBDIR) ) ) } @@ -121,4 +142,4 @@ class GetRemoteStatusOperationTest { const val HTTP_SOME_IP_WITH_PORT = "$HTTP_PREFIX$SOME_IP_WITH_PORT" const val HTTPS_SOME_IP_WITH_PORT = "$HTTPS_PREFIX$SOME_IP_WITH_PORT" } -} \ No newline at end of file +} diff --git a/owncloudComLibrary/src/test/java/com/owncloud/android/lib/StatusRequestorTest.kt b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/StatusRequesterTest.kt similarity index 80% rename from owncloudComLibrary/src/test/java/com/owncloud/android/lib/StatusRequestorTest.kt rename to owncloudComLibrary/src/test/java/com/owncloud/android/lib/StatusRequesterTest.kt index 2ea243c5..20b1c668 100644 --- a/owncloudComLibrary/src/test/java/com/owncloud/android/lib/StatusRequestorTest.kt +++ b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/StatusRequesterTest.kt @@ -1,5 +1,5 @@ /* ownCloud Android Library is available under MIT license -* Copyright (C) 2020 ownCloud GmbH. +* Copyright (C) 2021 ownCloud GmbH. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -30,7 +30,7 @@ import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Test -class StatusRequestorTest { +class StatusRequesterTest { private val requester = StatusRequester() @Test @@ -53,40 +53,45 @@ class StatusRequestorTest { @Test fun `update location - ok - replace relative path`() { - val newLocation = requester.updateLocationWithRedirectPath( - "$TEST_DOMAIN/some/other/subdir", SUB_PATH - ) + val newLocation = requester.updateLocationWithRedirectPath("$TEST_DOMAIN/some/other/subdir", SUB_PATH) assertEquals("$TEST_DOMAIN$SUB_PATH", newLocation) } @Test fun `check redirect to unsecure connection - ok - redirect to http`() { - assertTrue(requester.isRedirectedToNonSecureConnection( - false, SECURE_DOMAIN, UNSECURE_DOMAIN)) + assertTrue( + requester.isRedirectedToNonSecureConnection(false, SECURE_DOMAIN, UNSECURE_DOMAIN + ) + ) } @Test fun `check redirect to unsecure connection - ko - redirect to https from http`() { - assertFalse(requester.isRedirectedToNonSecureConnection( - false, UNSECURE_DOMAIN, SECURE_DOMAIN)) + assertFalse( + requester.isRedirectedToNonSecureConnection(false, UNSECURE_DOMAIN, SECURE_DOMAIN + ) + ) } @Test fun `check redirect to unsecure connection - ko - from https to https`() { - assertFalse(requester.isRedirectedToNonSecureConnection( - false, SECURE_DOMAIN, SECURE_DOMAIN)) + assertFalse( + requester.isRedirectedToNonSecureConnection(false, SECURE_DOMAIN, SECURE_DOMAIN) + ) } @Test fun `check redirect to unsecure connection - ok - from https to https with previous http`() { - assertTrue(requester.isRedirectedToNonSecureConnection( - true, SECURE_DOMAIN, SECURE_DOMAIN)) + assertTrue( + requester.isRedirectedToNonSecureConnection(true, SECURE_DOMAIN, SECURE_DOMAIN) + ) } @Test fun `check redirect to unsecure connection - ok - from http to http`() { - assertFalse(requester.isRedirectedToNonSecureConnection( - false, UNSECURE_DOMAIN, UNSECURE_DOMAIN)) + assertFalse( + requester.isRedirectedToNonSecureConnection(false, UNSECURE_DOMAIN, UNSECURE_DOMAIN) + ) } companion object {