client_test.go 3.9 KB

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