Skip to content

Commit

Permalink
Fix linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rg0now committed Nov 7, 2024
1 parent 6c5b370 commit a968c74
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 38 deletions.
2 changes: 1 addition & 1 deletion internal/client/periodic_timer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestPeriodicTimer(t *testing.T) {
time.Sleep(120 * time.Millisecond)
rt.Stop()
assert.False(t, rt.IsRunning(), "should not be running")
assert.Equal(t, 4, int(atomic.LoadUint64(&nCbs)), "should be called 4 times (actual: %d)", atomic.LoadUint64(&nCbs))
assert.Equal(t, 4, int(atomic.LoadUint64(&nCbs)), "should be called 4 times (actual: %d)", atomic.LoadUint64(&nCbs)) //nolint:gosec
})

t.Run("stop inside handler", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/proto/chandata.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (c *ChannelData) WriteHeader() {
_ = c.Raw[:channelDataHeaderSize]
binary.BigEndian.PutUint16(c.Raw[:channelDataNumberSize], uint16(c.Number))
binary.BigEndian.PutUint16(c.Raw[channelDataNumberSize:channelDataHeaderSize],
uint16(len(c.Data)),
uint16(len(c.Data)), //nolint:gosec
)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/server/nonce.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type NonceHash struct {
// Generate a nonce
func (n *NonceHash) Generate() (string, error) {
nonce := make([]byte, 8, nonceLength)
binary.BigEndian.PutUint64(nonce, uint64(time.Now().UnixMilli()))
binary.BigEndian.PutUint64(nonce, uint64(time.Now().UnixMilli())) //nolint:gosec

hash := hmac.New(sha256.New, n.key)
if _, err := hash.Write(nonce[:8]); err != nil {
Expand All @@ -55,7 +55,7 @@ func (n *NonceHash) Validate(nonce string) error {
return fmt.Errorf("%w: %v", errInvalidNonce, err) //nolint:errorlint
}

if ts := time.UnixMilli(int64(binary.BigEndian.Uint64(b))); time.Since(ts) > nonceLifetime {
if ts := time.UnixMilli(int64(binary.BigEndian.Uint64(b))); time.Since(ts) > nonceLifetime { //nolint:gosec
return errInvalidNonce
}

Expand Down
12 changes: 4 additions & 8 deletions internal/server/turn.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,11 @@ func handleAllocateRequest(r Request, m *stun.Message) error {
// client to a different server. The use of this error code and
// attribute follow the specification in [RFC5389].
lifetimeDuration := allocationLifeTime(m)
usernameAttr := &stun.Username{}
// Already checked realm/username in authenticateRequest
realmAttr := &stun.Realm{}
if err := realmAttr.GetFrom(m); err != nil {
return buildAndSendErr(r.Conn, r.SrcAddr, err, badRequestMsg...)
}
if err := usernameAttr.GetFrom(m); err != nil {
return buildAndSendErr(r.Conn, r.SrcAddr, err, badRequestMsg...)
}

_ = realmAttr.GetFrom(m)
usernameAttr := &stun.Username{}
_ = usernameAttr.GetFrom(m)
a, err := r.AllocationManager.CreateAllocation(
fiveTuple,
r.Conn,
Expand Down
2 changes: 1 addition & 1 deletion relay_address_generator_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (r *RelayAddressGeneratorPortRange) AllocatePacketConn(network string, requ
}

for try := 0; try < r.MaxRetries; try++ {
port := r.MinPort + uint16(r.Rand.Intn(int((r.MaxPort+1)-r.MinPort)))
port := r.MinPort + uint16(r.Rand.Intn(int((r.MaxPort+1)-r.MinPort))) //nolint:gosec

Check warning on line 87 in relay_address_generator_range.go

View check run for this annotation

Codecov / codecov/patch

relay_address_generator_range.go#L87

Added line #L87 was not covered by tests
conn, err := r.Net.ListenPacket(network, fmt.Sprintf("%s:%d", r.Address, port))
if err != nil {
continue
Expand Down
3 changes: 2 additions & 1 deletion server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ type EventHandlers struct {
// OnAllocationDeleted is called after an allocation has been removed.
OnAllocationDeleted func(srcAddr, dstAddr net.Addr, protocol, username, realm string)
// OnAllocationError is called when the readloop hdndling an allocation exits with an
// error wtih an error message.
// error with an error message.
OnAllocationError func(srcAddr, dstAddr net.Addr, protocol, message string)
// OnPermissionCreated is called after a new permission has been made to an IP address.
OnPermissionCreated func(srcAddr, dstAddr net.Addr, protocol, username, realm string, peer net.IP)
Expand Down Expand Up @@ -173,6 +173,7 @@ func genericEventHandler(handlers EventHandlers) allocation.EventHandler {
handlers.OnChannelDeleted(arg.SrcAddr, arg.DstAddr, arg.Protocol.String(),
arg.Username, arg.Realm, arg.PeerAddr, arg.ChannelNumber)
}
default:

Check warning on line 176 in server_config.go

View check run for this annotation

Codecov / codecov/patch

server_config.go#L176

Added line #L176 was not covered by tests
}
}
}
Expand Down
45 changes: 21 additions & 24 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
timeout = 200 * time.Millisecond
interval = 50 * time.Millisecond
stunAddr = "1.2.3.4:3478"
turnAddr = "1.2.3.4:3478"
)

func TestServer(t *testing.T) {
Expand Down Expand Up @@ -513,11 +514,11 @@ func buildVNetWithServerEventHandlers(handlers *EventHandlers) (*VNet, error) {
}, nil
}

func expectEvent(ch chan allocation.EventHandlerArgs, d time.Duration) (allocation.EventHandlerArgs, bool) {
func expectEvent(ch chan allocation.EventHandlerArgs) (allocation.EventHandlerArgs, bool) {
select {
case res := <-ch:
return res, true
case <-time.After(d):
case <-time.After(timeout):
return allocation.EventHandlerArgs{}, false
}
}
Expand Down Expand Up @@ -589,8 +590,6 @@ func TestServerVNet(t *testing.T) {
assert.NoError(t, lconn.Close())
}()

turnAddr := "1.2.3.4:3478"

log.Debug("creating a client.")
client, err := NewClient(&ClientConfig{
TURNServerAddr: turnAddr,
Expand All @@ -608,7 +607,7 @@ func TestServerVNet(t *testing.T) {
relayConn, err := client.Allocate()
assert.NoError(t, err, "should succeed")

event, ok := expectEvent(events, timeout)
event, ok := expectEvent(events)
assert.True(t, ok, "should receive an event")
assert.Equal(t, allocation.OnAuth, event.Type, "should receive an OnAuth event")
udpAddr, ok := event.SrcAddr.(*net.UDPAddr)
Expand All @@ -623,7 +622,7 @@ func TestServerVNet(t *testing.T) {
assert.Equal(t, "Allocate", event.Method)
assert.True(t, event.Verdict)

event, ok = expectEvent(events, timeout)
event, ok = expectEvent(events)
assert.True(t, ok, "should receive an event")
assert.Equal(t, allocation.OnAllocationCreated, event.Type, "should receive an OnAllocationCreated event")
udpAddr, ok = event.SrcAddr.(*net.UDPAddr)
Expand All @@ -649,7 +648,7 @@ func TestServerVNet(t *testing.T) {
_, err = relayConn.WriteTo([]byte("test"), peerAddr)
assert.NoError(t, err, "should succeed")

event, ok = expectEvent(events, timeout)
event, ok = expectEvent(events)
assert.True(t, ok, "should receive an event")
assert.Equal(t, allocation.OnAuth, event.Type, "should receive an OnAuth event")
udpAddr, ok = event.SrcAddr.(*net.UDPAddr)
Expand All @@ -664,7 +663,7 @@ func TestServerVNet(t *testing.T) {
assert.Equal(t, "CreatePermission", event.Method)
assert.True(t, event.Verdict)

event, ok = expectEvent(events, timeout)
event, ok = expectEvent(events)
assert.True(t, ok, "should receive an event")
assert.Equal(t, allocation.OnPermissionCreated, event.Type, "should receive an OnPermissionCreated event")
udpAddr, ok = event.SrcAddr.(*net.UDPAddr)
Expand All @@ -682,7 +681,7 @@ func TestServerVNet(t *testing.T) {
_, err = relayConn.WriteTo([]byte("test"), peerAddr)
assert.NoError(t, err, "should succeed")

event, ok = expectEvent(events, timeout)
event, ok = expectEvent(events)
assert.True(t, ok, "should receive an event")
assert.Equal(t, allocation.OnAuth, event.Type, "should receive an OnAuth event")
udpAddr, ok = event.SrcAddr.(*net.UDPAddr)
Expand All @@ -697,7 +696,7 @@ func TestServerVNet(t *testing.T) {
assert.Equal(t, "ChannelBind", event.Method)
assert.True(t, event.Verdict)

event, ok = expectEvent(events, timeout)
event, ok = expectEvent(events)
assert.True(t, ok, "should receive an event")
assert.Equal(t, allocation.OnChannelCreated, event.Type, "should receive an OnChannelCreated event")
udpAddr, ok = event.SrcAddr.(*net.UDPAddr)
Expand All @@ -724,7 +723,7 @@ func TestServerVNet(t *testing.T) {
log.Debug("Closing relay connection")
assert.NoError(t, relayConn.Close(), "relay conn close should succeed")

event, ok = expectEvent(events, timeout)
event, ok = expectEvent(events)
assert.True(t, ok, "should receive an event")
assert.Equal(t, allocation.OnAuth, event.Type, "should receive an OnAuth event")
udpAddr, ok = event.SrcAddr.(*net.UDPAddr)
Expand All @@ -739,7 +738,7 @@ func TestServerVNet(t *testing.T) {
assert.Equal(t, "Refresh", event.Method)
assert.True(t, event.Verdict)

event, ok = expectEvent(events, timeout)
event, ok = expectEvent(events)
assert.True(t, ok, "should receive an event")
assert.Equal(t, allocation.OnPermissionDeleted, event.Type, "should receive an OnPermissionDeleted event")
udpAddr, ok = event.SrcAddr.(*net.UDPAddr)
Expand All @@ -753,7 +752,7 @@ func TestServerVNet(t *testing.T) {
assert.Equal(t, "pion.ly", event.Realm)
assert.True(t, net.ParseIP("1.2.3.5").Equal(event.PeerIP))

event, ok = expectEvent(events, timeout)
event, ok = expectEvent(events)
assert.True(t, ok, "should receive an event")
assert.Equal(t, allocation.OnChannelDeleted, event.Type, "should receive an OnChannelDeleted event")
udpAddr, ok = event.SrcAddr.(*net.UDPAddr)
Expand All @@ -767,7 +766,7 @@ func TestServerVNet(t *testing.T) {
assert.Equal(t, "pion.ly", event.Realm)
assert.Equal(t, channelBind.Number, proto.ChannelNumber(event.ChannelNumber))

event, ok = expectEvent(events, timeout)
event, ok = expectEvent(events)
assert.True(t, ok, "should receive an event")
assert.Equal(t, allocation.OnAllocationDeleted, event.Type, "should receive an OnAllocationDeleted event")
udpAddr, ok = event.SrcAddr.(*net.UDPAddr)
Expand Down Expand Up @@ -818,8 +817,6 @@ func TestServerVNet(t *testing.T) {
assert.NoError(t, lconn.Close())
}()

turnAddr := "1.2.3.4:3478"

log.Debug("creating a client.")
client, err := NewClient(&ClientConfig{
TURNServerAddr: turnAddr,
Expand Down Expand Up @@ -857,8 +854,6 @@ func TestServerVNet(t *testing.T) {
assert.NoError(t, lconn.Close())
}()

turnAddr := "1.2.3.4:3478"

log.Debug("creating a client.")
client, err := NewClient(&ClientConfig{
TURNServerAddr: turnAddr,
Expand Down Expand Up @@ -906,15 +901,19 @@ func TestServerVNet(t *testing.T) {
},
OnChannelCreated: func(srcAddr, dstAddr net.Addr, protocol, username, realm string, peer net.Addr, channelNumber uint16) {
checkAllocation(srcAddr, dstAddr, protocol, username, realm)
assert.True(t, net.ParseIP("1.2.3.5").Equal(peerAddr.IP))
assert.Equal(t, 80, peerAddr.Port)
addr, ok := peer.(*net.UDPAddr)
assert.True(t, ok)
assert.True(t, addr.IP.Equal(peerAddr.IP))
assert.Equal(t, peerAddr.Port, addr.Port)
assert.NotZero(t, channelNumber)
channelCreated.Add(1)
},
OnChannelDeleted: func(srcAddr, dstAddr net.Addr, protocol, username, realm string, peer net.Addr, channelNumber uint16) {
checkAllocation(srcAddr, dstAddr, protocol, username, realm)
assert.True(t, net.ParseIP("1.2.3.5").Equal(peerAddr.IP))
assert.Equal(t, 80, peerAddr.Port)
addr, ok := peer.(*net.UDPAddr)
assert.True(t, ok)
assert.True(t, addr.IP.Equal(peerAddr.IP))
assert.Equal(t, peerAddr.Port, addr.Port)
assert.NotZero(t, channelNumber)
channelDeleted.Add(1)
},
Expand All @@ -932,8 +931,6 @@ func TestServerVNet(t *testing.T) {
assert.NoError(t, lconn.Close())
}()

turnAddr := "1.2.3.4:3478"

log.Debug("creating a client.")
client, err := NewClient(&ClientConfig{
TURNServerAddr: turnAddr,
Expand Down

0 comments on commit a968c74

Please sign in to comment.