1 #!/usr/bin/env python 2 # Copyright (c) 2012 The Chromium 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 import os 7 import shutil 8 import sys 9 import tempfile 10 import unittest 11 12 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) 13 TOOLS_DIR = os.path.dirname(SCRIPT_DIR) 14 CHROME_SRC = os.path.dirname(os.path.dirname(os.path.dirname(TOOLS_DIR))) 15 MOCK_DIR = os.path.join(CHROME_SRC, "third_party", "pymock") 16 17 # For the mock library 18 sys.path.append(MOCK_DIR) 19 import mock 20 21 # For getos, the module under test 22 sys.path.append(TOOLS_DIR) 23 import getos 24 import oshelpers 25 26 27 class TestCaseExtended(unittest.TestCase): 28 """Monkey patch some 2.7-only TestCase features.""" 29 # TODO(sbc): remove this once we switch to python2.7 everywhere 30 def assertIn(self, expr1, expr2, msg=None): 31 if hasattr(super(TestCaseExtended, self), 'assertIn'): 32 return super(TestCaseExtended, self).assertIn(expr1, expr2, msg) 33 if expr1 not in expr2: 34 self.fail(msg or '%r not in %r' % (expr1, expr2)) 35 36 37 class TestGetos(TestCaseExtended): 38 def setUp(self): 39 self.patch1 = mock.patch.dict('os.environ', 40 {'NACL_SDK_ROOT': os.path.dirname(TOOLS_DIR)}) 41 self.patch1.start() 42 self.patch2 = mock.patch.object(oshelpers, 'FindExeInPath', 43 return_value='/bin/ls') 44 self.patch2.start() 45 46 def tearDown(self): 47 self.patch1.stop() 48 self.patch2.stop() 49 50 def testGetSDKPath(self): 51 """honors environment variable.""" 52 with mock.patch.dict('os.environ', {'NACL_SDK_ROOT': 'dummy'}): 53 self.assertEqual(getos.GetSDKPath(), 'dummy') 54 55 def testGetSDKPathDefault(self): 56 """defaults to relative path.""" 57 del os.environ['NACL_SDK_ROOT'] 58 self.assertEqual(getos.GetSDKPath(), os.path.dirname(TOOLS_DIR)) 59 60 def testGetPlatform(self): 61 """returns a valid platform.""" 62 platform = getos.GetPlatform() 63 self.assertIn(platform, ('mac', 'linux', 'win')) 64 65 def testGetSystemArch(self): 66 """returns a valid architecture.""" 67 arch = getos.GetSystemArch(getos.GetPlatform()) 68 self.assertIn(arch, ('x86_64', 'x86_32', 'arm')) 69 70 def testGetChromePathEnv(self): 71 """honors CHROME_PATH environment.""" 72 with mock.patch.dict('os.environ', {'CHROME_PATH': '/dummy/file'}): 73 expect = "Invalid CHROME_PATH.*/dummy/file" 74 if hasattr(self, 'assertRaisesRegexp'): 75 with self.assertRaisesRegexp(getos.Error, expect): 76 getos.GetChromePath() 77 else: 78 # TODO(sbc): remove this path once we switch to python2.7 everywhere 79 self.assertRaises(getos.Error, getos.GetChromePath) 80 81 def testGetChromePathCheckExists(self): 82 """checks that existence of explicitly CHROME_PATH is checked.""" 83 mock_location = '/bin/ls' 84 if getos.GetPlatform() == 'win': 85 mock_location = 'c:\\nowhere' 86 with mock.patch.dict('os.environ', {'CHROME_PATH': mock_location}): 87 with mock.patch('os.path.exists') as mock_exists: 88 chrome = getos.GetChromePath() 89 self.assertEqual(chrome, mock_location) 90 mock_exists.assert_called_with(chrome) 91 92 def testGetHelperPath(self): 93 """GetHelperPath checks that helper exists.""" 94 platform = getos.GetPlatform() 95 # The helper is only needed on linux. On other platforms 96 # GetHelperPath() simply return empty string. 97 if platform == 'linux': 98 with mock.patch('os.path.exists') as mock_exists: 99 helper = getos.GetHelperPath(platform) 100 mock_exists.assert_called_with(helper) 101 else: 102 helper = getos.GetHelperPath(platform) 103 self.assertEqual(helper, '') 104 105 def testGetIrtBinPath(self): 106 """GetIrtBinPath checks that irtbin exists.""" 107 platform = getos.GetPlatform() 108 with mock.patch('os.path.exists') as mock_exists: 109 irt = getos.GetIrtBinPath(platform) 110 mock_exists.assert_called_with(irt) 111 112 def testGetLoaderPath(self): 113 """checks that loader exists.""" 114 platform = getos.GetPlatform() 115 with mock.patch('os.path.exists') as mock_exists: 116 loader = getos.GetLoaderPath(platform) 117 mock_exists.assert_called_with(loader) 118 119 def testGetNaClArch(self): 120 """returns a valid architecture.""" 121 platform = getos.GetPlatform() 122 # Since the unix implementation of GetNaClArch will run objdump on the 123 # chrome binary, and we want to be able to run this test without chrome 124 # installed we mock the GetChromePath call to return a known system binary, 125 # which objdump will work with. 126 with mock.patch('getos.GetChromePath') as mock_chrome_path: 127 mock_chrome_path.return_value = '/bin/ls' 128 arch = getos.GetNaClArch(platform) 129 self.assertIn(arch, ('x86_64', 'x86_32', 'arm')) 130 131 132 class TestGetosWithTempdir(TestCaseExtended): 133 def setUp(self): 134 self.tempdir = tempfile.mkdtemp("_sdktest") 135 self.patch = mock.patch.dict('os.environ', 136 {'NACL_SDK_ROOT': self.tempdir}) 137 self.patch.start() 138 139 def tearDown(self): 140 shutil.rmtree(self.tempdir) 141 self.patch.stop() 142 143 def testGetSDKVersion(self): 144 """correctly parses README to find SDK version.""" 145 expected_version = (16, 196) 146 with open(os.path.join(self.tempdir, 'README'), 'w') as out: 147 out.write('Version: %s\n' % expected_version[0]) 148 out.write('Chrome Revision: %s\n' % expected_version[1]) 149 150 version = getos.GetSDKVersion() 151 self.assertEqual(version, expected_version) 152 153 154 if __name__ == '__main__': 155 unittest.main() 156