get label tests running

This commit is contained in:
jopohl
2016-05-29 14:35:03 +02:00
parent 21804128f2
commit cab7aa9f63
10 changed files with 75 additions and 103 deletions

View File

@@ -26,6 +26,7 @@ from urh.ui.ui_analysis_frame import Ui_FAnalysis
from urh.util import FileOperator
from urh.util.Errors import Errors
from urh.util.Formatter import Formatter
from urh.util.Logger import logger
from urh.util.ProjectManager import ProjectManager
@@ -435,6 +436,7 @@ class CompareFrameController(QFrame):
first_block_indices = []
prev_line = 0
for proto in self.protocol_list:
proto.default_labelset = self.proto_analyzer.default_labelset
abs_time = 0
rel_time = 0
if proto.show:
@@ -458,6 +460,7 @@ class CompareFrameController(QFrame):
block.relative_time = rel_time
num_blocks += 1
block.labelset = self.proto_analyzer.default_labelset
self.proto_analyzer.blocks.append(block)
self.proto_analyzer.used_symbols |= proto.used_symbols
@@ -968,12 +971,15 @@ class CompareFrameController(QFrame):
self.protocol_model.search_results[:] = []
self.protocol_model.search_value = ""
def set_protocol_label_visibility(self, lbl: ProtocolLabel, group: ProtocolGroup = None):
group = self.get_labelset_for_label(lbl) if not group else group
start, end = group.get_label_range(lbl, self.ui.cbProtoView.currentIndex(), True)
def set_protocol_label_visibility(self, lbl: ProtocolLabel, block: ProtocolBlock = None):
try:
block = block if block else next(block for block in self.proto_analyzer.blocks if lbl in block.labelset)
start, end = block.get_label_range(lbl, self.ui.cbProtoView.currentIndex(), True)
for i in range(start, end):
self.ui.tblViewProtocol.setColumnHidden(i, not lbl.show)
for i in range(start, end):
self.ui.tblViewProtocol.setColumnHidden(i, not lbl.show)
except Exception as e:
logger.warning(e)
def get_labelset_for_label(self, lbl: ProtocolLabel) -> LabelSet:
for lblset in self.proto_analyzer.labelsets:
@@ -1057,11 +1063,12 @@ class CompareFrameController(QFrame):
def show_only_labels(self):
visible_columns = set()
for lbl in self.proto_analyzer.protocol_labels:
if lbl.show:
start, end = self.proto_analyzer.get_label_range(lbl, self.ui.cbProtoView.currentIndex(),
True)
visible_columns |= (set(range(start, end)))
for block in self.proto_analyzer.blocks:
for lbl in block.labelset:
if lbl.show:
start, end = block.get_label_range(lbl=lbl, view=self.ui.cbProtoView.currentIndex(),
decode=True)
visible_columns |= (set(range(start, end)))
for i in range(self.protocol_model.col_count):
if i in visible_columns:

View File

@@ -23,19 +23,6 @@ class GeneratorTableModel(TableModel):
self.decode = False
self.is_generator = True
def refresh_bgcolors_and_tooltips(self):
self.background_colors.clear()
self.tooltips.clear()
label_colors = constants.LABEL_COLORS
for lbl in self.protocol.protocol_labels:
bg_color = label_colors[lbl.color_index]
for i in lbl.block_numbers:
start, end = self.protocol.get_label_range(lbl, self.proto_view, self.decode)
for j in range(start, end):
self.background_colors[i, j] = bg_color
self.tooltips[i, j] = lbl.name
def refresh_fonts(self):
self.bold_fonts.clear()
self.text_colors.clear()
@@ -46,10 +33,8 @@ class GeneratorTableModel(TableModel):
for j in range(f[0], f[1]):
self.bold_fonts[i, j] = True
for lbl in pac.protocol_labels:
if lbl.active_fuzzing:
i = lbl.refblock
for j in range(*pac.get_label_range(lbl, self.proto_view, False)):
for lbl in block.active_fuzzing_labels:
for j in range(*block.get_label_range(lbl=lbl, view=self.proto_view, decode=False)):
self.bold_fonts[i, j] = True
self.text_colors[i, j] = QColor("orange")

View File

@@ -118,7 +118,7 @@ class TableModel(QAbstractTableModel):
self.tooltips.clear()
label_colors = constants.LABEL_COLORS
for i, block in enumerate(self.controller.proto_analyzer.blocks):
for i, block in enumerate(self.protocol.blocks):
for lbl in block.labelset:
bg_color = label_colors[lbl.color_index]
start, end = block.get_label_range(lbl, self.proto_view, self.decode)

