1
0

sess_test.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. package kcp
  2. import (
  3. "crypto/sha1"
  4. "fmt"
  5. "io"
  6. "log"
  7. "net"
  8. "net/http"
  9. _ "net/http/pprof"
  10. "sync"
  11. "testing"
  12. "time"
  13. "golang.org/x/crypto/pbkdf2"
  14. )
  15. const portEcho = "127.0.0.1:9999"
  16. const portSink = "127.0.0.1:19999"
  17. const portTinyBufferEcho = "127.0.0.1:29999"
  18. const portListerner = "127.0.0.1:9998"
  19. const salt = "kcptest"
  20. var key = []byte("testkey")
  21. var fec = 4
  22. var pass = pbkdf2.Key(key, []byte(portSink), 4096, 32, sha1.New)
  23. func init() {
  24. go func() {
  25. log.Println(http.ListenAndServe("localhost:6060", nil))
  26. }()
  27. go echoServer()
  28. go sinkServer()
  29. go tinyBufferEchoServer()
  30. println("beginning tests, encryption:salsa20, fec:10/3")
  31. }
  32. func dialEcho() (*UDPSession, error) {
  33. //block, _ := NewNoneBlockCrypt(pass)
  34. //block, _ := NewSimpleXORBlockCrypt(pass)
  35. //block, _ := NewTEABlockCrypt(pass[:16])
  36. //block, _ := NewAESBlockCrypt(pass)
  37. block, _ := NewSalsa20BlockCrypt(pass)
  38. sess, err := DialWithOptions(portEcho, block, 10, 3)
  39. if err != nil {
  40. panic(err)
  41. }
  42. sess.SetStreamMode(true)
  43. sess.SetStreamMode(false)
  44. sess.SetStreamMode(true)
  45. sess.SetWindowSize(4096, 4096)
  46. sess.SetReadBuffer(4 * 1024 * 1024)
  47. sess.SetWriteBuffer(4 * 1024 * 1024)
  48. sess.SetStreamMode(true)
  49. sess.SetNoDelay(1, 10, 2, 1)
  50. sess.SetMtu(1400)
  51. sess.SetMtu(1600)
  52. sess.SetMtu(1400)
  53. sess.SetACKNoDelay(true)
  54. sess.SetDeadline(time.Now().Add(time.Minute))
  55. return sess, err
  56. }
  57. func dialSink() (*UDPSession, error) {
  58. sess, err := DialWithOptions(portSink, nil, 0, 0)
  59. if err != nil {
  60. panic(err)
  61. }
  62. sess.SetStreamMode(true)
  63. sess.SetWindowSize(4096, 4096)
  64. sess.SetReadBuffer(4 * 1024 * 1024)
  65. sess.SetWriteBuffer(4 * 1024 * 1024)
  66. sess.SetStreamMode(true)
  67. sess.SetNoDelay(1, 10, 2, 1)
  68. sess.SetMtu(1400)
  69. sess.SetACKNoDelay(true)
  70. sess.SetDeadline(time.Now().Add(time.Minute))
  71. return sess, err
  72. }
  73. func dialTinyBufferEcho() (*UDPSession, error) {
  74. //block, _ := NewNoneBlockCrypt(pass)
  75. //block, _ := NewSimpleXORBlockCrypt(pass)
  76. //block, _ := NewTEABlockCrypt(pass[:16])
  77. //block, _ := NewAESBlockCrypt(pass)
  78. block, _ := NewSalsa20BlockCrypt(pass)
  79. sess, err := DialWithOptions(portTinyBufferEcho, block, 10, 3)
  80. if err != nil {
  81. panic(err)
  82. }
  83. return sess, err
  84. }
  85. //////////////////////////
  86. func listenEcho() (net.Listener, error) {
  87. //block, _ := NewNoneBlockCrypt(pass)
  88. //block, _ := NewSimpleXORBlockCrypt(pass)
  89. //block, _ := NewTEABlockCrypt(pass[:16])
  90. //block, _ := NewAESBlockCrypt(pass)
  91. block, _ := NewSalsa20BlockCrypt(pass)
  92. return ListenWithOptions(portEcho, block, 10, 3)
  93. }
  94. func listenTinyBufferEcho() (net.Listener, error) {
  95. //block, _ := NewNoneBlockCrypt(pass)
  96. //block, _ := NewSimpleXORBlockCrypt(pass)
  97. //block, _ := NewTEABlockCrypt(pass[:16])
  98. //block, _ := NewAESBlockCrypt(pass)
  99. block, _ := NewSalsa20BlockCrypt(pass)
  100. return ListenWithOptions(portTinyBufferEcho, block, 10, 3)
  101. }
  102. func listenSink() (net.Listener, error) {
  103. return ListenWithOptions(portSink, nil, 0, 0)
  104. }
  105. func echoServer() {
  106. l, err := listenEcho()
  107. if err != nil {
  108. panic(err)
  109. }
  110. go func() {
  111. kcplistener := l.(*Listener)
  112. kcplistener.SetReadBuffer(4 * 1024 * 1024)
  113. kcplistener.SetWriteBuffer(4 * 1024 * 1024)
  114. kcplistener.SetDSCP(46)
  115. for {
  116. s, err := l.Accept()
  117. if err != nil {
  118. return
  119. }
  120. // coverage test
  121. s.(*UDPSession).SetReadBuffer(4 * 1024 * 1024)
  122. s.(*UDPSession).SetWriteBuffer(4 * 1024 * 1024)
  123. go handleEcho(s.(*UDPSession))
  124. }
  125. }()
  126. }
  127. func sinkServer() {
  128. l, err := listenSink()
  129. if err != nil {
  130. panic(err)
  131. }
  132. go func() {
  133. kcplistener := l.(*Listener)
  134. kcplistener.SetReadBuffer(4 * 1024 * 1024)
  135. kcplistener.SetWriteBuffer(4 * 1024 * 1024)
  136. kcplistener.SetDSCP(46)
  137. for {
  138. s, err := l.Accept()
  139. if err != nil {
  140. return
  141. }
  142. go handleSink(s.(*UDPSession))
  143. }
  144. }()
  145. }
  146. func tinyBufferEchoServer() {
  147. l, err := listenTinyBufferEcho()
  148. if err != nil {
  149. panic(err)
  150. }
  151. go func() {
  152. for {
  153. s, err := l.Accept()
  154. if err != nil {
  155. return
  156. }
  157. go handleTinyBufferEcho(s.(*UDPSession))
  158. }
  159. }()
  160. }
  161. ///////////////////////////
  162. func handleEcho(conn *UDPSession) {
  163. conn.SetStreamMode(true)
  164. conn.SetWindowSize(4096, 4096)
  165. conn.SetNoDelay(1, 10, 2, 1)
  166. conn.SetDSCP(46)
  167. conn.SetMtu(1400)
  168. conn.SetACKNoDelay(false)
  169. conn.SetReadDeadline(time.Now().Add(time.Hour))
  170. conn.SetWriteDeadline(time.Now().Add(time.Hour))
  171. buf := make([]byte, 65536)
  172. for {
  173. n, err := conn.Read(buf)
  174. if err != nil {
  175. panic(err)
  176. }
  177. conn.Write(buf[:n])
  178. }
  179. }
  180. func handleSink(conn *UDPSession) {
  181. conn.SetStreamMode(true)
  182. conn.SetWindowSize(4096, 4096)
  183. conn.SetNoDelay(1, 10, 2, 1)
  184. conn.SetDSCP(46)
  185. conn.SetMtu(1400)
  186. conn.SetACKNoDelay(false)
  187. conn.SetReadDeadline(time.Now().Add(time.Hour))
  188. conn.SetWriteDeadline(time.Now().Add(time.Hour))
  189. buf := make([]byte, 65536)
  190. for {
  191. _, err := conn.Read(buf)
  192. if err != nil {
  193. panic(err)
  194. }
  195. }
  196. }
  197. func handleTinyBufferEcho(conn *UDPSession) {
  198. conn.SetStreamMode(true)
  199. buf := make([]byte, 2)
  200. for {
  201. n, err := conn.Read(buf)
  202. if err != nil {
  203. panic(err)
  204. }
  205. conn.Write(buf[:n])
  206. }
  207. }
  208. ///////////////////////////
  209. func TestTimeout(t *testing.T) {
  210. cli, err := dialEcho()
  211. if err != nil {
  212. panic(err)
  213. }
  214. buf := make([]byte, 10)
  215. //timeout
  216. cli.SetDeadline(time.Now().Add(time.Second))
  217. <-time.After(2 * time.Second)
  218. n, err := cli.Read(buf)
  219. if n != 0 || err == nil {
  220. t.Fail()
  221. }
  222. cli.Close()
  223. }
  224. func TestSendRecv(t *testing.T) {
  225. cli, err := dialEcho()
  226. if err != nil {
  227. panic(err)
  228. }
  229. cli.SetWriteDelay(true)
  230. cli.SetDUP(1)
  231. const N = 100
  232. buf := make([]byte, 10)
  233. for i := 0; i < N; i++ {
  234. msg := fmt.Sprintf("hello%v", i)
  235. cli.Write([]byte(msg))
  236. if n, err := cli.Read(buf); err == nil {
  237. if string(buf[:n]) != msg {
  238. t.Fail()
  239. }
  240. } else {
  241. panic(err)
  242. }
  243. }
  244. cli.Close()
  245. }
  246. func TestTinyBufferReceiver(t *testing.T) {
  247. cli, err := dialTinyBufferEcho()
  248. if err != nil {
  249. panic(err)
  250. }
  251. const N = 100
  252. snd := byte(0)
  253. fillBuffer := func(buf []byte) {
  254. for i := 0; i < len(buf); i++ {
  255. buf[i] = snd
  256. snd++
  257. }
  258. }
  259. rcv := byte(0)
  260. check := func(buf []byte) bool {
  261. for i := 0; i < len(buf); i++ {
  262. if buf[i] != rcv {
  263. return false
  264. }
  265. rcv++
  266. }
  267. return true
  268. }
  269. sndbuf := make([]byte, 7)
  270. rcvbuf := make([]byte, 7)
  271. for i := 0; i < N; i++ {
  272. fillBuffer(sndbuf)
  273. cli.Write(sndbuf)
  274. if n, err := io.ReadFull(cli, rcvbuf); err == nil {
  275. if !check(rcvbuf[:n]) {
  276. t.Fail()
  277. }
  278. } else {
  279. panic(err)
  280. }
  281. }
  282. cli.Close()
  283. }
  284. func TestClose(t *testing.T) {
  285. cli, err := dialEcho()
  286. if err != nil {
  287. panic(err)
  288. }
  289. buf := make([]byte, 10)
  290. cli.Close()
  291. if cli.Close() == nil {
  292. t.Fail()
  293. }
  294. n, err := cli.Write(buf)
  295. if n != 0 || err == nil {
  296. t.Fail()
  297. }
  298. n, err = cli.Read(buf)
  299. if n != 0 || err == nil {
  300. t.Fail()
  301. }
  302. cli.Close()
  303. }
  304. func TestParallel1024CLIENT_64BMSG_64CNT(t *testing.T) {
  305. var wg sync.WaitGroup
  306. wg.Add(1024)
  307. for i := 0; i < 1024; i++ {
  308. go parallel_client(&wg)
  309. }
  310. wg.Wait()
  311. }
  312. func parallel_client(wg *sync.WaitGroup) (err error) {
  313. cli, err := dialEcho()
  314. if err != nil {
  315. panic(err)
  316. }
  317. err = echo_tester(cli, 64, 64)
  318. wg.Done()
  319. return
  320. }
  321. func BenchmarkEchoSpeed4K(b *testing.B) {
  322. speedclient(b, 4096)
  323. }
  324. func BenchmarkEchoSpeed64K(b *testing.B) {
  325. speedclient(b, 65536)
  326. }
  327. func BenchmarkEchoSpeed512K(b *testing.B) {
  328. speedclient(b, 524288)
  329. }
  330. func BenchmarkEchoSpeed1M(b *testing.B) {
  331. speedclient(b, 1048576)
  332. }
  333. func speedclient(b *testing.B, nbytes int) {
  334. b.ReportAllocs()
  335. cli, err := dialEcho()
  336. if err != nil {
  337. panic(err)
  338. }
  339. if err := echo_tester(cli, nbytes, b.N); err != nil {
  340. b.Fail()
  341. }
  342. b.SetBytes(int64(nbytes))
  343. }
  344. func BenchmarkSinkSpeed4K(b *testing.B) {
  345. sinkclient(b, 4096)
  346. }
  347. func BenchmarkSinkSpeed64K(b *testing.B) {
  348. sinkclient(b, 65536)
  349. }
  350. func BenchmarkSinkSpeed256K(b *testing.B) {
  351. sinkclient(b, 524288)
  352. }
  353. func BenchmarkSinkSpeed1M(b *testing.B) {
  354. sinkclient(b, 1048576)
  355. }
  356. func sinkclient(b *testing.B, nbytes int) {
  357. b.ReportAllocs()
  358. cli, err := dialSink()
  359. if err != nil {
  360. panic(err)
  361. }
  362. sink_tester(cli, nbytes, b.N)
  363. b.SetBytes(int64(nbytes))
  364. }
  365. func echo_tester(cli net.Conn, msglen, msgcount int) error {
  366. buf := make([]byte, msglen)
  367. for i := 0; i < msgcount; i++ {
  368. // send packet
  369. if _, err := cli.Write(buf); err != nil {
  370. return err
  371. }
  372. // receive packet
  373. nrecv := 0
  374. for {
  375. n, err := cli.Read(buf)
  376. if err != nil {
  377. return err
  378. } else {
  379. nrecv += n
  380. if nrecv == msglen {
  381. break
  382. }
  383. }
  384. }
  385. }
  386. return nil
  387. }
  388. func sink_tester(cli *UDPSession, msglen, msgcount int) error {
  389. // sender
  390. buf := make([]byte, msglen)
  391. for i := 0; i < msgcount; i++ {
  392. if _, err := cli.Write(buf); err != nil {
  393. return err
  394. }
  395. }
  396. return nil
  397. }
  398. func TestSNMP(t *testing.T) {
  399. t.Log(DefaultSnmp.Copy())
  400. t.Log(DefaultSnmp.Header())
  401. t.Log(DefaultSnmp.ToSlice())
  402. DefaultSnmp.Reset()
  403. t.Log(DefaultSnmp.ToSlice())
  404. }
  405. func TestListenerClose(t *testing.T) {
  406. l, err := ListenWithOptions(portListerner, nil, 10, 3)
  407. if err != nil {
  408. t.Fail()
  409. }
  410. l.SetReadDeadline(time.Now().Add(time.Second))
  411. l.SetWriteDeadline(time.Now().Add(time.Second))
  412. l.SetDeadline(time.Now().Add(time.Second))
  413. time.Sleep(2 * time.Second)
  414. if _, err := l.Accept(); err == nil {
  415. t.Fail()
  416. }
  417. l.Close()
  418. fakeaddr, _ := net.ResolveUDPAddr("udp6", "127.0.0.1:1111")
  419. if l.closeSession(fakeaddr) {
  420. t.Fail()
  421. }
  422. }