tests: Add null-byte exec tests
Some commands may print out NULL bytes in the middle of their output,
make sure everything works correctly.
Tomas Bzatek authored 3 years ago
Vojtech Trefny committed 3 years ago
279 | 279 | status = BlockDev.utils_exec_and_report_progress(["bash", "-c", "for i in {1..%d}; do echo \"%s\"; echo \"%s\" >&2; done" % (cnt, self.EXEC_PROGRESS_MSG, self.EXEC_PROGRESS_MSG)], None, None) |
280 | 280 | self.assertTrue(status) |
281 | 281 | |
282 | def test_exec_null_bytes(self): | |
283 | """Verify that null bytes in the output are processed properly""" | |
284 | ||
285 | TEST_DATA = ["First line", "Second line", "Third line"] | |
286 | ||
287 | status, out = BlockDev.utils_exec_and_capture_output(["bash", "-c", "echo -e \"%s\\0%s\\n%s\"" % (TEST_DATA[0], TEST_DATA[1], TEST_DATA[2])]) | |
288 | self.assertTrue(status) | |
289 | self.assertTrue(TEST_DATA[0] in out) | |
290 | self.assertTrue(TEST_DATA[1] in out) | |
291 | self.assertTrue(TEST_DATA[2] in out) | |
292 | self.assertFalse("kuku!" in out) | |
293 | ||
294 | # ten matches | |
295 | cnt = 10 | |
296 | self.num_matches = 0 | |
297 | status = BlockDev.utils_exec_and_report_progress(["bash", "-c", "for i in {1..%d}; do echo -e \"%s\\0%s\"; done" % (cnt, self.EXEC_PROGRESS_MSG, self.EXEC_PROGRESS_MSG)], None, self.my_exec_progress_func) | |
298 | self.assertTrue(status) | |
299 | self.assertEqual(self.num_matches, cnt * 2) | |
300 | ||
301 | # 100k matches | |
302 | cnt = 100000 | |
303 | self.num_matches = 0 | |
304 | status = BlockDev.utils_exec_and_report_progress(["bash", "-c", "for i in {1..%d}; do echo -e \"%s\\0%s\"; done" % (cnt, self.EXEC_PROGRESS_MSG, self.EXEC_PROGRESS_MSG)], None, self.my_exec_progress_func) | |
305 | self.assertTrue(status) | |
306 | self.assertEqual(self.num_matches, cnt * 2) | |
307 | ||
308 | # 100k matches on stderr | |
309 | self.num_matches = 0 | |
310 | status = BlockDev.utils_exec_and_report_progress(["bash", "-c", "for i in {1..%d}; do echo -e \"%s\\0%s\" >&2; done" % (cnt, self.EXEC_PROGRESS_MSG, self.EXEC_PROGRESS_MSG)], None, self.my_exec_progress_func) | |
311 | self.assertTrue(status) | |
312 | self.assertEqual(self.num_matches, cnt * 2) | |
313 | ||
314 | # 100k matches on stderr and stdout | |
315 | self.num_matches = 0 | |
316 | status = BlockDev.utils_exec_and_report_progress(["bash", "-c", "for i in {1..%d}; do echo -e \"%s\\0%s\"; echo -e \"%s\\0%s\" >&2; done" % (cnt, self.EXEC_PROGRESS_MSG, self.EXEC_PROGRESS_MSG, self.EXEC_PROGRESS_MSG, self.EXEC_PROGRESS_MSG)], None, self.my_exec_progress_func) | |
317 | self.assertTrue(status) | |
318 | self.assertEqual(self.num_matches, cnt * 4) | |
319 | ||
320 | # stdout and stderr output, non-zero return from the command and very long exception message | |
321 | self.num_matches = 0 | |
322 | with six.assertRaisesRegex(self, GLib.GError, r"Process reported exit code 66"): | |
323 | status = BlockDev.utils_exec_and_report_progress(["bash", "-c", "for i in {1..%d}; do echo -e \"%s\\0%s\"; echo -e \"%s\\0%s\" >&2; done; exit 66" % (cnt, self.EXEC_PROGRESS_MSG, self.EXEC_PROGRESS_MSG, self.EXEC_PROGRESS_MSG, self.EXEC_PROGRESS_MSG)], None, self.my_exec_progress_func) | |
324 | self.assertEqual(self.num_matches, cnt * 4) | |
325 | ||
326 | # no progress reporting callback given, tests slightly different code path | |
327 | status = BlockDev.utils_exec_and_report_progress(["bash", "-c", "for i in {1..%d}; do echo -e \"%s\\0%s\"; echo -e \"%s\\0%s\" >&2; done" % (cnt, self.EXEC_PROGRESS_MSG, self.EXEC_PROGRESS_MSG, self.EXEC_PROGRESS_MSG, self.EXEC_PROGRESS_MSG)], None, None) | |
328 | self.assertTrue(status) | |
329 | ||
282 | 330 | |
283 | 331 | class UtilsDevUtilsTestCase(UtilsTestCase): |
284 | 332 | @tag_test(TestTags.NOSTORAGE, TestTags.CORE) |