Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Side by Side Diff: appengine/monorail/framework/test/filecontent_test.py

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

Powered by Google App Engine
This is Rietveld 408576698