imap_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package goimap
  2. import (
  3. "fmt"
  4. "net"
  5. "net/netip"
  6. "reflect"
  7. "testing"
  8. "time"
  9. )
  10. func Test_paramsErr(t *testing.T) {
  11. }
  12. func Test_getCommand(t *testing.T) {
  13. type args struct {
  14. line string
  15. }
  16. tests := []struct {
  17. name string
  18. args args
  19. want string
  20. want1 string
  21. want2 string
  22. }{
  23. {
  24. "STATUS命令测试",
  25. args{`15.64 STATUS "Deleted Messages" (MESSAGES UIDNEXT UIDVALIDITY UNSEEN)`},
  26. "15.64",
  27. "STATUS",
  28. `"Deleted Messages" (MESSAGES UIDNEXT UIDVALIDITY UNSEEN)`,
  29. },
  30. {
  31. "LOGIN命令测试",
  32. args{`a LOGIN admin 666666`},
  33. "a",
  34. "LOGIN",
  35. `admin 666666`,
  36. },
  37. {
  38. "SELECT命令测试",
  39. args{`9.79 SELECT INBOX`},
  40. "9.79",
  41. "SELECT",
  42. `INBOX`,
  43. },
  44. {
  45. "CAPABILITY命令测试",
  46. args{`1.81 CAPABILITY`},
  47. "1.81",
  48. "CAPABILITY",
  49. ``,
  50. },
  51. {
  52. "DELETE命令测试",
  53. args{`3.183 SELECT "Deleted Messages"`},
  54. "3.183",
  55. "SELECT",
  56. `"Deleted Messages"`,
  57. },
  58. {
  59. "异常命令测试",
  60. args{`GET/HTTP/1.0`},
  61. "",
  62. "",
  63. ``,
  64. },
  65. {
  66. "FETCH命令测试",
  67. args{`4.189 FETCH 7:38 (INTERNALDATE UID RFC822.SIZE FLAGS BODY.PEEK[HEADER.FIELDS (date subject from to cc message-id in-reply-to references content-type x-priority x-uniform-type-identifier x-universally-unique-identifier list-id list-unsubscribe bimi-indicator bimi-location x-bimi-indicator-hash authentication-results dkim-signature)])`},
  68. "4.189",
  69. "FETCH",
  70. `7:38 (INTERNALDATE UID RFC822.SIZE FLAGS BODY.PEEK[HEADER.FIELDS (date subject from to cc message-id in-reply-to references content-type x-priority x-uniform-type-identifier x-universally-unique-identifier list-id list-unsubscribe bimi-indicator bimi-location x-bimi-indicator-hash authentication-results dkim-signature)])`,
  71. },
  72. {
  73. "FETCH命令测试2",
  74. args{`4.167 FETCH 1:41 (FLAGS UID)`},
  75. "4.167",
  76. "FETCH",
  77. `1:41 (FLAGS UID)`,
  78. },
  79. {
  80. "UID FETCH命令测试",
  81. args{`4.200 UID FETCH 5 BODY.PEEK[HEADER]`},
  82. "4.200",
  83. "UID FETCH",
  84. `5 BODY.PEEK[HEADER]`,
  85. },
  86. {
  87. "UID Search命令测试",
  88. args{`C117 UID SEARCH UID 46:*`},
  89. "C117",
  90. "UID SEARCH",
  91. `UID 46:*`,
  92. },
  93. }
  94. for _, tt := range tests {
  95. t.Run(tt.name, func(t *testing.T) {
  96. got, got1, got2 := getCommand(tt.args.line)
  97. if got != tt.want {
  98. t.Errorf("getCommand() got = %v, want %v", got, tt.want)
  99. }
  100. if got1 != tt.want1 {
  101. t.Errorf("getCommand() got1 = %v, want %v", got1, tt.want1)
  102. }
  103. if !reflect.DeepEqual(got2, tt.want2) {
  104. t.Errorf("getCommand() got2 = %v, want %v", got2, tt.want2)
  105. }
  106. })
  107. }
  108. }
  109. type mockConn struct{}
  110. func (m mockConn) Read(b []byte) (n int, err error) {
  111. fmt.Println("Read")
  112. return 0, err
  113. }
  114. func (m mockConn) Write(b []byte) (n int, err error) {
  115. return 0, err
  116. }
  117. func (m mockConn) Close() error {
  118. return nil
  119. }
  120. func (m mockConn) LocalAddr() net.Addr {
  121. return net.TCPAddrFromAddrPort(netip.AddrPort{})
  122. }
  123. func (m mockConn) RemoteAddr() net.Addr {
  124. return net.TCPAddrFromAddrPort(netip.AddrPort{})
  125. }
  126. func (m mockConn) SetDeadline(t time.Time) error {
  127. return nil
  128. }
  129. func (m mockConn) SetReadDeadline(t time.Time) error {
  130. return nil
  131. }
  132. func (m mockConn) SetWriteDeadline(t time.Time) error {
  133. return nil
  134. }
  135. //
  136. //func TestServer_doCommand(t *testing.T) {
  137. // type args struct {
  138. // session *Session
  139. // rawLine string
  140. // conn net.Conn
  141. // reader *bufio.Reader
  142. // }
  143. // tests := []struct {
  144. // name string
  145. // args args
  146. // }{
  147. // {
  148. // name: "StatusTest",
  149. // args: args{
  150. // session: &Session{
  151. // Status: AUTHORIZED,
  152. //
  153. // },
  154. // rawLine: `9.33 STATUS "Sent Messages" (MESSAGES UIDNEXT UIDVALIDITY UNSEEN)`,
  155. // conn: &mockConn{},
  156. // reader: &bufio.Reader{},
  157. // },
  158. // },
  159. // {
  160. // name: "StatusTest2",
  161. // args: args{
  162. // session: &Session{
  163. // Status: AUTHORIZED,
  164. // },
  165. // rawLine: `9.33 STATUS INBOX (MESSAGES UIDNEXT UIDVALIDITY UNSEEN)`,
  166. // conn: &mockConn{},
  167. // reader: &bufio.Reader{},
  168. // },
  169. // },
  170. // }
  171. // for _, tt := range tests {
  172. // t.Run(tt.name, func(t *testing.T) {
  173. // s := &Server{
  174. // }
  175. // s.doCommand(tt.args.session, tt.args.rawLine, tt.args.conn, tt.args.reader)
  176. // })
  177. // }
  178. //}