|
| 1 | +// Copyright 2025 Shift Cryptosecurity AG |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package u2fhid |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "encoding/binary" |
| 20 | + "strings" |
| 21 | + "testing" |
| 22 | +) |
| 23 | + |
| 24 | +// testRW mocks io.ReadWriteCloser with variable write chunk handling |
| 25 | +type testRW struct { |
| 26 | + writeBuffer bytes.Buffer |
| 27 | + writeChunkSize int // max bytes to write per call |
| 28 | +} |
| 29 | + |
| 30 | +func (t *testRW) Write(p []byte) (n int, err error) { |
| 31 | + if t.writeChunkSize == 0 || t.writeChunkSize > len(p) { |
| 32 | + t.writeBuffer.Write(p) |
| 33 | + return len(p), nil |
| 34 | + } |
| 35 | + |
| 36 | + written := t.writeChunkSize |
| 37 | + t.writeBuffer.Write(p[:written]) |
| 38 | + return written, nil |
| 39 | +} |
| 40 | + |
| 41 | +func (t *testRW) Read(p []byte) (n int, err error) { return 0, nil } |
| 42 | +func (t *testRW) Close() error { return nil } |
| 43 | + |
| 44 | +func generateExpectedFrame(cmd byte, message string) []byte { |
| 45 | + buf := new(bytes.Buffer) |
| 46 | + |
| 47 | + // Handle empty message case |
| 48 | + if len(message) == 0 { |
| 49 | + return buf.Bytes() |
| 50 | + } |
| 51 | + |
| 52 | + // Init packet |
| 53 | + binary.Write(buf, binary.BigEndian, cid) |
| 54 | + buf.WriteByte(cmd) |
| 55 | + binary.Write(buf, binary.BigEndian, uint16(len(message))) |
| 56 | + |
| 57 | + // Split the message into init part and remaining |
| 58 | + remaining := message |
| 59 | + initData := remaining[:min(len(remaining), 57)] |
| 60 | + remaining = remaining[len(initData):] |
| 61 | + buf.WriteString(initData) |
| 62 | + if len(initData) < 57 { |
| 63 | + buf.Write(bytes.Repeat([]byte{0xee}, 57-len(initData))) |
| 64 | + } |
| 65 | + |
| 66 | + // Continue with remaining data for continuation frames |
| 67 | + for seq := 0; len(remaining) > 0; seq++ { |
| 68 | + cont := new(bytes.Buffer) |
| 69 | + binary.Write(cont, binary.BigEndian, cid) |
| 70 | + cont.WriteByte(uint8(seq)) |
| 71 | + |
| 72 | + contData := remaining[:min(len(remaining), 59)] |
| 73 | + cont.WriteString(contData) |
| 74 | + remaining = remaining[len(contData):] |
| 75 | + if cont.Len() < 64 { |
| 76 | + cont.Write(bytes.Repeat([]byte{0xee}, 64-cont.Len())) |
| 77 | + } |
| 78 | + buf.Write(cont.Bytes()) |
| 79 | + } |
| 80 | + |
| 81 | + return buf.Bytes() |
| 82 | +} |
| 83 | + |
| 84 | +func TestSendFrame(t *testing.T) { |
| 85 | + const testCMD = 0xab |
| 86 | + |
| 87 | + tests := []struct { |
| 88 | + name string |
| 89 | + input string |
| 90 | + chunkSize int |
| 91 | + expectedFrames string |
| 92 | + }{ |
| 93 | + { |
| 94 | + name: "empty message", |
| 95 | + input: "", |
| 96 | + chunkSize: 64, |
| 97 | + expectedFrames: "", |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "small message (exact init frame)", |
| 101 | + input: strings.Repeat("a", 57), |
| 102 | + chunkSize: 64, |
| 103 | + expectedFrames: strings.Repeat("a", 57), |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "message requiring one continuation frame", |
| 107 | + input: strings.Repeat("b", 58), |
| 108 | + chunkSize: 32, |
| 109 | + expectedFrames: strings.Repeat("b", 58), |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "multi-frame message with uneven writes", |
| 113 | + input: strings.Repeat("c", 500), |
| 114 | + chunkSize: 7, |
| 115 | + expectedFrames: strings.Repeat("c", 500), |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "boundary case with minimal writes", |
| 119 | + input: strings.Repeat("d", 64*3), |
| 120 | + chunkSize: 1, |
| 121 | + expectedFrames: strings.Repeat("d", 64*3), |
| 122 | + }, |
| 123 | + } |
| 124 | + |
| 125 | + for _, tt := range tests { |
| 126 | + t.Run(tt.name, func(t *testing.T) { |
| 127 | + rw := &testRW{writeChunkSize: tt.chunkSize} |
| 128 | + comm := NewCommunication(rw, testCMD) |
| 129 | + |
| 130 | + err := comm.SendFrame(tt.input) |
| 131 | + if err != nil { |
| 132 | + t.Fatalf("SendFrame failed: %v", err) |
| 133 | + } |
| 134 | + |
| 135 | + expected := generateExpectedFrame(testCMD, tt.input) |
| 136 | + actualCount := rw.writeBuffer.Len() / 64 * 64 |
| 137 | + actual := rw.writeBuffer.Bytes()[:actualCount] |
| 138 | + |
| 139 | + if !bytes.Equal(expected, actual) { |
| 140 | + t.Errorf("Frame mismatch\nExpected:\n% x\n\nGot:\n% x", expected, actual) |
| 141 | + } |
| 142 | + |
| 143 | + // Verify all padding bytes |
| 144 | + totalFrames := len(expected) / 64 |
| 145 | + for frameNum := range totalFrames { |
| 146 | + frameStart := frameNum * 64 |
| 147 | + frameEnd := (frameNum + 1) * 64 |
| 148 | + frame := expected[frameStart:frameEnd] |
| 149 | + |
| 150 | + // Check CID in every frame |
| 151 | + if binary.BigEndian.Uint32(frame[0:4]) != cid { |
| 152 | + t.Error("Invalid CID in frame") |
| 153 | + } |
| 154 | + |
| 155 | + if frameNum == 0 { |
| 156 | + // Init frame checks |
| 157 | + if frame[4] != testCMD { |
| 158 | + t.Error("Invalid command byte in init frame") |
| 159 | + } |
| 160 | + |
| 161 | + dataLength := binary.BigEndian.Uint16(frame[5:7]) |
| 162 | + if dataLength != uint16(len(tt.input)) { |
| 163 | + t.Error("Invalid data length in init frame") |
| 164 | + } |
| 165 | + } else if frame[4] != byte(frameNum-1) { |
| 166 | + // Continuation frame checks |
| 167 | + t.Error("Invalid sequence number in continuation frame") |
| 168 | + } |
| 169 | + |
| 170 | + // Verify padding bytes (last byte of filled data to end) |
| 171 | + dataEnd := len(tt.input) - (57 + (frameNum-1)*59) |
| 172 | + if frameNum > 0 && dataEnd < 0 { |
| 173 | + dataEnd = 0 |
| 174 | + } |
| 175 | + paddingStart := 7 + dataEnd |
| 176 | + if frameNum == 0 { |
| 177 | + paddingStart = 7 + min(len(tt.input), 57) |
| 178 | + } |
| 179 | + |
| 180 | + if paddingStart < 64 { |
| 181 | + paddingBytes := frame[paddingStart:] |
| 182 | + for _, b := range paddingBytes { |
| 183 | + if b != 0xee { |
| 184 | + t.Errorf("Invalid padding byte: %02x", b) |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + }) |
| 190 | + } |
| 191 | +} |
0 commit comments