Testing ground for GUI
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main_test.go 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // Copyright (c) 2015 - 2018, Přemysl Janouch <p@janouch.name>
  3. //
  4. // Permission to use, copy, modify, and/or distribute this software for any
  5. // purpose with or without fee is hereby granted.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. //
  15. package main
  16. import (
  17. "crypto/tls"
  18. "net"
  19. "os"
  20. "reflect"
  21. "syscall"
  22. "testing"
  23. )
  24. func TestSplitString(t *testing.T) {
  25. var splitStringTests = []struct {
  26. s, delims string
  27. ignoreEmpty bool
  28. result []string
  29. }{
  30. {",a,,bc", ",", false, []string{"", "a", "", "bc"}},
  31. {",a,,bc", ",", true, []string{"a", "bc"}},
  32. {"a,;bc,", ",;", false, []string{"a", "", "bc", ""}},
  33. {"a,;bc,", ",;", true, []string{"a", "bc"}},
  34. {"", ",", false, []string{""}},
  35. {"", ",", true, nil},
  36. }
  37. for i, d := range splitStringTests {
  38. got := splitString(d.s, d.delims, d.ignoreEmpty)
  39. if !reflect.DeepEqual(got, d.result) {
  40. t.Errorf("case %d: %v should be %v\n", i, got, d.result)
  41. }
  42. }
  43. }
  44. func socketpair() (*os.File, *os.File, error) {
  45. pair, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
  46. if err != nil {
  47. return nil, nil, err
  48. }
  49. // See go #24331, this makes 1.11 use the internal poller
  50. // while there wasn't a way to achieve that before.
  51. if err := syscall.SetNonblock(int(pair[0]), true); err != nil {
  52. return nil, nil, err
  53. }
  54. if err := syscall.SetNonblock(int(pair[1]), true); err != nil {
  55. return nil, nil, err
  56. }
  57. fa := os.NewFile(uintptr(pair[0]), "a")
  58. if fa == nil {
  59. return nil, nil, os.ErrInvalid
  60. }
  61. fb := os.NewFile(uintptr(pair[1]), "b")
  62. if fb == nil {
  63. fa.Close()
  64. return nil, nil, os.ErrInvalid
  65. }
  66. return fa, fb, nil
  67. }
  68. func TestDetectTLS(t *testing.T) {
  69. detectTLSFromFunc := func(t *testing.T, writer func(net.Conn)) bool {
  70. // net.Pipe doesn't use file descriptors, we need a socketpair.
  71. sockA, sockB, err := socketpair()
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. defer sockA.Close()
  76. defer sockB.Close()
  77. fcB, err := net.FileConn(sockB)
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. go writer(fcB)
  82. fcA, err := net.FileConn(sockA)
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. sc, err := fcA.(syscall.Conn).SyscallConn()
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. return detectTLS(sc)
  91. }
  92. t.Run("SSL_2.0", func(t *testing.T) {
  93. if !detectTLSFromFunc(t, func(fc net.Conn) {
  94. // The obsolete, useless, unsupported SSL 2.0 record format.
  95. _, _ = fc.Write([]byte{0x80, 0x01, 0x01})
  96. }) {
  97. t.Error("could not detect SSL")
  98. }
  99. })
  100. t.Run("crypto_tls", func(t *testing.T) {
  101. if !detectTLSFromFunc(t, func(fc net.Conn) {
  102. conn := tls.Client(fc, &tls.Config{InsecureSkipVerify: true})
  103. _ = conn.Handshake()
  104. }) {
  105. t.Error("could not detect TLS")
  106. }
  107. })
  108. t.Run("text", func(t *testing.T) {
  109. if detectTLSFromFunc(t, func(fc net.Conn) {
  110. _, _ = fc.Write([]byte("ПРЕВЕД"))
  111. }) {
  112. t.Error("detected UTF-8 as TLS")
  113. }
  114. })
  115. t.Run("EOF", func(t *testing.T) {
  116. type connCloseWriter interface {
  117. net.Conn
  118. CloseWrite() error
  119. }
  120. if detectTLSFromFunc(t, func(fc net.Conn) {
  121. _ = fc.(connCloseWriter).CloseWrite()
  122. }) {
  123. t.Error("detected EOF as TLS")
  124. }
  125. })
  126. }
  127. func TestIRC(t *testing.T) {
  128. msg := ircParseMessage(
  129. `@first=a\:\s\r\n\\;2nd :srv hi there :good m8 :how are you?`)
  130. if !reflect.DeepEqual(msg.tags, map[string]string{
  131. "first": "a; \r\n\\",
  132. "2nd": "",
  133. }) {
  134. t.Error("tags parsed incorrectly")
  135. }
  136. if msg.nick != "srv" || msg.user != "" || msg.host != "" {
  137. t.Error("server name parsed incorrectly")
  138. }
  139. if msg.command != "hi" {
  140. t.Error("command name parsed incorrectly")
  141. }
  142. if !reflect.DeepEqual(msg.params,
  143. []string{"there", "good m8 :how are you?"}) {
  144. t.Error("params parsed incorrectly")
  145. }
  146. if !ircEqual("[fag]^", "{FAG}~") {
  147. t.Error("string case comparison not according to RFC 2812")
  148. }
  149. // TODO: More tests.
  150. }