Skip to content

Commit 355472e

Browse files
committed
Add test case for file rename after a delay
Add a test case which copies a hex file to a device with the wrong extension, delays for 10s and then renames the file to the correct extension. This simulates downloading a file from a browser.
1 parent 4dccc9c commit 355472e

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

test/msd_test.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ def __init__(self, board, parent_test, test_name):
7676
self._mock_file_list_after = []
7777
self._mock_dir_list_after = []
7878
self._programming_file_name = None
79+
self._new_name = None
80+
self._new_name_delay = None
7981

8082
def set_shutils_copy(self, source_file_name):
8183
"""
@@ -118,6 +120,11 @@ def set_expected_failure_msg(self, msg):
118120
assert msg is None or type(msg) is str
119121
self._expected_failure_msg = msg
120122

123+
def set_rename_after_delay(self, delay, name):
124+
"""Set delay and name to rename file to"""
125+
self._new_name = name
126+
self._new_name_delay = delay
127+
121128
def add_mock_files(self, file_list):
122129
"""Add a list of tuples containing a file and contents"""
123130
self._mock_file_list.extend(file_list)
@@ -202,7 +209,18 @@ def run(self):
202209
with open(file_path, 'wb') as file_handle:
203210
file_handle.write(file_contents)
204211

205-
self.board.wait_for_remount(test_info)
212+
# Delay if set
213+
if self._new_name_delay is not None:
214+
time.sleep(self._new_name_delay)
215+
216+
# Wait for remount if the copied file is present
217+
if os.path.isfile(programming_file_name):
218+
if self._new_name is not None:
219+
new_name = self.board.get_file_path(self._new_name)
220+
os.rename(programming_file_name, new_name)
221+
self.board.wait_for_remount(test_info)
222+
else:
223+
test_info.failure('Missed device remount')
206224

207225
# Verify the disk is still valid
208226
self.board.test_fs(test_info)
@@ -336,6 +354,16 @@ def test_mass_storage(workspace, parent_test):
336354
# when a binary file is loaded, since there is no way to
337355
# tell where the end of the file is.
338356

357+
# Test writing a file with an unknown extension and then renaming it
358+
# This simulates a browser downloading a file
359+
# Note - firefox on linux typically creates an empty file and
360+
# after ~8s the contents get flushed to the disk
361+
test = MassStorageTester(board, test_info, "Rename after delay")
362+
test.set_programming_data(hex_file_contents, 'image.cdownload')
363+
test.set_rename_after_delay(10.0, 'image.hex')
364+
test.set_expected_data(bin_file_contents)
365+
test.run()
366+
339367
# Finally, load a good binary
340368
test = MassStorageTester(board, test_info, "Load good file to restore state")
341369
test.set_programming_data(hex_file_contents, 'image.hex')

0 commit comments

Comments
 (0)