Skip to content

Commit

Permalink
chore(tests): test packet direction in net e2e
Browse files Browse the repository at this point in the history
In HTTP e2e test, add a packet direction test.
  • Loading branch information
NDStrahilevitz committed Nov 23, 2023
1 parent 8cef954 commit fb990be
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
35 changes: 27 additions & 8 deletions signatures/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,25 @@ func GetPathFromRawAddr(addr map[string]string) (string, error) {
// Network Protocol Event Types
//

// GetPacketMetadata converts json to PacketMetadata
func GetPacketMetadata(
event trace.Event,
argName string) (
trace.PacketMetadata,
error) {
arg, err := GetTraceeArgumentByName(event, argName, GetArgOps{DefaultArgs: false})
if err != nil {
return trace.PacketMetadata{}, err
}

argPacketMetadata, ok := arg.Value.(trace.PacketMetadata)
if ok {
return argPacketMetadata, nil
}

return trace.PacketMetadata{}, fmt.Errorf("packet metadata: type error (should be trace.PacketMetadata, is %T)", arg.Value)
}

// GetProtoIPv4ByName converts json to ProtoIPv4
func GetProtoIPv4ByName(
event trace.Event,
Expand Down Expand Up @@ -198,7 +217,7 @@ func GetProtoIPv4ByName(
return argProtoIPv4, nil
}

return trace.ProtoIPv4{}, fmt.Errorf("protocol IPv4: type error")
return trace.ProtoIPv4{}, fmt.Errorf("protocol IPv4: type error (should be trace.ProtoIPv4, is %T)", arg.Value)
}

// GetProtoIPv6ByName converts json to ProtoIPv6
Expand Down Expand Up @@ -231,7 +250,7 @@ func GetProtoIPv6ByName(
return argProtoIPv6, nil
}

return trace.ProtoIPv6{}, fmt.Errorf("protocol IPv6: type error")
return trace.ProtoIPv6{}, fmt.Errorf("protocol IPv6: type error (should be trace.ProtoIPv6, is %T)", arg.Value)
}

// GetProtoUDPByName converts json to ProtoUDP
Expand All @@ -258,7 +277,7 @@ func GetProtoUDPByName(
return argProtoUDP, nil
}

return trace.ProtoUDP{}, fmt.Errorf("protocol UDP: type error")
return trace.ProtoUDP{}, fmt.Errorf("protocol UDP: type error (should be trace.ProtoUDP, is %T)", arg.Value)
}

// GetProtoTCPByName converts json to ProtoTCP
Expand Down Expand Up @@ -298,7 +317,7 @@ func GetProtoTCPByName(
return argProtoTCP, nil
}

return trace.ProtoTCP{}, fmt.Errorf("protocol TCP: type error")
return trace.ProtoTCP{}, fmt.Errorf("protocol TCP: type error (should be trace.ProtoTCP, is %T)", arg.Value)
}

// GetProtoICMPByName converts json to ProtoICMP
Expand All @@ -325,7 +344,7 @@ func GetProtoICMPByName(
return argProtoICMP, nil
}

return trace.ProtoICMP{}, fmt.Errorf("protocol ICMP: type error")
return trace.ProtoICMP{}, fmt.Errorf("protocol ICMP: type error (should be trace.ProtoICMP, is %T)", arg.Value)
}

// GetProtoICMPv6ByName converts json to ProtoICMPv6
Expand All @@ -352,7 +371,7 @@ func GetProtoICMPv6ByName(
return argProtoICMPv6, nil
}

return trace.ProtoICMPv6{}, fmt.Errorf("protocol ICMPv6: type error")
return trace.ProtoICMPv6{}, fmt.Errorf("protocol ICMPv6: type error (should be trace.ProtoICMPv6, is %T)", arg.Value)
}

// GetProtoDNSByName converts json to ProtoDNS
Expand Down Expand Up @@ -395,7 +414,7 @@ func GetProtoDNSByName(
return argProtoDNS, nil
}

return trace.ProtoDNS{}, fmt.Errorf("protocol DNS: type error")
return trace.ProtoDNS{}, fmt.Errorf("protocol DNS: type error (should be trace.ProtoDNS, is %T)", arg.Value)
}

func GetProtoHTTPByName(
Expand All @@ -414,5 +433,5 @@ func GetProtoHTTPByName(
return argProtoHTTP, nil
}

return trace.ProtoHTTP{}, fmt.Errorf("protocol HTTP: type error")
return trace.ProtoHTTP{}, fmt.Errorf("protocol HTTP: type error (should be trace.ProtoHTTP, is %T)", arg.Value)
}
20 changes: 20 additions & 0 deletions tests/e2e-net-signatures/e2e-http.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (sig *e2eHTTP) OnEvent(event protocol.Event) error {
return fmt.Errorf("failed to cast event's payload")
}

if eventObj.ProcessName != "curl" {
return nil
}

if eventObj.EventName == "net_packet_http" {
http, err := helpers.GetProtoHTTPByName(eventObj, "proto_http")
if err != nil {
Expand All @@ -58,6 +62,15 @@ func (sig *e2eHTTP) OnEvent(event protocol.Event) error {
return nil
}

md, err := helpers.GetPacketMetadata(eventObj, "metadata")
if err != nil {
return err
}

if !testHttpDirectionAndPacketDirection(&md, &http) {
return nil
}

if !strings.HasPrefix(http.Protocol, "HTTP/") {
return nil
}
Expand All @@ -78,3 +91,10 @@ func (sig *e2eHTTP) OnSignal(s detect.Signal) error {
}

func (sig *e2eHTTP) Close() {}

func testHttpDirectionAndPacketDirection(md *trace.PacketMetadata, http *trace.ProtoHTTP) bool {
// This test is done in the context of a curl request, but if it was in the context
// of a server then the direction of the packet would be opposite to the http direction
return (http.Direction == "request" && md.Direction == trace.PacketEgress) ||
(http.Direction == "response" && md.Direction == trace.PacketIngress)
}

0 comments on commit fb990be

Please sign in to comment.