def test__convert_to_binary_with_noop(self, open_mock, read_csv_mock, memmap_mock, isfile_mock, getmtime_mock): open_mock.side_effect = self.file_selector read_csv_mock.return_value = pd.DataFrame(data=self.legend) memmap_mock.return_value = self.df.values isfile_mock.return_value = True getmtime_mock.side_effect = lambda f_name: \ 1 if f_name == "/tmp/timing.bin" else 0 a = Analysis("/tmp") self.assertTrue(a.load_data().equals(self.df)) read_csv_mock.assert_called_with("/tmp/legend.csv") memmap_mock.assert_called_with("/tmp/timing.bin", dtype=np.float64, mode="r", shape=(10, 2), order="C") self.assertEqual(isfile_mock.call_args_list, [ mock.call("/tmp/timing.bin"), mock.call("/tmp/legend.csv"), mock.call("/tmp/timing.bin.shape"), mock.call("/tmp/timing.bin"), mock.call("/tmp/legend.csv"), mock.call("/tmp/timing.bin.shape") ])
def test_load_data(self, convert_mock, open_mock, read_csv_mock, memmap_mock): open_mock.side_effect = self.file_selector read_csv_mock.return_value = pd.DataFrame(data=self.legend) memmap_mock.return_value = self.df.values a = Analysis("/tmp") self.assertTrue(a.load_data().equals(self.df)) convert_mock.assert_called_with() read_csv_mock.assert_called_with("/tmp/legend.csv") memmap_mock.assert_called_with("/tmp/timing.bin", dtype=np.float64, mode="r", shape=(10, 2), order="C")
class TestPlots(unittest.TestCase): def setUp(self): data = { 'A': [ 0.000758130, 0.000696718, 0.000980080, 0.000988899, 0.000875510, 0.000734843, 0.000754852, 0.000667378, 0.000671230, 0.000790935 ], 'B': [ 0.000758130, 0.000696718, 0.000980080, 0.000988899, 0.000875510, 0.000734843, 0.000754852, 0.000667378, 0.000671230, 0.000790935 ] } timings = pd.DataFrame(data=data) mock_read_csv = mock.Mock() mock_read_csv.return_value = timings with mock.patch("tlsfuzzer.analysis.Analysis.load_data", mock_read_csv): self.analysis = Analysis("/tmp") self.analysis.load_data = mock_read_csv def test_ecdf_plot(self): with mock.patch("tlsfuzzer.analysis.FigureCanvas.print_figure", mock.Mock()) as mock_save: self.analysis.ecdf_plot() self.assertEqual(mock_save.call_args_list, [ mock.call('/tmp/ecdf_plot.png', bbox_inches='tight'), mock.call('/tmp/ecdf_plot_zoom_in.png', bbox_inches='tight') ]) def test_diff_ecdf_plot(self): with mock.patch("tlsfuzzer.analysis.FigureCanvas.print_figure", mock.Mock()) as mock_save: self.analysis.diff_ecdf_plot() self.assertEqual(mock_save.call_args_list, [ mock.call('/tmp/diff_ecdf_plot.png', bbox_inches='tight'), mock.call('/tmp/diff_ecdf_plot_zoom_in_98.png', bbox_inches='tight'), mock.call('/tmp/diff_ecdf_plot_zoom_in_33.png', bbox_inches='tight'), mock.call('/tmp/diff_ecdf_plot_zoom_in_10.png', bbox_inches='tight') ]) def test_scatter_plot(self): with mock.patch("tlsfuzzer.analysis.FigureCanvas.print_figure", mock.Mock()) as mock_save: self.analysis.scatter_plot() self.assertEqual(mock_save.call_args_list, [ mock.call('/tmp/scatter_plot.png', bbox_inches='tight'), mock.call('/tmp/scatter_plot_zoom_in.png', bbox_inches='tight') ]) def test_diff_scatter_plot(self): with mock.patch("tlsfuzzer.analysis.FigureCanvas.print_figure", mock.Mock()) as mock_save: self.analysis.diff_scatter_plot() self.assertEqual(mock_save.call_args_list, [ mock.call('/tmp/diff_scatter_plot.png', bbox_inches='tight'), mock.call('/tmp/diff_scatter_plot_zoom_in.png', bbox_inches='tight') ]) def test_box_plot(self): with mock.patch("tlsfuzzer.analysis.FigureCanvas.print_figure", mock.Mock()) as mock_save: with mock.patch("tlsfuzzer.analysis.Analysis._calc_percentiles")\ as mock_percentiles: mock_percentiles.return_value = pd.DataFrame( data={ 'A': [0.05, 0.25, 0.5, 0.75, 0.95], 'B': [0.55, 0.75, 1, 1.25, 1.45] }) self.analysis.box_plot() mock_save.assert_called_once() mock_percentiles.assert_called_once_with() @mock.patch("tlsfuzzer.analysis.np.memmap") @mock.patch("tlsfuzzer.analysis.os.remove") @mock.patch("tlsfuzzer.analysis.shutil.copyfile") def test__calc_percentiles(self, mock_copyfile, mock_remove, mock_memmap): mock_memmap.return_value = self.analysis.load_data() ret = self.analysis._calc_percentiles() self.assertIsNotNone(ret) self.assertEqual(ret.values[0, 0], 0.0006691114) mock_copyfile.assert_called_once_with("/tmp/timing.bin", "/tmp/.quantiles.tmp") mock_remove.assert_called_once_with("/tmp/.quantiles.tmp") def test_conf_interval_plot(self): with mock.patch("tlsfuzzer.analysis.FigureCanvas.print_figure", mock.Mock()) as mock_save: with mock.patch("__main__.__builtins__.open", mock.mock_open())\ as mock_open: self.analysis.conf_interval_plot() self.assertEqual(mock_save.call_args_list, [ mock.call('/tmp/conf_interval_plot_mean.png', bbox_inches='tight'), mock.call('/tmp/conf_interval_plot_median.png', bbox_inches='tight'), mock.call('/tmp/conf_interval_plot_trim_mean_05.png', bbox_inches='tight'), mock.call('/tmp/conf_interval_plot_trim_mean_25.png', bbox_inches='tight'), mock.call('/tmp/conf_interval_plot_trimean.png', bbox_inches='tight') ])