def testMakingContactResponseEmptyRecpt(self): INTRO = "Jääääää, why näääät?" m = message.ContactResponseMessage(senderId=None, senderName=None, message=INTRO, senderKey=None) self.assertRaises(CryptoError, m.createOutput, "")
def testMakingEncryptedContactAccept(self): INTRO = "You really shouldn't be able to read this because it should be encrypted and signed" RECPTKEYID = "3B898548F994C536" # id of key2 m = message.ContactResponseMessage(senderId=None, senderName=None, message=INTRO, senderKey=None) output = m.createOutput(RECPTKEYID) print("This should be encrypted:", output) # Check it's really encrypted by looking for the INTRO string for s in range(len(output) - len(INTRO)): x = "" try: x = output[s:s + len(INTRO)].decode("utf-8") print(x) except Exception: pass self.assertNotEqual(x, INTRO, "Message wasn't encrypted properly") # Test decryption bac = message.Message.MessageFromReceivedData(output) self.assertIsNone(bac, "shouldn't be able to decode the data") # Now we can cheat and add the private key2 to the keyring, then we should be able to decode it TestUtils.setupKeyring(["key2_private", "key1_public"]) bac = message.Message.MessageFromReceivedData(output) self.assertIsNotNone(bac, "should be able to decode the data")
def testMakingContactAccept(self): INTRO = "Jääääää, why näääät?" SENDER = TestUtils._ownTorId SENDERNAME = "Geoffrey Lancaster" KEY_BEGINNING = "-----BEGIN PGP PUBLIC KEY BLOCK-----" m = message.ContactResponseMessage(senderId=None, senderName=None, message=INTRO, senderKey=None) unenc = m.createUnencryptedOutput() bac = message.Message.MessageFromReceivedData(unenc, False) self.assertIsNotNone(bac, "couldn't decode the data") self.assertEqual(bac.messageType, message.Message.TYPE_CONTACT_RESPONSE, "Message type not right") print("The sender is", bac.senderId) print(repr(bac)) self.assertEqual(bac.senderId, SENDER, "Sender not right") self.assertEqual(bac.introMessage, INTRO, "Message not right") self.assertEqual(bac.senderName, SENDERNAME) self.assertTrue(bac.senderKey.startswith(KEY_BEGINNING), "Publickey not right")
def servePage(self, view, url, params): self.requirePageResources( ['button-compose.png', 'default.css', 'jquery-3.1.1.js']) DbI.exportAllAvatars(Config.getWebCacheDir()) messageList = None if url == "/send": print("send message of type '%(messageType)s' to id '%(sendTo)s'" % params) if params['messageType'] == "contactresponse": torId = params['sendTo'] if params.get("accept", "0") == "1": ContactMaker.handleAccept(torId) # Make sure this new contact has an empty avatar DbI.exportAllAvatars(Config.getWebCacheDir()) outmsg = message.ContactResponseMessage( message=params['messageBody']) else: ContactMaker.handleDeny(torId) outmsg = message.ContactDenyMessage() # Construct a ContactResponse message object for sending outmsg.recipients = [params['sendTo']] DbI.addToOutbox(outmsg) elif url.startswith("/delete/"): DbI.deleteFromInbox(params.get("msgId", "")) elif url in ["/search", "/search/"]: messageList = DbI.searchInboxMessages(params.get("searchTerm")) # Make dictionary to convert ids to names contactNames = { c['torid']: c['displayName'] for c in DbI.getProfiles() } unknownSender = I18nManager.getText("messages.sender.unknown") unknownRecpt = I18nManager.getText("messages.recpt.unknown") # Get contact requests, responses and mails from inbox conreqs = [] conresps = [] mailTree = MessageTree() if messageList is None: messageList = DbI.getInboxMessages() # TODO: Paging options? for m in messageList: if not m: continue m['msgId'] = str(m.get("_id", "")) if m['messageType'] == "contactrequest": conreqs.append(m) elif m['messageType'] == "contactrefer": senderId = m.get('fromId', None) m['senderName'] = contactNames.get(senderId, unknownSender) conreqs.append(m) elif m['messageType'] == "contactresponse": if not m.get('accepted', False): m['messageBody'] = I18nManager.getText( "messages.contactrequest.refused") m['fromName'] = DbI.getProfile(m['fromId'])["displayName"] elif not m.get('messageBody', False): m['messageBody'] = I18nManager.getText( "messages.contactrequest.accepted") conresps.append(m) else: senderId = m.get('fromId', None) if not senderId and m.get('signatureKeyId', None): senderId = DbI.findUserIdFromKeyId(m['signatureKeyId']) m['senderName'] = contactNames.get(senderId, unknownSender) m['sentTimeStr'] = self.makeLocalTimeString(m['timestamp']) # Split m['recipients'] by commas, and look up each id with contactNames recpts = m.get('recipients', '') if recpts: replyAll = recpts.split(",") m['recipients'] = ", ".join( [contactNames.get(i, unknownRecpt) for i in replyAll]) replyAll.append(senderId) m['replyAll'] = ",".join(replyAll) else: m['recipients'] = unknownRecpt m['replyAll'] = "" mailTree.addMsg(m) mails = mailTree.build() bodytext = self.messagestemplate.getHtml({ "contactrequests": conreqs, "contactresponses": conresps, "mails": mails, "nummessages": len(conreqs) + len(conresps) + len(mails), "webcachedir": Config.getWebCacheDir() }) contents = self.buildPage({ 'pageTitle': I18nManager.getText("messages.title"), 'pageBody': bodytext, 'pageFooter': "<p>Footer</p>" }) view.setHtml(contents)