Пример #1
0
func TestWriteFrame(t *testing.T) {
	var (
		ifce *net.Interface
		err  error
		d    Dev
	)
	if ifce, err = net.InterfaceByName(*dev); err != nil {
		t.Fatalf("getting interface error: %v", err)
	}
	d, err = NewDev(ifce, nil)
	if err != nil {
		t.Fatal(err)
	}
	dst, err := net.ParseMAC("ff:ff:ff:ff:ff:ff")
	if err != nil {
		t.Fatal(err)
	}
	src, err := net.ParseMAC("12:34:56:78:9a:bc")
	if err != nil {
		t.Fatal(err)
	}
	var frame ethernet.Frame
	w := func() (err error) {
		(&frame).Prepare(dst, src, ethernet.NotTagged, ethernet.WSMP, 13)
		copy(frame.Payload(), "Hello, World!")
		err = d.Write(frame)
		if err != nil {
			return
		}
		return
	}
	for i := 0; i < 16; i++ {
		if nil != w() {
			t.Fatal(err)
		}
	}

	(&frame).Resize(d.Interface().MTU)
	if err = d.Write(frame); err != nil {
		t.Fatalf("writing frame with payload size equal to MTU fails: %v", err)
	}

	(&frame).Resize(d.Interface().MTU + 1)
	if nil == d.Write(frame) {
		t.Fatalf("writing frame with payload larger than MTU does not fail")
	}

	err = d.Close()
	if err != nil {
		t.Fatal(err)
	}

	if err = w(); nil == err {
		t.Fatal("closed Dev can still write")
	}
}
Пример #2
0
func TestReadFrame(t *testing.T) {
	var (
		ifce *net.Interface
		err  error
		d    Dev
	)
	if ifce, err = net.InterfaceByName(*dev); err != nil {
		t.Fatalf("getting interface error: %v", err)
	}
	d, err = NewDev(ifce, nil)
	if err != nil {
		t.Fatal(err)
	}
	var to ethernet.Frame
	for i := 0; i < 16; i++ {
		err = d.Read(&to)
		t.Logf("got frame: from %v to %v (ethertype %v): % x\n", to.Source(), to.Destination(), to.Ethertype(), to.Payload())
		if err != nil {
			t.Fatal(err)
		}
	}

	err = d.Close()
	if err != nil {
		t.Fatal(err)
	}

	for i := 0; i < 1024; i++ {
		err = d.Read(&to)
		if err != nil {
			break
		}
	}
	if err == nil {
		t.Fatal("closed Dev can still read")
	}
}