Skip to content

Commit

Permalink
test(metadata): add unit testing for leech.readUmMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
tgragnato committed Sep 15, 2024
1 parent ad7ef69 commit 8f7d5d9
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions metadata/leech_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,53 @@ func TestReadExMessage(t *testing.T) {
})
}
}

func TestReadUmMessage(t *testing.T) {
t.Parallel()

tests := []struct {
name string
input []byte
expected []byte
expectedError bool
}{
{
name: "Valid ut_metadata message",
input: append([]byte{0, 0, 0, 7}, []byte{20, 1, 'h', 'e', 'l', 'l', 'o'}...),
expected: []byte{20, 1, 'h', 'e', 'l', 'l', 'o'},
expectedError: false,
},
{
name: "Non-ut_metadata extension message",
input: append([]byte{0, 0, 0, 7}, []byte{20, 2, 'h', 'e', 'l', 'l', 'o'}...),
expected: nil,
expectedError: true,
},
{
name: "Incomplete ut_metadata message",
input: []byte{0, 0, 0, 7, 20, 1, 'h'},
expected: nil,
expectedError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
peer1, peer2 := net.Pipe()
leech := &Leech{conn: peer1}

go func() {
peer2.Write(tt.input)

Check failure on line 323 in metadata/leech_test.go

View workflow job for this annotation

GitHub Actions / Build (1.22)

Error return value of `peer2.Write` is not checked (errcheck)
peer2.Close()
}()

result, err := leech.readUmMessage()
if (err != nil) != tt.expectedError {
t.Errorf("Expected error: %v, got: %v", tt.expectedError, err)
}
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("Expected: %v, got: %v", tt.expected, result)
}
})
}
}

0 comments on commit 8f7d5d9

Please sign in to comment.