client_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package ftp
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "net/textproto"
  6. "testing"
  7. "time"
  8. )
  9. const (
  10. testData = "Just some text"
  11. testDir = "mydir"
  12. )
  13. func TestConnPASV(t *testing.T) {
  14. testConn(t, true)
  15. }
  16. func TestConnEPSV(t *testing.T) {
  17. testConn(t, false)
  18. }
  19. func testConn(t *testing.T, passive bool) {
  20. if testing.Short() {
  21. t.Skip("skipping test in short mode.")
  22. }
  23. c, err := DialTimeout("localhost:21", 5*time.Second)
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. if passive {
  28. delete(c.features, "EPSV")
  29. }
  30. err = c.Login("anonymous", "anonymous")
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. err = c.NoOp()
  35. if err != nil {
  36. t.Error(err)
  37. }
  38. err = c.ChangeDir("incoming")
  39. if err != nil {
  40. t.Error(err)
  41. }
  42. data := bytes.NewBufferString(testData)
  43. err = c.Stor("test", data)
  44. if err != nil {
  45. t.Error(err)
  46. }
  47. _, err = c.List(".")
  48. if err != nil {
  49. t.Error(err)
  50. }
  51. err = c.Rename("test", "tset")
  52. if err != nil {
  53. t.Error(err)
  54. }
  55. r, err := c.Retr("tset")
  56. if err != nil {
  57. t.Error(err)
  58. } else {
  59. buf, err := ioutil.ReadAll(r)
  60. if err != nil {
  61. t.Error(err)
  62. }
  63. if string(buf) != testData {
  64. t.Errorf("'%s'", buf)
  65. }
  66. r.Close()
  67. }
  68. r, err = c.RetrFrom("tset", 5)
  69. if err != nil {
  70. t.Error(err)
  71. } else {
  72. buf, err := ioutil.ReadAll(r)
  73. if err != nil {
  74. t.Error(err)
  75. }
  76. expected := testData[5:]
  77. if string(buf) != expected {
  78. t.Errorf("read %q, expected %q", buf, expected)
  79. }
  80. r.Close()
  81. }
  82. err = c.Delete("tset")
  83. if err != nil {
  84. t.Error(err)
  85. }
  86. err = c.MakeDir(testDir)
  87. if err != nil {
  88. t.Error(err)
  89. }
  90. err = c.ChangeDir(testDir)
  91. if err != nil {
  92. t.Error(err)
  93. }
  94. dir, err := c.CurrentDir()
  95. if err != nil {
  96. t.Error(err)
  97. } else {
  98. if dir != "/incoming/"+testDir {
  99. t.Error("Wrong dir: " + dir)
  100. }
  101. }
  102. err = c.ChangeDirToParent()
  103. if err != nil {
  104. t.Error(err)
  105. }
  106. entries, err := c.NameList("/")
  107. if err != nil {
  108. t.Error(err)
  109. }
  110. if len(entries) != 1 || entries[0] != "/incoming" {
  111. t.Errorf("Unexpected entries: %v", entries)
  112. }
  113. err = c.RemoveDir(testDir)
  114. if err != nil {
  115. t.Error(err)
  116. }
  117. err = c.Logout()
  118. if err != nil {
  119. if protoErr := err.(*textproto.Error); protoErr != nil {
  120. if protoErr.Code != StatusNotImplemented {
  121. t.Error(err)
  122. }
  123. } else {
  124. t.Error(err)
  125. }
  126. }
  127. c.Quit()
  128. err = c.NoOp()
  129. if err == nil {
  130. t.Error("Expected error")
  131. }
  132. }
  133. // ftp.mozilla.org uses multiline 220 response
  134. func TestMultiline(t *testing.T) {
  135. if testing.Short() {
  136. t.Skip("skipping test in short mode.")
  137. }
  138. c, err := DialTimeout("ftp.mozilla.org:21", 5*time.Second)
  139. if err != nil {
  140. t.Fatal(err)
  141. }
  142. err = c.Login("anonymous", "anonymous")
  143. if err != nil {
  144. t.Fatal(err)
  145. }
  146. _, err = c.List(".")
  147. if err != nil {
  148. t.Error(err)
  149. }
  150. c.Quit()
  151. }
  152. func TestConnIPv6(t *testing.T) {
  153. if testing.Short() {
  154. t.Skip("skipping test in short mode.")
  155. }
  156. c, err := DialTimeout("[::1]:21", 5*time.Second)
  157. if err != nil {
  158. t.Fatal(err)
  159. }
  160. err = c.Login("anonymous", "anonymous")
  161. if err != nil {
  162. t.Fatal(err)
  163. }
  164. _, err = c.List(".")
  165. if err != nil {
  166. t.Error(err)
  167. }
  168. c.Quit()
  169. }
  170. // TestConnect tests the legacy Connect function
  171. func TestConnect(t *testing.T) {
  172. if testing.Short() {
  173. t.Skip("skipping test in short mode.")
  174. }
  175. c, err := Connect("localhost:21")
  176. if err != nil {
  177. t.Fatal(err)
  178. }
  179. c.Quit()
  180. }
  181. func TestTimeout(t *testing.T) {
  182. if testing.Short() {
  183. t.Skip("skipping test in short mode.")
  184. }
  185. c, err := DialTimeout("localhost:2121", 1*time.Second)
  186. if err == nil {
  187. t.Fatal("expected timeout, got nil error")
  188. c.Quit()
  189. }
  190. }
  191. func TestWrongLogin(t *testing.T) {
  192. if testing.Short() {
  193. t.Skip("skipping test in short mode.")
  194. }
  195. c, err := DialTimeout("localhost:21", 5*time.Second)
  196. if err != nil {
  197. t.Fatal(err)
  198. }
  199. defer c.Quit()
  200. err = c.Login("zoo2Shia", "fei5Yix9")
  201. if err == nil {
  202. t.Fatal("expected error, got nil")
  203. }
  204. }