View File

@@ -52,9 +52,14 @@ class LabelSet(list):
proto_label = ProtocolLabel(name=name, start=start, end=end, val_type_index=type_index, color_index=color_ind)
proto_label.signals.apply_decoding_changed.connect(self.handle_plabel_apply_decoding_changed)
self.append(proto_label)
self.sort()
if proto_label not in self:
proto_label.signals.apply_decoding_changed.connect(self.handle_plabel_apply_decoding_changed)
self.append(proto_label)
self.sort()
def add_label(self, lbl: ProtocolLabel):
self.add_protocol_label(lbl.start, lbl.end, type_index=0, name=lbl.name, color_ind=lbl.color_index)
def handle_plabel_apply_decoding_changed(self):
pass

View File

@@ -107,6 +107,9 @@ class ProtocolLabel(object):
def __eq__(self, other):
return self.start == other.start and self.end == other.end and self.name == other.name
def __hash__(self):
return hash("{}/{}/{}".format(self.start, self.end, self.name))
def __repr__(self):
return "Protocol Label - start: {0} end: {1} name: {2}".format(self.start, self.end, self.name)

View File

@@ -77,6 +77,11 @@ class ProtocolAnalyzer(object):
def default_labelset(self):
return self.labelsets[0]
@default_labelset.setter
def default_labelset(self, val: LabelSet):
self.labelsets[0] = val
@property
def protocol_labels(self):
return [lbl for labelset in self.labelsets for lbl in labelset]
@@ -472,17 +477,6 @@ class ProtocolAnalyzer(object):
except IndexError:
continue
# Refblocks der Labels updaten
for i in removable_block_indices:
labels_before = [p for p in self.protocol_labels
if p.refblock < i < p.refblock + p.nfuzzed]
labels_after = [p for p in self.protocol_labels if p.refblock > i]
for label in labels_after:
label.refblock -= 1
for label in labels_before:
label.nfuzzed -= 1
# Remove Empty Blocks and Pause after empty Blocks
for i in reversed(removable_block_indices):
del self.blocks[i]

View File

