Skip to content

Commit

Permalink
address comments from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
ainghazal committed Feb 7, 2024
1 parent 20deba9 commit 8a042ba
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
7 changes: 1 addition & 6 deletions internal/model/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,6 @@ func (p *Packet) Log(logger Logger, direction int) {
return
}

payloadLen := 0
if p.Payload != nil {
payloadLen = len(p.Payload)
}

logger.Debugf(
"%s %s {id=%d, acks=%v} localID=%x remoteID=%x [%d bytes]",
dir,
Expand All @@ -365,6 +360,6 @@ func (p *Packet) Log(logger Logger, direction int) {
p.ACKs,
p.LocalSessionID,
p.RemoteSessionID,
payloadLen,
len(p.Payload),
)
}
5 changes: 2 additions & 3 deletions internal/reliabletransport/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/apex/log"
"github.com/ooni/minivpn/internal/bytesx"
"github.com/ooni/minivpn/internal/model"
"github.com/ooni/minivpn/internal/runtimex"
"github.com/ooni/minivpn/internal/session"
"github.com/ooni/minivpn/internal/workers"
)
Expand All @@ -16,9 +17,7 @@ import (
func initManagers() (*workers.Manager, *session.Manager) {
w := workers.NewManager(log.Log)
s, err := session.NewManager(log.Log)
if err != nil {
panic(err)
}
runtimex.PanicOnError(err, "cannot create session manager")
return w, s
}

Expand Down
2 changes: 1 addition & 1 deletion internal/reliabletransport/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (ws *workersState) moveUpWorker() {
// We should be able to deterministically test how this affects the state machine.

// drop a packet that is not for our session
if !bytes.Equal([]byte(packet.RemoteSessionID[:]), []byte(ws.sessionManager.LocalSessionID())) {
if !bytes.Equal(packet.RemoteSessionID[:], ws.sessionManager.LocalSessionID()) {
ws.logger.Warnf(
"%s: packet with invalid RemoteSessionID: expected %x; got %x",
workerName,
Expand Down
10 changes: 10 additions & 0 deletions internal/runtimex/runtimex.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Package runtimex contains [runtime] extensions.
package runtimex

import "fmt"

// PanicIfFalse calls panic with the given message if the given statement is false.
func PanicIfFalse(stmt bool, message interface{}) {
if !stmt {
Expand All @@ -17,3 +19,11 @@ func PanicIfTrue(stmt bool, message interface{}) {

// Assert calls panic with the given message if the given statement is false.
var Assert = PanicIfFalse

// PanicOnError calls panic() if err is not nil. The type passed to panic
// is an error type wrapping the original error.
func PanicOnError(err error, message string) {
if err != nil {
panic(fmt.Errorf("%s: %w", message, err))
}
}
4 changes: 3 additions & 1 deletion internal/vpntest/packetio.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,17 @@ type Witness struct {
reader *PacketReader
}

// NewWitness constructs a Witness from a [PacketReader].
func NewWitness(r *PacketReader) *Witness {
return &Witness{r}
}

// Log returns the packet log from the internal reader this witness uses.
func (w *Witness) Log() PacketLog {
return w.reader.Log()
}

// VerifyACKs tells the underlying reader to wait for a given number of acks,
// VerifyNumberOfACKs tells the underlying reader to wait for a given number of acks,
// returns true if we have the same number of acks.
func (w *Witness) VerifyNumberOfACKs(start, total int, t time.Time) bool {
w.reader.WaitForNumberOfACKs(total, t)
Expand Down

0 comments on commit 8a042ba

Please sign in to comment.