wip: new encoder test

This commit is contained in:
AJ ONeal 2020-05-18 03:30:22 -06:00
parent c569a965e2
commit c5df63b11d
3 changed files with 66 additions and 15 deletions

View File

@ -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
}

View File

@ -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")
}
}

View File

@ -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))