1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-18 05:28:40 +00:00
trezor-firmware/core/tests/test_trezor.io.fatfs.py

132 lines
4.8 KiB
Python
Raw Normal View History

2019-06-17 18:09:58 +00:00
from common import *
from trezor import io
class TestTrezorIoFatfs(unittest.TestCase):
def setUp(self):
self.sd = io.SDCard()
self.sd.power(True)
self.fs = io.FatFS()
self.fs.mkfs()
self.fs.mount()
def tearDown(self):
self.fs.unmount()
self.sd.power(False)
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):
self.fs.mkdir("/%s" % self._dirname())
s = self.fs.stat("/%s" % self._dirname())
self.assertEqual(s, (0, "---d-", self._dirname()))
2019-06-17 18:09:58 +00:00
def test_listdir(self):
self.fs.mkdir("/%s" % self._dirname())
with self.fs.open("/%s" % self._filename(), "w") as f:
2019-06-17 18:09:58 +00:00
f.write(bytearray(b"test"))
with self.fs.open("/%s/%s" % (self._dirname(), self._filename("2")), "w") as f:
2019-06-17 18:09:58 +00:00
f.write(bytearray(b"testtest"))
l = [e for e in self.fs.listdir("/")]
self.assertEqual(l, [(0, "---d-", self._dirname()), (4, "----a", self._filename())])
l = [e for e in self.fs.listdir("/%s" % self._dirname())]
self.assertEqual(l, [(8, "----a", self._filename("2"))])
2019-06-17 18:09:58 +00:00
def test_unlink(self):
self.fs.mkdir("/%s" % self._dirname())
with self.fs.open("/%s" % self._filename(), "w") as f:
2019-06-17 18:09:58 +00:00
f.write(bytearray(b"test"))
s = self.fs.stat("/%s" % self._dirname())
self.assertEqual(s, (0, "---d-", self._dirname()))
s = self.fs.stat("/%s" % self._filename())
self.assertEqual(s, (4, "----a", self._filename()))
self.fs.unlink("/%s" % self._dirname())
self.fs.unlink("/%s" % self._filename())
2019-06-17 18:09:58 +00:00
with self.assertRaises(OSError):
self.fs.stat("/%s" % self._dirname())
2019-06-17 18:09:58 +00:00
with self.assertRaises(OSError):
self.assertRaises(self.fs.stat("/%s" % self._filename()))
2019-06-17 18:09:58 +00:00
def test_rename(self):
self.fs.mkdir("/%s" % self._dirname())
with self.fs.open("/%s" % self._filename(), "w") as f:
2019-06-17 18:09:58 +00:00
f.write(bytearray(b"test"))
s = self.fs.stat("/%s" % self._dirname())
self.assertEqual(s, (0, "---d-", self._dirname()))
s = self.fs.stat("/%s" % self._filename())
self.assertEqual(s, (4, "----a", self._filename()))
self.fs.rename("/%s" % self._dirname(), "/%s" % self._dirname("2"))
self.fs.rename("/%s" % self._filename(), "/%s" % self._filename("2"))
2019-06-17 18:09:58 +00:00
with self.assertRaises(OSError):
self.fs.stat("/%s" % self._dirname())
2019-06-17 18:09:58 +00:00
with self.assertRaises(OSError):
self.assertRaises(self.fs.stat("/%s" % self._filename()))
s = self.fs.stat("/%s" % self._dirname("2"))
self.assertEqual(s, (0, "---d-", self._dirname("2")))
s = self.fs.stat("/%s" % self._filename("2"))
self.assertEqual(s, (4, "----a", self._filename("2")))
2019-06-17 18:09:58 +00:00
def test_open_rw(self):
with self.fs.open("/%s" % self._filename(), "w") as f:
2019-06-17 18:09:58 +00:00
f.write(bytearray(b"test"))
with self.fs.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):
with self.fs.open("/%s" % self._filename(), "w") as f:
2019-06-17 18:09:58 +00:00
f.write(bytearray(b"test" * 200))
with self.fs.open("/%s" % self._filename(), "a") as f:
2019-06-17 18:09:58 +00:00
f.seek(800)
f.write(bytearray(b"TEST" * 200))
with self.fs.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):
with self.fs.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):
with self.fs.open("/%s" % self._filename(), "w") as f:
2019-06-17 18:09:58 +00:00
f.write(bytearray(b"test" * 100))
s = self.fs.stat("/%s" % self._filename())
self.assertEqual(s, (400, "----a", self._filename()))
with self.fs.open("/%s" % self._filename(), "a") as f:
2019-06-17 18:09:58 +00:00
f.seek(111)
f.truncate()
s = self.fs.stat("/%s" % self._filename())
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
if __name__ == "__main__":
unittest.main()