|
| 1 | +package probes |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func tsMetadata(t *testing.T, m map[string]any) json.RawMessage { |
| 9 | + t.Helper() |
| 10 | + b, err := json.Marshal(m) |
| 11 | + if err != nil { |
| 12 | + t.Fatalf("marshal metadata: %v", err) |
| 13 | + } |
| 14 | + return b |
| 15 | +} |
| 16 | + |
| 17 | +func TestExtractVoIPOptionsParsesBidirectionalServerFields(t *testing.T) { |
| 18 | + md := tsMetadata(t, map[string]any{ |
| 19 | + "bidirectional": true, |
| 20 | + "trafficsim": map[string]any{ |
| 21 | + "bidirectional_server": true, |
| 22 | + "client_probe_id": float64(42), |
| 23 | + "client_agent_id": float64(7), |
| 24 | + "voip_mode": true, |
| 25 | + "interval_ms": float64(20), |
| 26 | + "dscp": float64(46), |
| 27 | + }, |
| 28 | + }) |
| 29 | + |
| 30 | + opts := extractVoIPOptions(md) |
| 31 | + |
| 32 | + if !opts.Bidirectional { |
| 33 | + t.Error("Bidirectional not parsed from top-level flag") |
| 34 | + } |
| 35 | + if !opts.BidirectionalServer { |
| 36 | + t.Error("BidirectionalServer not parsed") |
| 37 | + } |
| 38 | + if opts.ClientProbeID != 42 { |
| 39 | + t.Errorf("ClientProbeID = %d, want 42", opts.ClientProbeID) |
| 40 | + } |
| 41 | + if opts.ClientAgentID != 7 { |
| 42 | + t.Errorf("ClientAgentID = %d, want 7", opts.ClientAgentID) |
| 43 | + } |
| 44 | + if !opts.VoIPMode || opts.IntervalMs != 20 || opts.DSCPValue != 46 { |
| 45 | + t.Errorf("VoIP settings not parsed: %+v", opts) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// Regression: the legacy bidirectional_receiver marker was parsed by NewTrafficSim |
| 50 | +// but NOT by extractVoIPOptions, so GetClientProbeForAgent could never match |
| 51 | +// legacy receiver probes. |
| 52 | +func TestExtractVoIPOptionsParsesLegacyReceiver(t *testing.T) { |
| 53 | + md := tsMetadata(t, map[string]any{ |
| 54 | + "trafficsim": map[string]any{ |
| 55 | + "bidirectional_receiver": true, |
| 56 | + "client_probe_id": float64(42), |
| 57 | + "client_agent_id": float64(7), |
| 58 | + }, |
| 59 | + }) |
| 60 | + |
| 61 | + opts := extractVoIPOptions(md) |
| 62 | + if !opts.BidirectionalReceiver { |
| 63 | + t.Error("legacy bidirectional_receiver not parsed") |
| 64 | + } |
| 65 | + if opts.ClientProbeID != 42 || opts.ClientAgentID != 7 { |
| 66 | + t.Errorf("client ids not parsed: %+v", opts) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// Regression: NewTrafficSim infers VoIP mode from interval_ms <= 50, but |
| 71 | +// extractVoIPOptions didn't — the server's reverse traffic paced differently |
| 72 | +// from the client's forward traffic. |
| 73 | +func TestExtractVoIPOptionsInfersVoIPFromFastInterval(t *testing.T) { |
| 74 | + md := tsMetadata(t, map[string]any{ |
| 75 | + "trafficsim": map[string]any{ |
| 76 | + "interval_ms": float64(20), // fast interval, voip_mode NOT set |
| 77 | + }, |
| 78 | + }) |
| 79 | + |
| 80 | + opts := extractVoIPOptions(md) |
| 81 | + if !opts.VoIPMode { |
| 82 | + t.Error("VoIP mode not inferred from interval_ms <= 50") |
| 83 | + } |
| 84 | + if opts.PayloadSize != VoIPPayloadSize { |
| 85 | + t.Errorf("PayloadSize = %d, want VoIP default %d", opts.PayloadSize, VoIPPayloadSize) |
| 86 | + } |
| 87 | + if opts.PacketsPerSec != 1000/20 { |
| 88 | + t.Errorf("PacketsPerSec = %d, want %d", opts.PacketsPerSec, 1000/20) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestExtractVoIPOptionsEmptyMetadataDefaults(t *testing.T) { |
| 93 | + opts := extractVoIPOptions(nil) |
| 94 | + if opts.VoIPMode || opts.Bidirectional || opts.BidirectionalServer || opts.BidirectionalReceiver { |
| 95 | + t.Errorf("unexpected flags from empty metadata: %+v", opts) |
| 96 | + } |
| 97 | + if opts.IntervalMs != TrafficSimDataInterval { |
| 98 | + t.Errorf("IntervalMs = %d, want default %d", opts.IntervalMs, TrafficSimDataInterval) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// Regression: the dynamically generated bidirectional server probe is virtual |
| 103 | +// (ID=0); the client's real probe ID rides in metadata. Using the probe's own |
| 104 | +// ID set ClientProbeID=0, which every reverse-path gate treats as "disabled". |
| 105 | +func TestResolveClientProbeID(t *testing.T) { |
| 106 | + cases := []struct { |
| 107 | + name string |
| 108 | + probe Probe |
| 109 | + opts TrafficSimOptions |
| 110 | + want uint |
| 111 | + }{ |
| 112 | + { |
| 113 | + name: "virtual bidir server probe uses metadata client_probe_id", |
| 114 | + probe: Probe{ID: 0, Server: true}, |
| 115 | + opts: TrafficSimOptions{BidirectionalServer: true, ClientProbeID: 42}, |
| 116 | + want: 42, |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "legacy receiver probe uses metadata client_probe_id", |
| 120 | + probe: Probe{ID: 9, Server: true}, |
| 121 | + opts: TrafficSimOptions{BidirectionalReceiver: true, ClientProbeID: 42}, |
| 122 | + want: 42, |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "real client probe uses its own ID", |
| 126 | + probe: Probe{ID: 7}, |
| 127 | + opts: TrafficSimOptions{Bidirectional: true}, |
| 128 | + want: 7, |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "server probe without metadata id falls back to own ID", |
| 132 | + probe: Probe{ID: 5, Server: true}, |
| 133 | + opts: TrafficSimOptions{BidirectionalServer: true}, |
| 134 | + want: 5, |
| 135 | + }, |
| 136 | + } |
| 137 | + for _, tc := range cases { |
| 138 | + t.Run(tc.name, func(t *testing.T) { |
| 139 | + if got := resolveClientProbeID(&tc.probe, tc.opts); got != tc.want { |
| 140 | + t.Errorf("resolveClientProbeID() = %d, want %d", got, tc.want) |
| 141 | + } |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +func TestShouldEnableBidirectional(t *testing.T) { |
| 147 | + bidirMD := tsMetadata(t, map[string]any{"bidirectional": true}) |
| 148 | + |
| 149 | + if !shouldEnableBidirectional(&Probe{Metadata: bidirMD}) { |
| 150 | + t.Error("metadata bidirectional flag not honored (new format)") |
| 151 | + } |
| 152 | + if !shouldEnableBidirectional(&Probe{ |
| 153 | + Targets: []ProbeTarget{{Target: "1.2.3.4:bidir"}}, |
| 154 | + }) { |
| 155 | + t.Error(":bidir target suffix not honored (legacy format)") |
| 156 | + } |
| 157 | + if shouldEnableBidirectional(&Probe{ |
| 158 | + Targets: []ProbeTarget{{Target: "1.2.3.4:5000"}}, |
| 159 | + }) { |
| 160 | + t.Error("plain probe should not enable bidirectional") |
| 161 | + } |
| 162 | + if shouldEnableBidirectional(nil) { |
| 163 | + t.Error("nil probe should not enable bidirectional") |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +func TestGetClientProbeForAgent(t *testing.T) { |
| 168 | + clientAgent := uint(5) |
| 169 | + |
| 170 | + bidirClientMD := tsMetadata(t, map[string]any{ |
| 171 | + "bidirectional": true, |
| 172 | + "trafficsim": map[string]any{"bidirectional": true}, |
| 173 | + }) |
| 174 | + bidirServerMD := tsMetadata(t, map[string]any{ |
| 175 | + "bidirectional": true, |
| 176 | + "trafficsim": map[string]any{ |
| 177 | + "bidirectional_server": true, |
| 178 | + "client_probe_id": float64(42), |
| 179 | + "client_agent_id": float64(clientAgent), |
| 180 | + }, |
| 181 | + }) |
| 182 | + |
| 183 | + t.Run("matches local client probe targeting the agent (mutual servers)", func(t *testing.T) { |
| 184 | + ts := &TrafficSim{} |
| 185 | + ts.SetAllProbes([]Probe{{ |
| 186 | + ID: 7, |
| 187 | + Type: ProbeType_TRAFFICSIM, |
| 188 | + Metadata: bidirClientMD, |
| 189 | + Targets: []ProbeTarget{{Target: "1.2.3.4:5000", AgentID: &clientAgent}}, |
| 190 | + }}) |
| 191 | + p := ts.GetClientProbeForAgent(clientAgent) |
| 192 | + if p == nil || p.ID != 7 { |
| 193 | + t.Fatalf("got %+v, want client probe 7", p) |
| 194 | + } |
| 195 | + }) |
| 196 | + |
| 197 | + t.Run("matches virtual bidirectional server probe by client_agent_id", func(t *testing.T) { |
| 198 | + ts := &TrafficSim{} |
| 199 | + ts.SetAllProbes([]Probe{{ |
| 200 | + ID: 0, |
| 201 | + Type: ProbeType_TRAFFICSIM, |
| 202 | + Server: true, |
| 203 | + Metadata: bidirServerMD, |
| 204 | + Targets: []ProbeTarget{{Target: "0.0.0.0:5000", AgentID: &clientAgent}}, |
| 205 | + }}) |
| 206 | + p := ts.GetClientProbeForAgent(clientAgent) |
| 207 | + if p == nil { |
| 208 | + t.Fatal("bidirectional server probe not matched") |
| 209 | + } |
| 210 | + if got := resolveClientProbeID(p, extractVoIPOptions(p.Metadata)); got != 42 { |
| 211 | + t.Errorf("resolved client probe ID = %d, want 42 from metadata", got) |
| 212 | + } |
| 213 | + }) |
| 214 | + |
| 215 | + t.Run("no match for unrelated agent or non-bidirectional probe", func(t *testing.T) { |
| 216 | + ts := &TrafficSim{} |
| 217 | + ts.SetAllProbes([]Probe{{ |
| 218 | + ID: 7, |
| 219 | + Type: ProbeType_TRAFFICSIM, |
| 220 | + Targets: []ProbeTarget{{Target: "1.2.3.4:5000", AgentID: &clientAgent}}, |
| 221 | + // no bidirectional metadata |
| 222 | + }}) |
| 223 | + if p := ts.GetClientProbeForAgent(clientAgent); p != nil { |
| 224 | + t.Errorf("non-bidirectional client probe should not match, got %+v", p) |
| 225 | + } |
| 226 | + if p := ts.GetClientProbeForAgent(999); p != nil { |
| 227 | + t.Errorf("unrelated agent should not match, got %+v", p) |
| 228 | + } |
| 229 | + }) |
| 230 | +} |
0 commit comments