diff --git a/app/build.gradle b/app/build.gradle index 47f1f9501..527d63567 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -4,6 +4,7 @@ apply plugin: 'kotlin-parcelize' apply plugin: 'kotlinx-serialization' apply plugin: 'com.google.gms.google-services' apply plugin: 'com.github.triplet.play' +apply plugin: 'de.mobilej.unmock' // apply plugin: "app.brant.amazonappstorepublisher" // Apply the Crashlytics Gradle plugin @@ -14,6 +15,11 @@ apply plugin: 'com.google.protobuf' apply plugin: 'kotlin-kapt' +unMock { + keep "android.net.Uri" + keep "android.util.Base64" +} + android { /* signingConfigs { diff --git a/app/src/main/java/com/geeksville/mesh/model/Channel.kt b/app/src/main/java/com/geeksville/mesh/model/Channel.kt index a2af3e92b..665a8de25 100644 --- a/app/src/main/java/com/geeksville/mesh/model/Channel.kt +++ b/app/src/main/java/com/geeksville/mesh/model/Channel.kt @@ -1,15 +1,7 @@ package com.geeksville.mesh.model -import android.graphics.Bitmap -import android.net.Uri -import android.util.Base64 import com.geeksville.mesh.ChannelProtos -import com.geeksville.mesh.MeshProtos import com.google.protobuf.ByteString -import com.google.zxing.BarcodeFormat -import com.google.zxing.MultiFormatWriter -import com.journeyapps.barcodescanner.BarcodeEncoder -import java.net.MalformedURLException /** Utility function to make it easy to declare byte arrays - FIXME move someplace better */ fun byteArrayOfInts(vararg ints: Int) = ByteArray(ints.size) { pos -> ints[pos].toByte() } @@ -74,14 +66,13 @@ data class Channel( */ val humanName: String get() { - val suffix: Char = if (settings.psk.size() != 1) { - // we have a full PSK, so hash it to generate the suffix - val code = settings.psk.fold(0, { acc, x -> acc xor (x.toInt() and 0xff) }) - - 'A' + (code % 26) - } else - '0' + settings.psk.byteAt(0).toInt() + // start with the PSK then xor in the name + val pskCode = xorHash(psk.toByteArray()) + val nameCode = xorHash(name.toByteArray()) + val suffix = 'A' + ((pskCode xor nameCode) % 26) return "#${name}-${suffix}" } } + +fun xorHash(b: ByteArray) = b.fold(0, { acc, x -> acc xor (x.toInt() and 0xff) }) \ No newline at end of file diff --git a/app/src/test/java/com/geeksville/mesh/NodeInfoTest.kt b/app/src/test/java/com/geeksville/mesh/NodeInfoTest.kt index aa9059e57..cf6713cc9 100644 --- a/app/src/test/java/com/geeksville/mesh/NodeInfoTest.kt +++ b/app/src/test/java/com/geeksville/mesh/NodeInfoTest.kt @@ -8,9 +8,10 @@ import org.junit.Test import java.util.* class NodeInfoTest { - val ni1 = NodeInfo(4, MeshUser("+one", "User One", "U1"), Position(37.1, 121.1, 35)) - val ni2 = NodeInfo(5, MeshUser("+two", "User Two", "U2"), Position(37.11, 121.1, 40)) - val ni3 = NodeInfo(6, MeshUser("+three", "User Three", "U3"), Position(37.101, 121.1, 40)) + val model = MeshProtos.HardwareModel.ANDROID_SIM + val ni1 = NodeInfo(4, MeshUser("+one", "User One", "U1", model), Position(37.1, 121.1, 35)) + val ni2 = NodeInfo(5, MeshUser("+two", "User Two", "U2", model), Position(37.11, 121.1, 40)) + val ni3 = NodeInfo(6, MeshUser("+three", "User Three", "U3", model), Position(37.101, 121.1, 40)) private val currentDefaultLocale = LocaleListCompat.getDefault().get(0) diff --git a/app/src/test/java/com/geeksville/mesh/model/ChannelSetTest.kt b/app/src/test/java/com/geeksville/mesh/model/ChannelSetTest.kt new file mode 100644 index 000000000..4ac76e13f --- /dev/null +++ b/app/src/test/java/com/geeksville/mesh/model/ChannelSetTest.kt @@ -0,0 +1,16 @@ +package com.geeksville.mesh.model + +import android.net.Uri +import org.junit.Assert +import org.junit.Test + +class ChannelSetTest { + /** make sure we match the python and device code behavior */ + @Test + fun matchPython() { + val url = Uri.parse("https://www.meshtastic.org/d/#CgUYAyIBAQ") + val cs = ChannelSet(url) + Assert.assertEquals("LongSlow", cs.primaryChannel!!.name, ) + Assert.assertEquals("#LongSlow-V", cs.primaryChannel!!.humanName, ) + } +} \ No newline at end of file diff --git a/app/src/test/java/com/geeksville/mesh/service/MeshServiceTest.kt b/app/src/test/java/com/geeksville/mesh/service/MeshServiceTest.kt index f8db6328c..aabea7598 100644 --- a/app/src/test/java/com/geeksville/mesh/service/MeshServiceTest.kt +++ b/app/src/test/java/com/geeksville/mesh/service/MeshServiceTest.kt @@ -1,31 +1,32 @@ -package com.geeksville.mesh.service - -import com.geeksville.mesh.MeshUser -import com.geeksville.mesh.NodeInfo -import com.geeksville.mesh.Position -import org.junit.Assert -import org.junit.Test - - -class MeshServiceTest { - - val nodeInfo = NodeInfo(4, MeshUser("+one", "User One", "U1"), Position(37.1, 121.1, 35, 10)) - - @Test - fun givenNodeInfo_whenUpdatingWithNewTime_thenPositionTimeIsUpdated() { - - val newerTime = 20 - updateNodeInfoTime(nodeInfo, newerTime) - Assert.assertEquals(newerTime, nodeInfo.position?.time) - } - - @Test - fun givenNodeInfo_whenUpdatingWithOldTime_thenPositionTimeIsNotUpdated() { - val olderTime = 5 - val timeBeforeTryingToUpdate = nodeInfo.position?.time - updateNodeInfoTime(nodeInfo, olderTime) - Assert.assertEquals(timeBeforeTryingToUpdate, nodeInfo.position?.time) - } -} - - +package com.geeksville.mesh.service + +import com.geeksville.mesh.MeshProtos +import com.geeksville.mesh.MeshUser +import com.geeksville.mesh.NodeInfo +import com.geeksville.mesh.Position +import org.junit.Assert +import org.junit.Test + + +class MeshServiceTest { + val model = MeshProtos.HardwareModel.ANDROID_SIM + val nodeInfo = NodeInfo(4, MeshUser("+one", "User One", "U1", model), Position(37.1, 121.1, 35, 10)) + + @Test + fun givenNodeInfo_whenUpdatingWithNewTime_thenPositionTimeIsUpdated() { + + val newerTime = 20 + updateNodeInfoTime(nodeInfo, newerTime) + Assert.assertEquals(newerTime, nodeInfo.position?.time) + } + + @Test + fun givenNodeInfo_whenUpdatingWithOldTime_thenPositionTimeIsNotUpdated() { + val olderTime = 5 + val timeBeforeTryingToUpdate = nodeInfo.position?.time + updateNodeInfoTime(nodeInfo, olderTime) + Assert.assertEquals(timeBeforeTryingToUpdate, nodeInfo.position?.time) + } +} + + diff --git a/build.gradle b/build.gradle index 183d7bf8d..ba99badad 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,7 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:4.1.2' + classpath 'com.android.tools.build:gradle:4.1.3' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version" @@ -28,6 +28,9 @@ buildscript { //classpath "app.brant:amazonappstorepublisher:0.1.0" classpath 'com.github.triplet.gradle:play-publisher:2.8.0' + + // for unit testing https://github.com/bjoernQ/unmock-plugin + classpath 'com.github.bjoernq:unmockplugin:0.7.6' } }