fix unit test dependency on SDK_VERSION

This commit is contained in:
Adam Velebil 2022-10-05 17:16:17 +02:00
parent 9b65a23cb9
commit bae9c8c8e3
No known key found for this signature in database
GPG Key ID: AC6D6B9D715FC084
2 changed files with 56 additions and 19 deletions

View File

@ -0,0 +1,30 @@
/*
* Copyright (C) 2022 Yubico.
*
* 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 com.yubico.authenticator
import java.lang.reflect.Field
import java.lang.reflect.Modifier
object TestUtil {
fun setFinalStatic(field: Field, newValue: Any?) {
field.isAccessible = true
val modifiersField = Field::class.java.getDeclaredField("modifiers")
modifiersField.isAccessible = true
modifiersField.setInt(field, field.modifiers and Modifier.FINAL.inv())
field.set(null, newValue)
}
}

View File

@ -1,6 +1,8 @@
package com.yubico.authenticator.yubikit
import android.hardware.usb.UsbDevice
import android.os.Build
import com.yubico.authenticator.TestUtil
import com.yubico.authenticator.device.Version
import com.yubico.yubikit.android.transport.nfc.NfcYubiKeyDevice
import com.yubico.yubikit.android.transport.usb.UsbYubiKeyDevice
@ -13,6 +15,11 @@ import org.mockito.Mockito.mock
class SkyHelperTest {
init {
// TODO: test lower APIs
TestUtil.setFinalStatic(Build.VERSION::class.java.getField("SDK_INT"), 23)
}
@Test
fun `passing NfcYubiKeyDevice will throw`() {
assertThrows(IllegalArgumentException::class.java) {
@ -47,29 +54,29 @@ class SkyHelperTest {
`when`(ykDevice.usbDevice.version).thenReturn("3.00")
SkyHelper.getDeviceInfo(ykDevice).also {
assertEquals(it.version, Version(3, 0, 0))
assertEquals(Version(3, 0, 0), it.version)
}
`when`(ykDevice.usbDevice.version).thenReturn("3.47")
SkyHelper.getDeviceInfo(ykDevice).also {
assertEquals(it.version, Version(3, 4, 7))
assertEquals(Version(3, 4, 7), it.version)
}
// lower than 3 should return 0.0.0
`when`(ykDevice.usbDevice.version).thenReturn("2.10")
SkyHelper.getDeviceInfo(ykDevice).also {
assertEquals(it.version, VERSION_0)
assertEquals(VERSION_0, it.version)
}
// greater or equal 4.0.0 should return 0.0.0
`when`(ykDevice.usbDevice.version).thenReturn("4.00")
SkyHelper.getDeviceInfo(ykDevice).also {
assertEquals(it.version, VERSION_0)
assertEquals(VERSION_0, it.version)
}
`when`(ykDevice.usbDevice.version).thenReturn("4.37")
SkyHelper.getDeviceInfo(ykDevice).also {
assertEquals(it.version, VERSION_0)
assertEquals(VERSION_0, it.version)
}
}
@ -81,28 +88,28 @@ class SkyHelperTest {
`when`(ykDevice.usbDevice.version).thenReturn("3.00")
SkyHelper.getDeviceInfo(ykDevice).also {
assertEquals(it.version, Version(3, 0, 0))
assertEquals(Version(3, 0, 0), it.version)
}
`when`(ykDevice.usbDevice.version).thenReturn("3.47")
SkyHelper.getDeviceInfo(ykDevice).also {
assertEquals(it.version, Version(3, 4, 7))
assertEquals(Version(3, 4, 7), it.version)
}
`when`(ykDevice.usbDevice.version).thenReturn("4.00")
SkyHelper.getDeviceInfo(ykDevice).also {
assertEquals(it.version, Version(4, 0, 0))
assertEquals(Version(4, 0, 0), it.version)
}
`when`(ykDevice.usbDevice.version).thenReturn("4.37")
SkyHelper.getDeviceInfo(ykDevice).also {
assertEquals(it.version, Version(4, 3, 7))
assertEquals(Version(4, 3, 7), it.version)
}
// lower than 3 should return 0.0.0
`when`(ykDevice.usbDevice.version).thenReturn("2.10")
SkyHelper.getDeviceInfo(ykDevice).also {
assertEquals(it.version, VERSION_0)
assertEquals(VERSION_0, it.version)
}
}
@ -115,18 +122,18 @@ class SkyHelperTest {
`when`(ykDevice.usbDevice.version).thenReturn("4.00")
SkyHelper.getDeviceInfo(ykDevice).also {
assertEquals(it.version, Version(4, 0, 0))
assertEquals(Version(4, 0, 0), it.version)
}
`when`(ykDevice.usbDevice.version).thenReturn("4.37")
SkyHelper.getDeviceInfo(ykDevice).also {
assertEquals(it.version, Version(4, 3, 7))
assertEquals(Version(4, 3, 7), it.version)
}
// lower than 4 should return 0.0.0
`when`(ykDevice.usbDevice.version).thenReturn("3.47")
SkyHelper.getDeviceInfo(ykDevice).also {
assertEquals(it.version, VERSION_0)
assertEquals(VERSION_0, it.version)
}
}
@ -138,32 +145,32 @@ class SkyHelperTest {
`when`(ykDevice.usbDevice.version).thenReturn("")
SkyHelper.getDeviceInfo(ykDevice).also {
assertEquals(it.version, VERSION_0)
assertEquals(VERSION_0, it.version)
}
`when`(ykDevice.usbDevice.version).thenReturn("yubico")
SkyHelper.getDeviceInfo(ykDevice).also {
assertEquals(it.version, VERSION_0)
assertEquals(VERSION_0, it.version)
}
`when`(ykDevice.usbDevice.version).thenReturn("4")
SkyHelper.getDeviceInfo(ykDevice).also {
assertEquals(it.version, VERSION_0)
assertEquals(VERSION_0, it.version)
}
`when`(ykDevice.usbDevice.version).thenReturn("4.")
SkyHelper.getDeviceInfo(ykDevice).also {
assertEquals(it.version, VERSION_0)
assertEquals(VERSION_0, it.version)
}
`when`(ykDevice.usbDevice.version).thenReturn("4.0")
SkyHelper.getDeviceInfo(ykDevice).also {
assertEquals(it.version, VERSION_0)
assertEquals(VERSION_0, it.version)
}
`when`(ykDevice.usbDevice.version).thenReturn("4.0.0")
SkyHelper.getDeviceInfo(ykDevice).also {
assertEquals(it.version, VERSION_0)
assertEquals(VERSION_0, it.version)
}
}