12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037 |
- // Copyright © 2015 Daniel Fu <daniel820313@gmail.com>.
- // Copyright © 2019 Loki 'l0k18' Verloren <stalker.loki@protonmail.ch>.
- // Copyright © 2020 Gridfinity, LLC. <admin@gridfinity.com>.
- // Copyright © 2020 Jeffrey H. Johnson <jeff@gridfinity.com>.
- //
- // All rights reserved.
- //
- // All use of this code is governed by the MIT license.
- // The complete license is available in the LICENSE file.
- package lkcp9_test
- import (
- "fmt"
- "hash/fnv"
- "io"
- "log"
- "net"
- "net/http"
- _ "net/http/pprof"
- "runtime"
- "runtime/debug"
- "sync"
- "testing"
- "time"
- u "go.gridfinity.dev/leaktestfe"
- "go.gridfinity.dev/lkcp9"
- "golang.org/x/crypto/pbkdf2"
- )
- const (
- portEcho = "127.0.0.1:9079"
- portSink = "127.0.0.1:19609"
- portTinyBufferEcho = "127.0.0.1:29609"
- portListerner = "127.0.0.1:9078"
- )
- var (
- key = []byte(
- "testkey",
- )
- pass = pbkdf2.Key(
- key,
- []byte(portSink),
- 4096,
- 32,
- fnv.New128a,
- )
- )
- func init() {
- go func() {
- log.Println(
- http.ListenAndServe(
- "127.0.0.1:8881",
- nil,
- ))
- }()
- go echoServer()
- go sinkServer()
- go tinyBufferEchoServer()
- println(
- "Beginning tests:\nEncryption: Salsa20n\nFEC: 10/3",
- )
- }
- func dialEcho() (
- *lkcp9.UDPSession,
- error,
- ) {
- block, _ := lkcp9.NewSalsa20BlockCrypt(
- pass,
- )
- sess, err := lkcp9.DialWithOptions(
- portEcho,
- block,
- 10,
- 3,
- )
- if err != nil {
- panic(
- err,
- )
- }
- sess.SetStreamMode(
- true,
- )
- sess.SetStreamMode(
- false,
- )
- sess.SetStreamMode(
- true,
- )
- sess.SetWindowSize(
- 1024,
- 1024,
- )
- sess.SetReadBuffer(
- 16 * 1024 * 1024,
- )
- sess.SetWriteBuffer(
- 16 * 1024 * 1024,
- )
- sess.SetStreamMode(
- true,
- )
- sess.SetNoDelay(
- 1, 10, 2, 1,
- )
- sess.SetMtu(
- 1400,
- )
- sess.SetMtu(
- 1600,
- )
- sess.SetMtu(
- 1400,
- )
- sess.SetACKNoDelay(
- true,
- )
- sess.SetACKNoDelay(
- false,
- )
- sess.SetDeadline(time.Now().Add(
- time.Minute,
- ))
- runtime.GC()
- debug.FreeOSMemory()
- return sess, err
- }
- func dialSink() (
- *lkcp9.UDPSession,
- error,
- ) {
- sess, err := lkcp9.DialWithOptions(
- portSink,
- nil,
- 0,
- 0,
- )
- if err != nil {
- panic(
- err,
- )
- }
- sess.SetStreamMode(
- true,
- )
- sess.SetWindowSize(
- 1024, 1024,
- )
- sess.SetReadBuffer(
- 16 * 1024 * 1024,
- )
- sess.SetWriteBuffer(
- 16 * 1024 * 1024)
- sess.SetStreamMode(
- true,
- )
- sess.SetNoDelay(
- 1, 10, 2, 1,
- )
- sess.SetMtu(
- 1400,
- )
- sess.SetACKNoDelay(
- false,
- )
- sess.SetDeadline(
- time.Now().Add(
- time.Minute,
- ),
- )
- return sess, err
- }
- func dialTinyBufferEcho() (
- *lkcp9.UDPSession,
- error,
- ) {
- block, _ := lkcp9.NewSalsa20BlockCrypt(
- pass,
- )
- sess, err := lkcp9.DialWithOptions(
- portTinyBufferEcho,
- block,
- 10,
- 3,
- )
- if err != nil {
- panic(
- err,
- )
- }
- runtime.GC()
- return sess, err
- }
- func listenEcho() (
- net.Listener,
- error,
- ) {
- block, _ := lkcp9.NewSalsa20BlockCrypt(
- pass,
- )
- runtime.GC()
- debug.FreeOSMemory()
- return lkcp9.ListenWithOptions(
- portEcho,
- block,
- 10,
- 3,
- )
- }
- func listenTinyBufferEcho() (
- net.Listener,
- error,
- ) {
- block, _ := lkcp9.NewSalsa20BlockCrypt(
- pass,
- )
- runtime.GC()
- debug.FreeOSMemory()
- return lkcp9.ListenWithOptions(
- portTinyBufferEcho,
- block,
- 10,
- 3,
- )
- }
- func listenSink() (
- net.Listener,
- error,
- ) {
- runtime.GC()
- debug.FreeOSMemory()
- return lkcp9.ListenWithOptions(
- portSink,
- nil,
- 0,
- 0,
- )
- }
- func echoServer() {
- l, err := listenEcho()
- if err != nil {
- panic(
- err,
- )
- }
- go func() {
- Kcplistener := l.(*lkcp9.Listener)
- Kcplistener.SetReadBuffer(4 * 1024 * 1024)
- Kcplistener.SetWriteBuffer(4 * 1024 * 1024)
- Kcplistener.SetDSCP(46)
- for {
- s, err := l.Accept()
- if err != nil {
- return
- }
- s.(*lkcp9.UDPSession).SetReadBuffer(4 * 1024 * 1024)
- s.(*lkcp9.UDPSession).SetWriteBuffer(4 * 1024 * 1024)
- go handleEcho(s.(*lkcp9.UDPSession))
- }
- }()
- runtime.GC()
- debug.FreeOSMemory()
- }
- func sinkServer() {
- l, err := listenSink()
- if err != nil {
- panic(
- err,
- )
- }
- go func() {
- Kcplistener := l.(*lkcp9.Listener)
- Kcplistener.SetReadBuffer(
- 4 * 1024 * 1024,
- )
- Kcplistener.SetWriteBuffer(
- 4 * 1024 * 1024,
- )
- Kcplistener.SetDSCP(
- 46,
- )
- for {
- s, err := l.Accept()
- if err != nil {
- return
- }
- go handleSink(s.(*lkcp9.UDPSession))
- }
- }()
- runtime.GC()
- debug.FreeOSMemory()
- }
- func tinyBufferEchoServer() {
- l, err := listenTinyBufferEcho()
- if err != nil {
- panic(
- err,
- )
- }
- go func() {
- for {
- s, err := l.Accept()
- if err != nil {
- return
- }
- go handleTinyBufferEcho(s.(*lkcp9.UDPSession))
- }
- }()
- runtime.GC()
- debug.FreeOSMemory()
- }
- func handleEcho(
- conn *lkcp9.UDPSession,
- ) {
- conn.SetStreamMode(
- true,
- )
- conn.SetWindowSize(
- 4096,
- 4096,
- )
- conn.SetNoDelay(
- 1,
- 10,
- 2,
- 1,
- )
- conn.SetDSCP(
- 46,
- )
- conn.SetMtu(
- 1400,
- )
- conn.SetACKNoDelay(
- false,
- )
- conn.SetReadDeadline(
- time.Now().Add(time.Hour),
- )
- conn.SetWriteDeadline(
- time.Now().Add(time.Hour),
- )
- buf := make(
- []byte,
- 65536,
- )
- for {
- n, err := conn.Read(
- buf,
- )
- if err != nil {
- panic(
- err,
- )
- }
- conn.Write(
- buf[:n],
- )
- }
- runtime.GC()
- debug.FreeOSMemory()
- }
- func handleSink(
- conn *lkcp9.UDPSession,
- ) {
- conn.SetStreamMode(
- true,
- )
- conn.SetWindowSize(
- 4096,
- 4096,
- )
- conn.SetNoDelay(
- 1,
- 10,
- 2,
- 1,
- )
- conn.SetDSCP(
- 46,
- )
- conn.SetMtu(
- 1400,
- )
- conn.SetACKNoDelay(
- false,
- )
- conn.SetReadDeadline(
- time.Now().Add(time.Hour),
- )
- conn.SetWriteDeadline(
- time.Now().Add(time.Hour),
- )
- buf := make(
- []byte,
- 65536,
- )
- for {
- _, err := conn.Read(
- buf,
- )
- if err != nil {
- panic(
- err,
- )
- }
- }
- runtime.GC()
- debug.FreeOSMemory()
- }
- func handleTinyBufferEcho(
- conn *lkcp9.UDPSession,
- ) {
- conn.SetStreamMode(
- true,
- )
- buf := make(
- []byte,
- 2,
- )
- for {
- n, err := conn.Read(
- buf,
- )
- if err != nil {
- panic(
- err,
- )
- }
- conn.Write(
- buf[:n],
- )
- }
- runtime.GC()
- debug.FreeOSMemory()
- }
- func TestTimeout(
- t *testing.T,
- ) {
- defer u.Leakplug(
- t,
- )
- cli, err := dialEcho()
- if err != nil {
- panic(
- err,
- )
- }
- buf := make(
- []byte,
- 10,
- )
- cli.SetDeadline(
- time.Now().Add(time.Second),
- )
- <-time.After(
- 2 * time.Second,
- )
- n, err := cli.Read(buf)
- if n != 0 || err == nil {
- t.Fail()
- }
- cli.Close()
- runtime.GC()
- debug.FreeOSMemory()
- }
- func TestSendRecv(
- t *testing.T,
- ) {
- defer u.Leakplug(
- t,
- )
- cli, err := dialEcho()
- if err != nil {
- panic(
- err,
- )
- }
- cli.SetWriteDelay(
- true,
- )
- cli.SetDUP(
- 1,
- )
- const (
- N = 100
- )
- buf := make(
- []byte,
- 10,
- )
- for i := 0; i < N; i++ {
- msg := fmt.Sprintf(
- "hello%v",
- i,
- )
- cli.Write(
- []byte(msg),
- )
- if n, err := cli.Read(
- buf,
- ); err == nil {
- if string(
- buf[:n],
- ) != msg {
- t.Fail()
- }
- } else {
- panic(
- err,
- )
- }
- }
- cli.Close()
- runtime.GC()
- debug.FreeOSMemory()
- }
- func TestSendVector(
- t *testing.T,
- ) {
- defer u.Leakplug(
- t,
- )
- cli, err := dialEcho()
- if err != nil {
- panic(
- err,
- )
- }
- cli.SetWriteDelay(
- false,
- )
- const N = 100
- buf := make(
- []byte,
- 20,
- )
- v := make(
- [][]byte,
- 2,
- )
- for i := 0; i < N; i++ {
- v[0] = []byte(
- fmt.Sprintf(
- "holas%v",
- i,
- ))
- v[1] = []byte(
- fmt.Sprintf(
- "amigo%v",
- i,
- ))
- msg :=
- fmt.Sprintf(
- "holas%vamigo%v",
- i,
- i,
- )
- cli.WriteBuffers(
- v,
- )
- if n, err := cli.Read(
- buf,
- ); err == nil {
- if string(
- buf[:n],
- ) != msg {
- t.Error(
- string(buf[:n]),
- msg,
- )
- }
- } else {
- panic(
- err,
- )
- }
- }
- cli.Close()
- runtime.GC()
- debug.FreeOSMemory()
- }
- func TestTinyBufferReceiver(
- t *testing.T,
- ) {
- defer u.Leakplug(
- t,
- )
- cli, err := dialTinyBufferEcho()
- if err != nil {
- panic(
- err,
- )
- }
- const (
- N = 100
- )
- snd := byte(
- 0,
- )
- fillBuffer := func(
- buf []byte,
- ) {
- for i := 0; i < len(
- buf,
- ); i++ {
- buf[i] = snd
- snd++
- }
- }
- rcv := byte(
- 0,
- )
- check := func(
- buf []byte,
- ) bool {
- for i := 0; i < len(
- buf,
- ); i++ {
- if buf[i] != rcv {
- return false
- }
- rcv++
- }
- return true
- }
- sndbuf := make(
- []byte,
- 7,
- )
- rcvbuf := make(
- []byte,
- 7,
- )
- for i := 0; i < N; i++ {
- fillBuffer(
- sndbuf,
- )
- cli.Write(
- sndbuf,
- )
- if n, err := io.ReadFull(
- cli,
- rcvbuf,
- ); err == nil {
- if !check(
- rcvbuf[:n],
- ) {
- t.Fail()
- }
- } else {
- panic(
- err,
- )
- }
- }
- cli.Close()
- runtime.GC()
- debug.FreeOSMemory()
- }
- func TestClose(
- t *testing.T,
- ) {
- defer u.Leakplug(
- t,
- )
- cli, err := dialEcho()
- if err != nil {
- panic(
- err,
- )
- }
- buf := make(
- []byte,
- 10,
- )
- cli.Close()
- if cli.Close() == nil {
- t.Fail()
- }
- n, err := cli.Write(
- buf,
- )
- if n != 0 || err == nil {
- t.Fail()
- }
- n, err = cli.Read(
- buf,
- )
- if n != 0 || err == nil {
- t.Fail()
- }
- cli.Close()
- runtime.GC()
- debug.FreeOSMemory()
- }
- func TestMassivelyParallel_Concurrent_2048_Clients_128_byte_Messages_128_Iterations(
- t *testing.T,
- ) {
- runtime.GC()
- debug.FreeOSMemory()
- t.Parallel()
- _ = runtime.GOMAXPROCS(runtime.NumCPU() * 16)
- t.Log(fmt.Sprintf("Starting Goroutines=%v", runtime.NumGoroutine()))
- defer u.Leakplug(
- t,
- )
- var wg sync.WaitGroup
- wg.Add(
- 2048,
- )
- for i := 0; i < 2048; i++ {
- go parallel_client(
- &wg,
- )
- }
- t.Log(fmt.Sprintf("Activate Goroutines=%v", runtime.NumGoroutine()))
- wg.Wait()
- t.Log(fmt.Sprintf("Utilized Goroutines=%v", runtime.NumGoroutine()))
- runtime.GC()
- debug.FreeOSMemory()
- }
- func parallel_client(
- wg *sync.WaitGroup,
- ) (
- err error,
- ) {
- cli, err := dialEcho()
- if err != nil {
- panic(
- err,
- )
- }
- err = echo_tester(
- cli,
- 128,
- 128,
- )
- wg.Done()
- runtime.GC()
- debug.FreeOSMemory()
- return
- }
- func BenchmarkEchoSpeed4K(
- b *testing.B,
- ) {
- speedclient(
- b,
- 4096,
- )
- runtime.GC()
- debug.FreeOSMemory()
- }
- func BenchmarkEchoSpeed64K(
- b *testing.B,
- ) {
- speedclient(
- b,
- 65536,
- )
- runtime.GC()
- debug.FreeOSMemory()
- }
- func BenchmarkEchoSpeed512K(
- b *testing.B,
- ) {
- speedclient(b,
- 524288,
- )
- runtime.GC()
- debug.FreeOSMemory()
- }
- func BenchmarkEchoSpeed1M(
- b *testing.B,
- ) {
- speedclient(
- b,
- 1048576,
- )
- runtime.GC()
- debug.FreeOSMemory()
- }
- func speedclient(
- b *testing.B,
- nbytes int,
- ) {
- b.ReportAllocs()
- cli, err := dialEcho()
- if err != nil {
- panic(
- err,
- )
- }
- if err := echo_tester(
- cli,
- nbytes,
- b.N,
- ); err != nil {
- b.Fail()
- }
- b.SetBytes(
- int64(nbytes),
- )
- runtime.GC()
- debug.FreeOSMemory()
- }
- func BenchmarkSinkSpeed4K(
- b *testing.B,
- ) {
- sinkclient(
- b,
- 4096,
- )
- runtime.GC()
- debug.FreeOSMemory()
- }
- func BenchmarkSinkSpeed64K(
- b *testing.B,
- ) {
- sinkclient(
- b,
- 65536,
- )
- runtime.GC()
- debug.FreeOSMemory()
- }
- func BenchmarkSinkSpeed256K(
- b *testing.B,
- ) {
- sinkclient(
- b,
- 524288,
- )
- runtime.GC()
- debug.FreeOSMemory()
- }
- func BenchmarkSinkSpeed1M(
- b *testing.B,
- ) {
- sinkclient(
- b,
- 1048576,
- )
- runtime.GC()
- debug.FreeOSMemory()
- }
- func sinkclient(
- b *testing.B,
- nbytes int,
- ) {
- b.ReportAllocs()
- cli, err := dialSink()
- if err != nil {
- panic(
- err,
- )
- }
- sink_tester(
- cli,
- nbytes,
- b.N,
- )
- b.SetBytes(
- int64(nbytes),
- )
- runtime.GC()
- debug.FreeOSMemory()
- }
- func echo_tester(
- cli net.Conn,
- msglen,
- msgcount int,
- ) error {
- buf := make(
- []byte,
- msglen,
- )
- for i := 0; i < msgcount; i++ {
- if _, err := cli.Write(
- buf,
- ); err != nil {
- return err
- }
- nrecv := 0
- for {
- n, err := cli.Read(
- buf,
- )
- if err != nil {
- return err
- } else {
- nrecv += n
- if nrecv == msglen {
- break
- }
- }
- }
- }
- runtime.GC()
- return nil
- }
- func sink_tester(
- cli *lkcp9.UDPSession,
- msglen,
- msgcount int,
- ) error {
- buf := make(
- []byte,
- msglen,
- )
- for i := 0; i < msgcount; i++ {
- if _, err := cli.Write(
- buf,
- ); err != nil {
- return err
- }
- }
- runtime.GC()
- debug.FreeOSMemory()
- return nil
- }
- func TestSNSI(
- t *testing.T,
- ) {
- defer u.Leakplug(
- t,
- )
- t.Log(
- *lkcp9.DefaultSnsi.Copy(),
- )
- t.Log(
- lkcp9.DefaultSnsi.Header(),
- )
- t.Log(
- lkcp9.DefaultSnsi.ToSlice(),
- )
- lkcp9.DefaultSnsi.Reset()
- t.Log(
- lkcp9.DefaultSnsi.ToSlice(),
- )
- runtime.GC()
- debug.FreeOSMemory()
- }
- func TestListenerClose(
- t *testing.T,
- ) {
- defer u.Leakplug(
- t,
- )
- l, err := lkcp9.ListenWithOptions(
- portListerner,
- nil,
- 10,
- 3,
- )
- if err != nil {
- runtime.GC()
- debug.FreeOSMemory()
- t.Fail()
- }
- l.SetReadDeadline(
- time.Now().Add(
- time.Second,
- ))
- l.SetWriteDeadline(
- time.Now().Add(
- time.Second,
- ))
- l.SetDeadline(
- time.Now().Add(
- time.Second,
- ))
- time.Sleep(
- 2 * time.Second,
- )
- if _, err := l.Accept(); err == nil {
- t.Fail()
- }
- runtime.GC()
- debug.FreeOSMemory()
- l.Close()
- fakeaddr, _ := net.ResolveUDPAddr(
- "udp6",
- "127.0.0.1:1111",
- )
- if l.CloseSession(
- fakeaddr,
- ) {
- runtime.GC()
- debug.FreeOSMemory()
- t.Fail()
- }
- runtime.GC()
- debug.FreeOSMemory()
- }
|