2024-01-26 11:16:55 +00:00
|
|
|
from common import * # isort:skip
|
2019-06-17 18:09:58 +00:00
|
|
|
|
2023-06-28 11:09:03 +00:00
|
|
|
from trezor import utils
|
2023-06-28 10:58:54 +00:00
|
|
|
|
2023-06-28 11:09:03 +00:00
|
|
|
if utils.USE_SD_CARD:
|
|
|
|
from trezorio import fatfs, sdcard
|
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=""):
|
2021-09-27 10:13:51 +00:00
|
|
|
return f"FILE{suffix}.TXT"
|
2019-06-23 13:58:33 +00:00
|
|
|
|
|
|
|
def _dirname(self, suffix=""):
|
2021-09-27 10:13:51 +00:00
|
|
|
return f"TREZOR{suffix}"
|
2019-06-23 13:58:33 +00:00
|
|
|
|
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):
|
2021-09-27 10:13:51 +00:00
|
|
|
fatfs.mkdir(f"/{self._dirname()}")
|
|
|
|
s = fatfs.stat(f"/{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):
|
2021-09-27 10:13:51 +00:00
|
|
|
fatfs.mkdir(f"/{self._dirname()}")
|
|
|
|
with fatfs.open(f"{self._filename()}", "w") as f:
|
2019-06-17 18:09:58 +00:00
|
|
|
f.write(bytearray(b"test"))
|
2021-09-27 10:13:51 +00:00
|
|
|
with fatfs.open(f"/{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())]
|
|
|
|
)
|
2021-09-27 10:13:51 +00:00
|
|
|
l = [e for e in fatfs.listdir(f"/{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):
|
2021-09-27 10:13:51 +00:00
|
|
|
fatfs.mkdir(f"/{self._dirname()}")
|
|
|
|
with fatfs.open(f"/{self._filename()}", "w") as f:
|
2019-06-17 18:09:58 +00:00
|
|
|
f.write(bytearray(b"test"))
|
2021-09-27 10:13:51 +00:00
|
|
|
s = fatfs.stat(f"/{self._dirname()}")
|
2019-06-23 13:58:33 +00:00
|
|
|
self.assertEqual(s, (0, "---d-", self._dirname()))
|
2021-09-27 10:13:51 +00:00
|
|
|
s = fatfs.stat(f"/{self._filename()}")
|
2019-06-23 13:58:33 +00:00
|
|
|
self.assertEqual(s, (4, "----a", self._filename()))
|
2021-09-27 10:13:51 +00:00
|
|
|
fatfs.unlink(f"/{self._dirname()}")
|
|
|
|
fatfs.unlink(f"/{self._filename()}")
|
2020-03-05 11:24:26 +00:00
|
|
|
with self.assertRaises(fatfs.FatFSError):
|
2021-09-27 10:13:51 +00:00
|
|
|
fatfs.stat(f"/{self._dirname()}")
|
2020-03-05 11:24:26 +00:00
|
|
|
with self.assertRaises(fatfs.FatFSError):
|
2021-09-27 10:13:51 +00:00
|
|
|
self.assertRaises(fatfs.stat(f"/{self._filename()}"))
|
2019-06-17 18:09:58 +00:00
|
|
|
|
|
|
|
def test_rename(self):
|
2021-09-27 10:13:51 +00:00
|
|
|
fatfs.mkdir(f"/{self._dirname()}")
|
|
|
|
with fatfs.open(f"/{self._filename()}", "w") as f:
|
2019-06-17 18:09:58 +00:00
|
|
|
f.write(bytearray(b"test"))
|
2021-09-27 10:13:51 +00:00
|
|
|
s = fatfs.stat(f"/{self._dirname()}")
|
2019-06-23 13:58:33 +00:00
|
|
|
self.assertEqual(s, (0, "---d-", self._dirname()))
|
2021-09-27 10:13:51 +00:00
|
|
|
s = fatfs.stat(f"/{self._filename()}")
|
2019-06-23 13:58:33 +00:00
|
|
|
self.assertEqual(s, (4, "----a", self._filename()))
|
2021-09-27 10:13:51 +00:00
|
|
|
fatfs.rename(f"/{self._dirname()}", f"/{self._dirname('2')}")
|
|
|
|
fatfs.rename(f"/{self._filename()}", f"/{self._filename('2')}")
|
2020-03-05 11:24:26 +00:00
|
|
|
with self.assertRaises(fatfs.FatFSError):
|
2021-09-27 10:13:51 +00:00
|
|
|
fatfs.stat(f"/{self._dirname()}")
|
2020-03-05 11:24:26 +00:00
|
|
|
with self.assertRaises(fatfs.FatFSError):
|
2021-09-27 10:13:51 +00:00
|
|
|
self.assertRaises(fatfs.stat(f"/{self._filename()}"))
|
|
|
|
s = fatfs.stat(f"/{self._dirname('2')}")
|
2019-06-23 13:58:33 +00:00
|
|
|
self.assertEqual(s, (0, "---d-", self._dirname("2")))
|
2021-09-27 10:13:51 +00:00
|
|
|
s = fatfs.stat(f"/{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):
|
2021-09-27 10:13:51 +00:00
|
|
|
with fatfs.open(f"/{self._filename()}", "w") as f:
|
2019-06-17 18:09:58 +00:00
|
|
|
f.write(bytearray(b"test"))
|
2021-09-27 10:13:51 +00:00
|
|
|
with fatfs.open(f"/{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):
|
2021-09-27 10:13:51 +00:00
|
|
|
with fatfs.open(f"/{self._filename()}", "w") as f:
|
2019-06-17 18:09:58 +00:00
|
|
|
f.write(bytearray(b"test" * 200))
|
2021-09-27 10:13:51 +00:00
|
|
|
with fatfs.open(f"{self._filename()}", "a") as f:
|
2019-06-17 18:09:58 +00:00
|
|
|
f.seek(800)
|
|
|
|
f.write(bytearray(b"TEST" * 200))
|
2021-09-27 10:13:51 +00:00
|
|
|
with fatfs.open(f"/{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):
|
2021-09-27 10:13:51 +00:00
|
|
|
with fatfs.open(f"/{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):
|
2021-09-27 10:13:51 +00:00
|
|
|
with fatfs.open(f"/{self._filename()}", "w") as f:
|
2019-06-17 18:09:58 +00:00
|
|
|
f.write(bytearray(b"test" * 100))
|
2021-09-27 10:13:51 +00:00
|
|
|
s = fatfs.stat(f"/{self._filename()}")
|
2019-06-23 13:58:33 +00:00
|
|
|
self.assertEqual(s, (400, "----a", self._filename()))
|
2021-09-27 10:13:51 +00:00
|
|
|
with fatfs.open(f"/{self._filename()}", "a") as f:
|
2019-06-17 18:09:58 +00:00
|
|
|
f.seek(111)
|
|
|
|
f.truncate()
|
2021-09-27 10:13:51 +00:00
|
|
|
s = fatfs.stat(f"/{self._filename()}")
|
2019-06-23 13:58:33 +00:00
|
|
|
self.assertEqual(s, (111, "----a", self._filename()))
|
|
|
|
|
|
|
|
|
|
|
|
class TestTrezorIoFatfsLfn(TestTrezorIoFatfs):
|
|
|
|
def _filename(self, suffix=""):
|
2021-09-27 10:13:51 +00:00
|
|
|
return f"reallylongfilename{suffix}.textfile"
|
2019-06-23 13:58:33 +00:00
|
|
|
|
|
|
|
def _dirname(self, suffix=""):
|
2021-09-27 10:13:51 +00:00
|
|
|
return f"reallylongdirname{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", ()),
|
|
|
|
]
|
|
|
|
|
|
|
|
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")
|
2020-03-05 11:24:26 +00:00
|
|
|
except fatfs.FatFSError as e:
|
|
|
|
self.assertIsInstance(e, fatfs.NoFilesystem)
|
|
|
|
# check that the proper error code is set on the NoFilesystem subclass
|
|
|
|
self.assertEqual(e.args[0], fatfs.FR_NO_FILESYSTEM)
|
2020-02-26 17:41:00 +00:00
|
|
|
self.assertFalse(fatfs.is_mounted())
|
|
|
|
|
|
|
|
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")
|
2020-03-05 11:24:26 +00:00
|
|
|
except fatfs.FatFSError as e:
|
|
|
|
self.assertEqual(e.args[0], fatfs.FR_LOCKED)
|
2020-02-26 17:41:00 +00:00
|
|
|
|
|
|
|
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")
|
2020-03-05 11:24:26 +00:00
|
|
|
except fatfs.FatFSError as e:
|
|
|
|
self.assertIsInstance(e, fatfs.NotMounted)
|
|
|
|
# check that the proper error code is set on the NotMounted subclass
|
|
|
|
self.assertEqual(e.args[0], fatfs.FR_NOT_READY)
|
2020-02-26 17:41:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestTrezorIoFatfsAndSdcard(unittest.TestCase):
|
|
|
|
def test_sd_power(self):
|
|
|
|
sdcard.power_off()
|
|
|
|
self.assertFalse(fatfs.is_mounted())
|
2020-03-05 11:24:26 +00:00
|
|
|
self.assertRaises(fatfs.FatFSError, fatfs.mount)
|
2020-02-26 17:41:00 +00:00
|
|
|
|
|
|
|
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__":
|
2023-06-28 11:09:03 +00:00
|
|
|
if utils.USE_SD_CARD:
|
|
|
|
unittest.main()
|