Skip to content

Commit

Permalink
add a test to exercise more code paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregory Russell committed Nov 11, 2021
1 parent 05ab81b commit ddd009d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
8 changes: 4 additions & 4 deletions schema/pcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ type AlphaFields struct {
SynAckTime time.Time `bigquery:"syn_ack_time" json:"syn_ack_time"`
Packets int64 `bigquery:"packets" json:"packets"`
OptionCounts []int64 `bigquery:"option_counts" json:"option_counts"`
FirstECECount uint64 `bigquery:"first_ece_count" json:"first_ece_count"`
SecondECECount uint64 `bigquery:"second_ece_count" json:"second_ece_count"`
FirstRetransmits uint64 `bigquery:"first_retransmits" json:"first_retransmits"`
SecondRetransmits uint64 `bigquery:"second_retransmits" json:"second_retransmits"`
FirstECECount int64 `bigquery:"first_ece_count" json:"first_ece_count"`
SecondECECount int64 `bigquery:"second_ece_count" json:"second_ece_count"`
FirstRetransmits int64 `bigquery:"first_retransmits" json:"first_retransmits"`
SecondRetransmits int64 `bigquery:"second_retransmits" json:"second_retransmits"`
Sacks int64 `bigquery:"sacks" json:"sacks"`
TotalSrcSeq int64 `bigquery:"total_src_seq" json:"total_src_seq"`
TotalDstSeq int64 `bigquery:"total_dst_seq" json:"total_dst_seq"`
Expand Down
34 changes: 20 additions & 14 deletions tcp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ type sackBlock struct {
}

type Tracker struct {
initialized bool
errors uint32 // Number of errors encountered while parsing
packets uint32 // Number of calls to Seq function
seq uint32 // The last sequence number observed, not counting retransmits
synFin uint32 // zero, one or two, depending on whether SYN and FIN were sent
sent uint64 // actual bytes sent, including retransmits, but not SYN or FIN
retransmits uint64 // bytes retransmitted
initialized bool
errors uint32 // Number of errors encountered while parsing
packets uint32 // Number of calls to Seq function
seq uint32 // The last sequence number observed, not counting retransmits
synFin uint32 // zero, one or two, depending on whether SYN and FIN were sent
sent uint64 // actual bytes sent, including retransmits, but not SYN or FIN
retransmits uint64 // bytes retransmitted
retransPackets uint32 // number of retransmitted packets

sendUNA uint32 // greatest observed ack
acks uint32 // number of acks (from other side)
Expand Down Expand Up @@ -119,6 +120,7 @@ func (t *Tracker) Seq(clock uint32, length uint16, synFin bool) (int32, bool) {
if delta < 0 {
// DO NOT update w.seq or w.lastDataLength, as this is a retransmit
t.sent += uint64(length)
t.retransPackets++
t.retransmits += uint64(length)
return inflight, true
}
Expand Down Expand Up @@ -269,9 +271,13 @@ func (s *state) Option(port layers.TCPPort, opt layers.TCPOption) {
}
case layers.TCPOptionKindTimestamps:
case layers.TCPOptionKindWindowScale:
// TODO should this change after initialization?
sparse.Printf("%v WindowScale change %d -> %d\n", port, s.WindowScale, opt.OptionData[0])
s.WindowScale = opt.OptionData[0]
if len(opt.OptionData) != 1 {
info.Println("Invalid WindowScale option length", len(opt.OptionData))
} else {
// TODO should this change after initialization?
sparse.Printf("%v WindowScale change %d -> %d\n", port, s.WindowScale, opt.OptionData[0])
s.WindowScale = opt.OptionData[0]
}
default:
}
}
Expand Down Expand Up @@ -465,10 +471,10 @@ func (p *Parser) Parse(data []byte) (*schema.AlphaFields, error) {
info.Printf("%20s: %v\n", p.LeftState.SrcPort, p.LeftState.SeqTracker.Summary())
info.Printf("%20s: %v\n", p.RightState.SrcPort, p.RightState.SeqTracker.Summary())

alpha.FirstECECount = p.LeftState.ECECount
alpha.SecondECECount = p.RightState.ECECount
alpha.FirstRetransmits = p.LeftState.SeqTracker.retransmits
alpha.SecondRetransmits = p.RightState.SeqTracker.retransmits
alpha.FirstECECount = int64(p.LeftState.ECECount)
alpha.SecondECECount = int64(p.RightState.ECECount)
alpha.FirstRetransmits = int64(p.LeftState.SeqTracker.retransPackets)
alpha.SecondRetransmits = int64(p.RightState.SeqTracker.retransPackets)
// TODO update these names
alpha.TotalSrcSeq = int64(p.LeftState.SeqTracker.ByteCount())
alpha.TotalDstSeq = int64(p.RightState.SeqTracker.ByteCount())
Expand Down
32 changes: 32 additions & 0 deletions tcp/tcp_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tcp_test

import (
"io/ioutil"
"os"
"testing"

"github.com/m-lab/etl/tcp"
Expand Down Expand Up @@ -89,3 +91,33 @@ func TestTracker_SendNext(t *testing.T) {
})
}*/
}

// TestParse exercises a lot of code, on the ipv4 path. It only checks
// some basic stats, which are assumed to be correct, but not inspected.
func TestParse(t *testing.T) {
f, err := os.Open("testfiles/ndt-nnwk2_1611335823_00000000000C2DFE.pcap")
if err != nil {
t.Fatal(err)
}
data, err := ioutil.ReadAll(f)
if err != nil {
t.Fatal(err)
}
tcp := tcp.Parser{}
summary, err := tcp.Parse(data)
if err != nil {
t.Fatal(err)
}
if summary.Packets != 336 {
t.Errorf("Packets = %v, want %v", summary.Packets, 336)
}
if summary.FirstRetransmits != 11 {
t.Errorf("FirstRetransmits = %v, want %v", summary.FirstRetransmits, 11)
}
if summary.SecondRetransmits != 8 {
t.Errorf("SecondRetransmits = %v, want %v", summary.SecondRetransmits, 8)
}
if summary.TruncatedPackets != 0 {
t.Errorf("TruncatedPackets = %v, want %v", summary.TruncatedPackets, 0)
}
}
Binary file not shown.

0 comments on commit ddd009d

Please sign in to comment.