|
| 1 | +/******************************************************************************* |
| 2 | +* (c) Zondax AG |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +********************************************************************************/ |
| 16 | + |
| 17 | +package ledger_go |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "testing" |
| 24 | +) |
| 25 | + |
| 26 | +func TestPrepareChunks(t *testing.T) { |
| 27 | + tests := []struct { |
| 28 | + name string |
| 29 | + bip44PathBytes []byte |
| 30 | + transaction []byte |
| 31 | + expected int |
| 32 | + }{ |
| 33 | + { |
| 34 | + name: "Large transaction", |
| 35 | + bip44PathBytes: []byte{0x01, 0x02, 0x03}, |
| 36 | + transaction: make([]byte, 500), |
| 37 | + expected: 12, // 1 path chunk + 11 data chunks (500/48 = 10.4, so 11 chunks) |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "Small transaction", |
| 41 | + bip44PathBytes: []byte{0x01, 0x02, 0x03}, |
| 42 | + transaction: make([]byte, 100), |
| 43 | + expected: 4, // 1 path chunk + 3 data chunks (100/48 = 2.08, so 3 chunks) |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "Exact chunk size transaction", |
| 47 | + bip44PathBytes: []byte{0x01, 0x02, 0x03}, |
| 48 | + transaction: make([]byte, 48), |
| 49 | + expected: 2, // 1 path chunk + 1 data chunk |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "One byte over chunk size", |
| 53 | + bip44PathBytes: []byte{0x01, 0x02, 0x03}, |
| 54 | + transaction: make([]byte, 49), |
| 55 | + expected: 3, // 1 path chunk + 2 data chunks |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "Empty transaction", |
| 59 | + bip44PathBytes: []byte{0x01, 0x02, 0x03}, |
| 60 | + transaction: []byte{}, |
| 61 | + expected: 1, // 1 path chunk only |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + for _, tt := range tests { |
| 66 | + t.Run(tt.name, func(t *testing.T) { |
| 67 | + chunks := PrepareChunks(tt.bip44PathBytes, tt.transaction) |
| 68 | + |
| 69 | + if len(chunks) != tt.expected { |
| 70 | + t.Errorf("Expected %d chunks, got %d", tt.expected, len(chunks)) |
| 71 | + } |
| 72 | + |
| 73 | + // Verify first chunk is the BIP44 path |
| 74 | + if !bytes.Equal(chunks[0], tt.bip44PathBytes) { |
| 75 | + t.Error("First chunk doesn't match BIP44 path") |
| 76 | + } |
| 77 | + |
| 78 | + // Verify transaction data is properly chunked |
| 79 | + if len(tt.transaction) > 0 { |
| 80 | + reconstructed := make([]byte, 0, len(tt.transaction)) |
| 81 | + for i := 1; i < len(chunks); i++ { |
| 82 | + reconstructed = append(reconstructed, chunks[i]...) |
| 83 | + } |
| 84 | + |
| 85 | + if !bytes.Equal(reconstructed, tt.transaction) { |
| 86 | + t.Error("Transaction data not properly chunked") |
| 87 | + } |
| 88 | + } |
| 89 | + }) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestBuildChunkedAPDU(t *testing.T) { |
| 94 | + tests := []struct { |
| 95 | + name string |
| 96 | + cla byte |
| 97 | + ins byte |
| 98 | + p1 byte |
| 99 | + p2 byte |
| 100 | + data []byte |
| 101 | + expected []byte |
| 102 | + }{ |
| 103 | + { |
| 104 | + name: "Basic APDU", |
| 105 | + cla: 0x80, |
| 106 | + ins: 0x02, |
| 107 | + p1: ChunkInit, |
| 108 | + p2: 0x00, |
| 109 | + data: []byte{0x01, 0x02, 0x03}, |
| 110 | + expected: []byte{0x80, 0x02, 0x00, 0x00, 0x03, 0x01, 0x02, 0x03}, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "Empty data", |
| 114 | + cla: 0x80, |
| 115 | + ins: 0x02, |
| 116 | + p1: ChunkLast, |
| 117 | + p2: 0x00, |
| 118 | + data: []byte{}, |
| 119 | + expected: []byte{0x80, 0x02, 0x02, 0x00, 0x00}, |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + for _, tt := range tests { |
| 124 | + t.Run(tt.name, func(t *testing.T) { |
| 125 | + result := BuildChunkedAPDU(tt.cla, tt.ins, tt.p1, tt.p2, tt.data) |
| 126 | + |
| 127 | + if !bytes.Equal(result, tt.expected) { |
| 128 | + t.Errorf("Expected %X, got %X", tt.expected, result) |
| 129 | + } |
| 130 | + }) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// MockLedgerDevice for testing ProcessChunks |
| 135 | +type MockLedgerDevice struct { |
| 136 | + responses [][]byte |
| 137 | + callCount int |
| 138 | + commands [][]byte |
| 139 | +} |
| 140 | + |
| 141 | +func (m *MockLedgerDevice) Exchange(command []byte) ([]byte, error) { |
| 142 | + m.commands = append(m.commands, command) |
| 143 | + if m.callCount < len(m.responses) { |
| 144 | + response := m.responses[m.callCount] |
| 145 | + m.callCount++ |
| 146 | + return response, nil |
| 147 | + } |
| 148 | + return []byte{0x90, 0x00}, nil |
| 149 | +} |
| 150 | + |
| 151 | +func (m *MockLedgerDevice) Close() error { |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +func TestProcessChunks(t *testing.T) { |
| 156 | + mockDevice := &MockLedgerDevice{ |
| 157 | + responses: [][]byte{ |
| 158 | + {0x01, 0x02}, |
| 159 | + {0x03, 0x04}, |
| 160 | + {0x05, 0x06}, |
| 161 | + }, |
| 162 | + } |
| 163 | + |
| 164 | + chunks := [][]byte{ |
| 165 | + {0xAA, 0xBB}, |
| 166 | + {0xCC, 0xDD}, |
| 167 | + {0xEE, 0xFF}, |
| 168 | + } |
| 169 | + |
| 170 | + response, err := ProcessChunksSimple(mockDevice, chunks, 0x80, 0x02, 0x00) |
| 171 | + if err != nil { |
| 172 | + t.Fatalf("ProcessChunks failed: %v", err) |
| 173 | + } |
| 174 | + |
| 175 | + // Check that we got the last response |
| 176 | + if !bytes.Equal(response, []byte{0x05, 0x06}) { |
| 177 | + t.Errorf("Expected last response, got %X", response) |
| 178 | + } |
| 179 | + |
| 180 | + // Check number of calls |
| 181 | + if mockDevice.callCount != 3 { |
| 182 | + t.Errorf("Expected 3 Exchange calls, got %d", mockDevice.callCount) |
| 183 | + } |
| 184 | + |
| 185 | + // Check P1 values for chunk descriptors |
| 186 | + expectedP1 := []byte{ChunkInit, ChunkAdd, ChunkLast} |
| 187 | + for i, cmd := range mockDevice.commands { |
| 188 | + if cmd[2] != expectedP1[i] { |
| 189 | + t.Errorf("Chunk %d: expected P1=%X, got %X", i, expectedP1[i], cmd[2]) |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +// MockErrorDevice for testing error handling |
| 195 | +type MockErrorDevice struct { |
| 196 | + errorToReturn error |
| 197 | + responseBytes []byte |
| 198 | +} |
| 199 | + |
| 200 | +func (m *MockErrorDevice) Exchange(command []byte) ([]byte, error) { |
| 201 | + return m.responseBytes, m.errorToReturn |
| 202 | +} |
| 203 | + |
| 204 | +func (m *MockErrorDevice) Close() error { |
| 205 | + return nil |
| 206 | +} |
| 207 | + |
| 208 | +func TestProcessChunksWithErrorHandler(t *testing.T) { |
| 209 | + mockDevice := &MockErrorDevice{ |
| 210 | + errorToReturn: errors.New("[APDU_CODE_BAD_KEY_HANDLE] The parameters in the data field are incorrect"), |
| 211 | + responseBytes: []byte{0xAB, 0xCD}, |
| 212 | + } |
| 213 | + |
| 214 | + chunks := [][]byte{ |
| 215 | + {0xAA, 0xBB}, |
| 216 | + } |
| 217 | + |
| 218 | + // Test with custom error handler (similar to Avalanche) |
| 219 | + _, err := ProcessChunks(mockDevice, chunks, 0x80, 0x02, 0x00, func(err error, response []byte, instruction byte) error { |
| 220 | + if instruction == 0x02 && err.Error() == "[APDU_CODE_BAD_KEY_HANDLE] The parameters in the data field are incorrect" { |
| 221 | + return fmt.Errorf("%w extra_info=(%s)", err, string(response)) |
| 222 | + } |
| 223 | + return err |
| 224 | + }) |
| 225 | + |
| 226 | + if err == nil { |
| 227 | + t.Fatal("Expected error but got none") |
| 228 | + } |
| 229 | + |
| 230 | + if !bytes.Contains([]byte(err.Error()), []byte("extra_info=")) { |
| 231 | + t.Errorf("Expected custom error handling with extra_info, got: %v", err) |
| 232 | + } |
| 233 | +} |
0 commit comments