github.com/fafucoder/cilium@v1.6.11/pkg/monitor/dissect_test.go (about)

     1  // Copyright 2016-2018 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build !privileged_tests
    16  
    17  package monitor
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "gopkg.in/check.v1"
    23  )
    24  
    25  // Hook up gocheck into the "go test" runner.
    26  func Test(t *testing.T) {
    27  	TestingT(t)
    28  }
    29  
    30  type MonitorSuite struct{}
    31  
    32  var _ = Suite(&MonitorSuite{})
    33  
    34  func (s *MonitorSuite) TestDissectSummary(c *C) {
    35  
    36  	srcMAC := "01:23:45:67:89:ab"
    37  	dstMAC := "02:33:45:67:89:ab"
    38  
    39  	srcIP := "1.2.3.4"
    40  	dstIP := "5.6.7.8"
    41  
    42  	sport := "80"
    43  	dport := "443"
    44  
    45  	// Generated in scapy:
    46  	// Ether(src="01:23:45:67:89:ab", dst="02:33:45:67:89:ab")/IP(src="1.2.3.4",dst="5.6.7.8")/TCP(sport=80,dport=443)
    47  	packetData := []byte{2, 51, 69, 103, 137, 171, 1, 35, 69, 103, 137, 171, 8, 0, 69, 0, 0, 40, 0, 1, 0, 0, 64, 6, 106, 188, 1, 2, 3, 4, 5, 6, 7, 8, 0, 80, 1, 187, 0, 0, 0, 0, 0, 0, 0, 0, 80, 2, 32, 0, 125, 196, 0, 0}
    48  
    49  	summary := GetDissectSummary(packetData)
    50  
    51  	c.Assert(summary.Ethernet, Not(Equals), "")
    52  	c.Assert(summary.IPv4, Not(Equals), "")
    53  	c.Assert(summary.TCP, Not(Equals), "")
    54  
    55  	c.Assert(summary.L2.Src, Equals, srcMAC)
    56  	c.Assert(summary.L2.Dst, Equals, dstMAC)
    57  
    58  	c.Assert(summary.L3.Src, Equals, srcIP)
    59  	c.Assert(summary.L3.Dst, Equals, dstIP)
    60  
    61  	c.Assert(summary.L4.Src, Equals, sport)
    62  	c.Assert(summary.L4.Dst, Equals, dport)
    63  }