From ea151b37f02f8080289be46ff5e35d1bf321444d Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Tue, 31 May 2022 15:56:12 +0100 Subject: [PATCH 1/7] adding test cases around parsing homeserver versions --- .../auth/version/HomeServerVersionTest.kt | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersionTest.kt diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersionTest.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersionTest.kt new file mode 100644 index 0000000000..09f85ea4b4 --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersionTest.kt @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.internal.auth.version + +import org.amshove.kluent.internal.assertEquals +import org.junit.Test + +class HomeServerVersionTest { + + @Test + fun `given a semantic version, when parsing then converts to home server version`() { + val cases = buildList { + addAll( + listOf( + case("0.5.1", expected = aVersion(0, 5, 1)), + case("1.0.0", expected = aVersion(1, 0, 0)), + case("1.10.3", expected = aVersion(1, 10, 3)) + ).withPrefixes("v", "r"), + ) + addAll( + listOf( + case("-1.5.1", expected = null), + case("1", expected = null), + case("a", expected = null), + case("1.0", expected = null), + case("1a.2b.3c", expected = null), + ) + ) + } + + cases.forEach { (input, expected) -> + val result = HomeServerVersion.parse(input) + + assertEquals(expected, result, "Expected $input to be $expected but got $result") + } + } +} + +private fun aVersion(major: Int, minor: Int, patch: Int) = HomeServerVersion(major, minor, patch) +private fun case(input: String, expected: HomeServerVersion?) = Case(input, expected) + +private fun List.withPrefixes(vararg prefixes: String) = map { case -> + prefixes.map { prefix -> case.copy(input = "$prefix${case.input}") } +}.flatten() + +private data class Case(val input: String, val expected: HomeServerVersion?) From 289ce7419d9139d878c352ffe2d08a3777a83bd1 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Tue, 31 May 2022 17:11:21 +0100 Subject: [PATCH 2/7] supporting homeserver versions without a patch value --- .../auth/version/HomeServerVersion.kt | 6 ++-- .../auth/version/HomeServerVersionTest.kt | 36 +++++++++---------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersion.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersion.kt index 815f8de2de..62c94700d2 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersion.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersion.kt @@ -38,14 +38,14 @@ internal data class HomeServerVersion( } companion object { - internal val pattern = Regex("""[r|v](\d+)\.(\d+)\.(\d+)""") + internal val pattern = Regex("""[r|v](\d+)\.(\d+)(?:\.(\d+))?""") internal fun parse(value: String): HomeServerVersion? { val result = pattern.matchEntire(value) ?: return null return HomeServerVersion( major = result.groupValues[1].toInt(), minor = result.groupValues[2].toInt(), - patch = result.groupValues[3].toInt() + patch = result.groupValues.getOptional(index = 3, default = "0").toInt() ) } @@ -59,3 +59,5 @@ internal data class HomeServerVersion( val v1_3_0 = HomeServerVersion(major = 1, minor = 3, patch = 0) } } + +private fun List.getOptional(index: Int, default: String) = getOrNull(index)?.ifEmpty { default } ?: default diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersionTest.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersionTest.kt index 09f85ea4b4..040918439b 100644 --- a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersionTest.kt +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersionTest.kt @@ -22,27 +22,23 @@ import org.junit.Test class HomeServerVersionTest { @Test - fun `given a semantic version, when parsing then converts to home server version`() { - val cases = buildList { - addAll( - listOf( - case("0.5.1", expected = aVersion(0, 5, 1)), - case("1.0.0", expected = aVersion(1, 0, 0)), - case("1.10.3", expected = aVersion(1, 10, 3)) - ).withPrefixes("v", "r"), - ) - addAll( - listOf( - case("-1.5.1", expected = null), - case("1", expected = null), - case("a", expected = null), - case("1.0", expected = null), - case("1a.2b.3c", expected = null), - ) - ) - } + fun `given a semantic version, when parsing, then converts to home server version`() { + val supportedVersions = listOf( + case("1.5", expected = aVersion(1, 5, 0)), + case("0.5.1", expected = aVersion(0, 5, 1)), + case("1.0.0", expected = aVersion(1, 0, 0)), + case("1.10.3", expected = aVersion(1, 10, 3)) + ).withPrefixes("v", "r") - cases.forEach { (input, expected) -> + val unsupportedVersions = listOf( + case("v-1.5.1", expected = null), + case("r1", expected = null), + case("a", expected = null), + case("1a.2b.3c", expected = null), + case("r", expected = null) + ) + + (supportedVersions + unsupportedVersions).forEach { (input, expected) -> val result = HomeServerVersion.parse(input) assertEquals(expected, result, "Expected $input to be $expected but got $result") From b3841c90066bb89be647314ad24bb592b4108e34 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Tue, 31 May 2022 17:17:37 +0100 Subject: [PATCH 3/7] adding changelog entry --- changelog.d/6017.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/6017.misc diff --git a/changelog.d/6017.misc b/changelog.d/6017.misc new file mode 100644 index 0000000000..2597f2d796 --- /dev/null +++ b/changelog.d/6017.misc @@ -0,0 +1 @@ +Adds support for parsing homeserver versions without a patch number From 4501c7cf45ebd37bd5fdc190a0ea5cff2bc68341 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 1 Jun 2022 10:44:10 +0100 Subject: [PATCH 4/7] adding test case for trailing . on the version --- .../android/sdk/internal/auth/version/HomeServerVersionTest.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersionTest.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersionTest.kt index 040918439b..e5b2935ef2 100644 --- a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersionTest.kt +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersionTest.kt @@ -32,6 +32,8 @@ class HomeServerVersionTest { val unsupportedVersions = listOf( case("v-1.5.1", expected = null), + case("1.4.", expected = null), + case("1.5.1.", expected = null), case("r1", expected = null), case("a", expected = null), case("1a.2b.3c", expected = null), From 3756b2d240077d21c8b7e0624f00d65eb1ead159 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 1 Jun 2022 10:44:23 +0100 Subject: [PATCH 5/7] adding trailing commas to lists --- .../sdk/internal/auth/version/HomeServerVersionTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersionTest.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersionTest.kt index e5b2935ef2..616604a708 100644 --- a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersionTest.kt +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersionTest.kt @@ -27,7 +27,7 @@ class HomeServerVersionTest { case("1.5", expected = aVersion(1, 5, 0)), case("0.5.1", expected = aVersion(0, 5, 1)), case("1.0.0", expected = aVersion(1, 0, 0)), - case("1.10.3", expected = aVersion(1, 10, 3)) + case("1.10.3", expected = aVersion(1, 10, 3)), ).withPrefixes("v", "r") val unsupportedVersions = listOf( @@ -37,7 +37,7 @@ class HomeServerVersionTest { case("r1", expected = null), case("a", expected = null), case("1a.2b.3c", expected = null), - case("r", expected = null) + case("r", expected = null), ) (supportedVersions + unsupportedVersions).forEach { (input, expected) -> From 10016fcb154ee416eca23b780f4048fba9e110f5 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 1 Jun 2022 13:45:59 +0100 Subject: [PATCH 6/7] using correct license for matrix sdk class --- .../android/sdk/internal/auth/version/HomeServerVersionTest.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersionTest.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersionTest.kt index 616604a708..413af9c434 100644 --- a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersionTest.kt +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersionTest.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 New Vector Ltd + * Copyright 2022 The Matrix.org Foundation C.I.C. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.matrix.android.sdk.internal.auth.version import org.amshove.kluent.internal.assertEquals From fa21b6d2241d3dc1bdcbf867d36836ed6f7b4b52 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Mon, 6 Jun 2022 14:16:01 +0100 Subject: [PATCH 7/7] creating generic extension for converting empty strings to null and making use for finding option regex match --- .../java/org/matrix/android/sdk/api/extensions/Strings.kt | 5 +++++ .../android/sdk/internal/auth/version/HomeServerVersion.kt | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/extensions/Strings.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/extensions/Strings.kt index 5e1350e327..2a8848e80a 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/extensions/Strings.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/extensions/Strings.kt @@ -27,3 +27,8 @@ fun CharSequence.ensurePrefix(prefix: CharSequence): CharSequence { * Append a new line and then the provided string. */ fun StringBuilder.appendNl(str: String) = append("\n").append(str) + +/** + * Returns null if the string is empty. + */ +fun String.ensureNotEmpty() = ifEmpty { null } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersion.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersion.kt index 62c94700d2..239adfdbbd 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersion.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/version/HomeServerVersion.kt @@ -16,6 +16,8 @@ package org.matrix.android.sdk.internal.auth.version +import org.matrix.android.sdk.api.extensions.ensureNotEmpty + /** * Values will take the form "rX.Y.Z". * Ref: https://matrix.org/docs/spec/client_server/latest#get-matrix-client-versions @@ -45,7 +47,7 @@ internal data class HomeServerVersion( return HomeServerVersion( major = result.groupValues[1].toInt(), minor = result.groupValues[2].toInt(), - patch = result.groupValues.getOptional(index = 3, default = "0").toInt() + patch = result.groupValues.getOrNull(index = 3)?.ensureNotEmpty()?.toInt() ?: 0 ) } @@ -59,5 +61,3 @@ internal data class HomeServerVersion( val v1_3_0 = HomeServerVersion(major = 1, minor = 3, patch = 0) } } - -private fun List.getOptional(index: Int, default: String) = getOrNull(index)?.ifEmpty { default } ?: default