2018-09-25 13:26:06 +00:00
|
|
|
package apdu
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-11-06 09:25:54 +00:00
|
|
|
"github.com/status-im/hardware-wallet-go/hexutils"
|
2018-09-25 13:26:06 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFindTag(t *testing.T) {
|
|
|
|
var (
|
|
|
|
tagData []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2018-09-27 09:23:12 +00:00
|
|
|
data := hexutils.HexToBytes("C1 02 BB CC C2 04 C3 02 11 22 C3 02 88 99")
|
2018-09-25 13:26:06 +00:00
|
|
|
|
|
|
|
tagData, err = FindTag(data, uint8(0xC1))
|
|
|
|
assert.NoError(t, err)
|
2018-09-27 09:23:12 +00:00
|
|
|
assert.Equal(t, "BB CC", hexutils.BytesToHexWithSpaces(tagData))
|
2018-09-25 13:26:06 +00:00
|
|
|
|
|
|
|
tagData, err = FindTag(data, uint8(0xC2))
|
|
|
|
assert.NoError(t, err)
|
2018-09-27 09:23:12 +00:00
|
|
|
assert.Equal(t, "C3 02 11 22", hexutils.BytesToHexWithSpaces(tagData))
|
2018-09-25 13:26:06 +00:00
|
|
|
|
|
|
|
tagData, err = FindTag(data, uint8(0xC3))
|
|
|
|
assert.NoError(t, err)
|
2018-09-27 09:23:12 +00:00
|
|
|
assert.Equal(t, "88 99", hexutils.BytesToHexWithSpaces(tagData))
|
2018-09-25 13:26:06 +00:00
|
|
|
|
|
|
|
tagData, err = FindTag(data, uint8(0xC2), uint8(0xC3))
|
|
|
|
assert.NoError(t, err)
|
2018-09-27 09:23:12 +00:00
|
|
|
assert.Equal(t, "11 22", hexutils.BytesToHexWithSpaces(tagData))
|
2018-09-25 13:26:06 +00:00
|
|
|
|
|
|
|
// tag not found
|
2018-09-27 09:23:12 +00:00
|
|
|
data = hexutils.HexToBytes("C1 00")
|
2018-09-25 15:44:25 +00:00
|
|
|
_, err = FindTag(data, uint8(0xC2))
|
2018-09-25 13:26:06 +00:00
|
|
|
assert.Equal(t, &ErrTagNotFound{uint8(0xC2)}, err)
|
|
|
|
|
|
|
|
// sub-tag not found
|
2018-09-27 09:23:12 +00:00
|
|
|
data = hexutils.HexToBytes("C1 02 C2 00")
|
2018-09-25 15:44:25 +00:00
|
|
|
_, err = FindTag(data, uint8(0xC1), uint8(0xC3))
|
2018-09-25 13:26:06 +00:00
|
|
|
assert.Equal(t, &ErrTagNotFound{uint8(0xC3)}, err)
|
|
|
|
}
|
2018-10-24 11:42:00 +00:00
|
|
|
|
|
|
|
func TestFindTagN(t *testing.T) {
|
|
|
|
data := hexutils.HexToBytes("0A 01 A1 0A 01 A2")
|
|
|
|
|
|
|
|
tagData, err := FindTagN(data, 0, uint8(0x0A))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "A1", hexutils.BytesToHexWithSpaces(tagData))
|
|
|
|
|
|
|
|
tagData, err = FindTagN(data, 1, uint8(0x0A))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "A2", hexutils.BytesToHexWithSpaces(tagData))
|
|
|
|
}
|