]>
Commit | Line | Data |
---|---|---|
6988a057 JH |
1 | package hotline |
2 | ||
481631f6 JH |
3 | import ( |
4 | "bytes" | |
5 | "fmt" | |
6 | "github.com/stretchr/testify/assert" | |
7 | "io" | |
8 | "testing" | |
9 | ) | |
6988a057 | 10 | |
df2735b2 JH |
11 | func TestTransfer_Read(t *testing.T) { |
12 | type fields struct { | |
13 | Protocol [4]byte | |
14 | ReferenceNumber [4]byte | |
15 | DataSize [4]byte | |
16 | RSVD [4]byte | |
17 | } | |
18 | type args struct { | |
19 | b []byte | |
20 | } | |
21 | tests := []struct { | |
22 | name string | |
23 | fields fields | |
24 | args args | |
25 | want int | |
26 | wantErr bool | |
27 | }{ | |
28 | { | |
29 | name: "when b is a valid transfer", | |
30 | fields: fields{ | |
31 | Protocol: [4]byte{}, | |
32 | ReferenceNumber: [4]byte{}, | |
33 | DataSize: [4]byte{}, | |
34 | RSVD: [4]byte{}, | |
35 | }, | |
36 | args: args{ | |
37 | b: []byte{ | |
38 | 0x48, 0x54, 0x58, 0x46, | |
39 | 0x00, 0x00, 0x00, 0x01, | |
40 | 0x00, 0x00, 0x00, 0x02, | |
41 | 0x00, 0x00, 0x00, 0x00, | |
42 | }, | |
43 | }, | |
44 | want: 16, | |
45 | wantErr: false, | |
46 | }, | |
47 | { | |
48 | name: "when b contains invalid transfer protocol", | |
49 | fields: fields{ | |
50 | Protocol: [4]byte{}, | |
51 | ReferenceNumber: [4]byte{}, | |
52 | DataSize: [4]byte{}, | |
53 | RSVD: [4]byte{}, | |
54 | }, | |
55 | args: args{ | |
56 | b: []byte{ | |
57 | 0x11, 0x11, 0x11, 0x11, | |
58 | 0x00, 0x00, 0x00, 0x01, | |
59 | 0x00, 0x00, 0x00, 0x02, | |
60 | 0x00, 0x00, 0x00, 0x00, | |
61 | }, | |
62 | }, | |
63 | want: 0, | |
64 | wantErr: true, | |
65 | }, | |
66 | { | |
67 | name: "when b does not contain expected len of bytes", | |
68 | fields: fields{ | |
69 | Protocol: [4]byte{}, | |
70 | ReferenceNumber: [4]byte{}, | |
71 | DataSize: [4]byte{}, | |
72 | RSVD: [4]byte{}, | |
73 | }, | |
74 | args: args{ | |
75 | b: []byte{ | |
76 | 0x48, 0x54, 0x58, 0x46, | |
77 | 0x00, 0x00, 0x00, 0x01, | |
78 | 0x00, 0x00, 0x00, 0x02, | |
79 | 0x00, 0x00, 0x00, | |
80 | }, | |
81 | }, | |
82 | want: 0, | |
83 | wantErr: true, | |
84 | }, | |
85 | } | |
86 | for _, tt := range tests { | |
87 | t.Run(tt.name, func(t *testing.T) { | |
88 | tf := &transfer{ | |
89 | Protocol: tt.fields.Protocol, | |
90 | ReferenceNumber: tt.fields.ReferenceNumber, | |
91 | DataSize: tt.fields.DataSize, | |
92 | RSVD: tt.fields.RSVD, | |
93 | } | |
94 | got, err := tf.Write(tt.args.b) | |
95 | if (err != nil) != tt.wantErr { | |
96 | t.Errorf("Read() error = %v, wantErr %v", err, tt.wantErr) | |
97 | return | |
98 | } | |
99 | if got != tt.want { | |
100 | t.Errorf("Read() got = %v, want %v", got, tt.want) | |
101 | } | |
102 | }) | |
103 | } | |
6988a057 | 104 | } |
481631f6 JH |
105 | |
106 | func Test_receiveFile(t *testing.T) { | |
107 | type args struct { | |
108 | conn io.Reader | |
109 | } | |
110 | tests := []struct { | |
111 | name string | |
112 | args args | |
113 | wantTargetFile []byte | |
114 | wantResForkFile []byte | |
115 | wantErr assert.ErrorAssertionFunc | |
116 | }{ | |
117 | { | |
118 | name: "transfers file", | |
119 | args: args{ | |
120 | conn: func() io.Reader { | |
121 | testFile := flattenedFileObject{ | |
122 | FlatFileHeader: NewFlatFileHeader(), | |
123 | FlatFileInformationForkHeader: FlatFileInformationForkHeader{}, | |
124 | FlatFileInformationFork: NewFlatFileInformationFork("testfile.txt", make([]byte, 8)), | |
125 | FlatFileDataForkHeader: FlatFileDataForkHeader{ | |
126 | ForkType: [4]byte{0x4d, 0x41, 0x43, 0x52}, // DATA | |
127 | CompressionType: [4]byte{0, 0, 0, 0}, | |
128 | RSVD: [4]byte{0, 0, 0, 0}, | |
129 | DataSize: [4]byte{0x00, 0x00, 0x00, 0x03}, | |
130 | }, | |
131 | FileData: nil, | |
132 | } | |
133 | fakeFileData := []byte{1, 2, 3} | |
134 | b := testFile.BinaryMarshal() | |
135 | b = append(b, fakeFileData...) | |
136 | return bytes.NewReader(b) | |
137 | }(), | |
138 | }, | |
139 | wantTargetFile: []byte{1, 2, 3}, | |
140 | wantResForkFile: []byte(nil), | |
141 | ||
142 | wantErr: assert.NoError, | |
143 | }, | |
144 | } | |
145 | for _, tt := range tests { | |
146 | t.Run(tt.name, func(t *testing.T) { | |
147 | targetFile := &bytes.Buffer{} | |
148 | resForkFile := &bytes.Buffer{} | |
149 | err := receiveFile(tt.args.conn, targetFile, resForkFile) | |
150 | if !tt.wantErr(t, err, fmt.Sprintf("receiveFile(%v, %v, %v)", tt.args.conn, targetFile, resForkFile)) { | |
151 | return | |
152 | } | |
153 | ||
154 | assert.Equalf(t, tt.wantTargetFile, targetFile.Bytes(), "receiveFile(%v, %v, %v)", tt.args.conn, targetFile, resForkFile) | |
155 | assert.Equalf(t, tt.wantResForkFile, resForkFile.Bytes(), "receiveFile(%v, %v, %v)", tt.args.conn, targetFile, resForkFile) | |
156 | }) | |
157 | } | |
158 | } |