2022-07-16 04:01:02 +00:00
|
|
|
package sniff_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
"testing"
|
|
|
|
|
2024-07-07 07:45:50 +00:00
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2022-07-16 04:01:02 +00:00
|
|
|
"github.com/sagernet/sing-box/common/sniff"
|
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSniffSTUN(t *testing.T) {
|
2022-07-16 13:49:54 +00:00
|
|
|
t.Parallel()
|
2022-07-16 04:01:02 +00:00
|
|
|
packet, err := hex.DecodeString("000100002112a44224b1a025d0c180c484341306")
|
|
|
|
require.NoError(t, err)
|
2024-07-07 07:45:50 +00:00
|
|
|
var metadata adapter.InboundContext
|
|
|
|
err = sniff.STUNMessage(context.Background(), &metadata, packet)
|
2022-07-16 04:01:02 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, metadata.Protocol, C.ProtocolSTUN)
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuzzSniffSTUN(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data []byte) {
|
2024-07-07 07:45:50 +00:00
|
|
|
var metadata adapter.InboundContext
|
|
|
|
if err := sniff.STUNMessage(context.Background(), &metadata, data); err == nil {
|
2022-07-16 04:01:02 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|