func Testv4Defrag(t *testing.T) { nomalPack := make(chan gopacket.Packet, 5) fragV4Pack := make(chan gopacket.Packet, 5) go v4Defrag(fragV4Pack, nomalPack) pack1 := gopacket.NewPacket(testPing1Frag1, layers.LinkTypeEthernet, gopacket.Default) fragV4Pack <- pack1 pack2 := gopacket.NewPacket(testPing1Frag2, layers.LinkTypeEthernet, gopacket.Default) fragV4Pack <- pack2 pack3 := gopacket.NewPacket(testPing1Frag3, layers.LinkTypeEthernet, gopacket.Default) fragV4Pack <- pack3 pack4 := gopacket.NewPacket(testPing1Frag4, layers.LinkTypeEthernet, gopacket.Default) fragV4Pack <- pack4 result := <-nomalPack ip := result.Layer(layers.LayerTypeIPv4) //TEST if the ip matches expectation if ip == nil { t.Errorf("defrag does not return IPV4 LAYER") } if len(ip.LayerPayload()) != 4508 { t.Fatalf("defrag: expecting a packet of 4508 bytes, got %d", len(ip.LayerPayload())) } validPayload := append(testPing1Frag1[34:], testPing1Frag2[34:]...) validPayload = append(validPayload, testPing1Frag3[34:]...) validPayload = append(validPayload, testPing1Frag4[34:]...) if bytes.Compare(validPayload, ip.LayerPayload()) != 0 { fmt.Println(bytediff.BashOutput.String( bytediff.Diff(validPayload, ip.LayerPayload()))) t.Errorf("defrag: payload is not correctly defragmented") } }
func Testv4Defrag(t *testing.T) { v4defragger := ip4defrag.NewIPv4Defragmenter() pack1 := gopacket.NewPacket(testPing1Frag1, layers.LinkTypeEthernet, gopacket.Default) pack2 := gopacket.NewPacket(testPing1Frag2, layers.LinkTypeEthernet, gopacket.Default) pack3 := gopacket.NewPacket(testPing1Frag3, layers.LinkTypeEthernet, gopacket.Default) pack4 := gopacket.NewPacket(testPing1Frag4, layers.LinkTypeEthernet, gopacket.Default) result, err := v4defragger.DefragIPv4(pack1.Layer(layers.LayerTypeIPv4).(*layers.IPv4)) if err != nil { t.Fatalf("error defragmenting pack1") } if result != nil { t.Fatalf("unexpected defragmented packet after pack1") } result, err = v4defragger.DefragIPv4(pack2.Layer(layers.LayerTypeIPv4).(*layers.IPv4)) if err != nil { t.Fatalf("error defragmenting pack2") } if result != nil { t.Fatalf("unexpected defragmented packet after pack2") } result, err = v4defragger.DefragIPv4(pack3.Layer(layers.LayerTypeIPv4).(*layers.IPv4)) if err != nil { t.Fatalf("error defragmenting pack3") } if result != nil { t.Fatalf("unexpected defragmented packet after pack3") } result, err = v4defragger.DefragIPv4(pack4.Layer(layers.LayerTypeIPv4).(*layers.IPv4)) if err != nil { t.Fatalf("error defragmenting pack4") } if result == nil { t.Fatalf("missing defragmented packet after pack4") } ip := result // TEST if the ip matches expectation if ip == nil { t.Errorf("defrag does not return IPV4 LAYER") } if len(ip.LayerPayload()) != 4508 { t.Fatalf("defrag: expecting a packet of 4508 bytes, got %d", len(ip.LayerPayload())) } validPayload := append(testPing1Frag1[34:], testPing1Frag2[34:]...) validPayload = append(validPayload, testPing1Frag3[34:]...) validPayload = append(validPayload, testPing1Frag4[34:]...) if bytes.Compare(validPayload, ip.LayerPayload()) != 0 { fmt.Println(bytediff.BashOutput.String( bytediff.Diff(validPayload, ip.LayerPayload()))) t.Errorf("defrag: payload is not correctly defragmented") } }
func main() { fmt.Println(bytediff.BashOutput.String(bytediff.Diff(sliceA, sliceB))) }