1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-26 09:16:22 +00:00

Update licenses, naming and apply CR suggestions

This commit is contained in:
Abel García de Prada 2021-02-26 15:22:11 +01:00
parent 3a79a86cd7
commit f248448e08
9 changed files with 135 additions and 75 deletions

View File

@ -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"

View File

@ -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<Cookie>, name: String): Boolean {
for (cookie: Cookie in cookies) {
if (cookie.name == name) {
return true;
return true
}
}
return false;
return false
}
fun getUpdatedCookies(oldCookies: List<Cookie>, newCookies: List<Cookie>): List<Cookie> {
val updatedList = ArrayList<Cookie>(newCookies);
val updatedList = ArrayList<Cookie>(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<Cookie>) {
// Avoid duplicated cookies but update
val currentCookies: List<Cookie> = sCookieStore[url.host] ?: ArrayList()
val updatedCookies: List<Cookie> = getUpdatedCookies(currentCookies, cookies);
sCookieStore[url.host] = updatedCookies;
val updatedCookies: List<Cookie> = getUpdatedCookies(currentCookies, cookies)
sCookieStore[url.host] = updatedCookies
}
override fun loadForRequest(url: HttpUrl) =

View File

@ -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();
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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<Cookie>().apply {
add(COOKIE_A)
add(COOKIE_B_OLD)
}
private val newCookies = ArrayList<Cookie>().apply {
add(COOKIE_B_NEW)
}
private val updatedCookies = ArrayList<Cookie>().apply {
add(COOKIE_A)
add(COOKIE_B_NEW)
}
private val cookieStore = HashMap<String, List<Cookie>>().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])

View File

@ -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)
)
)
}

View File

@ -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 {