def test_file_corrupt(datagram_small): """This tests DatagramInputFile's handling of a corrupt size header.""" dg, verify = datagram_small file = tempfile.NamedTemporaryFile(suffix='.bin') filename = core.Filename.from_os_specific(file.name) filename.make_true_case() dof = core.DatagramOutputFile() dof.open(filename) dof.put_datagram(dg) dof.close() # Corrupt the size header to 1GB file.seek(0) file.write(b'\xFF\xFF\xFF\x4F') file.flush() dg2 = core.Datagram() dif = core.DatagramInputFile() dif.open(filename) assert not dif.get_datagram(dg2) dif.close() # Truncate the file for size in [12, 8, 4, 3, 2, 1, 0]: file.truncate(size) dg2 = core.Datagram() dif = core.DatagramInputFile() dif.open(filename) assert not dif.get_datagram(dg2) dif.close()
def __handle_edit_tablet_finish(self, ret): if ret: dlgcfg = self.edit_dialog[1] mod_tablet = self.tablet mod_tablet.device_model = dlgcfg.deviceModelEntry.text() mod_tablet.serial = dlgcfg.serialNoEntry.text() self.mod_tablet = mod_tablet # send all the modifications dg = core.Datagram() dg.add_uint16(MSG_CLIENT_FINISH_EDIT_TABLET) dg.add_uint8(1) self.mod_tablet.write_datagram(dg) dg.add_uint16(len(self.mod_issues)) for i in range(len(self.mod_issues)): issue = self.mod_issues[i] issue.write_datagram(dg) dg.add_uint16(len(self.new_steps)) for i in range(len(self.new_steps)): step = self.new_steps[i] step.step_id = -1 # signifies new step step.write_datagram(dg) self.mgr.send_finish_edit_tablet(dg) else: dg = core.Datagram() dg.add_uint16(MSG_CLIENT_FINISH_EDIT_TABLET) dg.add_uint8(0) self.tablet.write_datagram(dg) self.mgr.send_finish_edit_tablet(dg) self.edit_dialog = None
def test_file_corrupt(datagram_small, tmpdir): """This tests DatagramInputFile's handling of a corrupt size header.""" dg, verify = datagram_small p = tmpdir.join('datagram.bin') filename = core.Filename.from_os_specific(str(p)) dof = core.DatagramOutputFile() dof.open(filename) dof.put_datagram(dg) dof.close() # Corrupt the size header to 1GB with p.open(mode='r+b') as f: f.seek(0) f.write(b'\xFF\xFF\xFF\x4F') dg2 = core.Datagram() dif = core.DatagramInputFile() dif.open(filename) assert not dif.get_datagram(dg2) dif.close() # Truncate the file for size in [12, 8, 4, 3, 2, 1, 0]: with p.open(mode='r+b') as f: f.truncate(size) dg2 = core.Datagram() dif = core.DatagramInputFile() dif.open(filename) assert not dif.get_datagram(dg2) dif.close()
def test_datagram_pickle(): import pickle dg = core.Datagram() assert pickle.loads(pickle.dumps(dg, -1)) == dg dg = core.Datagram(b'abc\x00') assert pickle.loads(pickle.dumps(dg, -1)) == dg
def test_datagram_cow(): dg1 = core.Datagram() dg1.append_data(b'1234') dg2 = core.Datagram(dg1) dg2.append_data(b'5678') assert dg1.get_message() == b'1234' assert dg2.get_message() == b'12345678'
def __handle_edit_student_finish(self, ret): print("Done editing") dg = core.Datagram() dg.add_uint16(MSG_CLIENT_FINISH_EDIT_USER) dg.add_string(self.editing_guid) dg.add_uint8(ret) if ret: dlgcfg = self.edit_dialog[1] has_insurance = dlgcfg.insuranceCheckBox.checkState() != 0 students = [student for student in self.students if student.guid == self.editing_guid] student = students[0] if len(students) > 0 else None dg.add_uint8(dlgcfg.pcsbAgreementCheckBox.checkState() != 0) dg.add_uint8(dlgcfg.catAgreementCheckBox.checkState() != 0) dg.add_uint8(has_insurance) dg.add_string(dlgcfg.insuranceAmountEdit.text()) dg.add_string(dlgcfg.tabletPCSBEdit.text()) dg.add_uint8(dlgcfg.equipmentReceiptCheck.checkState() != 0) if has_insurance and (student and not student.insurance_paid): print("Student now has insurance") dg.add_string(utils.get_date_string()) self.show_please_wait() g_server_connection.send(dg) self.edit_dialog = None
def __handle_pcsbtag_submit(self): dg = core.Datagram() dg.add_uint16(MSG_CLIENT_LOOKUP_TABLET) dg.add_string(self.ui.pcsbTagTextBox.text()) g_server_connection.send(dg) self.ui.pcsbTagTextBox.setReadOnly(True)
def test_assign(datagram_small): dg, verify = datagram_small dg2 = core.Datagram() dg2.assign(dg) dgi = core.DatagramIterator(dg2) verify(dgi)
def __handle_press_submit(self): dg = core.Datagram() dg.add_uint16(MSG_CLIENT_SUBMIT_ISSUE) dg.add_string(self.ui.pcsbTagTextBox.text()) dg.add_string(self.ui.descTextEntry.toPlainText()) dg.add_string(self.ui.dateEntry.text()) dg.add_string(self.ui.problemsTextEntry.toPlainText()) hws = [ hw for hw in self.hws if hw.name == self.ui.hwCombo.currentText() ] hw = hws[0] if len(hws) > 0 else None if hw: print("HW who submitted:", hw.guid) dg.add_string(hw.guid) else: dg.add_string("") g_server_connection.send(dg) QMessageBox.information( self, "Submitted", "Thank you for submitting your issue. The Net Assistants will get your tablet back to you as soon as possible." ) self.__reset()
def test_datagram_bytes(): """Tests that we can put and get a bytes object on Datagram.""" dg = core.Datagram(b'abc\x00') dg.append_data(b'\xff123') assert bytes(dg) == b'abc\x00\xff123' dgi = core.DatagramIterator(dg) dgi.get_remaining_bytes() == b'abc\x00\xff123'
def datagram_small(request): """Returns a small datagram, along with a verification function.""" dg = core.Datagram() dg.set_stdfloat_double(request.param) dg.add_uint8(3) dg.add_uint16(14159) dg.add_uint32(0xDEADBEEF) dg.add_uint64(0x0123456789ABCDEF) dg.add_int8(-77) dg.add_int16(-1) dg.add_int32(-972965890) dg.add_int64(-1001001001001001) dg.add_string('this is a string') dg.add_string32('this is another string') dg.add_string('this is yet a third string') dg.add_blob(b'blob data \x00\xf2\xa0\x00\x00') dg.add_blob32(b'\xc9\x8f\x00 test blob32') dg.add_stdfloat(800.2) dg.add_stdfloat(3.1415926) dg.add_stdfloat(2.7182818) def readback_function(dgi): assert dgi.get_remaining_size() > 0 assert dgi.get_uint8() == 3 assert dgi.get_uint16() == 14159 assert dgi.get_uint32() == 0xDEADBEEF assert dgi.get_uint64() == 0x0123456789ABCDEF assert dgi.get_int8() == -77 assert dgi.get_int16() == -1 assert dgi.get_int32() == -972965890 assert dgi.get_int64() == -1001001001001001 assert dgi.get_string() == 'this is a string' assert dgi.get_string32() == 'this is another string' assert dgi.get_string() == 'this is yet a third string' assert dgi.get_blob() == b'blob data \x00\xf2\xa0\x00\x00' assert dgi.get_blob32() == b'\xc9\x8f\x00 test blob32' assert dgi.get_stdfloat() == pytest.approx(800.2) assert dgi.get_stdfloat() == pytest.approx(3.1415926) assert dgi.get_stdfloat() == pytest.approx(2.7182818) assert dgi.get_remaining_size() == 0 return dg, readback_function
def __send_user(self, guid, connections): dg = core.Datagram() dg.add_uint16(MSG_SERVER_UPDATE_USER) student = Student.from_guid(guid) if not student: return student.write_datagram(dg) self.send(dg, connections)
def __handle_double_click_user_item(self, item): self.edit_req_item = item guid = item.guid print("Requesting edit for", guid) dg = core.Datagram() dg.add_uint16(MSG_CLIENT_EDIT_USER) dg.add_string(guid) # We will search active directory by guid g_server_connection.send(dg) self.show_please_wait()
def __handle_double_click_tablet_item(self, item): self.edit_req_item = item guid = item.guid print("Requesting edit for tablet", guid) dg = core.Datagram() dg.add_uint16(MSG_CLIENT_EDIT_TABLET) dg.add_string(guid) g_server_connection.send(dg) self.show_please_wait()
def __send_all_users(self, connections): dg = core.Datagram() dg.add_uint16(MSG_SERVER_GET_ALL_USERS_RESP) student_dg = core.Datagram() all_students = Student.get_ad_cat_student_list() num_students = 0 for ad_student in all_students: student = Student.from_active_directory_student(ad_student) if not student: continue student.write_datagram(student_dg) num_students += 1 dg.add_uint16(num_students) dg.append_data(student_dg.get_message()) self.send(dg, connections)
def do_file_test(dg, verify, filename): dof = core.DatagramOutputFile() dof.open(filename) dof.put_datagram(dg) dof.close() dg2 = core.Datagram() dif = core.DatagramInputFile() dif.open(filename) assert dif.get_datagram(dg2) dif.close() # This is normally saved by the DatagramOutputFile header. We cheat here. dg2.set_stdfloat_double(dg.get_stdfloat_double()) dgi = core.DatagramIterator(dg2) verify(dgi)
def datagram_large(): """Returns a big datagram, along with a verification function.""" dg = core.Datagram() for x in range(2000000): dg.add_uint32(x) dg.add_string('the magic words are squeamish ossifrage') def readback_function(dgi): assert dgi.get_remaining_size() > 0 for x in range(2000000): assert dgi.get_uint32() == x assert dgi.get_string() == 'the magic words are squeamish ossifrage' assert dgi.get_remaining_size() == 0 return dg, readback_function
def __init__(self): QMainWindow.__init__(self) from src.client.client_issuereport import Ui_MainWindow self.ui = Ui_MainWindow() self.ui.setupUi(self) self.ui.submitBtn.pressed.connect(self.__handle_press_submit) self.ui.pcsbTagTextBox.returnPressed.connect( self.__handle_pcsbtag_submit) self.ui.resetButton.pressed.connect(self.__reset) self.please_wait_dialog = QtWidgets.QMessageBox(self) self.please_wait_dialog.setStandardButtons( QtWidgets.QMessageBox.NoButton) self.please_wait_dialog.setText("Please wait...") self.please_wait_dialog.setWindowTitle("Information") self.please_wait_dialog.open() dg = core.Datagram() dg.add_uint16(MSG_CLIENT_REQ_NET_ASSISTANTS) g_server_connection.send(dg) self.hws = []
def __request_all_issue_steps(self): dg = core.Datagram() dg.add_uint16(MSG_CLIENT_GET_ALL_ISSUE_STEPS) g_server_connection.send(dg)
def __handle_datagram_netassistant(self, connection, client, dgi, msg_type): if msg_type == MSG_CLIENT_GET_ALL_TABLETS: dg = core.Datagram() dg.add_uint16(MSG_SERVER_GET_ALL_TABLETS_RESP) tablet_dg = core.Datagram() num_tablets = 0 all_tablets = Tablet.get_ad_tablet_list() for ad_tablet in all_tablets: tablet = Tablet.from_active_directory_tablet(ad_tablet) if not tablet: continue tablet.write_datagram(tablet_dg) num_tablets += 1 dg.add_uint16(num_tablets) dg.append_data(tablet_dg.get_message()) self.writer.send(dg, connection) elif msg_type == MSG_CLIENT_GET_ALL_USERS: self.__send_all_users(connection) elif msg_type == MSG_CLIENT_EDIT_USER: guid = dgi.get_string() print("Received edit user request for guid:", guid) dg = core.Datagram() dg.add_uint16(MSG_SERVER_EDIT_USER_RESP) if not guid in self.users_being_edited and client.user_editing is None: dg.add_uint8(1) dg.add_string(guid) client.user_editing = guid self.users_being_edited.append(guid) else: # User is already being edited by another client dg.add_uint8(0) self.writer.send(dg, connection) elif msg_type == MSG_CLIENT_EDIT_TABLET: guid = dgi.get_string() dg = core.Datagram() dg.add_uint16(MSG_SERVER_EDIT_TABLET_RESP) if not guid in self.tablets_being_edited and client.tablet_editing is None: dg.add_uint8(1) dg.add_string(guid) client.tablet_editing = guid self.tablets_being_edited.append(guid) else: # Tablet is already being edited by another client dg.add_uint8(0) self.writer.send(dg, connection) elif msg_type == MSG_CLIENT_FINISH_EDIT_TABLET: ret = dgi.get_uint8() mod_tablet = BaseTablet.from_datagram(dgi) guid = mod_tablet.guid if guid in self.tablets_being_edited and client.tablet_editing == guid: if ret: mod_tablet.__class__ = Tablet # hack mod_tablet.update() c = self.db_connection.cursor() issues_dg = core.Datagram() issues_dg.add_uint16(MSG_SERVER_UPDATE_ISSUE) num_issues = dgi.get_uint16() issues_dg.add_uint16(num_issues) for i in range(num_issues): issue = Issue.from_datagram(dgi) issue.write_database(c) issue.write_datagram(issues_dg) steps_dg = core.Datagram() steps_dg.add_uint16(MSG_SERVER_UPDATE_ISSUE_STEP) num_steps = dgi.get_uint16() steps_dg.add_uint16(num_steps) for i in range(num_steps): step = IssueStep.from_datagram(dgi) step.write_database(c) step.write_datagram(steps_dg) tablet_dg = core.Datagram() tablet_dg.add_uint16(MSG_SERVER_UPDATE_TABLET) mod_tablet.write_datagram(tablet_dg) # Send the updated data to all net clients. netconns = self.get_all_client_connections( CLIENT_NET_ASSISTANT) self.send(issues_dg, netconns) self.send(steps_dg, netconns) self.send(tablet_dg, netconns) self.db_connection.commit() print("Done editing tablet", guid) client.tablet_editing = None self.tablets_being_edited.remove(guid) else: print( "Suspicious: finished editing a tablet that wasn't being edited" ) elif msg_type == MSG_CLIENT_GET_ALL_ISSUES: c = self.db_connection.cursor() c.execute("SELECT * FROM TabletIssue") issues = c.fetchall() num_issues = len(issues) dg = core.Datagram() dg.add_uint16(MSG_SERVER_GET_ALL_ISSUES_RESP) dg.add_uint32(num_issues) for i in range(num_issues): data = issues[i] issue = Issue(*data) issue.write_datagram(dg) self.writer.send(dg, connection) elif msg_type == MSG_CLIENT_GET_ALL_ISSUE_STEPS: c = self.db_connection.cursor() c.execute("SELECT * FROM TabletIssueStep") steps = c.fetchall() num_steps = len(steps) dg = core.Datagram() dg.add_uint16(MSG_SERVER_GET_ALL_ISSUE_STEPS_RESP) dg.add_uint32(num_steps) for i in range(num_steps): data = steps[i] step = IssueStep(*data) step.write_datagram(dg) self.writer.send(dg, connection) elif msg_type == MSG_CLIENT_GET_ALL_LINKS: c = self.db_connection.cursor() c.execute("SELECT * FROM StudentTabletLink") links = c.fetchall() num_links = len(links) dg = core.Datagram() dg.add_uint16(MSG_SERVER_GET_ALL_LINKS_RESP) dg.add_uint32(num_links) for i in range(num_links): data = links[i] link = StudentTabletLink(*data) link.write_datagram(dg) self.writer.send(dg, connection) elif msg_type == MSG_CLIENT_FINISH_EDIT_USER: guid = dgi.get_string() if guid in self.users_being_edited and client.user_editing == guid: print("Done editing user", guid) client.user_editing = None self.users_being_edited.remove(guid) ret = dgi.get_uint8() if ret: pcsb_agreement = dgi.get_uint8() cat_agreement = dgi.get_uint8() insurance = dgi.get_uint8() insurance_amt = dgi.get_string() tablet_pcsb = dgi.get_string() equipment_receipt = dgi.get_uint8() error = 0 student = Student.from_guid(guid) has_new_insurance = False if insurance and not student.insurance_paid: has_new_insurance = True insurance_date = dgi.get_string() print("New insurance date", insurance_date) student.pcsb_agreement = pcsb_agreement student.cat_agreement = cat_agreement student.equipment_receipt = equipment_receipt student.insurance_paid = insurance student.insurance_amount = insurance_amt if not insurance: student.date_of_insurance = "" elif has_new_insurance: student.date_of_insurance = insurance_date if len(tablet_pcsb) > 0: tablet = Tablet.from_pcsb_tag(tablet_pcsb) if not tablet: error = 1 else: student.tablet_guid = Tablet.from_pcsb_tag( tablet_pcsb).guid else: student.tablet_guid = None student.update() dg = core.Datagram() dg.add_uint16(MSG_SERVER_FINISH_EDIT_USER_RESP) if error == 0: try: student.update_link() dg.add_uint8(1) except Exception as e: error = 2 if error == 1: dg.add_uint8(0) dg.add_string( "There was an error assigning the tablet: bad PCSB Tag" ) elif error == 2: dg.add_uint8(0) dg.add_string( "There was an error assigning the tablet: already assigned" ) self.writer.send(dg, connection) self.__send_user( guid, self.get_all_client_connections(CLIENT_NET_ASSISTANT)) # Send the updated link, if there is one c = self.db_connection.cursor() c.execute( "SELECT * FROM StudentTabletLink WHERE StudentGUID = ?", (student.guid, )) link = c.fetchone() if link: dg = core.Datagram() dg.add_uint16(MSG_SERVER_UPDATE_LINK) dg.add_uint16(1) slink = StudentTabletLink(*link) slink.write_datagram(dg) self.send( dg, self.get_all_client_connections( CLIENT_NET_ASSISTANT)) else: print( "Suspicious: finished editing a user that wasn't being edited" )
def test_copy(datagram_small): dg, verify = datagram_small dg2 = core.Datagram(dg) dgi = core.DatagramIterator(dg2) verify(dgi)
def __request_all_tablets(self): dg = core.Datagram() dg.add_uint16(MSG_CLIENT_GET_ALL_TABLETS) g_server_connection.send(dg)
def test_future_result(): # Cancelled fut = core.AsyncFuture() assert not fut.done() fut.cancel() with pytest.raises(CancelledError): fut.result() # None fut = core.AsyncFuture() fut.set_result(None) assert fut.done() assert fut.result() is None # Store int fut = core.AsyncFuture() fut.set_result(123) assert fut.result() == 123 # Store string fut = core.AsyncFuture() fut.set_result("test\000\u1234") assert fut.result() == "test\000\u1234" # Store TypedWritableReferenceCount tex = core.Texture() rc = tex.get_ref_count() fut = core.AsyncFuture() fut.set_result(tex) assert tex.get_ref_count() == rc + 1 assert fut.result() == tex assert tex.get_ref_count() == rc + 1 assert fut.result() == tex assert tex.get_ref_count() == rc + 1 fut = None assert tex.get_ref_count() == rc # Store EventParameter (no longer gets unwrapped) ep = core.EventParameter(0.5) fut = core.AsyncFuture() fut.set_result(ep) assert fut.result() is ep assert fut.result() is ep # Store TypedObject dg = core.Datagram(b"test") fut = core.AsyncFuture() fut.set_result(dg) assert fut.result() == dg assert fut.result() == dg # Store arbitrary Python object obj = object() rc = sys.getrefcount(obj) fut = core.AsyncFuture() fut.set_result(obj) assert sys.getrefcount(obj) == rc + 1 assert fut.result() is obj assert sys.getrefcount(obj) == rc + 1 assert fut.result() is obj assert sys.getrefcount(obj) == rc + 1 fut = None assert sys.getrefcount(obj) == rc
def __handle_datagram_student(self, connection, client, dgi, msg_type): if msg_type == MSG_CLIENT_LOOKUP_TABLET: pcsb_tag = dgi.get_string() dg = core.Datagram() dg.add_uint16(MSG_SERVER_LOOKUP_TABLET_RESP) tablet = Tablet.from_pcsb_tag(pcsb_tag) if not tablet: print("Can't find tablet with PCSB Tag %s" % pcsb_tag) dg.add_uint8(0) self.writer.send(dg, connection) return if not tablet.student_guid: student_name = "" student_email = "" student_grade = "" else: student = Student.from_guid(tablet.student_guid) student_name = student.first_name + " " + student.last_name student_grade = student.grade student_email = student.email if not student_grade: student_grade = "" if not student_email: student_email = "" dg.add_uint8(1) tablet.write_datagram(dg) dg.add_string(student_name) dg.add_string(student_grade) dg.add_string(student_email) self.writer.send(dg, connection) elif msg_type == MSG_CLIENT_SUBMIT_ISSUE: pcsb_tag = dgi.get_string() tablet = Tablet.from_pcsb_tag(pcsb_tag) if not tablet: print("Can't submit issue, pcsb tag %s not found" % pcsb_tag) return incident_desc = dgi.get_string() incident_date = dgi.get_string() problem_desc = dgi.get_string() hwguid = dgi.get_string() c = self.db_connection.cursor() issue = Issue(-1, tablet.guid, incident_desc, problem_desc, incident_date, 0, "", "", 2, "", 0, hwguid, "", 0) issue.write_database(c) self.db_connection.commit() print( "Submitting:\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s" % (pcsb_tag, incident_desc, incident_date, problem_desc, hwguid)) dg = core.Datagram() dg.add_uint16(MSG_SERVER_UPDATE_ISSUE) dg.add_uint16(1) issue.write_datagram(dg) self.send(dg, self.get_all_client_connections(CLIENT_NET_ASSISTANT)) elif msg_type == MSG_CLIENT_REQ_NET_ASSISTANTS: hws = Student.get_ad_net_assistants() # Borg doesn't want this selectable in the issue dropdown. for hw in hws: if hw.name == "CAT network assistant": hws.remove(hw) break dg = core.Datagram() dg.add_uint16(MSG_SERVER_NET_ASSISTANTS_RESP) num_hws = len(hws) dg.add_uint32(num_hws) for i in range(num_hws): hw = Student.from_active_directory_student(hws[i]) hw.write_datagram(dg) self.writer.send(dg, connection)
def __request_all_users(self): dg = core.Datagram() dg.add_uint16(MSG_CLIENT_GET_ALL_USERS) g_server_connection.send(dg)
def __check_datagrams(self): if self.reader.data_available(): dg = core.Datagram() if self.reader.get_data(dg): self.__handle_datagram(dg)
def identify(self): dg = core.Datagram() dg.add_uint16(MSG_CLIENT_IDENTIFY) dg.add_uint8(self.get_identity()) self.writer.send(dg, self.connection)
def test_datagram_get_message(): dg = core.Datagram(b'abc\x00') dg.append_data(b'\xff123') assert dg.get_message() == b'abc\x00\xff123'