client_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. err = c.Delete("tset")
  84. if err != nil {
  85. t.Error(err)
  86. }
  87. err = c.MakeDir(testDir)
  88. if err != nil {
  89. t.Error(err)
  90. }
  91. err = c.ChangeDir(testDir)
  92. if err != nil {
  93. t.Error(err)
  94. }
  95. dir, err := c.CurrentDir()
  96. if err != nil {
  97. t.Error(err)
  98. } else {
  99. if dir != "/incoming/"+testDir {
  100. t.Error("Wrong dir: " + dir)
  101. }
  102. }
  103. err = c.ChangeDirToParent()
  104. if err != nil {
  105. t.Error(err)
  106. }
  107. entries, err := c.NameList("/")
  108. if err != nil {
  109. t.Error(err)
  110. }
  111. if len(entries) != 1 || entries[0] != "/incoming" {
  112. t.Errorf("Unexpected entries: %v", entries)
  113. }
  114. err = c.RemoveDir(testDir)
  115. if err != nil {
  116. t.Error(err)
  117. }
  118. err = c.Logout()
  119. if err != nil {
  120. if protoErr := err.(*textproto.Error); protoErr != nil {
  121. if protoErr.Code != StatusNotImplemented {
  122. t.Error(err)
  123. }
  124. } else {
  125. t.Error(err)
  126. }
  127. }
  128. c.Quit()
  129. err = c.NoOp()
  130. if err == nil {
  131. t.Error("Expected error")
  132. }
  133. }
  134. func TestConnIPv6(t *testing.T) {
  135. if testing.Short() {
  136. t.Skip("skipping test in short mode.")
  137. }
  138. c, err := DialTimeout("[::1]: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. // TestConnect tests the legacy Connect function
  153. func TestConnect(t *testing.T) {
  154. if testing.Short() {
  155. t.Skip("skipping test in short mode.")
  156. }
  157. c, err := Connect("localhost:21")
  158. if err != nil {
  159. t.Fatal(err)
  160. }
  161. c.Quit()
  162. }
  163. func TestTimeout(t *testing.T) {
  164. if testing.Short() {
  165. t.Skip("skipping test in short mode.")
  166. }
  167. c, err := DialTimeout("localhost:2121", 1*time.Second)
  168. if err == nil {
  169. t.Fatal("expected timeout, got nil error")
  170. c.Quit()
  171. }
  172. }
  173. func TestWrongLogin(t *testing.T) {
  174. if testing.Short() {
  175. t.Skip("skipping test in short mode.")
  176. }
  177. c, err := DialTimeout("localhost:21", 5*time.Second)
  178. if err != nil {
  179. t.Fatal(err)
  180. }
  181. defer c.Quit()
  182. err = c.Login("zoo2Shia", "fei5Yix9")
  183. if err == nil {
  184. t.Fatal("expected error, got nil")
  185. }
  186. }