keycard-go/apdu/utils_test.go

56 lines
1.5 KiB
Go
Raw Normal View History

package apdu
import (
"testing"
2018-10-19 08:54:02 +00:00
"github.com/status-im/smartcard-go/hexutils"
"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")
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))
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))
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))
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))
// 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))
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))
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))
}