Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style |
| 3 | # license that can be found in the LICENSE file or at |
| 4 | # https://developers.google.com/open-source/licenses/bsd |
| 5 | |
| 6 | """Tests for the filecontent module.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import unittest |
| 12 | |
| 13 | from framework import filecontent |
| 14 | |
| 15 | |
| 16 | class MimeTest(unittest.TestCase): |
| 17 | """Test methods for the mime module.""" |
| 18 | |
| 19 | _TEST_EXTENSIONS_TO_CTYPES = { |
| 20 | 'html': 'text/plain', |
| 21 | 'htm': 'text/plain', |
| 22 | 'jpg': 'image/jpeg', |
| 23 | 'jpeg': 'image/jpeg', |
| 24 | 'pdf': 'application/pdf', |
| 25 | } |
| 26 | |
| 27 | _CODE_EXTENSIONS = [ |
| 28 | 'py', 'java', 'mf', 'bat', 'sh', 'php', 'vb', 'pl', 'sql', |
| 29 | 'patch', 'diff', |
| 30 | ] |
| 31 | |
| 32 | def testCommonExtensions(self): |
| 33 | """Tests some common extensions for their expected content types.""" |
| 34 | for ext, ctype in self._TEST_EXTENSIONS_TO_CTYPES.items(): |
| 35 | self.assertEqual( |
| 36 | filecontent.GuessContentTypeFromFilename('file.%s' % ext), |
| 37 | ctype) |
| 38 | |
| 39 | def testCaseDoesNotMatter(self): |
| 40 | """Ensure that case (upper/lower) of extension does not matter.""" |
| 41 | for ext, ctype in self._TEST_EXTENSIONS_TO_CTYPES.items(): |
| 42 | ext = ext.upper() |
| 43 | self.assertEqual( |
| 44 | filecontent.GuessContentTypeFromFilename('file.%s' % ext), |
| 45 | ctype) |
| 46 | |
| 47 | for ext in self._CODE_EXTENSIONS: |
| 48 | ext = ext.upper() |
| 49 | self.assertEqual( |
| 50 | filecontent.GuessContentTypeFromFilename('code.%s' % ext), |
| 51 | 'text/plain') |
| 52 | |
| 53 | def testCodeIsText(self): |
| 54 | """Ensure that code extensions are text/plain.""" |
| 55 | for ext in self._CODE_EXTENSIONS: |
| 56 | self.assertEqual( |
| 57 | filecontent.GuessContentTypeFromFilename('code.%s' % ext), |
| 58 | 'text/plain') |
| 59 | |
| 60 | def testNoExtensionIsText(self): |
| 61 | """Ensure that no extension indicates text/plain.""" |
| 62 | self.assertEqual( |
| 63 | filecontent.GuessContentTypeFromFilename('noextension'), |
| 64 | 'text/plain') |
| 65 | |
| 66 | def testUnknownExtension(self): |
| 67 | """Ensure that an obviously unknown extension returns is binary.""" |
| 68 | self.assertEqual( |
| 69 | filecontent.GuessContentTypeFromFilename('f.madeupextension'), |
| 70 | 'application/octet-stream') |
| 71 | |
| 72 | def testNoShockwaveFlash(self): |
| 73 | """Ensure that Shockwave files will NOT be served w/ that content type.""" |
| 74 | self.assertEqual( |
| 75 | filecontent.GuessContentTypeFromFilename('bad.swf'), |
| 76 | 'application/octet-stream') |
| 77 | |
| 78 | |
| 79 | class DecodeFileContentsTest(unittest.TestCase): |
| 80 | |
| 81 | def IsBinary(self, contents): |
| 82 | _contents, is_binary, _is_long = ( |
| 83 | filecontent.DecodeFileContents(contents)) |
| 84 | return is_binary |
| 85 | |
| 86 | def testFileIsBinaryEmpty(self): |
| 87 | self.assertFalse(self.IsBinary('')) |
| 88 | |
| 89 | def testFileIsBinaryShortText(self): |
| 90 | self.assertFalse(self.IsBinary('This is some plain text.')) |
| 91 | |
| 92 | def testLineLengthDetection(self): |
| 93 | unicode_str = ( |
| 94 | u'Some non-ascii chars - ' |
| 95 | u'\xa2\xfa\xb6\xe7\xfc\xea\xd0\xf4\xe6\xf0\xce\xf6\xbe') |
| 96 | short_line = unicode_str.encode('iso-8859-1') |
| 97 | long_line = (unicode_str * 100)[:filecontent._MAX_SOURCE_LINE_LEN_LOWER+1] |
| 98 | long_line = long_line.encode('iso-8859-1') |
| 99 | |
| 100 | lines = [short_line] * 100 |
| 101 | lines.append(long_line) |
| 102 | |
| 103 | # High lower ratio - text |
| 104 | self.assertFalse(self.IsBinary('\n'.join(lines))) |
| 105 | |
| 106 | lines.extend([long_line] * 99) |
| 107 | |
| 108 | # 50/50 lower/upper ratio - binary |
| 109 | self.assertTrue(self.IsBinary('\n'.join(lines))) |
| 110 | |
| 111 | # Single line too long - binary |
| 112 | lines = [short_line] * 100 |
| 113 | lines.append(short_line * 100) # Very long line |
| 114 | self.assertTrue(self.IsBinary('\n'.join(lines))) |
| 115 | |
| 116 | def testFileIsBinaryLongText(self): |
| 117 | self.assertFalse(self.IsBinary('This is plain text. \n' * 100)) |
| 118 | # long utf-8 lines are OK |
| 119 | self.assertFalse(self.IsBinary('This one long line. ' * 100)) |
| 120 | |
| 121 | def testFileIsBinaryLongBinary(self): |
| 122 | bin_string = ''.join([chr(c) for c in range(122, 252)]) |
| 123 | self.assertTrue(self.IsBinary(bin_string * 100)) |
| 124 | |
| 125 | def testFileIsTextByPath(self): |
| 126 | bin_string = ''.join([chr(c) for c in range(122, 252)] * 100) |
| 127 | unicode_str = ( |
| 128 | u'Some non-ascii chars - ' |
| 129 | u'\xa2\xfa\xb6\xe7\xfc\xea\xd0\xf4\xe6\xf0\xce\xf6\xbe') |
| 130 | long_line = (unicode_str * 100)[:filecontent._MAX_SOURCE_LINE_LEN_LOWER+1] |
| 131 | long_line = long_line.encode('iso-8859-1') |
| 132 | |
| 133 | for contents in [bin_string, long_line]: |
| 134 | self.assertTrue(filecontent.DecodeFileContents(contents, path=None)[1]) |
| 135 | self.assertTrue(filecontent.DecodeFileContents(contents, path='')[1]) |
| 136 | self.assertTrue(filecontent.DecodeFileContents(contents, path='foo')[1]) |
| 137 | self.assertTrue( |
| 138 | filecontent.DecodeFileContents(contents, path='foo.bin')[1]) |
| 139 | self.assertTrue( |
| 140 | filecontent.DecodeFileContents(contents, path='foo.zzz')[1]) |
| 141 | for path in ['a/b/Makefile.in', 'README', 'a/file.js', 'b.txt']: |
| 142 | self.assertFalse( |
| 143 | filecontent.DecodeFileContents(contents, path=path)[1]) |
| 144 | |
| 145 | def testFileIsBinaryByCommonExtensions(self): |
| 146 | contents = 'this is not examined' |
| 147 | self.assertTrue(filecontent.DecodeFileContents( |
| 148 | contents, path='junk.zip')[1]) |
| 149 | self.assertTrue(filecontent.DecodeFileContents( |
| 150 | contents, path='JUNK.ZIP')[1]) |
| 151 | self.assertTrue(filecontent.DecodeFileContents( |
| 152 | contents, path='/build/HelloWorld.o')[1]) |
| 153 | self.assertTrue(filecontent.DecodeFileContents( |
| 154 | contents, path='/build/Hello.class')[1]) |
| 155 | self.assertTrue(filecontent.DecodeFileContents( |
| 156 | contents, path='/trunk/libs.old/swing.jar')[1]) |
| 157 | |
| 158 | self.assertFalse(filecontent.DecodeFileContents( |
| 159 | contents, path='HelloWorld.cc')[1]) |
| 160 | self.assertFalse(filecontent.DecodeFileContents( |
| 161 | contents, path='Hello.java')[1]) |
| 162 | self.assertFalse(filecontent.DecodeFileContents( |
| 163 | contents, path='README')[1]) |
| 164 | self.assertFalse(filecontent.DecodeFileContents( |
| 165 | contents, path='READ.ME')[1]) |
| 166 | self.assertFalse(filecontent.DecodeFileContents( |
| 167 | contents, path='README.txt')[1]) |
| 168 | self.assertFalse(filecontent.DecodeFileContents( |
| 169 | contents, path='README.TXT')[1]) |
| 170 | self.assertFalse(filecontent.DecodeFileContents( |
| 171 | contents, path='/trunk/src/com/monorail/Hello.java')[1]) |
| 172 | self.assertFalse(filecontent.DecodeFileContents( |
| 173 | contents, path='/branches/1.2/resource.el')[1]) |
| 174 | self.assertFalse(filecontent.DecodeFileContents( |
| 175 | contents, path='/wiki/PageName.wiki')[1]) |
| 176 | |
| 177 | def testUnreasonablyLongFile(self): |
| 178 | contents = '\n' * (filecontent.SOURCE_FILE_MAX_LINES + 2) |
| 179 | _contents, is_binary, is_long = filecontent.DecodeFileContents( |
| 180 | contents) |
| 181 | self.assertFalse(is_binary) |
| 182 | self.assertTrue(is_long) |
| 183 | |
| 184 | contents = '\n' * 100 |
| 185 | _contents, is_binary, is_long = filecontent.DecodeFileContents( |
| 186 | contents) |
| 187 | self.assertFalse(is_binary) |
| 188 | self.assertFalse(is_long) |