1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-10 09:39:00 +00:00
trezor-firmware/core/tests/test_trezor.sdcard.py

94 lines
2.7 KiB
Python
Raw Normal View History

from common import * # isort:skip
2020-02-19 16:54:05 +00:00
from trezor import io, sdcard, utils
2020-02-19 16:54:05 +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
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-26 18:25:22 +00:00
with sdcard.filesystem():
self.assertTrue(fatfs.is_mounted())
# 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():
pass
2020-02-26 18:25:22 +00:00
self.assertFalse(fatfs.is_mounted())
# it should be possible to create an unmounted instance
2020-02-26 18:25:22 +00:00
with sdcard.filesystem(mounted=False):
fatfs.mkfs()
# 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__":
if utils.USE_SD_CARD:
unittest.main()