client_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package ftp
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "testing"
  6. "time"
  7. )
  8. const (
  9. testData = "Just some text"
  10. testDir = "mydir"
  11. )
  12. func TestConn(t *testing.T) {
  13. if testing.Short() {
  14. t.Skip("skipping test in short mode.")
  15. }
  16. c, err := DialTimeout("localhost:21", 5*time.Second)
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. err = c.Login("anonymous", "anonymous")
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. err = c.NoOp()
  25. if err != nil {
  26. t.Error(err)
  27. }
  28. data := bytes.NewBufferString(testData)
  29. err = c.Stor("test", data)
  30. if err != nil {
  31. t.Error(err)
  32. }
  33. _, err = c.List(".")
  34. if err != nil {
  35. t.Error(err)
  36. }
  37. err = c.Rename("test", "tset")
  38. if err != nil {
  39. t.Error(err)
  40. }
  41. r, err := c.Retr("tset")
  42. if err != nil {
  43. t.Error(err)
  44. } else {
  45. buf, err := ioutil.ReadAll(r)
  46. if err != nil {
  47. t.Error(err)
  48. }
  49. if string(buf) != testData {
  50. t.Errorf("'%s'", buf)
  51. }
  52. r.Close()
  53. }
  54. r, err = c.Retr("tset")
  55. if err != nil {
  56. t.Error(err)
  57. } else {
  58. r.Close()
  59. }
  60. err = c.Delete("tset")
  61. if err != nil {
  62. t.Error(err)
  63. }
  64. err = c.MakeDir(testDir)
  65. if err != nil {
  66. t.Error(err)
  67. }
  68. err = c.ChangeDir(testDir)
  69. if err != nil {
  70. t.Error(err)
  71. }
  72. dir, err := c.CurrentDir()
  73. if err != nil {
  74. t.Error(err)
  75. } else {
  76. if dir != "/"+testDir {
  77. t.Error("Wrong dir: " + dir)
  78. }
  79. }
  80. err = c.ChangeDirToParent()
  81. if err != nil {
  82. t.Error(err)
  83. }
  84. err = c.RemoveDir(testDir)
  85. if err != nil {
  86. t.Error(err)
  87. }
  88. err = c.Logout()
  89. if err != nil {
  90. t.Error(err)
  91. }
  92. c.Quit()
  93. err = c.NoOp()
  94. if err == nil {
  95. t.Error("Expected error")
  96. }
  97. }
  98. // ftp.mozilla.org uses multiline 220 response
  99. func TestMultiline(t *testing.T) {
  100. if testing.Short() {
  101. t.Skip("skipping test in short mode.")
  102. }
  103. c, err := DialTimeout("ftp.mozilla.org:21", 5*time.Second)
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. err = c.Login("anonymous", "anonymous")
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. _, err = c.List(".")
  112. if err != nil {
  113. t.Error(err)
  114. }
  115. c.Quit()
  116. }
  117. // antioche.antioche.eu.org with IPv6
  118. func TestConnIPv6(t *testing.T) {
  119. c, err := Connect("[2001:660:3302:282a:204:75ff:fe9f:9e11]:21")
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. err = c.Login("anonymous", "anonymous")
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. _, err = c.List(".")
  128. if err != nil {
  129. t.Error(err)
  130. }
  131. c.Quit()
  132. }