2018-09-14 13:30:16 +02:00
|
|
|
package apdu
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2018-09-25 15:26:06 +02:00
|
|
|
"regexp"
|
2018-09-14 13:30:16 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func hexToBytes(s string) []byte {
|
2018-09-25 15:26:06 +02:00
|
|
|
s = regexp.MustCompile(" ").ReplaceAllString(s, "")
|
2018-09-14 13:30:16 +02:00
|
|
|
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")
|
|
|
|
|
|
|
|
cmd := NewCommand(cla, ins, p1, p2, data)
|
|
|
|
expected := "80 50 01 02 08 84 76 23 36 C5 18 7F E8 00"
|
|
|
|
|
2018-09-19 15:29:43 +02:00
|
|
|
result, err := cmd.Serialize()
|
2018-09-14 13:30:16 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, bytesToHexWithSpaces(result))
|
|
|
|
}
|