@@ -38,6 +38,15 @@ class ProtocolAnalyzerContainer(ProtocolAnalyzer):
self.__group = ProtocolGroup("GeneratorGroup")
self.__group.add_protocol_item(ProtocolTreeItem(self, None)) # Warning: parent is None
@property
def protocol_labels(self):
result = list(set(lbl for block in self.blocks for lbl in block.labelset))
result.sort()
return result
@property
def num_blocks_successive_fuzzing(self):
result = self.num_blocks
@@ -79,7 +88,7 @@ class ProtocolAnalyzerContainer(ProtocolAnalyzer):
def insert_protocol_analyzer(self, index: int, proto_analyzer: ProtocolAnalyzer):
blocks = [ProtocolBlock(plain_bits=copy.copy(block.decoded_bits), pause=block.pause,
bit_alignment_positions=self.bit_alignment_positions, labelset = block.labelset,
bit_alignment_positions=self.bit_alignment_positions, labelset=copy.deepcopy(block.labelset),
rssi=block.rssi, modulator_indx=0, decoder=block.decoder, bit_len=block.bit_len,
exclude_from_decoding_labels=copy.copy(block.exclude_from_decoding_labels))
for block in proto_analyzer.blocks if block]
@@ -87,21 +96,6 @@ class ProtocolAnalyzerContainer(ProtocolAnalyzer):
self.blocks[index:0] = blocks
self.used_symbols |= proto_analyzer.used_symbols
proto_labels = [copy.deepcopy(lbl) for lbl in proto_analyzer.protocol_labels]
for l in proto_labels:
l.refblock += index
for l in self.protocol_labels:
if l.refblock > index:
l.refblock += proto_analyzer.num_blocks
for p in proto_labels:
self.__group.add_label(p, decode=False)
for block in self.blocks:
block.fuzz_labels[index:0] = [p for p in proto_labels if p not in block.fuzz_labels]
if len(self.pauses) > 0:
self.fuzz_pause = self.pauses[0]
@@ -126,10 +120,9 @@ class ProtocolAnalyzerContainer(ProtocolAnalyzer):
"""
result = []
appd_result = result.append
block_offsets = {}
for i, block in enumerate(self.blocks):
labels = [l for l in self.active_fuzzing_labels if l.refblock == i]
labels = block.active_fuzzing_labels
if len(labels) == 0:
appd_result(block)
@@ -137,7 +130,6 @@ class ProtocolAnalyzerContainer(ProtocolAnalyzer):
labels.sort()
n = sum([len(l.fuzz_values) for l in labels])
block_offsets[i] = n - 1
for l in labels:
l.nfuzzed = n
@@ -147,17 +139,11 @@ class ProtocolAnalyzerContainer(ProtocolAnalyzer):
cpy_bits[l.start:l.end] = bool_fuzz_val
fuz_block = ProtocolBlock(plain_bits=cpy_bits, pause=block.pause,
bit_alignment_positions=self.bit_alignment_positions, rssi=block.rssi,
labelset=block.labelset,
labelset=copy.deepcopy(block.labelset),
modulator_indx=block.modulator_indx, decoder=block.decoder,
fuzz_created=[(l.start, l.end)])
fuz_block.fuzz_labels = copy.deepcopy(block.fuzz_labels)
appd_result(fuz_block)
# Refblockindizes anpassen
for l in self.protocol_labels:
offset = sum(block_offsets[i] for i in block_offsets.keys() if i < l.refblock)
l.refblock_offset += offset
self.blocks = result
""":type: list of ProtocolBlock """
@@ -196,9 +182,8 @@ class ProtocolAnalyzerContainer(ProtocolAnalyzer):
fuzz_created.append((l.start, l.end))
fuzz_block = ProtocolBlock(plain_bits=cpy_bits, pause=block.pause, bit_alignment_positions=self.bit_alignment_positions,
rssi=block.rssi, labelset=block.labelset,
rssi=block.rssi, labelset=copy.deepcopy(block.labelset),
modulator_indx=block.modulator_indx, decoder=block.decoder, fuzz_created=fuzz_created[:])
fuzz_block.fuzz_labels = copy.deepcopy(block.fuzz_labels)
appd_result(fuzz_block)
block_offsets[i] = nvalues - 1
@@ -247,10 +232,9 @@ class ProtocolAnalyzerContainer(ProtocolAnalyzer):
fuzz_created.append((start, end))
fuz_block = ProtocolBlock(plain_bits=cpy_bits, pause=block.pause, bit_alignment_positions=self.bit_alignment_positions,
rssi=block.rssi, labelset=block.labelset,
rssi=block.rssi, labelset=copy.deepcopy(block.labelset),
modulator_indx=block.modulator_indx,
decoder=block.decoder, fuzz_created=fuzz_created)
fuz_block.fuzz_labels = copy.deepcopy(block.fuzz_labels)
appd_result(fuz_block)
block_offsets[i] = n - 1
@@ -267,7 +251,6 @@ class ProtocolAnalyzerContainer(ProtocolAnalyzer):
def clear(self):
self.blocks[:] = []
self.__group.clear_labels()
self.protocol_labels[:] = []
@@ -277,7 +260,7 @@ class ProtocolAnalyzerContainer(ProtocolAnalyzer):
def create_fuzzing_label(self, start, end, refblock) -> ProtocolLabel:
fuz_lbl = self.__group.add_protocol_label(start, end, refblock, 0, False)
for block in self.blocks:
block.fuzz_labels.append(fuz_lbl)
block.labelset.append(fuz_lbl)
return fuz_lbl
def remove_label(self, label: ProtocolLabel):

View File

@@ -49,8 +49,6 @@ class ProtocolBlock(object):
self.bit_alignment_positions = bit_alignment_positions
self.fuzz_created = fuzz_created if fuzz_created is not None else []
self.fuzz_labels = [] # Todo: remove
""":type: list of ProtocolLabel """
self.__decoded_bits = None
self.__encoded_bits = None
@@ -85,6 +83,10 @@ class ProtocolBlock(object):
self.clear_encoded_bits()
@property
def active_fuzzing_labels(self):
return [lbl for lbl in self.labelset if lbl.active_fuzzing]
def __getitem__(self, index: int):
return self.plain_bits[index]
@@ -107,40 +109,39 @@ class ProtocolBlock(object):
step = 1
number_elements = len(range(index.start, index.stop, step))
for l in self.fuzz_labels[:]:
for l in self.labelset[:]:
if index.start <= l.start and index.stop >= l.end:
self.fuzz_labels.remove(l)
self.labelset.remove(l)
elif index.stop - 1 < l.start:
l_cpy = copy.deepcopy(l)
l_cpy.start -= number_elements
l_cpy.end -= number_elements
self.fuzz_labels.remove(l)
self.fuzz_labels.append(l_cpy)
self.labelset.remove(l)
self.labelset.append(l_cpy)
elif index.start <= l.start and index.stop >= l.start:
self.fuzz_labels.remove(l)
self.labelset.remove(l)
elif index.start >= l.start and index.stop <= l.end:
self.fuzz_labels.remove(l)
self.labelset.remove(l)
elif index.start >= l.start and index.start < l.end:
self.fuzz_labels.remove(l)
self.labelset.remove(l)
else:
fuzz_labels = self.fuzz_labels
for l in fuzz_labels:
for l in self.labelset:
if index < l.start:
l_cpy = copy.deepcopy(l)
l_cpy.start -= 1
l_cpy.end -= 1
self.fuzz_labels.remove(l)
self.fuzz_labels.append(l_cpy)
self.labelset.remove(l)
self.labelset.append(l_cpy)
elif l.start < index < l.end:
l_cpy = copy.deepcopy(l)
l_cpy.start = index - 1
self.fuzz_labels.remove(l)
self.labelset.remove(l)
if l_cpy.end - l_cpy.start > 0:
self.fuzz_labels.append(l_cpy)
self.labelset.append(l_cpy)
del self.plain_bits[index]
@@ -371,7 +372,7 @@ class ProtocolBlock(object):
result += factor * (from_index - cur_index)
end = result + factor - 1
end = end if end < len(bits) else len(bits) - 1
#end = end if end < len(bits) else len(bits) - 1
return result, end

View File

@@ -133,13 +133,6 @@ class ProtocolGroup(object):
return int(start), int(math.ceil(end))
def add_label(self, lbl: ProtocolLabel, refresh=True, decode=True):
if lbl not in self.labels:
lbl.signals.apply_decoding_changed.connect(self.handle_plabel_apply_decoding_changed)
self.labels.append(lbl)
self.labels.sort()
def remove_label(self, label: ProtocolLabel):
try:
self.labels.remove(label)
@@ -163,8 +156,6 @@ class ProtocolGroup(object):
def set_labels(self, val):
self.__labels = val # For AmbleAssignPlugin
def clear_labels(self):
self.__labels[:] = []
def add_protocol_item(self, protocol_item):
"""

