From 918603ad5c2df2b7da122d5db7ba5792d0fc5886 Mon Sep 17 00:00:00 2001 From: matejcik Date: Wed, 19 Feb 2020 17:54:05 +0100 Subject: [PATCH] core: add unit test for sdcard wrapper --- core/tests/test_trezor.sdcard.py | 68 ++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 core/tests/test_trezor.sdcard.py diff --git a/core/tests/test_trezor.sdcard.py b/core/tests/test_trezor.sdcard.py new file mode 100644 index 0000000000..66cb304fb3 --- /dev/null +++ b/core/tests/test_trezor.sdcard.py @@ -0,0 +1,68 @@ +from common import * + +from trezor import io, sdcard + + +class TestTrezorSdcard(unittest.TestCase): + def test_power(self): + # io.sdcard.capacity() will return 0 if the card is not powered, + # non-zero value otherwise + self.assertEqual(io.sdcard.capacity(), 0) + with sdcard.get_filesystem(mounted=False): + self.assertTrue(io.sdcard.capacity() > 0) + self.assertEqual(io.sdcard.capacity(), 0) + + def test_nomount(self): + with sdcard.get_filesystem(mounted=False) as fs: + with self.assertRaises(OSError): + fs.listdir("/") + + def test_mount(self): + # set up a filesystem first + with sdcard.get_filesystem(mounted=False) as fs: + fs.mkfs() + + with sdcard.get_filesystem() as fs: + # the following should succeed + fs.listdir("/") + + # filesystem should not be available + with self.assertRaises(OSError): + fs.listdir("/") + + def test_nesting(self): + # set up a filesystem first + with sdcard.get_filesystem(mounted=False) as fs: + fs.mkfs() + + self.assertEqual(io.sdcard.capacity(), 0) + with sdcard.get_filesystem() as fs_a: + self.assertTrue(io.sdcard.capacity() > 0) + with sdcard.get_filesystem() as fs_b: + self.assertTrue(io.sdcard.capacity() > 0) + self.assertIs(fs_a, fs_b) + fs_b.listdir("/") + self.assertTrue(io.sdcard.capacity() > 0) + # filesystem should still be mounted + fs_a.listdir("/") + + self.assertEqual(io.sdcard.capacity(), 0) + # filesystem should not be available + with self.assertRaises(OSError): + fs_a.listdir("/") + + def test_mount_nomount(self): + with self.assertRaises(RuntimeError): + with sdcard.get_filesystem(mounted=True): + with sdcard.get_filesystem(mounted=False): + pass + + with self.assertRaises(RuntimeError): + with sdcard.get_filesystem(mounted=False): + with sdcard.get_filesystem(mounted=True): + pass + + + +if __name__ == "__main__": + unittest.main()