Skip to content

Commit 9c7d6da

Browse files
committed
add e2e test for dst-to-src packet capture
Signed-off-by: Aryan Bakliwal <[email protected]>
1 parent ba32aa4 commit 9c7d6da

File tree

1 file changed

+77
-32
lines changed

1 file changed

+77
-32
lines changed

test/e2e/packetcapture_test.go

+77-32
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,46 @@ func testPacketCaptureBasic(t *testing.T, data *TestData, sftpServerIP string, p
424424
},
425425
},
426426
},
427+
{
428+
name: "ipv4-udp-dst-to-src",
429+
ipVersion: 4,
430+
pc: getPacketCaptureCR(
431+
"ipv4-udp-dst-to-src",
432+
udpServerPodName,
433+
&crdv1alpha1.Packet{
434+
Protocol: &udpProto,
435+
IPFamily: v1.IPv4Protocol,
436+
TransportHeader: crdv1alpha1.TransportHeader{
437+
UDP: &crdv1alpha1.UDPHeader{
438+
DstPort: ptr.To(serverPodPort),
439+
},
440+
},
441+
},
442+
crdv1alpha1.CaptureDirectionDestinationToSource,
443+
packetCaptureHostPublicKey(pubKey2),
444+
),
445+
expectedStatus: crdv1alpha1.PacketCaptureStatus{
446+
NumberCaptured: 5,
447+
FilePath: getPcapURL("ipv4-udp-dst-to-src"),
448+
Conditions: []crdv1alpha1.PacketCaptureCondition{
449+
{
450+
Type: crdv1alpha1.PacketCaptureStarted,
451+
Status: metav1.ConditionStatus(v1.ConditionTrue),
452+
Reason: "Started",
453+
},
454+
{
455+
Type: crdv1alpha1.PacketCaptureComplete,
456+
Status: metav1.ConditionStatus(v1.ConditionTrue),
457+
Reason: "Succeed",
458+
},
459+
{
460+
Type: crdv1alpha1.PacketCaptureFileUploaded,
461+
Status: metav1.ConditionStatus(v1.ConditionTrue),
462+
Reason: "Succeed",
463+
},
464+
},
465+
},
466+
},
427467
{
428468
name: "ipv4-tcp-both",
429469
ipVersion: 4,
@@ -696,10 +736,15 @@ func verifyPacketFile(t *testing.T, pc *crdv1alpha1.PacketCapture, reader io.Rea
696736
ipLayer := packet.Layer(layers.LayerTypeIPv4)
697737
require.NotNil(t, ipLayer)
698738
ip, _ := ipLayer.(*layers.IPv4)
699-
if pc.Spec.Direction == crdv1alpha1.CaptureDirectionBoth {
739+
direction := pc.Spec.Direction
740+
switch direction {
741+
case crdv1alpha1.CaptureDirectionDestinationToSource:
742+
assert.Equal(t, srcIP.String(), ip.DstIP.String())
743+
assert.Equal(t, dstIP.String(), ip.SrcIP.String())
744+
case crdv1alpha1.CaptureDirectionBoth:
700745
assert.Contains(t, []string{srcIP.String(), dstIP.String()}, ip.SrcIP.String())
701746
assert.Contains(t, []string{srcIP.String(), dstIP.String()}, ip.DstIP.String())
702-
} else {
747+
default:
703748
assert.Equal(t, srcIP.String(), ip.SrcIP.String())
704749
assert.Equal(t, dstIP.String(), ip.DstIP.String())
705750
}
@@ -713,49 +758,49 @@ func verifyPacketFile(t *testing.T, pc *crdv1alpha1.PacketCapture, reader io.Rea
713758
if proto == nil {
714759
continue
715760
}
761+
762+
// addPortExpectations compares CRD ports with packet header ports based on capture direction
763+
addPortExpectations := func(crdSrcPort, crdDstPort *int32, hdrSrcPort, hdrDstPort int32) {
764+
switch direction {
765+
case crdv1alpha1.CaptureDirectionSourceToDestination:
766+
if crdDstPort != nil {
767+
assert.Equal(t, *crdDstPort, hdrDstPort)
768+
}
769+
if crdSrcPort != nil {
770+
assert.Equal(t, *crdSrcPort, hdrSrcPort)
771+
}
772+
case crdv1alpha1.CaptureDirectionDestinationToSource:
773+
if crdDstPort != nil {
774+
assert.Equal(t, *crdDstPort, hdrSrcPort)
775+
}
776+
if crdSrcPort != nil {
777+
assert.Equal(t, *crdSrcPort, hdrDstPort)
778+
}
779+
case crdv1alpha1.CaptureDirectionBoth:
780+
if crdDstPort != nil {
781+
assert.Contains(t, []int32{hdrSrcPort, hdrDstPort}, *crdDstPort)
782+
}
783+
if crdSrcPort != nil {
784+
assert.Contains(t, []int32{hdrSrcPort, hdrDstPort}, *crdSrcPort)
785+
}
786+
}
787+
}
788+
716789
if strings.ToUpper(proto.StrVal) == "TCP" || proto.IntVal == 6 {
717790
tcpLayer := packet.Layer(layers.LayerTypeTCP)
718791
require.NotNil(t, tcpLayer)
719792
tcp, _ := tcpLayer.(*layers.TCP)
720793
if packetSpec.TransportHeader.TCP != nil {
721794
ports := packetSpec.TransportHeader.TCP
722-
if pc.Spec.Direction == crdv1alpha1.CaptureDirectionBoth {
723-
if ports.DstPort != nil {
724-
assert.Contains(t, []int32{int32(tcp.SrcPort), int32(tcp.DstPort)}, *ports.DstPort)
725-
}
726-
if ports.SrcPort != nil {
727-
assert.Contains(t, []int32{int32(tcp.SrcPort), int32(tcp.DstPort)}, *ports.SrcPort)
728-
}
729-
} else {
730-
if ports.DstPort != nil {
731-
assert.Equal(t, *ports.DstPort, int32(tcp.DstPort))
732-
}
733-
if ports.SrcPort != nil {
734-
assert.Equal(t, *ports.SrcPort, int32(tcp.SrcPort))
735-
}
736-
}
795+
addPortExpectations(ports.SrcPort, ports.DstPort, int32(tcp.SrcPort), int32(tcp.DstPort))
737796
}
738797
} else if strings.ToUpper(proto.StrVal) == "UDP" || proto.IntVal == 17 {
739798
udpLayer := packet.Layer(layers.LayerTypeUDP)
740799
require.NotNil(t, udpLayer)
741800
udp, _ := udpLayer.(*layers.UDP)
742801
if packetSpec.TransportHeader.UDP != nil {
743802
ports := packetSpec.TransportHeader.UDP
744-
if pc.Spec.Direction == crdv1alpha1.CaptureDirectionBoth {
745-
if ports.DstPort != nil {
746-
assert.Contains(t, []int32{int32(udp.SrcPort), int32(udp.DstPort)}, *ports.DstPort)
747-
}
748-
if ports.SrcPort != nil {
749-
assert.Contains(t, []int32{int32(udp.SrcPort), int32(udp.DstPort)}, *ports.SrcPort)
750-
}
751-
} else {
752-
if ports.DstPort != nil {
753-
assert.Equal(t, *ports.DstPort, int32(udp.DstPort))
754-
}
755-
if ports.SrcPort != nil {
756-
assert.Equal(t, *ports.SrcPort, int32(udp.SrcPort))
757-
}
758-
}
803+
addPortExpectations(ports.SrcPort, ports.DstPort, int32(udp.SrcPort), int32(udp.DstPort))
759804
}
760805
} else if strings.ToUpper(proto.StrVal) == "ICMP" || proto.IntVal == 1 {
761806
icmpLayer := packet.Layer(layers.LayerTypeICMPv4)

0 commit comments

Comments
 (0)