net_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c) 2016, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package common
  20. import (
  21. "net"
  22. "sync/atomic"
  23. "testing"
  24. "testing/iotest"
  25. "time"
  26. "github.com/miekg/dns"
  27. )
  28. func TestLRUConns(t *testing.T) {
  29. lruConns := NewLRUConns()
  30. dummy1 := &dummyConn{}
  31. entry1 := lruConns.Add(dummy1)
  32. dummy2 := &dummyConn{}
  33. entry2 := lruConns.Add(dummy2)
  34. dummy3 := &dummyConn{}
  35. entry3 := lruConns.Add(dummy3)
  36. entry3.Touch()
  37. entry2.Touch()
  38. entry1.Touch()
  39. if dummy1.IsClosed() || dummy2.IsClosed() || dummy3.IsClosed() {
  40. t.Fatalf("unexpected IsClosed state")
  41. }
  42. lruConns.CloseOldest()
  43. if dummy1.IsClosed() || dummy2.IsClosed() || !dummy3.IsClosed() {
  44. t.Fatalf("unexpected IsClosed state")
  45. }
  46. lruConns.CloseOldest()
  47. if dummy1.IsClosed() || !dummy2.IsClosed() || !dummy3.IsClosed() {
  48. t.Fatalf("unexpected IsClosed state")
  49. }
  50. entry1.Remove()
  51. lruConns.CloseOldest()
  52. if dummy1.IsClosed() || !dummy2.IsClosed() || !dummy3.IsClosed() {
  53. t.Fatalf("unexpected IsClosed state")
  54. }
  55. }
  56. func TestIsBogon(t *testing.T) {
  57. if IsBogon(net.ParseIP("8.8.8.8")) {
  58. t.Errorf("unexpected bogon")
  59. }
  60. if !IsBogon(net.ParseIP("127.0.0.1")) {
  61. t.Errorf("unexpected non-bogon")
  62. }
  63. if !IsBogon(net.ParseIP("192.168.0.1")) {
  64. t.Errorf("unexpected non-bogon")
  65. }
  66. if !IsBogon(net.ParseIP("::1")) {
  67. t.Errorf("unexpected non-bogon")
  68. }
  69. if !IsBogon(net.ParseIP("fc00::")) {
  70. t.Errorf("unexpected non-bogon")
  71. }
  72. }
  73. func BenchmarkIsBogon(b *testing.B) {
  74. for i := 0; i < b.N; i++ {
  75. IsBogon(net.ParseIP("8.8.8.8"))
  76. }
  77. }
  78. func makeDNSQuery(domain string) ([]byte, error) {
  79. query := new(dns.Msg)
  80. query.SetQuestion(domain, dns.TypeA)
  81. query.RecursionDesired = true
  82. msg, err := query.Pack()
  83. if err != nil {
  84. return nil, err
  85. }
  86. return msg, nil
  87. }
  88. func TestParseDNSQuestion(t *testing.T) {
  89. domain := dns.Fqdn("www.example.com")
  90. msg, err := makeDNSQuery(domain)
  91. if err != nil {
  92. t.Fatalf("makeDNSQuery failed: %s", err)
  93. }
  94. checkDomain, err := ParseDNSQuestion(msg)
  95. if err != nil {
  96. t.Fatalf("ParseDNSQuestion failed: %s", err)
  97. }
  98. if checkDomain != domain {
  99. t.Fatalf("unexpected domain")
  100. }
  101. }
  102. func BenchmarkParseDNSQuestion(b *testing.B) {
  103. domain := dns.Fqdn("www.example.com")
  104. msg, err := makeDNSQuery(domain)
  105. if err != nil {
  106. b.Fatalf("makeDNSQuery failed: %s", err)
  107. }
  108. for i := 0; i < b.N; i++ {
  109. ParseDNSQuestion(msg)
  110. }
  111. }
  112. type dummyConn struct {
  113. t *testing.T
  114. timeout *time.Timer
  115. readBytesPerSecond int64
  116. writeBytesPerSecond int64
  117. isClosed int32
  118. }
  119. func (c *dummyConn) Read(b []byte) (n int, err error) {
  120. if c.readBytesPerSecond > 0 {
  121. sleep := time.Duration(float64(int64(len(b))*int64(time.Second)) / float64(c.readBytesPerSecond))
  122. time.Sleep(sleep)
  123. }
  124. if c.timeout != nil {
  125. select {
  126. case <-c.timeout.C:
  127. return 0, iotest.ErrTimeout
  128. default:
  129. }
  130. }
  131. return len(b), nil
  132. }
  133. func (c *dummyConn) Write(b []byte) (n int, err error) {
  134. if c.writeBytesPerSecond > 0 {
  135. sleep := time.Duration(float64(int64(len(b))*int64(time.Second)) / float64(c.writeBytesPerSecond))
  136. time.Sleep(sleep)
  137. }
  138. if c.timeout != nil {
  139. select {
  140. case <-c.timeout.C:
  141. return 0, iotest.ErrTimeout
  142. default:
  143. }
  144. }
  145. return len(b), nil
  146. }
  147. func (c *dummyConn) Close() error {
  148. atomic.StoreInt32(&c.isClosed, 1)
  149. return nil
  150. }
  151. func (c *dummyConn) IsClosed() bool {
  152. return atomic.LoadInt32(&c.isClosed) == 1
  153. }
  154. func (c *dummyConn) LocalAddr() net.Addr {
  155. c.t.Fatal("LocalAddr not implemented")
  156. return nil
  157. }
  158. func (c *dummyConn) RemoteAddr() net.Addr {
  159. c.t.Fatal("RemoteAddr not implemented")
  160. return nil
  161. }
  162. func (c *dummyConn) SetDeadline(t time.Time) error {
  163. duration := time.Until(t)
  164. if c.timeout == nil {
  165. c.timeout = time.NewTimer(duration)
  166. } else {
  167. if !c.timeout.Stop() {
  168. <-c.timeout.C
  169. }
  170. c.timeout.Reset(duration)
  171. }
  172. return nil
  173. }
  174. func (c *dummyConn) SetReadDeadline(t time.Time) error {
  175. c.t.Fatal("SetReadDeadline not implemented")
  176. return nil
  177. }
  178. func (c *dummyConn) SetWriteDeadline(t time.Time) error {
  179. c.t.Fatal("SetWriteDeadline not implemented")
  180. return nil
  181. }
  182. func (c *dummyConn) SetRateLimits(readBytesPerSecond, writeBytesPerSecond int64) {
  183. c.readBytesPerSecond = readBytesPerSecond
  184. c.writeBytesPerSecond = writeBytesPerSecond
  185. }