-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test for out of bounds. In the wild this should not happen for well-behaved servers. But there are ill-behaved servers too.
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Package tracex implements a handshake tracer that can be passed to the TUN constructor to | ||
// observe handshake events. | ||
package tracex | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ooni/minivpn/internal/model" | ||
) | ||
|
||
func Test_maybeAddTagsFromPacket(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
packetPayload []byte | ||
expectedTags []string | ||
}{ | ||
{ | ||
name: "Empty payload", | ||
packetPayload: []byte{}, | ||
expectedTags: []string{}, | ||
}, | ||
{ | ||
name: "Payload too short", | ||
packetPayload: []byte{0x16, 0x00, 0x00, 0x00, 0x00}, | ||
expectedTags: []string{}, | ||
}, | ||
{ | ||
name: "Client Hello", | ||
packetPayload: []byte{0x16, 0x00, 0x00, 0x00, 0x00, 0x01}, | ||
expectedTags: []string{"client_hello"}, | ||
}, | ||
{ | ||
name: "Server Hello", | ||
packetPayload: []byte{0x16, 0x00, 0x00, 0x00, 0x00, 0x02}, | ||
expectedTags: []string{"server_hello"}, | ||
}, | ||
{ | ||
name: "No tag matching", | ||
packetPayload: []byte{0x17, 0x00, 0x00, 0x00, 0x00, 0x01}, | ||
expectedTags: []string{}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
event := &Event{Tags: []string{}} | ||
packet := &model.Packet{Payload: tt.packetPayload} | ||
|
||
maybeAddTagsFromPacket(event, packet) | ||
|
||
// Check if tags are as expected | ||
if len(event.Tags) != len(tt.expectedTags) { | ||
t.Fatalf("Expected %v tags, but got %v", len(tt.expectedTags), len(event.Tags)) | ||
} | ||
|
||
for i, tag := range tt.expectedTags { | ||
if event.Tags[i] != tag { | ||
t.Errorf("Expected tag %v, but got %v", tag, event.Tags[i]) | ||
} | ||
} | ||
}) | ||
} | ||
} |