1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
from __future__ import unicode_literals
import textwrap
import time
import unittest
from fs import _ftp_parse as ftp_parse
try:
from unittest import mock
except ImportError:
import mock
time2017 = time.struct_time([2017, 11, 28, 1, 1, 1, 1, 332, 0])
class TestFTPParse(unittest.TestCase):
@mock.patch("time.localtime")
def test_parse_time(self, mock_localtime):
self.assertEqual(
ftp_parse._parse_time("JUL 05 1974", formats=["%b %d %Y"]), 142214400.0
)
mock_localtime.return_value = time2017
self.assertEqual(
ftp_parse._parse_time("JUL 05 02:00", formats=["%b %d %H:%M"]), 1499220000.0
)
self.assertEqual(
ftp_parse._parse_time("05-07-17 02:00AM", formats=["%d-%m-%y %I:%M%p"]),
1499220000.0,
)
self.assertEqual(ftp_parse._parse_time("notadate", formats=["%b %d %Y"]), None)
def test_parse(self):
self.assertListEqual(ftp_parse.parse([""]), [])
def test_parse_line(self):
self.assertIs(ftp_parse.parse_line("not a dir"), None)
@mock.patch("time.localtime")
def test_decode_linux(self, mock_localtime):
mock_localtime.return_value = time2017
directory = textwrap.dedent(
"""
lrwxrwxrwx 1 0 0 19 Jan 18 2006 debian -> ./pub/mirror/debian
drwxr-xr-x 10 0 0 4096 Aug 03 09:21 debian-archive
lrwxrwxrwx 1 0 0 27 Nov 30 2015 debian-backports -> pub/mirror/debian-backports
drwxr-xr-x 12 0 0 4096 Sep 29 13:13 pub
-rw-r--r-- 1 0 0 26 Mar 04 2010 robots.txt
drwxr-xr-x 8 foo bar 4096 Oct 4 09:05 test
drwxr-xr-x 8 f b 4096 Oct 4 09:05 test
drwxr-xr-x 2 foo-user foo-group 0 Jan 5 11:59 240485
drwxr-xr-x 2 foo.user$ foo@group_ 0 Jan 5 11:59 240485
"""
)
expected = [
{
"access": {
"group": "0",
"permissions": [
"g_r",
"g_w",
"g_x",
"o_r",
"o_w",
"o_x",
"u_r",
"u_w",
"u_x",
],
"user": "0",
},
"basic": {"is_dir": True, "name": "debian"},
"details": {"modified": 1137542400.0, "size": 19, "type": 1},
"ftp": {
"ls": "lrwxrwxrwx 1 0 0 19 Jan 18 2006 debian -> ./pub/mirror/debian"
},
},
{
"access": {
"group": "0",
"permissions": ["g_r", "g_x", "o_r", "o_x", "u_r", "u_w", "u_x"],
"user": "0",
},
"basic": {"is_dir": True, "name": "debian-archive"},
"details": {"modified": 1501752060.0, "size": 4096, "type": 1},
"ftp": {
"ls": "drwxr-xr-x 10 0 0 4096 Aug 03 09:21 debian-archive"
},
},
{
"access": {
"group": "0",
"permissions": [
"g_r",
"g_w",
"g_x",
"o_r",
"o_w",
"o_x",
"u_r",
"u_w",
"u_x",
],
"user": "0",
},
"basic": {"is_dir": True, "name": "debian-backports"},
"details": {"modified": 1448841600.0, "size": 27, "type": 1},
"ftp": {
"ls": "lrwxrwxrwx 1 0 0 27 Nov 30 2015 debian-backports -> pub/mirror/debian-backports"
},
},
{
"access": {
"group": "0",
"permissions": ["g_r", "g_x", "o_r", "o_x", "u_r", "u_w", "u_x"],
"user": "0",
},
"basic": {"is_dir": True, "name": "pub"},
"details": {"modified": 1506690780.0, "size": 4096, "type": 1},
"ftp": {
"ls": "drwxr-xr-x 12 0 0 4096 Sep 29 13:13 pub"
},
},
{
"access": {
"group": "0",
"permissions": ["g_r", "o_r", "u_r", "u_w"],
"user": "0",
},
"basic": {"is_dir": False, "name": "robots.txt"},
"details": {"modified": 1267660800.0, "size": 26, "type": 2},
"ftp": {
"ls": "-rw-r--r-- 1 0 0 26 Mar 04 2010 robots.txt"
},
},
{
"access": {
"group": "bar",
"permissions": ["g_r", "g_x", "o_r", "o_x", "u_r", "u_w", "u_x"],
"user": "foo",
},
"basic": {"is_dir": True, "name": "test"},
"details": {"modified": 1507107900.0, "size": 4096, "type": 1},
"ftp": {
"ls": "drwxr-xr-x 8 foo bar 4096 Oct 4 09:05 test"
},
},
{
"access": {
"group": "b",
"permissions": ["g_r", "g_x", "o_r", "o_x", "u_r", "u_w", "u_x"],
"user": "f",
},
"basic": {"is_dir": True, "name": "test"},
"details": {"modified": 1507107900.0, "size": 4096, "type": 1},
"ftp": {
"ls": "drwxr-xr-x 8 f b 4096 Oct 4 09:05 test"
},
},
{
"access": {
"group": "foo-group",
"permissions": ["g_r", "g_x", "o_r", "o_x", "u_r", "u_w", "u_x"],
"user": "foo-user",
},
"basic": {"is_dir": True, "name": "240485"},
"details": {"modified": 1483617540.0, "size": 0, "type": 1},
"ftp": {
"ls": "drwxr-xr-x 2 foo-user foo-group 0 Jan 5 11:59 240485"
},
},
{
"access": {
"group": "foo@group_",
"permissions": ["g_r", "g_x", "o_r", "o_x", "u_r", "u_w", "u_x"],
"user": "foo.user$",
},
"basic": {"is_dir": True, "name": "240485"},
"details": {"modified": 1483617540.0, "size": 0, "type": 1},
"ftp": {
"ls": "drwxr-xr-x 2 foo.user$ foo@group_ 0 Jan 5 11:59 240485"
},
},
]
parsed = ftp_parse.parse(directory.strip().splitlines())
self.assertListEqual(parsed, expected)
@mock.patch("time.localtime")
def test_decode_windowsnt(self, mock_localtime):
mock_localtime.return_value = time2017
directory = textwrap.dedent(
"""
unparsable line
11-02-17 02:00AM <DIR> docs
11-02-17 02:12PM <DIR> images
11-02-17 02:12PM <DIR> AM to PM
11-02-17 03:33PM 9276 logo.gif
05-11-20 22:11 <DIR> src
11-02-17 01:23 1 12
11-02-17 4:54 0 icon.bmp
11-02-17 4:54AM 0 icon.gif
11-02-17 4:54PM 0 icon.png
11-02-17 16:54 0 icon.jpg
"""
)
expected = [
{
"basic": {"is_dir": True, "name": "docs"},
"details": {"modified": 1486778400.0, "type": 1},
"ftp": {"ls": "11-02-17 02:00AM <DIR> docs"},
},
{
"basic": {"is_dir": True, "name": "images"},
"details": {"modified": 1486822320.0, "type": 1},
"ftp": {"ls": "11-02-17 02:12PM <DIR> images"},
},
{
"basic": {"is_dir": True, "name": "AM to PM"},
"details": {"modified": 1486822320.0, "type": 1},
"ftp": {"ls": "11-02-17 02:12PM <DIR> AM to PM"},
},
{
"basic": {"is_dir": False, "name": "logo.gif"},
"details": {"modified": 1486827180.0, "size": 9276, "type": 2},
"ftp": {"ls": "11-02-17 03:33PM 9276 logo.gif"},
},
{
"basic": {"is_dir": True, "name": "src"},
"details": {"modified": 1604614260.0, "type": 1},
"ftp": {"ls": "05-11-20 22:11 <DIR> src"},
},
{
"basic": {"is_dir": False, "name": "12"},
"details": {"modified": 1486776180.0, "size": 1, "type": 2},
"ftp": {"ls": "11-02-17 01:23 1 12"},
},
{
"basic": {"is_dir": False, "name": "icon.bmp"},
"details": {"modified": 1486788840.0, "size": 0, "type": 2},
"ftp": {"ls": "11-02-17 4:54 0 icon.bmp"},
},
{
"basic": {"is_dir": False, "name": "icon.gif"},
"details": {"modified": 1486788840.0, "size": 0, "type": 2},
"ftp": {"ls": "11-02-17 4:54AM 0 icon.gif"},
},
{
"basic": {"is_dir": False, "name": "icon.png"},
"details": {"modified": 1486832040.0, "size": 0, "type": 2},
"ftp": {"ls": "11-02-17 4:54PM 0 icon.png"},
},
{
"basic": {"is_dir": False, "name": "icon.jpg"},
"details": {"modified": 1486832040.0, "size": 0, "type": 2},
"ftp": {"ls": "11-02-17 16:54 0 icon.jpg"},
},
]
parsed = ftp_parse.parse(directory.strip().splitlines())
self.assertEqual(parsed, expected)
@mock.patch("time.localtime")
def test_decode_linux_suid(self, mock_localtime):
# reported in #451
mock_localtime.return_value = time2017
directory = textwrap.dedent(
"""
drwxr-sr-x 66 ftp ftp 8192 Mar 16 17:54 pub
-rw-r--r-- 1 ftp ftp 25 Mar 18 19:34 robots.txt
"""
)
expected = [
{
"access": {
"group": "ftp",
"permissions": [
"g_r",
"g_s",
"o_r",
"o_x",
"u_r",
"u_w",
"u_x",
],
"user": "ftp",
},
"basic": {"is_dir": True, "name": "pub"},
"details": {"modified": 1489686840.0, "size": 8192, "type": 1},
"ftp": {
"ls": "drwxr-sr-x 66 ftp ftp 8192 Mar 16 17:54 pub"
},
},
{
"access": {
"group": "ftp",
"permissions": [
"g_r",
"o_r",
"u_r",
"u_w",
],
"user": "ftp",
},
"basic": {"is_dir": False, "name": "robots.txt"},
"details": {"modified": 1489865640.0, "size": 25, "type": 2},
"ftp": {
"ls": "-rw-r--r-- 1 ftp ftp 25 Mar 18 19:34 robots.txt"
},
},
]
parsed = ftp_parse.parse(directory.strip().splitlines())
self.assertListEqual(parsed, expected)
@mock.patch("time.localtime")
def test_decode_linux_sticky(self, mock_localtime):
# reported in #451
mock_localtime.return_value = time2017
directory = textwrap.dedent(
"""
drwxr-xr-t 66 ftp ftp 8192 Mar 16 17:54 pub
"""
)
expected = [
{
"access": {
"group": "ftp",
"permissions": [
"g_r",
"g_x",
"o_r",
"o_t",
"u_r",
"u_w",
"u_x",
],
"user": "ftp",
},
"basic": {"is_dir": True, "name": "pub"},
"details": {"modified": 1489686840.0, "size": 8192, "type": 1},
"ftp": {
"ls": "drwxr-xr-t 66 ftp ftp 8192 Mar 16 17:54 pub"
},
},
]
self.maxDiff = None
parsed = ftp_parse.parse(directory.strip().splitlines())
self.assertListEqual(parsed, expected)
|