2019-06-17 18:09:58 +00:00
|
|
|
from common import *
|
|
|
|
|
2020-02-26 17:41:00 +00:00
|
|
|
from trezorio import sdcard, fatfs
|
2019-06-17 18:09:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestTrezorIoFatfs(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2020-02-26 17:41:00 +00:00
|
|
|
sdcard.power_on()
|
|
|
|
fatfs.mkfs()
|
|
|
|
fatfs.mount()
|
2019-06-17 18:09:58 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
2020-02-26 17:41:00 +00:00
|
|
|
fatfs.unmount()
|
|
|
|
sdcard.power_off()
|
2019-06-17 18:09:58 +00:00
|
|
|
|
2019-06-23 13:58:33 +00:00
|
|
|
def _filename(self, suffix=""):
|
|
|
|
return "FILE%s.TXT" % suffix
|
|
|
|
|
|
|
|
def _dirname(self, suffix=""):
|
|
|
|
return "TREZOR%s" % suffix
|
|
|
|
|
2019-06-17 18:09:58 +00:00
|
|
|
def test_basic(self):
|
|
|
|
# test just the stuff in setup and teardown
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_mkdir(self):
|
2020-02-26 17:41:00 +00:00
|
|
|
fatfs.mkdir("/%s" % self._dirname())
|
|
|
|
s = fatfs.stat("/%s" % self._dirname())
|
2019-06-23 13:58:33 +00:00
|
|
|
self.assertEqual(s, (0, "---d-", self._dirname()))
|
2019-06-17 18:09:58 +00:00
|
|
|
|
|
|
|
def test_listdir(self):
|
2020-02-26 17:41:00 +00:00
|
|
|
fatfs.mkdir("/%s" % self._dirname())
|
|
|
|
with fatfs.open("/%s" % self._filename(), "w") as f:
|
2019-06-17 18:09:58 +00:00
|
|
|
f.write(bytearray(b"test"))
|
2020-02-26 17:41:00 +00:00
|
|
|
with fatfs.open("/%s/%s" % (self._dirname(), self._filename("2")), "w") as f:
|
2019-06-17 18:09:58 +00:00
|
|
|
f.write(bytearray(b"testtest"))
|
2020-02-26 17:41:00 +00:00
|
|
|
l = [e for e in fatfs.listdir("/")]
|
|
|
|
self.assertEqual(
|
|
|
|
l, [(0, "---d-", self._dirname()), (4, "----a", self._filename())]
|
|
|
|
)
|
|
|
|
l = [e for e in fatfs.listdir("/%s" % self._dirname())]
|
2019-06-23 13:58:33 +00:00
|
|
|
self.assertEqual(l, [(8, "----a", self._filename("2"))])
|
2019-06-17 18:09:58 +00:00
|
|
|
|
|
|
|
def test_unlink(self):
|
2020-02-26 17:41:00 +00:00
|
|
|
fatfs.mkdir("/%s" % self._dirname())
|
|
|
|
with fatfs.open("/%s" % self._filename(), "w") as f:
|
2019-06-17 18:09:58 +00:00
|
|
|
f.write(bytearray(b"test"))
|
2020-02-26 17:41:00 +00:00
|
|
|
s = fatfs.stat("/%s" % self._dirname())
|
2019-06-23 13:58:33 +00:00
|
|
|
self.assertEqual(s, (0, "---d-", self._dirname()))
|
2020-02-26 17:41:00 +00:00
|
|
|
s = fatfs.stat("/%s" % self._filename())
|
2019-06-23 13:58:33 +00:00
|
|
|
self.assertEqual(s, (4, "----a", self._filename()))
|
2020-02-26 17:41:00 +00:00
|
|
|
fatfs.unlink("/%s" % self._dirname())
|
|
|
|
fatfs.unlink("/%s" % self._filename())
|
2019-06-17 18:09:58 +00:00
|
|
|
with self.assertRaises(OSError):
|
2020-02-26 17:41:00 +00:00
|
|
|
fatfs.stat("/%s" % self._dirname())
|
2019-06-17 18:09:58 +00:00
|
|
|
with self.assertRaises(OSError):
|
2020-02-26 17:41:00 +00:00
|
|
|
self.assertRaises(fatfs.stat("/%s" % self._filename()))
|
2019-06-17 18:09:58 +00:00
|
|
|
|
|
|
|
def test_rename(self):
|
2020-02-26 17:41:00 +00:00
|
|
|
fatfs.mkdir("/%s" % self._dirname())
|
|
|
|
with fatfs.open("/%s" % self._filename(), "w") as f:
|
2019-06-17 18:09:58 +00:00
|
|
|
f.write(bytearray(b"test"))
|
2020-02-26 17:41:00 +00:00
|
|
|
s = fatfs.stat("/%s" % self._dirname())
|
2019-06-23 13:58:33 +00:00
|
|
|
self.assertEqual(s, (0, "---d-", self._dirname()))
|
2020-02-26 17:41:00 +00:00
|
|
|
s = fatfs.stat("/%s" % self._filename())
|
2019-06-23 13:58:33 +00:00
|
|
|
self.assertEqual(s, (4, "----a", self._filename()))
|
2020-02-26 17:41:00 +00:00
|
|
|
fatfs.rename("/%s" % self._dirname(), "/%s" % self._dirname("2"))
|
|
|
|
fatfs.rename("/%s" % self._filename(), "/%s" % self._filename("2"))
|
2019-06-17 18:09:58 +00:00
|
|
|
with self.assertRaises(OSError):
|
2020-02-26 17:41:00 +00:00
|
|
|
fatfs.stat("/%s" % self._dirname())
|
2019-06-17 18:09:58 +00:00
|
|
|
with self.assertRaises(OSError):
|
2020-02-26 17:41:00 +00:00
|
|
|
self.assertRaises(fatfs.stat("/%s" % self._filename()))
|
|
|
|
s = fatfs.stat("/%s" % self._dirname("2"))
|
2019-06-23 13:58:33 +00:00
|
|
|
self.assertEqual(s, (0, "---d-", self._dirname("2")))
|
2020-02-26 17:41:00 +00:00
|
|
|
s = fatfs.stat("/%s" % self._filename("2"))
|
2019-06-23 13:58:33 +00:00
|
|
|
self.assertEqual(s, (4, "----a", self._filename("2")))
|
2019-06-17 18:09:58 +00:00
|
|
|
|
|
|
|
def test_open_rw(self):
|
2020-02-26 17:41:00 +00:00
|
|
|
with fatfs.open("/%s" % self._filename(), "w") as f:
|
2019-06-17 18:09:58 +00:00
|
|
|
f.write(bytearray(b"test"))
|
2020-02-26 17:41:00 +00:00
|
|
|
with fatfs.open("/%s" % self._filename(), "r") as f:
|
2019-06-17 18:09:58 +00:00
|
|
|
b = bytearray(100)
|
|
|
|
r = f.read(b)
|
|
|
|
self.assertEqual(r, 4)
|
|
|
|
self.assertEqual(bytes(b[:4]), b"test")
|
|
|
|
|
|
|
|
def test_open_a(self):
|
2020-02-26 17:41:00 +00:00
|
|
|
with fatfs.open("/%s" % self._filename(), "w") as f:
|
2019-06-17 18:09:58 +00:00
|
|
|
f.write(bytearray(b"test" * 200))
|
2020-02-26 17:41:00 +00:00
|
|
|
with fatfs.open("/%s" % self._filename(), "a") as f:
|
2019-06-17 18:09:58 +00:00
|
|
|
f.seek(800)
|
|
|
|
f.write(bytearray(b"TEST" * 200))
|
2020-02-26 17:41:00 +00:00
|
|
|
with fatfs.open("/%s" % self._filename(), "r") as f:
|
2019-06-17 18:09:58 +00:00
|
|
|
b = bytearray(2000)
|
|
|
|
r = f.read(b)
|
|
|
|
self.assertEqual(r, 1600)
|
|
|
|
self.assertEqual(bytes(b[:1600]), b"test" * 200 + b"TEST" * 200)
|
|
|
|
|
|
|
|
def test_seek(self):
|
2020-02-26 17:41:00 +00:00
|
|
|
with fatfs.open("/%s" % self._filename(), "w+") as f:
|
2019-06-17 18:09:58 +00:00
|
|
|
f.write(bytearray(b"test" * 10))
|
|
|
|
f.seek(2)
|
|
|
|
b = bytearray(8)
|
|
|
|
r = f.read(b)
|
|
|
|
self.assertEqual(r, 8)
|
|
|
|
self.assertEqual(bytes(b[:8]), b"sttestte")
|
|
|
|
|
|
|
|
def test_truncate(self):
|
2020-02-26 17:41:00 +00:00
|
|
|
with fatfs.open("/%s" % self._filename(), "w") as f:
|
2019-06-17 18:09:58 +00:00
|
|
|
f.write(bytearray(b"test" * 100))
|
2020-02-26 17:41:00 +00:00
|
|
|
s = fatfs.stat("/%s" % self._filename())
|
2019-06-23 13:58:33 +00:00
|
|
|
self.assertEqual(s, (400, "----a", self._filename()))
|
2020-02-26 17:41:00 +00:00
|
|
|
with fatfs.open("/%s" % self._filename(), "a") as f:
|
2019-06-17 18:09:58 +00:00
|
|
|
f.seek(111)
|
|
|
|
f.truncate()
|
2020-02-26 17:41:00 +00:00
|
|
|
s = fatfs.stat("/%s" % self._filename())
|
2019-06-23 13:58:33 +00:00
|
|
|
self.assertEqual(s, (111, "----a", self._filename()))
|
|
|
|
|
|
|
|
|
|
|
|
class TestTrezorIoFatfsLfn(TestTrezorIoFatfs):
|
|
|
|
def _filename(self, suffix=""):
|
|
|
|
return "reallylongfilename%s.textfile" % suffix
|
|
|
|
|
|
|
|
def _dirname(self, suffix=""):
|
|
|
|
return "reallylongdirname%s" % suffix
|
2019-06-17 18:09:58 +00:00
|
|
|
|
|
|
|
|
2020-02-26 17:41:00 +00:00
|
|
|
class TestTrezorIoFatfsMounting(unittest.TestCase):
|
|
|
|
MOUNTED_METHODS = [
|
|
|
|
("open", ("hello.txt", "w")),
|
|
|
|
("listdir", ("",)),
|
|
|
|
("mkdir", ("testdir",)),
|
|
|
|
("unlink", ("hello.txt",)),
|
|
|
|
("stat", ("testdir",)),
|
|
|
|
("rename", ("testdir", "newdir")),
|
|
|
|
("setlabel", ("label",)),
|
|
|
|
]
|
|
|
|
UNMOUNTED_METHODS = [
|
|
|
|
("mkfs", ()),
|
|
|
|
]
|
|
|
|
OTHER = {
|
|
|
|
"__name__",
|
|
|
|
"__class__",
|
|
|
|
"mount",
|
|
|
|
"unmount",
|
|
|
|
"is_mounted",
|
|
|
|
"FatFSFile",
|
|
|
|
"FatFSDir",
|
|
|
|
}
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
sdcard.power_on()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
sdcard.power_off()
|
|
|
|
|
|
|
|
def test_mount_unmount(self):
|
|
|
|
fatfs.mkfs()
|
|
|
|
|
|
|
|
self.assertFalse(fatfs.is_mounted())
|
|
|
|
fatfs.mount()
|
|
|
|
self.assertTrue(fatfs.is_mounted())
|
|
|
|
fatfs.mount()
|
|
|
|
self.assertTrue(fatfs.is_mounted())
|
|
|
|
fatfs.unmount()
|
|
|
|
self.assertFalse(fatfs.is_mounted())
|
|
|
|
|
|
|
|
def test_no_filesystem(self):
|
|
|
|
# trash FAT table
|
|
|
|
sdcard.write(0, bytes([0xFF] * sdcard.BLOCK_SIZE))
|
|
|
|
|
|
|
|
self.assertFalse(fatfs.is_mounted())
|
|
|
|
try:
|
|
|
|
fatfs.mount()
|
|
|
|
self.fail("should have raised")
|
|
|
|
except OSError as e:
|
|
|
|
self.assertEqual(e.args[0], 19) # ENODEV
|
|
|
|
self.assertFalse(fatfs.is_mounted())
|
|
|
|
|
|
|
|
def test_exhaustive(self):
|
|
|
|
all_symbols = (
|
|
|
|
set(name for name, call in (self.MOUNTED_METHODS + self.UNMOUNTED_METHODS))
|
|
|
|
| self.OTHER
|
|
|
|
)
|
|
|
|
self.assertEqual(set(dir(fatfs)), all_symbols)
|
|
|
|
|
|
|
|
def test_mounted(self):
|
|
|
|
fatfs.mkfs()
|
|
|
|
fatfs.mount()
|
|
|
|
self.assertTrue(fatfs.is_mounted())
|
|
|
|
|
|
|
|
for name, call in self.MOUNTED_METHODS:
|
|
|
|
function = getattr(fatfs, name)
|
|
|
|
function(*call)
|
|
|
|
|
|
|
|
for name, call in self.UNMOUNTED_METHODS:
|
|
|
|
function = getattr(fatfs, name)
|
|
|
|
try:
|
|
|
|
function(*call)
|
|
|
|
self.fail("should have raised")
|
|
|
|
except OSError as e:
|
|
|
|
self.assertEqual(e.args[0], 16) # EBUSY
|
|
|
|
|
|
|
|
def test_unmounted(self):
|
|
|
|
fatfs.unmount()
|
|
|
|
fatfs.mkfs()
|
|
|
|
self.assertFalse(fatfs.is_mounted())
|
|
|
|
|
|
|
|
for name, call in self.UNMOUNTED_METHODS:
|
|
|
|
function = getattr(fatfs, name)
|
|
|
|
function(*call)
|
|
|
|
self.assertFalse(fatfs.is_mounted())
|
|
|
|
|
|
|
|
for name, call in self.MOUNTED_METHODS:
|
|
|
|
function = getattr(fatfs, name)
|
|
|
|
try:
|
|
|
|
function(*call)
|
|
|
|
self.fail("should have raised")
|
|
|
|
except OSError as e:
|
|
|
|
self.assertEqual(e.args[0], 19) # ENODEV
|
|
|
|
|
|
|
|
|
|
|
|
class TestTrezorIoFatfsAndSdcard(unittest.TestCase):
|
|
|
|
def test_sd_power(self):
|
|
|
|
sdcard.power_off()
|
|
|
|
self.assertFalse(fatfs.is_mounted())
|
|
|
|
self.assertRaises(OSError, fatfs.mount)
|
|
|
|
|
|
|
|
sdcard.power_on()
|
|
|
|
self.assertFalse(fatfs.is_mounted())
|
|
|
|
fatfs.mkfs()
|
|
|
|
fatfs.mount()
|
|
|
|
self.assertTrue(fatfs.is_mounted())
|
|
|
|
|
|
|
|
sdcard.power_off()
|
|
|
|
self.assertFalse(fatfs.is_mounted())
|
|
|
|
|
|
|
|
|
2019-06-17 18:09:58 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|