Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / lib / filetype_unittest.py
1 #!/usr/bin/python
2 # Copyright 2014 The Chromium OS Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """Unittests for the filetype.py module."""
7
8 from __future__ import print_function
9
10 import os
11 import sys
12
13 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(
14     os.path.abspath(__file__)))))
15
16 import stat
17
18 from chromite.lib import cros_test_lib
19 from chromite.lib import osutils
20 from chromite.lib import filetype
21 from chromite.lib import unittest_lib
22
23
24 class SplitShebangTest(cros_test_lib.TestCase):
25   """Test the SplitShebang function."""
26
27   def testSimpleCase(self):
28     """Test a simple case."""
29     self.assertEquals(('/bin/sh', ''), filetype.SplitShebang('#!/bin/sh'))
30
31   def testCaseWithArguments(self):
32     """Test a case with arguments."""
33     self.assertEquals(('/bin/sh', '-i -c "ls"'),
34                       filetype.SplitShebang('#!/bin/sh  -i -c "ls"'))
35
36   def testCaseWithEndline(self):
37     """Test a case finished with a newline char."""
38     self.assertEquals(('/bin/sh', '-i'),
39                       filetype.SplitShebang('#!/bin/sh  -i\n'))
40
41   def testCaseWithSpaces(self):
42     """Test a case with several spaces in the line."""
43     self.assertEquals(('/bin/sh', '-i'),
44                       filetype.SplitShebang('#!  /bin/sh  -i   \n'))
45
46   def testInvalidCases(self):
47     """Thes invalid cases."""
48     self.assertRaises(ValueError, filetype.SplitShebang, '/bin/sh -i')
49     self.assertRaises(ValueError, filetype.SplitShebang, '#!')
50     self.assertRaises(ValueError, filetype.SplitShebang, '#!env python')
51
52
53 class FileTypeDecoderTest(cros_test_lib.TempDirTestCase):
54   """Test the FileTypeDecoder class."""
55
56   def testSpecialFiles(self):
57     """Tests special files, such as symlinks, directories and named pipes."""
58     somedir = os.path.join(self.tempdir, 'somedir')
59     osutils.SafeMakedirs(somedir)
60     self.assertEquals('inode/directory',
61                       filetype.FileTypeDecoder.DecodeFile(somedir))
62
63     a_fifo = os.path.join(self.tempdir, 'a_fifo')
64     os.mknod(a_fifo, stat.S_IFIFO)
65     self.assertEquals('inode/special',
66                       filetype.FileTypeDecoder.DecodeFile(a_fifo))
67
68     empty_file = os.path.join(self.tempdir, 'empty_file')
69     osutils.WriteFile(empty_file, '')
70     self.assertEquals('inode/empty',
71                       filetype.FileTypeDecoder.DecodeFile(empty_file))
72
73     a_link = os.path.join(self.tempdir, 'a_link')
74     os.symlink('somewhere', a_link)
75     self.assertEquals('inode/symlink',
76                       filetype.FileTypeDecoder.DecodeFile(a_link))
77
78   def testTextShebangFiles(self):
79     """Test shebangs (#!) file decoding based on the executed path."""
80     # If the file has only one line is considered a "shebang" rather than a
81     # script.
82     shebang = os.path.join(self.tempdir, 'shebang')
83     osutils.WriteFile(shebang, "#!/bin/python --foo --bar\n")
84     self.assertEquals('text/shebang',
85                       filetype.FileTypeDecoder.DecodeFile(shebang))
86
87     # A shebang with contents is considered a script.
88     script = os.path.join(self.tempdir, 'script')
89     osutils.WriteFile(script, "#!/bin/foobar --foo --bar\n\nexit 1\n")
90     self.assertEquals('text/script',
91                       filetype.FileTypeDecoder.DecodeFile(script))
92
93     bash_script = os.path.join(self.tempdir, 'bash_script')
94     osutils.WriteFile(bash_script,
95                       "#!/bin/bash --debug\n# Copyright\nexit 42\n")
96     self.assertEquals('text/script/bash',
97                       filetype.FileTypeDecoder.DecodeFile(bash_script))
98
99     pyscript = os.path.join(self.tempdir, 'pyscript')
100     osutils.WriteFile(pyscript,
101                       "#!/usr/bin/env PYTHONPATH=/foo python-2.7 -3\n# foo\n")
102     self.assertEquals('text/script/python',
103                       filetype.FileTypeDecoder.DecodeFile(pyscript))
104
105     perlscript = os.path.join(self.tempdir, 'perlscript')
106     osutils.WriteFile(perlscript, "#!/usr/local/bin/perl\n#\n")
107     self.assertEquals('text/script/perl',
108                       filetype.FileTypeDecoder.DecodeFile(perlscript))
109
110   def testTextPEMFiles(self):
111     """Test decoding various PEM files."""
112     # A RSA private key (sample from update_engine unittest).
113     some_cert = os.path.join(self.tempdir, 'some_cert')
114     osutils.WriteFile(some_cert,
115                       """-----BEGIN CERTIFICATE-----
116 MIIDJTCCAo6gAwIBAgIJAP6IycaMXlqsMA0GCSqGSIb3DQEBBQUAMIGLMQswCQYD
117 VQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTETMBEGA1UEChMKR29vZ2xlIElu
118 YzESMBAGA1UECxMJQ2hyb21lIE9TMRgwFgYDVQQDEw9PcGVuU1NMIFRlc3QgQ0Ex
119 JDAiBgkqhkiG9w0BCQEWFXNlY3VyaXR5QGNocm9taXVtLm9yZzAgFw0xMjA1MTcx
120 OTQ1MjJaGA8yMTEyMDExNDE5NDUyMlowgZ0xCzAJBgNVBAYTAlVTMRMwEQYDVQQI
121 EwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKEwpH
122 b29nbGUgSW5jMRIwEAYDVQQLEwlDaHJvbWUgT1MxEjAQBgNVBAMTCTEyNy4wLjAu
123 MTEkMCIGCSqGSIb3DQEJARYVc2VjdXJpdHlAY2hyb21pdW0ub3JnMIGfMA0GCSqG
124 SIb3DQEBAQUAA4GNADCBiQKBgQC5bxzyvNJFDmyThIGoFoZkN3rlQB8QoR80rS1u
125 8pLyqW5Vk2A0pNOvcxPrUHAUTgWhikqzymz4a4XoLxat53H/t/XmRYwZ9GVNZocz
126 Q4naWxtPyPqIBosMLnWu6FHUVO1lTdvhC6Pjw2i1S9Rq3dMsANU1IER4NR8XM+v6
127 qBg1XQIDAQABo3sweTAJBgNVHRMEAjAAMCwGCWCGSAGG+EIBDQQfFh1PcGVuU1NM
128 IEdlbmVyYXRlZCBDZXJ0aWZpY2F0ZTAdBgNVHQ4EFgQU+biqy5tbRGlUNLsEtjmy
129 7v1eYqowHwYDVR0jBBgwFoAUC0E889mD7bn2fXcEtA3HvUimV+0wDQYJKoZIhvcN
130 AQEFBQADgYEA2fJxpGwDbnUF5Z3mvZ81/pk8fVQdJvV5U93EA29VE1SaFA5S5qYS
131 zP1Ue0MX+RqMLKjnH+E6yEoo+kYD9rzagnvORefbJeM92SiHgHPeSm8F1nQtGclj
132 p8izLBlcKgPHwQLKxELmbS/xvt4cyHaLSIy50lLrdJeKtXjqq4PbH3Y=
133 -----END CERTIFICATE-----
134 """)
135     self.assertEquals('text/pem/cert',
136                       filetype.FileTypeDecoder.DecodeFile(some_cert))
137
138     # A RSA private key (sample from vboot_reference unittest).
139     rsa_key = os.path.join(self.tempdir, 'rsa_key')
140     osutils.WriteFile(rsa_key,
141                       """-----BEGIN RSA PRIVATE KEY-----
142 MIICXAIBAAKBgQCdYBOJIJvGX9vC4E5XD1jb9zJ99FzR4G0n8HNyWy5ZKyy/hi80
143 ibXpy6QdWcm4wqTvmVjU+20sP4AgzKC65fKyFvvAHUiD4yGr1qWtg4YFUcBbUiXO
144 CQ66W3AC4g2Ju9C16AzMpBk043bQsUQvxILEumQqQ1VS33uM7Kq8dWpL6QIDAQAB
145 AoGAb12y1WIu+gWRtWkX5wHkRty6bWmEWbzwYcgFWlJuDQnBg9MICqy8/7Js85w7
146 ZLTRFQC2XRmDW0GggRVtVHUu9X2jwkHR9+TWza4xAtYcSwDl6VJTHX2ygptrG/n9
147 qPFinfvnpiP7b2WNjC53V3cnjg3m+1B5zrmFxsVLDMVLQhECQQDN7i1NWZFVNfYa
148 GT2GSgMpD0nPXA1HHUvFFgnI9xJkBCewHzega+PrrrpMKZZWLpc4YCm3PK9nI8Nk
149 EmJE5HwNAkEAw6OpiOgWdRaJWx3+XBsFOhz6K86xwV0NpVb6ocrBKU/t0OqP+gZh
150 B/YBDfwXPr2w5FCwozUs/MrBdoYR3WnsTQJABNn/pzrc+azzx1mg4XEM8gKyMnhw
151 t6QxDMugH2Pywvh2FuglX1orXHoZWYIBULZ4SZO6Z96+IyfsiocEWasoYQJBALZ/
152 onO7BM/+0Oz1osSq1Aps45Yf/0OAmW0mITDyIZR3IkJjvSEf+D3j5wHzqn91lmC1
153 QMFOpoO+ZBA7asjfuXUCQGmHgpC0BuD4S1QlcF0nrVHTG7Y8KZ18s9qPJS3csuGf
154 or10mrNRF3tyGy8e/sw88a74Q/6v/PgChZHmq6QjOOU=
155 -----END RSA PRIVATE KEY-----
156 """)
157     self.assertEquals('text/pem/rsa-private',
158                       filetype.FileTypeDecoder.DecodeFile(rsa_key))
159
160   def testBinaryELFFiles(self):
161     """Test decoding ELF files."""
162     liba_so = os.path.join(self.tempdir, 'liba.so')
163     unittest_lib.BuildELF(liba_so, ['func_a'])
164     self.assertEquals('binary/elf/dynamic-so',
165                       filetype.FileTypeDecoder.DecodeFile(liba_so))
166
167     prog = os.path.join(self.tempdir, 'prog')
168     unittest_lib.BuildELF(prog,
169                           undefined_symbols=['func_a'],
170                           used_libs=['a'],
171                           executable=True)
172     self.assertEquals('binary/elf/dynamic-bin',
173                       filetype.FileTypeDecoder.DecodeFile(prog))
174
175     prog_static = os.path.join(self.tempdir, 'prog_static')
176     unittest_lib.BuildELF(prog_static, executable=True, static=True)
177     self.assertEquals('binary/elf/static',
178                       filetype.FileTypeDecoder.DecodeFile(prog_static))
179
180   def testBinaryCompressedFiles(self):
181     """Test decoding compressed files."""
182     compressed = os.path.join(self.tempdir, 'compressed')
183
184     # `echo hola | gzip -9`
185     osutils.WriteFile(compressed,
186                       '\x1f\x8b\x08\x00<\xce\x07T\x02\x03\xcb\xc8\xcfI\xe4\x02'
187                       '\x00x\xad\xdb\xd1\x05\x00\x00\x00')
188     self.assertEquals('binary/compressed/gzip',
189                       filetype.FileTypeDecoder.DecodeFile(compressed))
190
191     # `echo hola | bzip2 -9`
192     osutils.WriteFile(compressed,
193                       'BZh91AY&SY\xfa\xd4\xdb5\x00\x00\x01A\x00\x00\x10 D\xa0'
194                       '\x00!\x83A\x9a\t\xa8qw$S\x85\t\x0f\xadM\xb3P')
195     self.assertEquals('binary/compressed/bzip2',
196                       filetype.FileTypeDecoder.DecodeFile(compressed))
197
198     # `echo hola | xz -9`
199     osutils.WriteFile(
200         compressed,
201         '\xfd7zXZ\x00\x00\x04\xe6\xd6\xb4F\x02\x00!\x01\x16\x00\x00\x00t/\xe5'
202         '\xa3\x01\x00\x04hola\n\x00\x00\x00\x00\xdd\xb0\x00\xac6w~\x9d\x00\x01'
203         '\x1d\x05\xb8-\x80\xaf\x1f\xb6\xf3}\x01\x00\x00\x00\x00\x04YZ')
204     self.assertEquals('binary/compressed/xz',
205                       filetype.FileTypeDecoder.DecodeFile(compressed))
206
207   def testBinaryMiscFiles(self):
208     """Test for various binary file formats."""
209     # A timezone file.
210     some_timezone = os.path.join(self.tempdir, 'some_timezone')
211     osutils.WriteFile(
212         some_timezone,
213         'TZif2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
214         '\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
215         '\x00\x01\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00UTC\x00\x00\x00TZif2'
216         '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
217         '\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
218         '\x01\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00UTC\x00\x00\x00\nUTC0\n')
219     self.assertEquals('binary/tzfile',
220                       filetype.FileTypeDecoder.DecodeFile(some_timezone))
221
222     # A x86 boot sector with just nops.
223     bootsec = os.path.join(self.tempdir, 'bootsec')
224     osutils.WriteFile(bootsec, '\x90' * 510 + '\x55\xaa')
225     self.assertEquals('binary/bootsector/x86',
226                       filetype.FileTypeDecoder.DecodeFile(bootsec))
227
228
229 if __name__ == '__main__':
230   cros_test_lib.main()