Skip to content

Commit

Permalink
Merge pull request #14 from claucece/master
Browse files Browse the repository at this point in the history
Simplify some logic
  • Loading branch information
jleni authored Dec 17, 2019
2 parents f586aa9 + 3259ef3 commit abf8bf8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
17 changes: 9 additions & 8 deletions apduWrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ package ledger_go
import (
"bytes"
"fmt"
"github.com/stretchr/testify/assert"
"math"
"testing"
"unsafe"

"github.com/stretchr/testify/assert"
)

func Test_SerializePacket_EmptyCommand(t *testing.T) {
Expand Down Expand Up @@ -195,27 +196,27 @@ func Test_WrapCommandAPDU_CheckData(t *testing.T) {
packetSize)

// Check data in the first packet
assert.True(t, bytes.Compare(command[0:64-7], result[7:64]) == 0)
assert.True(t, bytes.Equal(command[0:64-7], result[7:64]))

result = result[64:]
command = command[64-7:]
// Check data in the second packet
assert.True(t, bytes.Compare(command[0:64-5], result[5:64]) == 0)
assert.True(t, bytes.Equal(command[0:64-5], result[5:64]))

result = result[64:]
command = command[64-5:]
// Check data in the third packet
assert.True(t, bytes.Compare(command[0:64-5], result[5:64]) == 0)
assert.True(t, bytes.Equal(command[0:64-5], result[5:64]))

result = result[64:]
command = command[64-5:]

// Check data in the last packet
assert.True(t, bytes.Compare(command[0:], result[5:5+len(command)]) == 0)
assert.True(t, bytes.Equal(command[0:], result[5:5+len(command)]))

// The remaining bytes in the result should be zeros
result = result[5+len(command):]
assert.True(t, bytes.Compare(result, make([]byte, len(result))) == 0)
assert.True(t, bytes.Equal(result, make([]byte, len(result))))
}

func Test_DeserializePacket_FirstPacket(t *testing.T) {
Expand Down Expand Up @@ -246,7 +247,7 @@ func Test_DeserializePacket_SecondMessage(t *testing.T) {
assert.Nil(t, err, "Simple deserialize should not have errors")
assert.Equal(t, 0, int(totalSize), "TotalSize should not be returned from deserialization of non-first packet")
assert.Equal(t, packetSize-firstPacketHeaderSize, len(output), "Size of the deserialized packet is wrong")
assert.True(t, bytes.Compare(output[:len(sampleCommand)], sampleCommand) == 0, "Deserialized message does not match the original")
assert.True(t, bytes.Equal(output[:len(sampleCommand)], sampleCommand), "Deserialized message does not match the original")
}

func Test_UnwrapApdu_SmokeTest(t *testing.T) {
Expand Down Expand Up @@ -279,6 +280,6 @@ func Test_UnwrapApdu_SmokeTest(t *testing.T) {

assert.Equal(t, len(input), len(output), "Input and output messages have different size")
assert.True(t,
bytes.Compare(input, output) == 0,
bytes.Equal(input, output),
"Input message does not match message which was serialized and then deserialized")
}
3 changes: 3 additions & 0 deletions ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ func (ledger *Ledger) Exchange(command []byte) ([]byte, error) {
readChannel := ledger.Read()

response, err := UnwrapResponseAPDU(Channel, readChannel, PacketSize)
if err != nil {
return nil, err
}

if len(response) < 2 {
return nil, fmt.Errorf("len(response) < 2")
Expand Down

0 comments on commit abf8bf8

Please sign in to comment.