| 1 | package sshfx |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | type testExtendedData struct { |
| 9 | value uint8 |
| 10 | } |
| 11 | |
| 12 | func (d *testExtendedData) MarshalBinary() ([]byte, error) { |
| 13 | buf := NewBuffer(make([]byte, 0, 4)) |
| 14 | |
| 15 | buf.AppendUint8(d.value ^ 0x2a) |
| 16 | |
| 17 | return buf.Bytes(), nil |
| 18 | } |
| 19 | |
| 20 | func (d *testExtendedData) UnmarshalBinary(data []byte) error { |
| 21 | buf := NewBuffer(data) |
| 22 | |
| 23 | v := buf.ConsumeUint8() |
| 24 | if buf.Err != nil { |
| 25 | return buf.Err |
| 26 | } |
| 27 | |
| 28 | d.value = v ^ 0x2a |
| 29 | |
| 30 | return nil |
| 31 | } |
| 32 | |
| 33 | var _ Packet = &ExtendedPacket{} |
| 34 | |
| 35 | func TestExtendedPacketNoData(t *testing.T) { |
| 36 | const ( |
| 37 | id = 42 |
| 38 | extendedRequest = "foo@example" |
| 39 | ) |
| 40 | |
| 41 | p := &ExtendedPacket{ |
| 42 | ExtendedRequest: extendedRequest, |
| 43 | } |
| 44 | |
| 45 | buf, err := ComposePacket(p.MarshalPacket(id, nil)) |
| 46 | if err != nil { |
| 47 | t.Fatal("unexpected error:", err) |
| 48 | } |
| 49 | |
| 50 | want := []byte{ |
| 51 | 0x00, 0x00, 0x00, 20, |
| 52 | 200, |
| 53 | 0x00, 0x00, 0x00, 42, |
| 54 | 0x00, 0x00, 0x00, 11, 'f', 'o', 'o', '@', 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 55 | } |
| 56 | |
| 57 | if !bytes.Equal(buf, want) { |
| 58 | t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want) |
| 59 | } |
| 60 | |
| 61 | *p = ExtendedPacket{} |
| 62 | |
| 63 | |
| 64 | if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { |
| 65 | t.Fatal("unexpected error:", err) |
| 66 | } |
| 67 | |
| 68 | if p.ExtendedRequest != extendedRequest { |
| 69 | t.Errorf("UnmarshalPacketBody(): ExtendedRequest was %q, but expected %q", p.ExtendedRequest, extendedRequest) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestExtendedPacketTestData(t *testing.T) { |
| 74 | const ( |
| 75 | id = 42 |
| 76 | extendedRequest = "foo@example" |
| 77 | textValue = 13 |
| 78 | ) |
| 79 | |
| 80 | const value = 13 |
| 81 | |
| 82 | p := &ExtendedPacket{ |
| 83 | ExtendedRequest: extendedRequest, |
| 84 | Data: &testExtendedData{ |
| 85 | value: textValue, |
| 86 | }, |
| 87 | } |
| 88 | |
| 89 | buf, err := ComposePacket(p.MarshalPacket(id, nil)) |
| 90 | if err != nil { |
| 91 | t.Fatal("unexpected error:", err) |
| 92 | } |
| 93 | |
| 94 | want := []byte{ |
| 95 | 0x00, 0x00, 0x00, 21, |
| 96 | 200, |
| 97 | 0x00, 0x00, 0x00, 42, |
| 98 | 0x00, 0x00, 0x00, 11, 'f', 'o', 'o', '@', 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 99 | 0x27, |
| 100 | } |
| 101 | |
| 102 | if !bytes.Equal(buf, want) { |
| 103 | t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want) |
| 104 | } |
| 105 | |
| 106 | *p = ExtendedPacket{ |
| 107 | Data: new(testExtendedData), |
| 108 | } |
| 109 | |
| 110 | |
| 111 | if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { |
| 112 | t.Fatal("unexpected error:", err) |
| 113 | } |
| 114 | |
| 115 | if p.ExtendedRequest != extendedRequest { |
| 116 | t.Errorf("UnmarshalPacketBody(): ExtendedRequest was %q, but expected %q", p.ExtendedRequest, extendedRequest) |
| 117 | } |
| 118 | |
| 119 | if buf, ok := p.Data.(*testExtendedData); !ok { |
| 120 | t.Errorf("UnmarshalPacketBody(): Data was type %T, but expected %T", p.Data, buf) |
| 121 | |
| 122 | } else if buf.value != value { |
| 123 | t.Errorf("UnmarshalPacketBody(): Data.value was %#x, but expected %#x", buf.value, value) |
| 124 | } |
| 125 | |
| 126 | *p = ExtendedPacket{} |
| 127 | |
| 128 | |
| 129 | if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { |
| 130 | t.Fatal("unexpected error:", err) |
| 131 | } |
| 132 | |
| 133 | if p.ExtendedRequest != extendedRequest { |
| 134 | t.Errorf("UnmarshalPacketBody(): ExtendedRequest was %q, but expected %q", p.ExtendedRequest, extendedRequest) |
| 135 | } |
| 136 | |
| 137 | wantBuffer := []byte{0x27} |
| 138 | |
| 139 | if buf, ok := p.Data.(*Buffer); !ok { |
| 140 | t.Errorf("UnmarshalPacketBody(): Data was type %T, but expected %T", p.Data, buf) |
| 141 | |
| 142 | } else if !bytes.Equal(buf.b, wantBuffer) { |
| 143 | t.Errorf("UnmarshalPacketBody(): Data was %X, but expected %X", buf.b, wantBuffer) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | var _ Packet = &ExtendedReplyPacket{} |
| 148 | |
| 149 | func TestExtendedReplyNoData(t *testing.T) { |
| 150 | const ( |
| 151 | id = 42 |
| 152 | ) |
| 153 | |
| 154 | p := &ExtendedReplyPacket{} |
| 155 | |
| 156 | buf, err := ComposePacket(p.MarshalPacket(id, nil)) |
| 157 | if err != nil { |
| 158 | t.Fatal("unexpected error:", err) |
| 159 | } |
| 160 | |
| 161 | want := []byte{ |
| 162 | 0x00, 0x00, 0x00, 5, |
| 163 | 201, |
| 164 | 0x00, 0x00, 0x00, 42, |
| 165 | } |
| 166 | |
| 167 | if !bytes.Equal(buf, want) { |
| 168 | t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want) |
| 169 | } |
| 170 | |
| 171 | *p = ExtendedReplyPacket{} |
| 172 | |
| 173 | |
| 174 | if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { |
| 175 | t.Fatal("unexpected error:", err) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | func TestExtendedReplyPacketTestData(t *testing.T) { |
| 180 | const ( |
| 181 | id = 42 |
| 182 | textValue = 13 |
| 183 | ) |
| 184 | |
| 185 | const value = 13 |
| 186 | |
| 187 | p := &ExtendedReplyPacket{ |
| 188 | Data: &testExtendedData{ |
| 189 | value: textValue, |
| 190 | }, |
| 191 | } |
| 192 | |
| 193 | buf, err := ComposePacket(p.MarshalPacket(id, nil)) |
| 194 | if err != nil { |
| 195 | t.Fatal("unexpected error:", err) |
| 196 | } |
| 197 | |
| 198 | want := []byte{ |
| 199 | 0x00, 0x00, 0x00, 6, |
| 200 | 201, |
| 201 | 0x00, 0x00, 0x00, 42, |
| 202 | 0x27, |
| 203 | } |
| 204 | |
| 205 | if !bytes.Equal(buf, want) { |
| 206 | t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want) |
| 207 | } |
| 208 | |
| 209 | *p = ExtendedReplyPacket{ |
| 210 | Data: new(testExtendedData), |
| 211 | } |
| 212 | |
| 213 | |
| 214 | if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { |
| 215 | t.Fatal("unexpected error:", err) |
| 216 | } |
| 217 | |
| 218 | if buf, ok := p.Data.(*testExtendedData); !ok { |
| 219 | t.Errorf("UnmarshalPacketBody(): Data was type %T, but expected %T", p.Data, buf) |
| 220 | |
| 221 | } else if buf.value != value { |
| 222 | t.Errorf("UnmarshalPacketBody(): Data.value was %#x, but expected %#x", buf.value, value) |
| 223 | } |
| 224 | |
| 225 | *p = ExtendedReplyPacket{} |
| 226 | |
| 227 | |
| 228 | if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { |
| 229 | t.Fatal("unexpected error:", err) |
| 230 | } |
| 231 | |
| 232 | wantBuffer := []byte{0x27} |
| 233 | |
| 234 | if buf, ok := p.Data.(*Buffer); !ok { |
| 235 | t.Errorf("UnmarshalPacketBody(): Data was type %T, but expected %T", p.Data, buf) |
| 236 | |
| 237 | } else if !bytes.Equal(buf.b, wantBuffer) { |
| 238 | t.Errorf("UnmarshalPacketBody(): Data was %X, but expected %X", buf.b, wantBuffer) |
| 239 | } |
| 240 | } |