/tests/replay_input/match_syscalls_unittest.py

https://github.com/thiagomanel/Beefs-trace-replayer · Python · 130 lines · 97 code · 29 blank · 4 comment · 9 complexity · 40ec28a9f4e412286811574ac9acda9a MD5 · raw file

  1. import unittest
  2. from match_syscalls import Matcher
  3. from match_syscalls import match_order
  4. from match_syscalls import *
  5. class TestMatchSyscalls(unittest.TestCase):
  6. # we to match, if it is ok then we do order match, if it ok then we do timing match
  7. def test_mknod_match(self):
  8. matcher = Matcher([parse_replay_output(line) for line in open("test_match/strace_mkdir").readlines()])
  9. results = matcher.match(parse_replay_input("1 0 - 0 - 1159 2364 32311 (eclipse) mkdir 1318539134542649-479 /tmp/jdt-images 511 0"), False)
  10. #there is a single match -> [pid 7817] mkdir("/tmp/jdt-images", 0777) = 0
  11. self.assertEquals(len(results), 1)
  12. (expected_syscall, called_syscall, ok_call, ok_args, ok_rvalue) = results[0]
  13. self.assertTrue(ok_call)
  14. self.assertTrue(ok_args)
  15. self.assertTrue(ok_rvalue)
  16. def test_parse_output_callname(self):
  17. out = parse_replay_output("[pid 28475] 1327429172.301389 mkdir(\"/tmp/jdt-images\", 0777) = 0")
  18. self.assertEquals(out.name, "mkdir")
  19. self.assertEquals(out.args[0], "/tmp/jdt-images")
  20. self.assertEquals(out.args[1], "0777")
  21. self.assertEquals(out.retvalue, "0")
  22. def test_parse_mkdir_expected_call(self):
  23. matcher = Matcher(open("test_match/strace_mkdir").readlines())
  24. r_input = parse_replay_input("1 0 - 0 - 1159 2364 32311 (eclipse) mkdir 1318539134542649-479 /tmp/jdt-images 511 0")
  25. self.assertEquals(r_input.op, "mkdir")
  26. self.assertEquals(r_input.args, ["/tmp/jdt-images", oct(511)])
  27. self.assertEquals(r_input.return_value, str(0))
  28. def test_match_ordering(self):
  29. matches = match_order("workflow_samples/workflow_single_command_mkdir",
  30. "test_order_match/workflow_single_command_mkdir.strace.output")
  31. self.assertEquals(len(matches), 1)# we have only 1 command in workflow_single_command_mkdir :)
  32. for (input_line, match_result, message) in matches:
  33. self.assertEquals("1 0 - 0 - 1159 2364 32311 (eclipse) mkdir 1318539134542649-479 /tmp/jdt-images 511 0", input_line)
  34. self.assertTrue(match_result)
  35. def test_match_timing_single_command(self):
  36. input_lines = ["1 0 - 0 - 1159 2364 32311 (eclipse) mkdir 1318539134542649-479 /tmp/jdt-images 511 0"]
  37. output_lines = ["[pid 28475] 1327429172.301389 mkdir(\"/tmp/jdt-images\", 0777) = 0"]
  38. r_input = [parse_replay_input(line) for line in input_lines]
  39. r_output = [parse_replay_output(clean_output)
  40. for clean_output in
  41. filter_and_clean_attached_calls(output_lines)]
  42. matches = match_timing(r_input, r_output)
  43. self.assertEquals(len(matches), 1)
  44. (input_call, replayed_call, match, message) = matches[0]
  45. self.assertEquals(input_call, r_input[0].original_line)
  46. self.assertEquals(replayed_call, str(r_output[0]))
  47. self.assertEquals(match, True)
  48. self.assertEquals(message, str(0))
  49. def __input_timestamp__(self, first, second):
  50. return "-".join([str(first), str(second)])
  51. def __output_timestamp__(self, first, second):
  52. return ".".join([str(first), str(second)])
  53. def test_match_timing_two_sequencial_command_with_zero_delta(self):
  54. input_delay = 479
  55. input_lines = ["1 0 - 1 2 1159 2364 32311 (eclipse) mkdir " + self.__input_timestamp__(10, 0) + " /tmp/jdt-images-1 511 0",
  56. "2 1 1 0 - 1159 2364 32311 (eclipse) mkdir " + self.__input_timestamp__(10, input_delay) + " /tmp/jdt-images-2 511 0"]
  57. output_lines = ["[pid 28475] " + self.__output_timestamp__(0, 0) + " mkdir(\"/tmp/jdt-images-1\", 0777) = 0",
  58. "[pid 28475] " + self.__output_timestamp__(0, input_delay) + " mkdir(\"/tmp/jdt-images-2\", 0777) = 0"]
  59. r_input = [parse_replay_input(line) for line in input_lines]
  60. r_output = [parse_replay_output(clean_output)
  61. for clean_output in
  62. filter_and_clean_attached_calls(output_lines)]
  63. #missing the third parameter means delta == 0
  64. matches = match_timing(r_input, r_output)
  65. self.assertEquals(len(matches), 2)
  66. (input_call, replayed_call, match, message) = matches[0]
  67. self.assertEquals(input_call, r_input[0].original_line)
  68. self.assertEquals(replayed_call, str(r_output[0]))
  69. self.assertEquals(match, True)
  70. self.assertEquals(message, str(0))
  71. (input_call, replayed_call, match, message) = matches[1]
  72. self.assertEquals(input_call, r_input[1].original_line)
  73. self.assertEquals(replayed_call, str(r_output[1]))
  74. self.assertEquals(match, True)
  75. self.assertEquals(message, str(0))
  76. def test_mismatch_timing_two_sequencial_command_with_zero_delta(self):
  77. input_delay = 479
  78. mismatch = -1
  79. in_lines = ["1 0 - 1 2 1159 2364 32311 (eclipse) mkdir " + self.__input_timestamp__(10, 0) + " /tmp/jdt-images-1 511 0",
  80. "2 1 1 0 - 1159 2364 32311 (eclipse) mkdir " + self.__input_timestamp__(10, input_delay) + " /tmp/jdt-images-2 511 0"]
  81. out_lines = ["[pid 28475] " + self.__output_timestamp__(0, 0) + " mkdir(\"/tmp/jdt-images-1\", 0777) = 0",
  82. "[pid 28475] " + self.__output_timestamp__(0, input_delay + mismatch) + " mkdir(\"/tmp/jdt-images-2\", 0777) = 0"]
  83. r_input = [parse_replay_input(line) for line in in_lines]
  84. r_output = [parse_replay_output(clean_output)
  85. for clean_output in
  86. filter_and_clean_attached_calls(out_lines)]
  87. #missing the third parameter means delta == 0
  88. matches = match_timing(r_input, r_output)
  89. self.assertEquals(len(matches), 2)
  90. (input_call, replayed_call, match, message) = matches[0]
  91. self.assertEquals(input_call, r_input[0].original_line)
  92. self.assertEquals(replayed_call, str(r_output[0]))
  93. self.assertEquals(match, True)
  94. self.assertEquals(message, str(0))
  95. (input_call, replayed_call, match, message) = matches[1]
  96. self.assertEquals(input_call, r_input[1].original_line)
  97. self.assertEquals(replayed_call, str(r_output[1]))
  98. self.assertEquals(match, False)
  99. self.assertEquals(message, str(mismatch))
  100. if __name__ == '__main__':
  101. unittest.main()