def test_messages(): for i in range(10): H = random_stack(protocols, 2, 10) msg = Message(-1, 1, H, "~payload~", len(H) - 1) assert not msg.valid msg = Message(-1, 1, H, "~payload~", 10) assert msg.valid print(msg)
def open_user_download_folder(self, position): selected_user = self.user_list[position] open_item = selected_user.save_path try: self.open_in_system(open_item) except AttributeError: Message.no_user_selected(self) except FileNotFoundError: Message.no_user_download_folder(self)
def user_list_widget_right_click(self): menu = QtWidgets.QMenu() try: position = self.user_list_widget.currentRow() open_user_foler = menu.addAction('Open User Download Folder') open_user_foler.triggered.connect( lambda: self.open_user_download_folder(position)) except AttributeError: Message.no_user_download_folder(self) menu.exec(QtGui.QCursor.pos())
def subreddit_list_widget_right_click(self): menu = QtWidgets.QMenu() try: position = self.subreddit_list_widget.currentRow() open_sub_folder = menu.addAction('Open Subreddit Download Folder') open_sub_folder.triggered.connect( lambda: self.open_subreddit_download_folder(position)) except FileNotFoundError: Message.no_subreddit_download_folder(self) menu.exec(QtGui.QCursor.pos())
def add_blacklist_user(self, new_user): if new_user != '' and ' ' not in new_user: if any(new_user.lower() == name.lower() for name in self.user_blacklist): Message.name_in_list(self) else: self.user_blacklist.append(new_user) self.blacklist_users_listview.addItem(new_user) self.blacklist_users_listview.sortItems() else: Message.not_valid_name(self)
def add_subreddit(self, new_sub): """Adds the subreddit entered into the dialog to the subreddit list""" if new_sub != '' and ' ' not in new_sub: if any(new_sub.lower() == name.lower() for name in self.subreddit_watchlist): Message.name_in_list(self) else: self.subreddit_watchlist.append(new_sub) self.sub_watchlist_listview.addItem(new_sub) self.sub_watchlist_listview.sortItems() else: Message.not_valid_name(self)
def run_node(self): round = self.env.get_round() myid = self.env.get_id(self) if round == 0: if self.env.get_id(self) == HalfHalfSenderAdversary.SENDER: self.input = self.env.get_input(self) for i in range(1, (self.env.get_n() >> 1) + 1): self.env.put_packet(self, self.pki.sign( self, Message(myid, self.input)), i) for i in range((self.env.get_n() >> 1) + 1, self.env.get_n()): self.env.put_packet(self, self.pki.sign( self, Message(myid, 1 - self.input)), i) else: pass
def open_subreddit_download_folder(self, position): selected_sub = self.subreddit_list[position] sub_folder = '%s%s%s' % (selected_sub.save_path, '/' if not selected_sub.save_path.endswith('/') else '', selected_sub.name) try: if sys.platform == 'win32': os.startfile(sub_folder) else: opener = 'open' if sys.platform == 'darwin' else 'xdg-open' subprocess.call([opener, sub_folder]) except AttributeError: Message.no_user_selected(self) except FileNotFoundError: Message.no_subreddit_download_folder(self)
def run_node(self): round = self.env.get_round() myid = self.env.get_id(self) self.attack_value = self.env.get_input(myid) if round == 0: for i in range(0, self.env.get_f()): self.env.put_packet( self, self.pki.sign(self, Message(myid, self.attack_value)), i) for i in range(self.env.get_f(), self.env.get_n()): self.env.put_packet( self, self.pki.sign(self, Message(myid, 1 - self.attack_value)), i) else: pass
def test_routing_2(): """ A ← → B ← → C ← → D ------------------- A, D: x → x B, C: xy → x, x → xy """ graph = nx.Graph() # adding edges graph.add_edges_from([("A", "B"), ("B", "C"), ("C", "D")]) network = Network(graph) x_to_x = AdaptationFunction("x", "x", CV) x_to_xy = AdaptationFunction("x", "xy", EC) xy_to_x = AdaptationFunction("xy", "x", DC) nodes = dict([(char, StatsNode(char, network, [x_to_x])) for char in "AD"] + [(char, StatsNode(char, network, [x_to_xy, xy_to_x])) for char in "BC"]) network.set_nodes(nodes) for i in range(10): network.send_after_convergence("A", "B", Message("A", "D", ["x"], "------")) network.start(0.1) received, sent = routing_tables(nodes) stats(nodes) assert received == sent assert network.sent == sum([x.msg_received for x in nodes.values()]) print('\n' + ' Converged in {:5f} -- Stopped in {:5f} '.center( 60, '=').format(network.convergence_time(), network.duration))
def test_routing_3(): """ D ← → C ↖↘ ↙↗ A ← → B ← → E ← → F ← → G --------------------------------- A, G: x → x B: x → x, y → y, z → z C: x → xh, y → yh D: h → y, h → z E: yz → y F: xy → x --------------------------------- Desired route : A → B → C → D → B → C → D → B → E → F → G _ → x → x → xh → xy → xy → xyh → xyz → xyz → xy → x """ graph = nx.Graph() # adding edges graph.add_edges_from([("A", "B"), ("B", "C"), ("C", "D"), ("D", "B"), ("B", "E"), ("E", "F"), ("F", "G")]) network = Network(graph) x__x = AdaptationFunction('x', 'x', CV) y__y = AdaptationFunction('y', 'y', CV) z__z = AdaptationFunction('z', 'z', CV) x__xh = AdaptationFunction('x', 'xh', EC) y__yh = AdaptationFunction('y', 'yh', EC) h__y = AdaptationFunction('h', 'y', CV) h__z = AdaptationFunction('h', 'z', CV) yz__y = AdaptationFunction('yz', 'y', DC) xy__x = AdaptationFunction('xy', 'x', DC) nodes = { 'A': StatsNode('A', network, [x__x]), 'B': StatsNode('B', network, [x__x, y__y, z__z]), 'C': StatsNode('C', network, [x__xh, y__yh]), 'D': StatsNode('D', network, [h__y, h__z]), 'E': StatsNode('E', network, [yz__y]), 'F': StatsNode('F', network, [xy__x]), 'G': StatsNode('G', network, [x__x]) } network.set_nodes(nodes) for i in range(10): network.send_after_convergence("A", "B", Message("A", "G", ["x"], "------")) network.start(0.1) received, sent = routing_tables(nodes) stats(nodes) assert received == sent assert network.sent == sum([x.msg_received for x in nodes.values()]) print('\n' + ' Converged in {:5f} -- Stopped in {:5f} '.center( 60, '=').format(network.convergence_time(), network.duration))
def run_node(self): round = self.env.get_round() myid = self.env.get_id(self) if round == 0: if self.env.get_id(self) == DolevStrongRoundAdversary.SENDER: for i in range(1, self.env.get_n() - self.env.get_tf() + 1): self.env.put_packet(self, self.pki.sign(self, Message(myid, 1)), i) elif round == self.env.get_tf() - 1: if self.env.get_id(self) == DolevStrongRoundAdversary.MASTER: msg = self.pki.sign(self, Message(myid, 0)) for node in DolevStrongRoundAdversary.adv_pool: if node is not self: msg = self.pki.sign( node, Message(self.env.get_id(node), msg)) if self.env.get_id(self) == DolevStrongRoundAdversary.SENDER: for i in range(1, self.env.get_n() - self.env.get_tf()): self.env.put_packet(self, msg, i) else: pass
def send(ev): global sock, entry txt = entry.get() entry.delete("0", END) if not sock: setup(txt) return if len(txt) == 0: return message = Message.Message(txt) data = pickle.dumps(message) sock.send(data)
def accept(self): if self._restore_defaults: ret = Message.restore_defaults_warning(self) else: ret = True if ret: self.set_post_limit = self.post_limit_spinbox.value() self.restrict_download_by_score = self.restrict_to_score_checkbox.isChecked() if self.restrict_download_by_score: self.post_score_method = self.post_score_combo.currentIndex() self.subreddit_sort_change() self.sub_sort_top_method = self.sub_sort_top_combo.currentIndex() if self.restrict_by_custom_date_checkbox.isChecked(): self.custom_date = int(time.mktime(time.strptime(self.date_limit_edit.text(), '%m/%d/%Y %H:%M:%S'))) else: self.custom_date = 0 self.settings.setValue('imgur_client_id', self.imgur_client_id) self.settings.setValue('imgur_client_secret', self.imgur_client_secret) self.settings.setValue('reddit_account_username', self.reddit_account_username) self.settings.setValue('reddit_account_password', self.reddit_account_password) self.settings.setValue('auto_save_checkbox', self.auto_save_checkbox.checkState()) self.settings.setValue('date_restriction_checkbox', self.date_restriction_checkbox.checkState()) self.settings.setValue('restrict_to_score_checkbox', self.restrict_to_score_checkbox.checkState()) self.settings.setValue('post_score_combo_text', self.post_score_combo.currentText()) self.settings.setValue('restrict_to_score_checkbox', self.restrict_to_score_checkbox.checkState()) self.settings.setValue('sub_sort_hot_radio', self.sub_sort_hot_radio.isChecked()) self.settings.setValue('sub_sort_rising_radio', self.sub_sort_rising_radio.isChecked()) self.settings.setValue('sub_sort_controversial_radio', self.sub_sort_controversial_radio.isChecked()) self.settings.setValue('sub_sort_new_radio', self.sub_sort_new_radio.isChecked()) self.settings.setValue('sub_sort_top_radio', self.sub_sort_top_radio.isChecked()) self.settings.setValue('sub_sort_top_combo', self.sub_sort_top_combo.currentIndex()) self.settings.setValue('post_score_combo', self.post_score_combo.currentIndex()) self.settings.setValue('post_score_limit_spinbox', self.post_score_limit_spin_box.value()) self.settings.setValue('post_limit_spinbox', self.post_limit_spinbox.value()) self.settings.setValue('link_filter_video_checkbox', self.link_filter_video_checkbox.checkState()) self.settings.setValue('link_filter_image_checkbox', self.link_filter_image_checkbox.checkState()) self.settings.setValue('link_filter_avoid_duplicates_checkbox', self.link_filter_avoid_duplicates_checkbox.checkState()) self.settings.setValue('subreddit_save_by_combo', self.subreddit_save_by_combo.currentIndex()) self.settings.setValue('name_downloads_by_combo', self.name_downloads_by_combo.currentIndex()) self.settings.setValue('save_directory_line_edit', self.save_directory_line_edit.text()) self.settings.setValue('restrict_by_custom_date_checkbox', self.restrict_by_custom_date_checkbox.isChecked()) self.settings.setValue('settings_custom_date', self.custom_date) self.settings.setValue('thread_limit', self.thread_limit_spinbox.value()) self.settings.setValue('save_undownloaded_content', self.save_undownloaded_content_checkbox.isChecked()) super().accept()
def test_MongoHandler(self): with open('logging_config_files/logging_config.yaml', 'rt') as file: config = yaml.safe_load(file.read()) dictConfig(config) # Variables hostname = socket.gethostname() internal_ip = socket.gethostbyname(socket.gethostname()) # Configs logging.raiseExceptions = False # Main logger - mongo and console my_logger = logging.getLogger('mongo-logger') payload = Message(machine=22, snif=2, success=False, host=hostname, internal_ip=internal_ip) my_logger.info("some info message", extra=payload.as_dict()) my_logger.error("some warning message", extra=payload.as_dict())
def run_node(self): if self.called: return self.called = True round = self.env.get_round() self.attack_value = 1 if round == 0: for j in range(0, self.env.get_tf()): for node in self.con.node_id.keys(): if node.env.get_id(node) == j: chameleon = node # chameleon is used to pretend the corrupt node break for i in range(0, self.env.get_f()): self.env.put_packet( self, self.pki.sign(chameleon, Message(j, self.attack_value)), i) for i in range(self.env.get_f(), self.env.get_n()): self.env.put_packet( self, self.pki.sign(chameleon, Message(j, 1 - self.attack_value)), i) else: pass
def closeEvent(self, QCloseEvent): if self.running: if Message.downloader_running_warning(self): try: self.thread.terminate() except AttributeError: pass try: self.user_finder_download_thread.terminate() except AttributeError: pass self.close_finder() else: QCloseEvent.ignore() else: self.close_finder()
def test_routing_4(): """ B ← → C ← → D ↙↗ ↖↘ A ← → E ← → F --------------------------------- A, B, C: x → x D: x → y E: x → xy F: y → y --------------------------------- """ graph = nx.Graph() # adding edges graph.add_edges_from([("A", "B"), ("B", "C"), ("C", "D"), ("D", "F"), ("A", "E"), ("E", "F")]) network = Network(graph) x__x = AdaptationFunction('x', 'x', CV) y__y = AdaptationFunction('y', 'y', CV) x__y = AdaptationFunction('x', 'y', CV) x__xy = AdaptationFunction('x', 'xy', EC) nodes = { 'A': StatsNode('A', network, [x__x]), 'B': StatsNode('B', network, [x__x]), 'C': StatsNode('C', network, [x__x]), 'D': StatsNode('D', network, [x__y]), 'E': StatsNode('E', network, [x__xy]), 'F': StatsNode('F', network, [y__y]) } network.set_nodes(nodes) for i in range(10): network.send_after_convergence("B", "A", Message("A", "F", ["x"], "------")) network.start(0.1) received, sent = routing_tables(nodes) stats(nodes) assert received == sent assert network.sent == sum([x.msg_received for x in nodes.values()]) print('\n' + ' Converged in {:5f} -- Stopped in {:5f} '.center( 60, '=').format(network.convergence_time(), network.duration))
def remove_invalid_subreddits(self, sub): """If a subreddit in the list is found to not be valid, this removes the subreddit if the user wants to do so""" if Message.subreddit_not_valid(self, sub): current_row = self.subreddit_watchlist.index(sub) self.subreddit_watchlist.remove(sub) self.sub_watchlist_listview.takeItem(current_row)
def test_adapt_func_apply(): n = 10 for x in protocols: for y in protocols: H = random_stack(protocols, n, n) msg_x = Message("start", "end", H + [x], 2 * n) msg_y = Message("start", "end", H + [y], 2 * n) msg_xy = Message("start", "end", H + [x, y], 2 * n) msg_yx = Message("start", "end", H + [y, x], 2 * n) encap_x = encapsulation(x, y) decap_xy = decapsulation(x, y) conv_x = conversion(x, y) # Correct calls assert conv_x.apply(msg_x.copy()).stack == H + [y] assert conv_x.apply(msg_yx.copy()).stack == H + [y, y] assert encap_x.apply(msg_x.copy()).stack == H + [x, y] assert encap_x.apply(msg_yx.copy()).stack == H + [y, x, y] assert decap_xy.apply(msg_xy.copy()).stack == H + [x] if x != y: with pytest.raises(Exception): conv_x.apply(msg_y.copy()) conv_x.apply(msg_xy.copy()) encap_x.apply(msg_y.copy()) encap_x.apply(msg_xy.copy()) decap_xy.apply(msg_yx.copy())
def test_adapt_func_reverse(): def chain_apply(msg, f, g): copy = msg.copy() f.apply(copy) return g.apply(copy) n = 4 for x in protocols: for y in protocols: H = random_stack(protocols, n, n) msg_x = Message("start", "end", H + [x], 2 * n) msg_y = Message("start", "end", H + [y], 2 * n) msg_xy = Message("start", "end", H + [x, y], 2 * n) encap_x_xy = encapsulation(x, y) decap_xy_x = decapsulation(x, y) conv_x_y = conversion(x, y) assert msg_x.stack == chain_apply(msg_x.copy(), conv_x_y, conv_x_y.reverse).stack assert msg_y.stack == chain_apply(msg_y.copy(), conv_x_y.reverse, conv_x_y).stack assert msg_x.stack == chain_apply(msg_x.copy(), encap_x_xy, encap_x_xy.reverse).stack assert msg_xy.stack == chain_apply(msg_xy.copy(), encap_x_xy.reverse, encap_x_xy).stack assert msg_xy.stack == chain_apply(msg_xy.copy(), decap_xy_x, decap_xy_x.reverse).stack assert msg_x.stack == chain_apply(msg_x.copy(), decap_xy_x.reverse, decap_xy_x).stack