View File

@@ -30,6 +30,8 @@ class TestLabels(unittest.TestCase):
self.cframe.add_protocol_label(0, 40, 1, 0, edit_label_name = False) # Sync
self.cframe.add_protocol_label(43, 43, 2, 0, edit_label_name = False) # FuzzBit
self.assertEqual(len(self.cframe.active_labelset), 2)
def tearDown(self):
constants.SETTINGS.setValue('rel_symbol_length', self.old_sym_len) # Restore Symbol Length
@@ -72,7 +74,7 @@ class TestLabels(unittest.TestCase):
self.assertEqual(len(labels), 2)
# Fuzz Label
lbl = labels[1]
lbl = self.gframe.table_model.protocol.blocks[0].labelset[1]
lbl.fuzz_values.append("1")
lbl.add_fuzz_value()
lbl.add_fuzz_value()
@@ -84,8 +86,9 @@ class TestLabels(unittest.TestCase):
self.gframe.refresh_table()
self.gframe.ui.btnFuzz.setEnabled(True)
self.gframe.ui.btnFuzz.click()
self.assertTrue(lbl.active_fuzzing)
self.assertIn(lbl, self.gframe.table_model.protocol.blocks[0].labelset)
self.assertEqual(self.gframe.table_model.row_count, 4 + 3)
self.assertEqual(lbl.refblock, 2)
# Check if Background for fuzzed labels is drawn correctly
self.__check_background_is_drawn(lbl, 43, 43)
@@ -108,8 +111,8 @@ class TestLabels(unittest.TestCase):
def __check_background_is_drawn(self, lbl, lbl_start, lbl_end):
pac = self.gframe.table_model.protocol
for i in range(self.gframe.table_model.row_count):
labels_for_block = pac.blocks[i].fuzz_labels
labels_for_block = pac.blocks[i].labelset
self.assertIn(lbl, labels_for_block)
start, end = pac.get_label_range(lbl, self.gframe.table_model.proto_view, False)
start, end = pac.blocks[i].get_label_range(lbl, self.gframe.table_model.proto_view, False)
self.assertEqual(start, lbl_start)
self.assertEqual(end, lbl_end + 1)