package com.chameth.yaotp.algos import com.chameth.yaotp.toHexString import com.natpryce.hamkrest.assertion.assert import com.natpryce.hamkrest.equalTo import org.junit.Test class HotpTest { private val rfcByteArray = byteArrayOfInts(0x1f, 0x86, 0x98, 0x69, 0x0e, 0x02, 0xca, 0x16, 0x61, 0x85, 0x50, 0xef, 0x7f, 0x19, 0xda, 0x8e, 0x94, 0x5b, 0x55, 0x5a) @Test fun testOffsetValue_returnsLastFourBitsAsInt_usingRfcValue() { assert.that(offsetValue(rfcByteArray), equalTo(10)) } @Test fun testOffsetValue_returnsLastFourBitsAsInt_usingShorterValue() { assert.that(offsetValue(byteArrayOfInts(0x55, 0x5a)), equalTo(10)) } @Test fun testOffset_returnsFourBytesAtOffsetValue() { assert.that(offset(rfcByteArray).toHexString(), equalTo("50ef7f19")) } @Test fun testMask_returnsValueUnchanged_ifMsbIsZero() { assert.that(mask(byteArrayOfInts(0x50, 0xef, 0x7f, 0x19)).toHexString(), equalTo("50ef7f19")) } @Test fun testMask_returnsMaskedValue_ifMsbIsNotZero() { assert.that(mask(byteArrayOfInts(0xA0, 0xef, 0x7f, 0x19)).toHexString(), equalTo("20ef7f19")) } @Test fun testHmacToHotp_withDefaultLength() { assert.that(hmacToHotp(rfcByteArray), equalTo(872921)) } @Test fun testHotp_withRfcTestData() { val key = "12345678901234567890".toByteArray() val expected = listOf("755224", "287082", "359152", "969429", "338314", "254676", "287922", "162583", "399871", "520489") expected.forEachIndexed { index, i -> assert.that(hotp(HotpParams(key), byteArrayOfInts(0, 0, 0, 0, 0, 0, 0, index)).otp, equalTo(i)) } } private fun byteArrayOfInts(vararg ints: Int) = ByteArray(ints.size) { pos -> ints[pos].toByte() } }