Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions backend/wireguard/stats_outbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ func TestGetStatsOutboundsUsesInterfaceDeltaAndLink(t *testing.T) {
t.Fatalf("GetStats failed: %v", err)
}

if got := statValueByType(t, resp, "downlink"); got != 50 {
t.Fatalf("unexpected downlink delta: got %d want 50", got)
if got := statValueByType(t, resp, "uplink"); got != 50 {
t.Fatalf("unexpected uplink delta: got %d want 50", got)
}
if got := statValueByType(t, resp, "uplink"); got != 60 {
t.Fatalf("unexpected uplink delta: got %d want 60", got)
if got := statValueByType(t, resp, "downlink"); got != 60 {
t.Fatalf("unexpected downlink delta: got %d want 60", got)
}

for _, stat := range resp.GetStats() {
Expand Down Expand Up @@ -126,11 +126,11 @@ func TestGetStatsOutboundsResetBaseline(t *testing.T) {
if err != nil {
t.Fatalf("GetStats failed: %v", err)
}
if got := statValueByType(t, resp, "downlink"); got != 40 {
t.Fatalf("unexpected downlink delta before reset: got %d want 40", got)
if got := statValueByType(t, resp, "uplink"); got != 40 {
t.Fatalf("unexpected uplink delta before reset: got %d want 40", got)
}
if got := statValueByType(t, resp, "uplink"); got != 60 {
t.Fatalf("unexpected uplink delta before reset: got %d want 60", got)
if got := statValueByType(t, resp, "downlink"); got != 60 {
t.Fatalf("unexpected downlink delta before reset: got %d want 60", got)
}

resp, err = wg.GetStats(context.Background(), &common.StatRequest{
Expand All @@ -140,23 +140,23 @@ func TestGetStatsOutboundsResetBaseline(t *testing.T) {
if err != nil {
t.Fatalf("GetStats reset failed: %v", err)
}
if got := statValueByType(t, resp, "downlink"); got != 60 {
t.Fatalf("unexpected downlink delta on reset: got %d want 60", got)
if got := statValueByType(t, resp, "uplink"); got != 60 {
t.Fatalf("unexpected uplink delta on reset: got %d want 60", got)
}
if got := statValueByType(t, resp, "uplink"); got != 100 {
t.Fatalf("unexpected uplink delta on reset: got %d want 100", got)
if got := statValueByType(t, resp, "downlink"); got != 100 {
t.Fatalf("unexpected downlink delta on reset: got %d want 100", got)
}

resp, err = wg.GetStats(context.Background(), &common.StatRequest{Type: common.StatType_Outbounds})
if err != nil {
t.Fatalf("GetStats post-reset failed: %v", err)
}
if got := statValueByType(t, resp, "downlink"); got != 10 {
t.Fatalf("unexpected downlink delta after reset: got %d want 10", got)
}
if got := statValueByType(t, resp, "uplink"); got != 10 {
t.Fatalf("unexpected uplink delta after reset: got %d want 10", got)
}
if got := statValueByType(t, resp, "downlink"); got != 10 {
t.Fatalf("unexpected downlink delta after reset: got %d want 10", got)
}
}

func TestGetStatsOutboundsRebasesOnCounterRollback(t *testing.T) {
Expand Down Expand Up @@ -190,11 +190,11 @@ func TestGetStatsOutboundsRebasesOnCounterRollback(t *testing.T) {
if err != nil {
t.Fatalf("GetStats after rollback failed: %v", err)
}
if got := statValueByType(t, resp, "downlink"); got != 20 {
t.Fatalf("unexpected downlink delta after rollback rebase: got %d want 20", got)
if got := statValueByType(t, resp, "uplink"); got != 20 {
t.Fatalf("unexpected uplink delta after rollback rebase: got %d want 20", got)
}
if got := statValueByType(t, resp, "uplink"); got != 10 {
t.Fatalf("unexpected uplink delta after rollback rebase: got %d want 10", got)
if got := statValueByType(t, resp, "downlink"); got != 10 {
t.Fatalf("unexpected downlink delta after rollback rebase: got %d want 10", got)
}
}

Expand Down
29 changes: 29 additions & 0 deletions pkg/stats/direction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package stats

import (
"context"
"testing"
)

func TestBytesReceivedFromAWireGuardPeerAreReportedAsUplink(t *testing.T) {
tracker := New()
tracker.UpdateStatsBatch([]Sample{{PublicKey: "peer", Email: "user", Rx: 1000, Tx: 7}})

got := map[string]int64{}
for _, s := range tracker.GetUsersStats(context.Background(), false).GetStats() {
got[s.Type] = s.Value
}
if got["uplink"] != 1000 || got["downlink"] != 7 {
t.Fatalf("peer Rx=1000 (client upload) Tx=7 (client download) reported as uplink=%d downlink=%d", got["uplink"], got["downlink"])
}
}

func TestInterfaceBytesReceivedAreReportedAsUplink(t *testing.T) {
got := map[string]int64{}
for _, s := range BuildInterfaceStats("wg0", "", 1000, 7) {
got[s.Type] = s.Value
}
if got["uplink"] != 1000 || got["downlink"] != 7 {
t.Fatalf("interface rx=1000 tx=7 reported as uplink=%d downlink=%d", got["uplink"], got["downlink"])
}
}
8 changes: 4 additions & 4 deletions pkg/stats/interface_counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,20 @@ func buildDeltaStats(name, link string, rx, tx int64) []*common.Stat {
}

stats := make([]*common.Stat, 0, 2)
if tx > 0 {
if rx > 0 {
stats = append(stats, &common.Stat{
Name: name,
Type: "uplink",
Link: link,
Value: tx,
Value: rx,
})
}
if rx > 0 {
if tx > 0 {
stats = append(stats, &common.Stat{
Name: name,
Type: "downlink",
Link: link,
Value: rx,
Value: tx,
})
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/stats/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ func TestStatsTracker_GetStats(t *testing.T) {
}

for _, s := range resp.Stats {
if s.Type == "downlink" && s.Value != 1000 {
if s.Type == "uplink" && s.Value != 1000 {
t.Errorf("Expected rx=1000, got %d", s.Value)
}
if s.Type == "uplink" && s.Value != 2000 {
if s.Type == "downlink" && s.Value != 2000 {
t.Errorf("Expected tx=2000, got %d", s.Value)
}
}
Expand All @@ -48,10 +48,10 @@ func TestStatsTracker_GetStatsReset(t *testing.T) {

resp := tracker.GetStats(ctx, []string{"key1"}, true)
for _, s := range resp.Stats {
if s.Type == "downlink" && s.Value != 2000 {
if s.Type == "uplink" && s.Value != 2000 {
t.Errorf("Expected rx=2000, got %d", s.Value)
}
if s.Type == "uplink" && s.Value != 4000 {
if s.Type == "downlink" && s.Value != 4000 {
t.Errorf("Expected tx=4000, got %d", s.Value)
}
}
Expand Down Expand Up @@ -200,10 +200,10 @@ func TestStatsTracker_ReaddAfterDeletePreservesPendingDelta(t *testing.T) {

resp := tracker.GetStats(ctx, []string{"key1"}, false)
for _, s := range resp.Stats {
if s.Type == "downlink" && s.Value != 1100 {
if s.Type == "uplink" && s.Value != 1100 {
t.Errorf("Expected pending plus new rx=1100, got %d", s.Value)
}
if s.Type == "uplink" && s.Value != 2200 {
if s.Type == "downlink" && s.Value != 2200 {
t.Errorf("Expected pending plus new tx=2200, got %d", s.Value)
}
}
Expand All @@ -220,9 +220,9 @@ func TestStatsTracker_ReaddAfterDeletePreservesPendingDelta(t *testing.T) {
func pullTotals(tracker *Tracker) (rx, tx int64) {
for _, s := range tracker.GetUsersStats(context.Background(), true).Stats {
switch s.Type {
case "downlink":
rx += s.Value
case "uplink":
rx += s.Value
case "downlink":
tx += s.Value
}
}
Expand Down
Loading