parse_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package ftp
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "time"
  7. )
  8. var (
  9. // now is the current time for all tests
  10. now = newTime(2017, time.March, 10, 23, 00)
  11. thisYear, _, _ = now.Date()
  12. previousYear = thisYear - 1
  13. )
  14. type line struct {
  15. line string
  16. name string
  17. size uint64
  18. entryType EntryType
  19. time time.Time
  20. }
  21. type symlinkLine struct {
  22. line string
  23. name string
  24. target string
  25. }
  26. type unsupportedLine struct {
  27. line string
  28. err error
  29. }
  30. var listTests = []line{
  31. // UNIX ls -l style
  32. {"drwxr-xr-x 3 110 1002 3 Dec 02 2009 pub", "pub", 0, EntryTypeFolder, newTime(2009, time.December, 2)},
  33. {"drwxr-xr-x 3 110 1002 3 Dec 02 2009 p u b", "p u b", 0, EntryTypeFolder, newTime(2009, time.December, 2)},
  34. {"-rw-r--r-- 1 marketwired marketwired 12016 Mar 16 2016 2016031611G087802-001.newsml", "2016031611G087802-001.newsml", 12016, EntryTypeFile, newTime(2016, time.March, 16)},
  35. {"-rwxr-xr-x 3 110 1002 1234567 Dec 02 2009 fileName", "fileName", 1234567, EntryTypeFile, newTime(2009, time.December, 2)},
  36. {"lrwxrwxrwx 1 root other 7 Jan 25 00:17 bin -> usr/bin", "bin", 0, EntryTypeLink, newTime(thisYear, time.January, 25, 0, 17)},
  37. // Another ls style
  38. {"drwxr-xr-x folder 0 Aug 15 05:49 !!!-Tipp des Haus!", "!!!-Tipp des Haus!", 0, EntryTypeFolder, newTime(thisYear, time.August, 15, 5, 49)},
  39. {"drwxrwxrwx folder 0 Aug 11 20:32 P0RN", "P0RN", 0, EntryTypeFolder, newTime(thisYear, time.August, 11, 20, 32)},
  40. {"-rw-r--r-- 0 18446744073709551615 18446744073709551615 Nov 16 2006 VIDEO_TS.VOB", "VIDEO_TS.VOB", 18446744073709551615, EntryTypeFile, newTime(2006, time.November, 16)},
  41. // Microsoft's FTP servers for Windows
  42. {"---------- 1 owner group 1803128 Jul 10 10:18 ls-lR.Z", "ls-lR.Z", 1803128, EntryTypeFile, newTime(thisYear, time.July, 10, 10, 18)},
  43. {"d--------- 1 owner group 0 Nov 9 19:45 Softlib", "Softlib", 0, EntryTypeFolder, newTime(previousYear, time.November, 9, 19, 45)},
  44. // WFTPD for MSDOS
  45. {"-rwxrwxrwx 1 noone nogroup 322 Aug 19 1996 message.ftp", "message.ftp", 322, EntryTypeFile, newTime(1996, time.August, 19)},
  46. // RFC3659 format: https://tools.ietf.org/html/rfc3659#section-7
  47. {"modify=20150813224845;perm=fle;type=cdir;unique=119FBB87U4;UNIX.group=0;UNIX.mode=0755;UNIX.owner=0; .", ".", 0, EntryTypeFolder, newTime(2015, time.August, 13, 22, 48, 45)},
  48. {"modify=20150813224845;perm=fle;type=pdir;unique=119FBB87U4;UNIX.group=0;UNIX.mode=0755;UNIX.owner=0; ..", "..", 0, EntryTypeFolder, newTime(2015, time.August, 13, 22, 48, 45)},
  49. {"modify=20150806235817;perm=fle;type=dir;unique=1B20F360U4;UNIX.group=0;UNIX.mode=0755;UNIX.owner=0; movies", "movies", 0, EntryTypeFolder, newTime(2015, time.August, 6, 23, 58, 17)},
  50. {"modify=20150814172949;perm=flcdmpe;type=dir;unique=85A0C168U4;UNIX.group=0;UNIX.mode=0777;UNIX.owner=0; _upload", "_upload", 0, EntryTypeFolder, newTime(2015, time.August, 14, 17, 29, 49)},
  51. {"modify=20150813175250;perm=adfr;size=951;type=file;unique=119FBB87UE;UNIX.group=0;UNIX.mode=0644;UNIX.owner=0; welcome.msg", "welcome.msg", 951, EntryTypeFile, newTime(2015, time.August, 13, 17, 52, 50)},
  52. // Format and types have first letter UpperCase
  53. {"Modify=20150813175250;Perm=adfr;Size=951;Type=file;Unique=119FBB87UE;UNIX.group=0;UNIX.mode=0644;UNIX.owner=0; welcome.msg", "welcome.msg", 951, EntryTypeFile, newTime(2015, time.August, 13, 17, 52, 50)},
  54. // DOS DIR command output
  55. {"08-07-15 07:50PM 718 Post_PRR_20150901_1166_265118_13049.dat", "Post_PRR_20150901_1166_265118_13049.dat", 718, EntryTypeFile, newTime(2015, time.August, 7, 19, 50)},
  56. {"08-10-15 02:04PM <DIR> Billing", "Billing", 0, EntryTypeFolder, newTime(2015, time.August, 10, 14, 4)},
  57. // dir and file names that contain multiple spaces
  58. {"drwxr-xr-x 3 110 1002 3 Dec 02 2009 spaces dir name", "spaces dir name", 0, EntryTypeFolder, newTime(2009, time.December, 2)},
  59. {"-rwxr-xr-x 3 110 1002 1234567 Dec 02 2009 file name", "file name", 1234567, EntryTypeFile, newTime(2009, time.December, 2)},
  60. {"-rwxr-xr-x 3 110 1002 1234567 Dec 02 2009 foo bar ", " foo bar ", 1234567, EntryTypeFile, newTime(2009, time.December, 2)},
  61. // Odd link count from hostedftp.com
  62. {"-r-------- 0 user group 65222236 Feb 24 00:39 RegularFile", "RegularFile", 65222236, EntryTypeFile, newTime(thisYear, time.February, 24, 0, 39)},
  63. // Line with ACL persmissions
  64. {"-rwxrw-r--+ 1 521 101 2080 May 21 10:53 data.csv", "data.csv", 2080, EntryTypeFile, newTime(thisYear, time.May, 21, 10, 53)},
  65. }
  66. var listTestsSymlink = []symlinkLine{
  67. {"lrwxrwxrwx 1 root other 7 Jan 25 00:17 bin -> usr/bin", "bin", "usr/bin"},
  68. {"lrwxrwxrwx 1 0 1001 27 Jul 07 2017 R-3.4.0.pkg -> el-capitan/base/R-3.4.0.pkg", "R-3.4.0.pkg", "el-capitan/base/R-3.4.0.pkg"},
  69. }
  70. // Not supported, we expect a specific error message
  71. var listTestsFail = []unsupportedLine{
  72. {"d [R----F--] supervisor 512 Jan 16 18:53 login", errUnsupportedListLine},
  73. {"- [R----F--] rhesus 214059 Oct 20 15:27 cx.exe", errUnsupportedListLine},
  74. {"drwxr-xr-x 3 110 1002 3 Dec 02 209 pub", errUnsupportedListDate},
  75. {"modify=20150806235817;invalid;UNIX.owner=0; movies", errUnsupportedListLine},
  76. {"Zrwxrwxrwx 1 root other 7 Jan 25 00:17 bin -> usr/bin", errUnknownListEntryType},
  77. {"total 1", errUnsupportedListLine},
  78. {"000000000x ", errUnsupportedListLine}, // see https://github.com/jlaffaye/ftp/issues/97
  79. {"", errUnsupportedListLine},
  80. }
  81. func TestParseValidListLine(t *testing.T) {
  82. for _, lt := range listTests {
  83. t.Run(fmt.Sprintf("parseListLine(%v)", lt.line), func(t *testing.T) {
  84. entry, err := parseListLine(lt.line, now, time.UTC)
  85. if err != nil {
  86. t.Errorf("returned err = %v", err)
  87. return
  88. }
  89. if entry.Name != lt.name {
  90. t.Errorf("Name = '%v', want '%v'", entry.Name, lt.name)
  91. }
  92. if entry.Type != lt.entryType {
  93. t.Errorf("EntryType = %v, want %v", entry.Type, lt.entryType)
  94. }
  95. if entry.Size != lt.size {
  96. t.Errorf("Size = %v, want %v", entry.Size, lt.size)
  97. }
  98. if !entry.Time.Equal(lt.time) {
  99. t.Errorf("Time = %v, want %v", entry.Time, lt.time)
  100. }
  101. })
  102. }
  103. }
  104. func TestParseSymlinks(t *testing.T) {
  105. for _, lt := range listTestsSymlink {
  106. entry, err := parseListLine(lt.line, now, time.UTC)
  107. if err != nil {
  108. t.Errorf("parseListLine(%v) returned err = %v", lt.line, err)
  109. continue
  110. }
  111. if entry.Name != lt.name {
  112. t.Errorf("parseListLine(%v).Name = '%v', want '%v'", lt.line, entry.Name, lt.name)
  113. }
  114. if entry.Target != lt.target {
  115. t.Errorf("parseListLine(%v).Target = '%v', want '%v'", lt.line, entry.Target, lt.target)
  116. }
  117. if entry.Type != EntryTypeLink {
  118. t.Errorf("parseListLine(%v).EntryType = %v, want EntryTypeLink", lt.line, entry.Type)
  119. }
  120. }
  121. }
  122. func TestParseUnsupportedListLine(t *testing.T) {
  123. for _, lt := range listTestsFail {
  124. t.Run(fmt.Sprintf("parseListLine(%v)", lt.line), func(t *testing.T) {
  125. _, err := parseListLine(lt.line, now, time.UTC)
  126. if err == nil {
  127. t.Error("expected to fail")
  128. }
  129. if err != lt.err {
  130. t.Errorf("expected to fail with error: '%s'; was: '%s'", lt.err.Error(), err.Error())
  131. }
  132. })
  133. }
  134. }
  135. func TestSettime(t *testing.T) {
  136. tests := []struct {
  137. line string
  138. expected time.Time
  139. }{
  140. // this year, in the past
  141. {"Feb 10 23:00", newTime(thisYear, time.February, 10, 23)},
  142. // this year, less than six months in the future
  143. {"Sep 10 22:59", newTime(thisYear, time.September, 10, 22, 59)},
  144. // previous year, otherwise it would be more than 6 months in the future
  145. {"Sep 10 23:00", newTime(previousYear, time.September, 10, 23)},
  146. // far in the future
  147. {"Jan 23 2019", newTime(2019, time.January, 23)},
  148. }
  149. for _, test := range tests {
  150. entry := &Entry{}
  151. entry.setTime(strings.Fields(test.line), now, time.UTC)
  152. if !entry.Time.Equal(test.expected) {
  153. t.Errorf("setTime(%v).Time = %v, want %v", test.line, entry.Time, test.expected)
  154. }
  155. }
  156. }
  157. // newTime builds a UTC time from the given year, month, day, hour and minute
  158. func newTime(year int, month time.Month, day int, hourMinSec ...int) time.Time {
  159. var hour, min, sec int
  160. switch len(hourMinSec) {
  161. case 0:
  162. // nothing
  163. case 3:
  164. sec = hourMinSec[2]
  165. fallthrough
  166. case 2:
  167. min = hourMinSec[1]
  168. fallthrough
  169. case 1:
  170. hour = hourMinSec[0]
  171. default:
  172. panic("too many arguments")
  173. }
  174. return time.Date(year, month, day, hour, min, sec, 0, time.UTC)
  175. }