def testUnreasonablyLongFile(self): contents = '\n' * (filecontent.SOURCE_FILE_MAX_LINES + 2) _contents, is_binary, is_long = filecontent.DecodeFileContents( contents) self.assertFalse(is_binary) self.assertTrue(is_long) contents = '\n' * 100 _contents, is_binary, is_long = filecontent.DecodeFileContents( contents) self.assertFalse(is_binary) self.assertFalse(is_long)
def GatherPageData(self, mr): """Build up a dictionary of data values to use when rendering the page. Args: mr: commonly used info parsed from the request. Returns: Dict of values used by EZT for rendering the page. """ issue, comment = self._GetIssueAndComment(mr) message_body_unicode, is_binary, _is_long = ( filecontent.DecodeFileContents(comment.inbound_message)) # Take out the iso8859-1 non-breaking-space characters that gmail # inserts between consecutive spaces when quoting text in a reply. # You can see this in gmail by sending a plain text reply to a # message that had multiple spaces on some line, then use the # "Show original" menu item to view your reply, you will see "=A0". #message_body_unicode = message_body_unicode.replace(u'\xa0', u' ') page_data = { 'local_id': issue.local_id, 'seq': comment.sequence, 'is_binary': ezt.boolean(is_binary), 'message_body': message_body_unicode, } return page_data
def testFileIsTextByPath(self): bin_string = ''.join([chr(c) for c in range(122, 252)] * 100) unicode_str = ( u'Some non-ascii chars - ' u'\xa2\xfa\xb6\xe7\xfc\xea\xd0\xf4\xe6\xf0\xce\xf6\xbe') long_line = (unicode_str * 100)[:filecontent._MAX_SOURCE_LINE_LEN_LOWER+1] long_line = long_line.encode('iso-8859-1') for contents in [bin_string, long_line]: self.assertTrue(filecontent.DecodeFileContents(contents, path=None)[1]) self.assertTrue(filecontent.DecodeFileContents(contents, path='')[1]) self.assertTrue(filecontent.DecodeFileContents(contents, path='foo')[1]) self.assertTrue( filecontent.DecodeFileContents(contents, path='foo.bin')[1]) self.assertTrue( filecontent.DecodeFileContents(contents, path='foo.zzz')[1]) for path in ['a/b/Makefile.in', 'README', 'a/file.js', 'b.txt']: self.assertFalse( filecontent.DecodeFileContents(contents, path=path)[1])
def IsBinary(self, contents): _contents, is_binary, _is_long = ( filecontent.DecodeFileContents(contents)) return is_binary
def testFileIsBinaryByCommonExtensions(self): contents = 'this is not examined' self.assertTrue(filecontent.DecodeFileContents( contents, path='junk.zip')[1]) self.assertTrue(filecontent.DecodeFileContents( contents, path='JUNK.ZIP')[1]) self.assertTrue(filecontent.DecodeFileContents( contents, path='/build/HelloWorld.o')[1]) self.assertTrue(filecontent.DecodeFileContents( contents, path='/build/Hello.class')[1]) self.assertTrue(filecontent.DecodeFileContents( contents, path='/trunk/libs.old/swing.jar')[1]) self.assertFalse(filecontent.DecodeFileContents( contents, path='HelloWorld.cc')[1]) self.assertFalse(filecontent.DecodeFileContents( contents, path='Hello.java')[1]) self.assertFalse(filecontent.DecodeFileContents( contents, path='README')[1]) self.assertFalse(filecontent.DecodeFileContents( contents, path='READ.ME')[1]) self.assertFalse(filecontent.DecodeFileContents( contents, path='README.txt')[1]) self.assertFalse(filecontent.DecodeFileContents( contents, path='README.TXT')[1]) self.assertFalse(filecontent.DecodeFileContents( contents, path='/trunk/src/com/monorail/Hello.java')[1]) self.assertFalse(filecontent.DecodeFileContents( contents, path='/branches/1.2/resource.el')[1]) self.assertFalse(filecontent.DecodeFileContents( contents, path='/wiki/PageName.wiki')[1])
def GatherPageData(self, mr): """Parse the attachment ID from the request and serve its content. Args: mr: commonly used info parsed from the request. Returns: Dict of values used by EZT for rendering almost the page. """ with mr.profiler.Phase('get issue, comment, and attachment'): try: attachment, issue = tracker_helpers.GetAttachmentIfAllowed( mr, self.services) except exceptions.NoSuchIssueException: webapp2.abort(404, 'issue not found') except exceptions.NoSuchAttachmentException: webapp2.abort(404, 'attachment not found') except exceptions.NoSuchCommentException: webapp2.abort(404, 'comment not found') content = [] if attachment.gcs_object_id: bucket_name = app_identity.get_default_gcs_bucket_name() full_path = '/' + bucket_name + attachment.gcs_object_id logging.info("reading gcs: %s" % full_path) with cloudstorage.open(full_path, 'r') as f: content = f.read() filesize = len(content) # This servlet only displays safe textual attachments. The user should # not have been given a link to this servlet for any other kind. if not attachment_helpers.IsViewableText(attachment.mimetype, filesize): self.abort(400, 'not a text file') u_text, is_binary, too_large = filecontent.DecodeFileContents(content) lines = prettify.PrepareSourceLinesForHighlighting( u_text.encode('utf8')) config = self.services.config.GetProjectConfig(mr.cnxn, mr.project_id) granted_perms = tracker_bizobj.GetGrantedPerms(issue, mr.auth.effective_ids, config) page_perms = self.MakePagePerms(mr, issue, permissions.DELETE_ISSUE, permissions.CREATE_ISSUE, granted_perms=granted_perms) page_data = { 'issue_tab_mode': 'issueDetail', 'local_id': issue.local_id, 'filename': attachment.filename, 'filesize': template_helpers.BytesKbOrMb(filesize), 'file_lines': lines, 'is_binary': ezt.boolean(is_binary), 'too_large': ezt.boolean(too_large), 'code_reviews': None, 'page_perms': page_perms, } if is_binary or too_large: page_data['should_prettify'] = ezt.boolean(False) else: page_data.update( prettify.BuildPrettifyData(len(lines), attachment.filename)) return page_data