client_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.Retr("tset")
  69. if err != nil {
  70. t.Error(err)
  71. } else {
  72. r.Close()
  73. }
  74. err = c.Delete("tset")
  75. if err != nil {
  76. t.Error(err)
  77. }
  78. err = c.MakeDir(testDir)
  79. if err != nil {
  80. t.Error(err)
  81. }
  82. err = c.ChangeDir(testDir)
  83. if err != nil {
  84. t.Error(err)
  85. }
  86. dir, err := c.CurrentDir()
  87. if err != nil {
  88. t.Error(err)
  89. } else {
  90. if dir != "/incoming/"+testDir {
  91. t.Error("Wrong dir: " + dir)
  92. }
  93. }
  94. err = c.ChangeDirToParent()
  95. if err != nil {
  96. t.Error(err)
  97. }
  98. err = c.RemoveDir(testDir)
  99. if err != nil {
  100. t.Error(err)
  101. }
  102. err = c.Logout()
  103. if err != nil {
  104. if protoErr := err.(*textproto.Error); protoErr != nil {
  105. if protoErr.Code != StatusNotImplemented {
  106. t.Error(err)
  107. }
  108. } else {
  109. t.Error(err)
  110. }
  111. }
  112. c.Quit()
  113. err = c.NoOp()
  114. if err == nil {
  115. t.Error("Expected error")
  116. }
  117. }
  118. // ftp.mozilla.org uses multiline 220 response
  119. func TestMultiline(t *testing.T) {
  120. if testing.Short() {
  121. t.Skip("skipping test in short mode.")
  122. }
  123. c, err := DialTimeout("ftp.mozilla.org:21", 5*time.Second)
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. err = c.Login("anonymous", "anonymous")
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. _, err = c.List(".")
  132. if err != nil {
  133. t.Error(err)
  134. }
  135. c.Quit()
  136. }
  137. // antioche.antioche.eu.org with IPv6
  138. func TestConnIPv6(t *testing.T) {
  139. if testing.Short() {
  140. t.Skip("skipping test in short mode.")
  141. }
  142. c, err := DialTimeout("[::1]:21", 5*time.Second)
  143. if err != nil {
  144. t.Fatal(err)
  145. }
  146. err = c.Login("anonymous", "anonymous")
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. _, err = c.List(".")
  151. if err != nil {
  152. t.Error(err)
  153. }
  154. c.Quit()
  155. }