use unmock to test and fix the channel suffix generation to match device

pull/262/head
Kevin Hester 2021-03-19 17:42:26 +08:00
rodzic c405cdc200
commit 699d5076b5
6 zmienionych plików z 68 dodań i 50 usunięć

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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