go-bgp

a collection of golang BGP tools to monitor, archive and serve
git clone git://git.2f30.org/go-bgp
Log | Files | Refs | README

mrt_test.go (3343B)


      1 package mrt
      2 
      3 import (
      4 	"bufio"
      5 	"bytes"
      6 	"encoding/binary"
      7 	"fmt"
      8 	"net"
      9 	"os"
     10 	"testing"
     11 )
     12 
     13 func TestMrtHdr(t *testing.T) {
     14 	buf := new(bytes.Buffer)
     15 	var tdate, tlen uint32 = 1, 4
     16 	var ttype, tsubtype uint16 = 2, 3
     17 	mrt := &MrtHdr{tdate, ttype, tsubtype, tlen}
     18 	fmt.Printf("date:%v type:%v subtype:%v len:%v\n", tdate, ttype, tsubtype, tlen)
     19 	binary.Write(buf, binary.BigEndian, mrt)
     20 	fmt.Printf("binary mrt: %x\n", buf.Bytes())
     21 	mhdr, err := NewMrtHdr(buf.Bytes())
     22 	if err != nil {
     23 		t.Fatal(err)
     24 	}
     25 	fmt.Printf("recreating MrtHdr from binary :%+v \n", mhdr)
     26 }
     27 
     28 func TestMrtPFunc(t *testing.T) {
     29 	var (
     30 		tt1, ts1 = uint16(1), uint16(0)  //start
     31 		tt2, ts2 = uint16(3), uint16(1)  //i am dead , but wrong subtype
     32 		tt3, ts3 = uint16(2), uint16(0)  //deprecated
     33 		tt4, ts4 = uint16(11), uint16(0) //ospf state change
     34 		tbuf     = []byte{0, 0, 0, 0, 0, 0, 0, 0}
     35 		tf       parsefunc
     36 		ok       bool
     37 	)
     38 	//binbuf := new(bytes.Buffer)
     39 	mrt1 := &MrtMsg{
     40 		Hdr:    MrtHdr{1, tt1, ts1, 10},
     41 		Msg: tbuf,
     42 	}
     43 	mrt2 := &MrtMsg{
     44 		Hdr:    MrtHdr{1, tt2, ts2, 10},
     45 		Msg: tbuf,
     46 	}
     47 	mrt3 := &MrtMsg{
     48 		Hdr:    MrtHdr{1, tt3, ts3, 10},
     49 		Msg: tbuf,
     50 	}
     51 	mrt4 := &MrtMsg{
     52 		Hdr:    MrtHdr{1, tt4, ts4, 10},
     53 		Msg: tbuf,
     54 	}
     55 	fmt.Println("trying to parse informational message")
     56 	if tf, ok = mrt1.PFunc(); !ok {
     57 		t.Fatal("tf should be non nil")
     58 	}
     59 	hdr := tf(mrt1.Msg)
     60 	fmt.Printf("type is :%s\n", hdr.Type())
     61 	fmt.Println("trying to parse informational message with opt string")
     62 	mrt1.Msg = []byte{'f', 'o', 'o', ' ', 's', 't', 'r'}
     63 	mrt1.Hdr.Mrt_type = tt2
     64 	if tf, ok = mrt1.PFunc(); !ok {
     65 		t.Fatal("tf should be non nil")
     66 	}
     67 	hdr = tf(mrt1.Msg)
     68 	fmt.Printf("type is :%s\n", hdr.Type())
     69 	fmt.Println("trying to parse malformed informational message")
     70 	if tf, ok = mrt2.PFunc(); ok {
     71 		t.Fatal("this should fail with tf being nil cause subtype is non-0")
     72 	}
     73 	fmt.Println("trying to parse deprecated message")
     74 	if tf, ok = mrt3.PFunc(); ok {
     75 		t.Fatal("this should fail with tf being nil cause it's deprecated")
     76 	}
     77 	fmt.Println("trying to parse OSPF message")
     78 	//first call to littleendian to come to hostbyteorder and then switch to big
     79 	binary.BigEndian.PutUint32(mrt4.Msg[:4], binary.LittleEndian.Uint32(net.IPv4(1, 2, 3, 4).To4()))
     80 	binary.BigEndian.PutUint32(mrt4.Msg[4:], binary.LittleEndian.Uint32(net.IPv4(5, 6, 7, 8).To4()))
     81 	//binary.Write(binbuf, binary.BigEndian, net.IPv4allsys.To4())
     82 	//mrt4.Msg = make([]byte,8)
     83 	//mrt4.Msg = binbuf.Bytes()
     84 	//copy(mrt4.BGPMsg,binbuf.Bytes())
     85 	if tf, ok = mrt4.PFunc(); !ok {
     86 		t.Fatal("this shouldn't fail")
     87 	}
     88 	hdr = tf(mrt4.Msg)
     89 	fmt.Printf("type is :%s .String representation: %s\n", hdr.Type(), hdr)
     90 }
     91 
     92 func TestScan(t *testing.T) {
     93 	fmt.Println("testing the scanner interface")
     94 	f, err := os.Open("../tests/mrt3")
     95 	if err != nil {
     96 		t.Fatal(err)
     97 	}
     98 	mrtscanner := bufio.NewScanner(f)
     99 	mrtscanner.Split(SplitMrt)
    100 	count := 0
    101 	for mrtscanner.Scan() {
    102 		count++
    103 		dat := mrtscanner.Bytes()
    104 		h, _ := NewMrtHdr(dat[:MrtHdr_size]) /* the error has been checked in Read() */
    105 		if h.Mrt_len == 0 {
    106 			t.Logf("terminating from 0 mrt len")
    107 			return
    108 		}
    109 		mrtmsg := MrtMsg{Hdr: h, Msg: dat[MrtHdr_size:]}
    110 		if tf, ok := mrtmsg.PFunc(); ok {
    111 			tf(mrtmsg.Msg)
    112 		}
    113 	}
    114 	if err := mrtscanner.Err(); err != nil {
    115 		fmt.Printf("error: %s", err)
    116 	}
    117 	fmt.Printf("scanned and parsed: %d entries from bufio\n", count)
    118 }