From c5df63b11d4da6e642e47a3924ba5e8f7b1a2b53 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 18 May 2020 03:30:22 -0600 Subject: [PATCH] wip: new encoder test --- mplexer/packer/packer.go | 13 ++++++++---- mplexer/packer/packer_test.go | 37 +++++++++++++++++++++++++++++++++++ mplexer/packer/parser_test.go | 31 ++++++++++++++++++----------- 3 files changed, 66 insertions(+), 15 deletions(-) create mode 100644 mplexer/packer/packer_test.go diff --git a/mplexer/packer/packer.go b/mplexer/packer/packer.go index afe1d18..b4c2ef2 100644 --- a/mplexer/packer/packer.go +++ b/mplexer/packer/packer.go @@ -1,12 +1,17 @@ package packer import ( - "strconv" + "fmt" ) -func Marshal(addr Addr, body []byte) ([]byte, []byte) { - header := []byte(`IPv4,192.168.1.101,6743,` + strconv.Itoa(len(body)) + `,http,80,ex1.telebit.io`) +func Encode(src, dst Addr, domain string, payload []byte) ([]byte, []byte, error) { + n := len(payload) + header := []byte(fmt.Sprintf( + "%s,%s,%d,%d,%s,%d,%s,\n", + src.family, src.addr, src.port, + n, dst.scheme, dst.port, domain, + )) raw := []byte{255 - 1, byte(len(header))} header = append(raw, header...) - return header, body + return header, payload, nil } diff --git a/mplexer/packer/packer_test.go b/mplexer/packer/packer_test.go new file mode 100644 index 0000000..2c879d4 --- /dev/null +++ b/mplexer/packer/packer_test.go @@ -0,0 +1,37 @@ +package packer + +import ( + "strconv" + "testing" +) + +func TestEncodeDataMessage(t *testing.T) { + src := Addr{ + family: "IPv4", + addr: "192.168.1.101", + port: 6743, + } + dst := Addr{ + family: src.family, + port: 80, + scheme: "http", + } + domain := "ex1.telebit.io" + + payload := []byte("Hello, World!") + header := []byte("IPv4,192.168.1.101,6743," + strconv.Itoa(len(payload)) + ",http,80,ex1.telebit.io,\n") + //header = append([]byte{V1, byte(len(header))}, header...) + header = append([]byte{254, byte(len(header))}, header...) + + h, b, err := Encode(src, dst, domain, payload) + if nil != err { + t.Fatal(err) + } + + if string(header) != string(h) { + t.Fatalf("header %q should have matched %q", h, header) + } + if string(b) != string(payload) { + t.Fatal("payload should be the exact reference to the original slice") + } +} diff --git a/mplexer/packer/parser_test.go b/mplexer/packer/parser_test.go index 67d97e9..93f046b 100644 --- a/mplexer/packer/parser_test.go +++ b/mplexer/packer/parser_test.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net" - "strconv" "testing" "time" ) @@ -41,14 +40,24 @@ func TestParseWholeBlock(t *testing.T) { } p := NewParser(ctx, th) - body := []byte(`Hello, World!`) - fmt.Println("payload len", len(body)) - header := []byte("IPv4,192.168.1.101,6743," + strconv.Itoa(len(body)) + ",http,80,ex1.telebit.io,\n") - fmt.Println("header len", len(header)) - raw := []byte{255 - 1, byte(len(header))} - raw = append(raw, header...) - raw = append(raw, body...) - fmt.Println("total len", len(raw)) + payload := []byte(`Hello, World!`) + fmt.Println("payload len", len(payload)) + src := Addr{ + family: "IPv4", + addr: "192.168.1.101", + port: 6743, + } + dst := Addr{ + family: "IPv4", + port: 80, + scheme: "http", + } + domain := "ex1.telebit.io" + h, b, err := Encode(src, dst, domain, payload) + if nil != err { + t.Fatal(err) + } + raw := append(h, b...) n, err := p.Write(raw) if nil != err { t.Fatal(err) @@ -60,8 +69,8 @@ func TestParseWholeBlock(t *testing.T) { if 1 != th.chunksParsed { t.Fatal("should have parsed one chunck") } - if len(body) != th.bytesRead { - t.Fatalf("should have parsed a body of %d bytes, but saw %d\n", len(body), th.bytesRead) + if len(payload) != th.bytesRead { + t.Fatalf("should have parsed a payload of %d bytes, but saw %d\n", len(payload), th.bytesRead) } if n != len(raw) { t.Fatalf("should have parsed all %d bytes, not just %d\n", n, len(raw))