parse_test.go 7.5 KB

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