]>
Commit | Line | Data |
---|---|---|
6988a057 JH |
1 | package hotline |
2 | ||
3 | import "testing" | |
4 | ||
df2735b2 JH |
5 | func TestTransfer_Read(t *testing.T) { |
6 | type fields struct { | |
7 | Protocol [4]byte | |
8 | ReferenceNumber [4]byte | |
9 | DataSize [4]byte | |
10 | RSVD [4]byte | |
11 | } | |
12 | type args struct { | |
13 | b []byte | |
14 | } | |
15 | tests := []struct { | |
16 | name string | |
17 | fields fields | |
18 | args args | |
19 | want int | |
20 | wantErr bool | |
21 | }{ | |
22 | { | |
23 | name: "when b is a valid transfer", | |
24 | fields: fields{ | |
25 | Protocol: [4]byte{}, | |
26 | ReferenceNumber: [4]byte{}, | |
27 | DataSize: [4]byte{}, | |
28 | RSVD: [4]byte{}, | |
29 | }, | |
30 | args: args{ | |
31 | b: []byte{ | |
32 | 0x48, 0x54, 0x58, 0x46, | |
33 | 0x00, 0x00, 0x00, 0x01, | |
34 | 0x00, 0x00, 0x00, 0x02, | |
35 | 0x00, 0x00, 0x00, 0x00, | |
36 | }, | |
37 | }, | |
38 | want: 16, | |
39 | wantErr: false, | |
40 | }, | |
41 | { | |
42 | name: "when b contains invalid transfer protocol", | |
43 | fields: fields{ | |
44 | Protocol: [4]byte{}, | |
45 | ReferenceNumber: [4]byte{}, | |
46 | DataSize: [4]byte{}, | |
47 | RSVD: [4]byte{}, | |
48 | }, | |
49 | args: args{ | |
50 | b: []byte{ | |
51 | 0x11, 0x11, 0x11, 0x11, | |
52 | 0x00, 0x00, 0x00, 0x01, | |
53 | 0x00, 0x00, 0x00, 0x02, | |
54 | 0x00, 0x00, 0x00, 0x00, | |
55 | }, | |
56 | }, | |
57 | want: 0, | |
58 | wantErr: true, | |
59 | }, | |
60 | { | |
61 | name: "when b does not contain expected len of bytes", | |
62 | fields: fields{ | |
63 | Protocol: [4]byte{}, | |
64 | ReferenceNumber: [4]byte{}, | |
65 | DataSize: [4]byte{}, | |
66 | RSVD: [4]byte{}, | |
67 | }, | |
68 | args: args{ | |
69 | b: []byte{ | |
70 | 0x48, 0x54, 0x58, 0x46, | |
71 | 0x00, 0x00, 0x00, 0x01, | |
72 | 0x00, 0x00, 0x00, 0x02, | |
73 | 0x00, 0x00, 0x00, | |
74 | }, | |
75 | }, | |
76 | want: 0, | |
77 | wantErr: true, | |
78 | }, | |
79 | } | |
80 | for _, tt := range tests { | |
81 | t.Run(tt.name, func(t *testing.T) { | |
82 | tf := &transfer{ | |
83 | Protocol: tt.fields.Protocol, | |
84 | ReferenceNumber: tt.fields.ReferenceNumber, | |
85 | DataSize: tt.fields.DataSize, | |
86 | RSVD: tt.fields.RSVD, | |
87 | } | |
88 | got, err := tf.Write(tt.args.b) | |
89 | if (err != nil) != tt.wantErr { | |
90 | t.Errorf("Read() error = %v, wantErr %v", err, tt.wantErr) | |
91 | return | |
92 | } | |
93 | if got != tt.want { | |
94 | t.Errorf("Read() got = %v, want %v", got, tt.want) | |
95 | } | |
96 | }) | |
97 | } | |
6988a057 | 98 | } |