@@ -424,6 +424,46 @@ func testPacketCaptureBasic(t *testing.T, data *TestData, sftpServerIP string, p
424
424
},
425
425
},
426
426
},
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
+ },
427
467
{
428
468
name : "ipv4-tcp-both" ,
429
469
ipVersion : 4 ,
@@ -696,10 +736,15 @@ func verifyPacketFile(t *testing.T, pc *crdv1alpha1.PacketCapture, reader io.Rea
696
736
ipLayer := packet .Layer (layers .LayerTypeIPv4 )
697
737
require .NotNil (t , ipLayer )
698
738
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 :
700
745
assert .Contains (t , []string {srcIP .String (), dstIP .String ()}, ip .SrcIP .String ())
701
746
assert .Contains (t , []string {srcIP .String (), dstIP .String ()}, ip .DstIP .String ())
702
- } else {
747
+ default :
703
748
assert .Equal (t , srcIP .String (), ip .SrcIP .String ())
704
749
assert .Equal (t , dstIP .String (), ip .DstIP .String ())
705
750
}
@@ -713,49 +758,49 @@ func verifyPacketFile(t *testing.T, pc *crdv1alpha1.PacketCapture, reader io.Rea
713
758
if proto == nil {
714
759
continue
715
760
}
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
+
716
789
if strings .ToUpper (proto .StrVal ) == "TCP" || proto .IntVal == 6 {
717
790
tcpLayer := packet .Layer (layers .LayerTypeTCP )
718
791
require .NotNil (t , tcpLayer )
719
792
tcp , _ := tcpLayer .(* layers.TCP )
720
793
if packetSpec .TransportHeader .TCP != nil {
721
794
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 ))
737
796
}
738
797
} else if strings .ToUpper (proto .StrVal ) == "UDP" || proto .IntVal == 17 {
739
798
udpLayer := packet .Layer (layers .LayerTypeUDP )
740
799
require .NotNil (t , udpLayer )
741
800
udp , _ := udpLayer .(* layers.UDP )
742
801
if packetSpec .TransportHeader .UDP != nil {
743
802
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 ))
759
804
}
760
805
} else if strings .ToUpper (proto .StrVal ) == "ICMP" || proto .IntVal == 1 {
761
806
icmpLayer := packet .Layer (layers .LayerTypeICMPv4 )
0 commit comments