imap_test.go 3.9 KB

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