add hexutils pkg

This commit is contained in:
Andrea Franz 2018-09-27 11:23:12 +02:00
parent 3f962927cf
commit fa35b117ff
No known key found for this signature in database
GPG Key ID: 4F0D2F2D9DE7F29D
5 changed files with 45 additions and 58 deletions

View File

@ -1,45 +1,23 @@
package apdu
import (
"encoding/hex"
"fmt"
"log"
"regexp"
"testing"
"github.com/status-im/status-go/smartcard/hexutils"
"github.com/stretchr/testify/assert"
)
func hexToBytes(s string) []byte {
s = regexp.MustCompile(" ").ReplaceAllString(s, "")
b := make([]byte, hex.DecodedLen(len(s)))
_, err := hex.Decode(b, []byte(s))
if err != nil {
log.Fatal(err)
}
return b[:]
}
func bytesToHexWithSpaces(b []byte) string {
return fmt.Sprintf("% X", b)
}
func bytesToHex(b []byte) string {
return fmt.Sprintf("%X", b)
}
func TestNewCommand(t *testing.T) {
var cla uint8 = 0x80
var ins uint8 = 0x50
var p1 uint8 = 1
var p2 uint8 = 2
data := hexToBytes("84762336c5187fe8")
data := hexutils.HexToBytes("84762336c5187fe8")
cmd := NewCommand(cla, ins, p1, p2, data)
expected := "80 50 01 02 08 84 76 23 36 C5 18 7F E8 00"
result, err := cmd.Serialize()
assert.NoError(t, err)
assert.Equal(t, expected, bytesToHexWithSpaces(result))
assert.Equal(t, expected, hexutils.BytesToHexWithSpaces(result))
}

View File

@ -3,11 +3,12 @@ package apdu
import (
"testing"
"github.com/status-im/status-go/smartcard/hexutils"
"github.com/stretchr/testify/assert"
)
func TestParseResponse(t *testing.T) {
raw := hexToBytes("000002650183039536622002003b5e508f751c0af3016e3fbc23d3a69000")
raw := hexutils.HexToBytes("000002650183039536622002003b5e508f751c0af3016e3fbc23d3a69000")
resp, err := ParseResponse(raw)
assert.NoError(t, err)
@ -16,17 +17,17 @@ func TestParseResponse(t *testing.T) {
assert.Equal(t, uint16(0x9000), resp.Sw)
expected := "000002650183039536622002003B5E508F751C0AF3016E3FBC23D3A6"
assert.Equal(t, expected, bytesToHex(resp.Data))
assert.Equal(t, expected, hexutils.BytesToHex(resp.Data))
}
func TestParseResponse_BadData(t *testing.T) {
raw := hexToBytes("")
raw := hexutils.HexToBytes("")
_, err := ParseResponse(raw)
assert.Equal(t, ErrBadRawResponse, err)
}
func TestResp_IsOK(t *testing.T) {
raw := hexToBytes("01029000")
raw := hexutils.HexToBytes("01029000")
resp, err := ParseResponse(raw)
assert.NoError(t, err)
assert.True(t, resp.IsOK())

View File

@ -3,6 +3,7 @@ package apdu
import (
"testing"
"github.com/status-im/status-go/smartcard/hexutils"
"github.com/stretchr/testify/assert"
)
@ -12,31 +13,31 @@ func TestFindTag(t *testing.T) {
err error
)
data := hexToBytes("C1 02 BB CC C2 04 C3 02 11 22 C3 02 88 99")
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)
assert.Equal(t, "BB CC", bytesToHexWithSpaces(tagData))
assert.Equal(t, "BB CC", hexutils.BytesToHexWithSpaces(tagData))
tagData, err = FindTag(data, uint8(0xC2))
assert.NoError(t, err)
assert.Equal(t, "C3 02 11 22", bytesToHexWithSpaces(tagData))
assert.Equal(t, "C3 02 11 22", hexutils.BytesToHexWithSpaces(tagData))
tagData, err = FindTag(data, uint8(0xC3))
assert.NoError(t, err)
assert.Equal(t, "88 99", bytesToHexWithSpaces(tagData))
assert.Equal(t, "88 99", hexutils.BytesToHexWithSpaces(tagData))
tagData, err = FindTag(data, uint8(0xC2), uint8(0xC3))
assert.NoError(t, err)
assert.Equal(t, "11 22", bytesToHexWithSpaces(tagData))
assert.Equal(t, "11 22", hexutils.BytesToHexWithSpaces(tagData))
// tag not found
data = hexToBytes("C1 00")
data = hexutils.HexToBytes("C1 00")
_, err = FindTag(data, uint8(0xC2))
assert.Equal(t, &ErrTagNotFound{uint8(0xC2)}, err)
// sub-tag not found
data = hexToBytes("C1 02 C2 00")
data = hexutils.HexToBytes("C1 02 C2 00")
_, err = FindTag(data, uint8(0xC1), uint8(0xC3))
assert.Equal(t, &ErrTagNotFound{uint8(0xC3)}, err)
}

View File

@ -1,32 +1,12 @@
package globalplatform
import (
"encoding/hex"
"fmt"
"log"
"testing"
"github.com/status-im/status-go/smartcard/hexutils"
"github.com/stretchr/testify/assert"
)
func hexToBytes(s string) []byte {
b := make([]byte, hex.DecodedLen(len(s)))
_, err := hex.Decode(b, []byte(s))
if err != nil {
log.Fatal(err)
}
return b[:]
}
func bytesToHexWithSpaces(b []byte) string {
return fmt.Sprintf("% X", b)
}
func bytesToHex(b []byte) string {
return fmt.Sprintf("%X", b)
}
func TestCommandSelect(t *testing.T) {
aid := []byte{}
cmd := NewCommandSelect(aid)
@ -38,7 +18,7 @@ func TestCommandSelect(t *testing.T) {
}
func TestCommandInitializeUpdate(t *testing.T) {
challenge := hexToBytes("010203")
challenge := hexutils.HexToBytes("010203")
cmd := NewCommandInitializeUpdate(challenge)
assert.Equal(t, uint8(0x80), cmd.Cla)

27
hexutils/hexutils.go Normal file
View File

@ -0,0 +1,27 @@
package hexutils
import (
"encoding/hex"
"fmt"
"log"
"regexp"
)
func HexToBytes(s string) []byte {
s = regexp.MustCompile(" ").ReplaceAllString(s, "")
b := make([]byte, hex.DecodedLen(len(s)))
_, err := hex.Decode(b, []byte(s))
if err != nil {
log.Fatal(err)
}
return b[:]
}
func BytesToHexWithSpaces(b []byte) string {
return fmt.Sprintf("% X", b)
}
func BytesToHex(b []byte) string {
return fmt.Sprintf("%X", b)
}