Skip to content

Commit d79000d

Browse files
jeplerdpgeorge
authored andcommitted
tests/extmod: Add (failing) test for VfsPosix in readonly mode.
I noticed that operations such as unlink could be performed on a nominally read-only VfsPosix. Signed-off-by: Jeff Epler <jepler@gmail.com>
1 parent 6515cd0 commit d79000d

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

tests/extmod/vfs_posix_readonly.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Test for VfsPosix
2+
3+
try:
4+
import gc, os, vfs, errno
5+
6+
vfs.VfsPosix
7+
except (ImportError, AttributeError):
8+
print("SKIP")
9+
raise SystemExit
10+
11+
# We need a directory for testing that doesn't already exist.
12+
# Skip the test if it does exist.
13+
temp_dir = "vfs_posix_readonly_test_dir"
14+
try:
15+
os.stat(temp_dir)
16+
raise SystemExit("Target directory {} exists".format(temp_dir))
17+
except OSError:
18+
pass
19+
20+
# mkdir (skip test if whole filesystem is readonly)
21+
try:
22+
os.mkdir(temp_dir)
23+
except OSError as e:
24+
if e.errno == errno.EROFS:
25+
print("SKIP")
26+
raise SystemExit
27+
28+
fs_factory = lambda: vfs.VfsPosix(temp_dir)
29+
30+
# mount
31+
fs = fs_factory()
32+
vfs.mount(fs, "/vfs")
33+
34+
with open("/vfs/file", "w") as f:
35+
f.write("content")
36+
37+
# test reading works
38+
with open("/vfs/file") as f:
39+
print("file:", f.read())
40+
41+
os.mkdir("/vfs/emptydir")
42+
43+
# umount
44+
vfs.umount("/vfs")
45+
46+
# mount read-only
47+
fs = fs_factory()
48+
vfs.mount(fs, "/vfs", readonly=True)
49+
50+
# test reading works
51+
with open("/vfs/file") as f:
52+
print("file 2:", f.read())
53+
54+
# test writing fails
55+
try:
56+
with open("/vfs/test_write", "w"):
57+
pass
58+
print("opened")
59+
except OSError as er:
60+
print(repr(er))
61+
62+
# test removing fails
63+
try:
64+
os.unlink("/vfs/file")
65+
print("unlinked")
66+
except OSError as er:
67+
print(repr(er))
68+
69+
# test renaming fails
70+
try:
71+
os.rename("/vfs/file2", "/vfs/renamed")
72+
print("renamed")
73+
except OSError as er:
74+
print(repr(er))
75+
76+
# test removing directory fails
77+
try:
78+
os.rmdir("/vfs/emptydir")
79+
print("rmdir'd")
80+
except OSError as er:
81+
print(repr(er))
82+
83+
# test creating directory fails
84+
try:
85+
os.mkdir("/vfs/emptydir2")
86+
print("mkdir'd")
87+
except OSError as er:
88+
print(repr(er))
89+
90+
# umount
91+
vfs.umount("/vfs")
92+
93+
fs = fs_factory()
94+
vfs.mount(fs, "/vfs")
95+
96+
os.rmdir("/vfs/emptydir")
97+
os.unlink("/vfs/file")
98+
99+
os.rmdir(temp_dir)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
file: content
2+
file 2: content
3+
OSError(30,)
4+
OSError(30,)
5+
OSError(30,)
6+
OSError(30,)
7+
OSError(30,)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy