2024-01-26 11:16:55 +00:00
|
|
|
from common import * # isort:skip
|
2020-02-19 16:54:05 +00:00
|
|
|
|
2023-06-28 11:09:03 +00:00
|
|
|
from trezor import io, sdcard, utils
|
2020-02-19 16:54:05 +00:00
|
|
|
|
2023-06-28 11:09:03 +00:00
|
|
|
if utils.USE_SD_CARD:
|
|
|
|
fatfs = io.fatfs
|
2020-02-19 16:54:05 +00:00
|
|
|
|
2023-06-28 10:46:29 +00:00
|
|
|
|
2020-02-19 16:54:05 +00:00
|
|
|
class TestTrezorSdcard(unittest.TestCase):
|
|
|
|
def test_power(self):
|
2020-02-26 18:25:22 +00:00
|
|
|
# sdcard.capacity() will return 0 if the card is not powered,
|
2020-02-19 16:54:05 +00:00
|
|
|
# non-zero value otherwise
|
2020-02-26 18:25:22 +00:00
|
|
|
self.assertEqual(sdcard.capacity(), 0)
|
|
|
|
with sdcard.filesystem(mounted=False):
|
|
|
|
self.assertTrue(sdcard.capacity() > 0)
|
|
|
|
self.assertEqual(sdcard.capacity(), 0)
|
2020-02-19 16:54:05 +00:00
|
|
|
|
|
|
|
def test_nomount(self):
|
2020-02-26 18:25:22 +00:00
|
|
|
with sdcard.filesystem(mounted=False):
|
|
|
|
self.assertFalse(fatfs.is_mounted())
|
2020-02-19 16:54:05 +00:00
|
|
|
|
|
|
|
def test_mount(self):
|
|
|
|
# set up a filesystem first
|
2020-02-26 18:25:22 +00:00
|
|
|
with sdcard.filesystem(mounted=False):
|
|
|
|
fatfs.mkfs()
|
2020-02-19 16:54:05 +00:00
|
|
|
|
2020-02-26 18:25:22 +00:00
|
|
|
with sdcard.filesystem():
|
|
|
|
self.assertTrue(fatfs.is_mounted())
|
2020-02-19 16:54:05 +00:00
|
|
|
|
2020-02-26 18:25:22 +00:00
|
|
|
self.assertFalse(fatfs.is_mounted())
|
2020-02-19 16:54:05 +00:00
|
|
|
|
|
|
|
def test_nesting(self):
|
|
|
|
# set up a filesystem first
|
2020-02-26 18:25:22 +00:00
|
|
|
with sdcard.filesystem(mounted=False):
|
|
|
|
fatfs.mkfs()
|
|
|
|
|
|
|
|
self.assertEqual(sdcard.capacity(), 0)
|
|
|
|
with sdcard.filesystem():
|
|
|
|
self.assertTrue(sdcard.capacity() > 0)
|
|
|
|
self.assertTrue(fatfs.is_mounted())
|
|
|
|
with sdcard.filesystem():
|
|
|
|
self.assertTrue(sdcard.capacity() > 0)
|
|
|
|
self.assertTrue(fatfs.is_mounted())
|
|
|
|
|
|
|
|
self.assertTrue(sdcard.capacity() > 0)
|
|
|
|
self.assertTrue(fatfs.is_mounted())
|
|
|
|
|
|
|
|
self.assertEqual(sdcard.capacity(), 0)
|
|
|
|
self.assertFalse(fatfs.is_mounted())
|
2020-02-19 16:54:05 +00:00
|
|
|
|
|
|
|
def test_mount_nomount(self):
|
|
|
|
with self.assertRaises(RuntimeError):
|
2020-02-26 18:25:22 +00:00
|
|
|
with sdcard.filesystem(mounted=True):
|
|
|
|
with sdcard.filesystem(mounted=False):
|
2020-02-19 16:54:05 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
with self.assertRaises(RuntimeError):
|
2020-02-26 18:25:22 +00:00
|
|
|
with sdcard.filesystem(mounted=False):
|
|
|
|
with sdcard.filesystem(mounted=True):
|
2020-02-19 16:54:05 +00:00
|
|
|
pass
|
|
|
|
|
2020-02-21 14:53:16 +00:00
|
|
|
def test_failed_mount(self):
|
|
|
|
# set up a filesystem first
|
2020-02-26 18:25:22 +00:00
|
|
|
with sdcard.filesystem(mounted=False):
|
|
|
|
fatfs.mkfs()
|
2020-02-21 14:53:16 +00:00
|
|
|
|
2020-02-26 18:25:22 +00:00
|
|
|
with sdcard.filesystem():
|
|
|
|
self.assertTrue(fatfs.is_mounted())
|
2020-02-21 14:53:16 +00:00
|
|
|
|
|
|
|
# trash filesystem
|
|
|
|
io.sdcard.power_on()
|
|
|
|
io.sdcard.write(0, bytes([0xFF] * io.sdcard.BLOCK_SIZE))
|
|
|
|
io.sdcard.power_off()
|
|
|
|
|
|
|
|
# mounting should now fail
|
|
|
|
with self.assertRaises(OSError):
|
2020-02-26 18:25:22 +00:00
|
|
|
with sdcard.filesystem():
|
2020-02-21 14:53:16 +00:00
|
|
|
pass
|
|
|
|
|
2020-02-26 18:25:22 +00:00
|
|
|
self.assertFalse(fatfs.is_mounted())
|
|
|
|
|
2020-02-21 14:53:16 +00:00
|
|
|
# it should be possible to create an unmounted instance
|
2020-02-26 18:25:22 +00:00
|
|
|
with sdcard.filesystem(mounted=False):
|
|
|
|
fatfs.mkfs()
|
2020-02-21 14:53:16 +00:00
|
|
|
|
|
|
|
# mounting should now succeed
|
2020-02-26 18:25:22 +00:00
|
|
|
with sdcard.filesystem():
|
|
|
|
self.assertTrue(fatfs.is_mounted())
|
2020-02-19 16:54:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-06-28 11:09:03 +00:00
|
|
|
if utils.USE_SD_CARD:
|
|
|
|
unittest.main()
|