PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/python/google/appengine/tools/devappserver2/python/stubs_test.py

https://gitlab.com/gregtyka/frankenserver
Python | 314 lines | 284 code | 13 blank | 17 comment | 2 complexity | a87e377baf76fbe613575ec614a02690 MD5 | raw file
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2007 Google Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. """Tests for google.appengine.tools.devappserver2.python.stubs."""
  18. import errno
  19. import locale
  20. import mimetypes
  21. import os
  22. import random
  23. import shutil
  24. import sys
  25. import tempfile
  26. import unittest
  27. import google
  28. from distutils import util
  29. import mox
  30. from google.appengine.tools.devappserver2.python import stubs
  31. class StubsTest(unittest.TestCase):
  32. def setUp(self):
  33. super(StubsTest, self).setUp()
  34. self.platform = sys.platform
  35. self.mox = mox.Mox()
  36. self.mox.StubOutWithMock(locale, 'setlocale')
  37. self.mox.StubOutWithMock(util, 'get_platform')
  38. self.mox.StubOutWithMock(stubs.FakeFile, 'is_file_accessible')
  39. def tearDown(self):
  40. self.mox.UnsetStubs()
  41. sys.platform = self.platform
  42. super(StubsTest, self).tearDown()
  43. def test_os_error_not_implemented(self):
  44. with self.assertRaises(OSError) as cm:
  45. stubs.os_error_not_implemented()
  46. self.mox.VerifyAll()
  47. e = cm.exception
  48. self.assertEqual(errno.ENOSYS, e.errno)
  49. self.assertEqual('Function not implemented', e.strerror)
  50. self.assertIsNone(e.filename)
  51. def test_return_minus_one(self):
  52. self.assertEqual(-1, stubs.return_minus_one())
  53. def test_fake_uname(self):
  54. self.assertEqual(('Linux', '', '', '', ''), stubs.fake_uname())
  55. def test_fake_access_accessible(self):
  56. stubs.FakeFile.is_file_accessible(__file__).AndReturn(True)
  57. self.mox.ReplayAll()
  58. self.assertTrue(stubs.fake_access(__file__, os.R_OK))
  59. self.mox.VerifyAll()
  60. def test_fake_access_inaccessible(self):
  61. stubs.FakeFile.is_file_accessible(__file__).AndReturn(False)
  62. self.mox.ReplayAll()
  63. self.assertFalse(stubs.fake_access(__file__, os.R_OK))
  64. self.mox.VerifyAll()
  65. def test_fake_access_write(self):
  66. self.mox.ReplayAll()
  67. self.assertFalse(stubs.fake_access(__file__, os.W_OK))
  68. self.mox.VerifyAll()
  69. def test_fake_open_accessible(self):
  70. stubs.FakeFile.is_file_accessible(__file__).AndReturn(True)
  71. self.mox.ReplayAll()
  72. os.close(stubs.fake_open(__file__, os.O_RDONLY))
  73. self.mox.VerifyAll()
  74. def test_fake_open_inaccessible(self):
  75. stubs.FakeFile.is_file_accessible(__file__).AndReturn(False)
  76. self.mox.ReplayAll()
  77. with self.assertRaises(OSError) as cm:
  78. stubs.fake_open(__file__, os.O_RDONLY)
  79. self.mox.VerifyAll()
  80. e = cm.exception
  81. self.assertEqual(errno.ENOENT, e.errno)
  82. self.assertEqual('No such file or directory', e.strerror)
  83. self.assertEqual(__file__, e.filename)
  84. self.mox.VerifyAll()
  85. def test_fake_open_write(self):
  86. self.mox.ReplayAll()
  87. with self.assertRaises(OSError) as cm:
  88. stubs.fake_open(__file__, os.O_RDWR)
  89. self.mox.VerifyAll()
  90. e = cm.exception
  91. self.assertEqual(errno.EROFS, e.errno)
  92. self.assertEqual('Read-only file system', e.strerror)
  93. self.assertEqual(__file__, e.filename)
  94. self.mox.VerifyAll()
  95. def test_fake_set_locale_allowed(self):
  96. locale.setlocale(0, 'C')
  97. locale.setlocale(0, 'C')
  98. locale.setlocale(0, 'C')
  99. locale.setlocale(0, 'C')
  100. self.mox.ReplayAll()
  101. stubs.fake_set_locale(0, 'C', original_setlocale=locale.setlocale)
  102. stubs.fake_set_locale(0, None, original_setlocale=locale.setlocale)
  103. stubs.fake_set_locale(0, '', original_setlocale=locale.setlocale)
  104. stubs.fake_set_locale(0, 'POSIX', original_setlocale=locale.setlocale)
  105. self.mox.VerifyAll()
  106. def test_fake_set_locale_not_allowed(self):
  107. self.mox.ReplayAll()
  108. self.assertRaises(locale.Error, stubs.fake_set_locale, 0, 'AAAA')
  109. self.mox.VerifyAll()
  110. def test_fake_get_platform(self):
  111. sys.platform = 'linux2'
  112. self.mox.ReplayAll()
  113. self.assertEqual('linux-', stubs.fake_get_platform())
  114. self.mox.VerifyAll()
  115. def test_fake_get_platform_darwin(self):
  116. sys.platform = 'darwin'
  117. self.mox.ReplayAll()
  118. self.assertEqual('macosx-', stubs.fake_get_platform())
  119. self.mox.VerifyAll()
  120. def test_restricted_path_function_allowed(self):
  121. fake_function = self.mox.CreateMockAnything()
  122. fake_function('foo', bar='baz').AndReturn(1)
  123. stubs.FakeFile.is_file_accessible('foo').AndReturn(True)
  124. self.mox.ReplayAll()
  125. restricted_path_fake_function = stubs.RestrictedPathFunction(fake_function)
  126. self.assertEqual(1, restricted_path_fake_function('foo', bar='baz'))
  127. self.mox.VerifyAll()
  128. def test_restricted_path_function_not_allowed(self):
  129. fake_function = self.mox.CreateMockAnything()
  130. stubs.FakeFile.is_file_accessible('foo').AndReturn(False)
  131. self.mox.ReplayAll()
  132. restricted_path_fake_function = stubs.RestrictedPathFunction(fake_function)
  133. with self.assertRaises(OSError) as cm:
  134. restricted_path_fake_function('foo', bar='baz')
  135. self.mox.VerifyAll()
  136. e = cm.exception
  137. self.assertEqual(errno.EACCES, e.errno)
  138. self.assertEqual('path not accessible', e.strerror)
  139. self.assertEqual('foo', e.filename)
  140. class FakeFileTest(unittest.TestCase):
  141. def setUp(self):
  142. super(FakeFileTest, self).setUp()
  143. self.mox = mox.Mox()
  144. self.tempdir = tempfile.mkdtemp()
  145. stubs.FakeFile._application_paths = []
  146. stubs.FakeFile.set_skip_files('^$')
  147. stubs.FakeFile.set_static_files('^$')
  148. def tearDown(self):
  149. stubs.FakeFile._application_paths = []
  150. self.mox.UnsetStubs()
  151. shutil.rmtree(self.tempdir)
  152. super(FakeFileTest, self).tearDown()
  153. def test_init_accessible(self):
  154. self.mox.StubOutWithMock(stubs.FakeFile, 'is_file_accessible')
  155. stubs.FakeFile.is_file_accessible(__file__).AndReturn(True)
  156. self.mox.ReplayAll()
  157. with stubs.FakeFile(__file__) as f:
  158. fake_file_content = f.read()
  159. self.mox.VerifyAll()
  160. with open(__file__) as f:
  161. real_file_content = f.read()
  162. self.assertEqual(real_file_content, fake_file_content)
  163. def test_init_inaccessible(self):
  164. self.mox.StubOutWithMock(stubs.FakeFile, 'is_file_accessible')
  165. stubs.FakeFile.is_file_accessible(__file__).AndReturn(False)
  166. self.mox.ReplayAll()
  167. self.assertRaises(IOError, stubs.FakeFile, __file__)
  168. self.mox.VerifyAll()
  169. def test_init_to_write(self):
  170. self.mox.StubOutWithMock(stubs.FakeFile, 'is_file_accessible')
  171. self.mox.ReplayAll()
  172. self.assertRaises(IOError, stubs.FakeFile, __file__, 'w')
  173. self.mox.VerifyAll()
  174. def test_init_to_append(self):
  175. self.mox.StubOutWithMock(stubs.FakeFile, 'is_file_accessible')
  176. self.mox.ReplayAll()
  177. self.assertRaises(IOError, stubs.FakeFile, __file__, 'a')
  178. self.mox.VerifyAll()
  179. def test_init_to_read_plus(self):
  180. self.mox.StubOutWithMock(stubs.FakeFile, 'is_file_accessible')
  181. self.mox.ReplayAll()
  182. self.assertRaises(IOError, stubs.FakeFile, __file__, 'r+')
  183. self.mox.VerifyAll()
  184. def test_is_accessible_accessible(self):
  185. open(os.path.join(self.tempdir, 'allowed'), 'w').close()
  186. stubs.FakeFile.set_allowed_paths(self.tempdir, [])
  187. self.assertTrue(stubs.FakeFile.is_file_accessible(
  188. os.path.join(self.tempdir, 'allowed')))
  189. def test_is_accessible_not_accessible(self):
  190. open(os.path.join(self.tempdir, 'not_allowed'), 'w').close()
  191. stubs.FakeFile.set_allowed_paths(os.path.join(self.tempdir, 'allowed'), [])
  192. self.assertFalse(stubs.FakeFile.is_file_accessible(
  193. os.path.join(self.tempdir, 'not_allowed')))
  194. def test_is_accessible_accessible_directory(self):
  195. os.mkdir(os.path.join(self.tempdir, 'allowed'))
  196. stubs.FakeFile.set_allowed_paths(self.tempdir, [])
  197. self.assertTrue(stubs.FakeFile.is_file_accessible(
  198. os.path.join(self.tempdir, 'allowed')))
  199. def test_is_accessible_not_accessible_directory(self):
  200. os.mkdir(os.path.join(self.tempdir, 'not_allowed'))
  201. stubs.FakeFile.set_allowed_paths(os.path.join(self.tempdir, 'allowed'), [])
  202. self.assertFalse(stubs.FakeFile.is_file_accessible(
  203. os.path.join(self.tempdir, 'not_allowed')))
  204. def test_is_accessible_accessible_in_application_dir(self):
  205. open(os.path.join(self.tempdir, 'allowed'), 'w').close()
  206. stubs.FakeFile.set_allowed_paths('.', [self.tempdir])
  207. self.assertTrue(stubs.FakeFile.is_file_accessible(
  208. os.path.join(self.tempdir, 'allowed')))
  209. def test_is_accessible_accessible_directory_in_application_dir(self):
  210. os.mkdir(os.path.join(self.tempdir, 'allowed'))
  211. stubs.FakeFile.set_allowed_paths('.', [self.tempdir])
  212. self.assertTrue(stubs.FakeFile.is_file_accessible(
  213. os.path.join(self.tempdir, 'allowed')))
  214. def test_is_accessible_mimetypes_files(self):
  215. stubs.FakeFile.set_allowed_paths(self.tempdir, [])
  216. for filename in mimetypes.knownfiles:
  217. if os.path.isfile(filename):
  218. self.assertTrue(stubs.FakeFile.is_file_accessible(filename))
  219. def test_is_accessible_skipped(self):
  220. open(os.path.join(self.tempdir, 'allowed'), 'w').close()
  221. stubs.FakeFile.set_allowed_paths(self.tempdir, [])
  222. stubs.FakeFile.set_skip_files('^%s$' % 'allowed')
  223. self.assertFalse(stubs.FakeFile.is_file_accessible(
  224. os.path.join(self.tempdir, 'allowed')))
  225. def test_is_accessible_skipped_root_appdir(self):
  226. stubs.FakeFile.set_allowed_paths(self.tempdir, [])
  227. stubs.FakeFile.set_skip_files(r'^(\..*)|$')
  228. self.assertTrue(stubs.FakeFile.is_file_accessible(self.tempdir))
  229. def test_is_accessible_skipped_root_appdir_with_trailing_slash(self):
  230. stubs.FakeFile.set_allowed_paths(self.tempdir, [])
  231. stubs.FakeFile.set_skip_files(r'^(\..*)|$')
  232. self.assertTrue(stubs.FakeFile.is_file_accessible(
  233. '%s%s' % (self.tempdir, os.path.sep)))
  234. def test_is_accessible_skipped_and_not_accessible(self):
  235. stubs.FakeFile.set_allowed_paths(os.path.join(self.tempdir, 'allowed'), [])
  236. stubs.FakeFile.set_skip_files('^.*%s.*$' % 'not_allowed')
  237. self.assertFalse(stubs.FakeFile.is_file_accessible(
  238. os.path.join(self.tempdir, 'not_allowed')))
  239. def test_is_accessible_skipped_outside_appdir(self):
  240. stubs.FakeFile.set_allowed_paths(os.path.join(self.tempdir, 'foo'),
  241. [os.path.join(self.tempdir, 'allowed')])
  242. stubs.FakeFile.set_skip_files('^.*%s.*$' % 'filename')
  243. self.assertTrue(stubs.FakeFile.is_file_accessible(
  244. os.path.join(self.tempdir, 'allowed', 'filename')))
  245. def test_is_accessible_static(self):
  246. open(os.path.join(self.tempdir, 'allowed'), 'w').close()
  247. stubs.FakeFile.set_allowed_paths(self.tempdir, [])
  248. stubs.FakeFile.set_static_files('^%s$' % 'allowed')
  249. self.assertFalse(stubs.FakeFile.is_file_accessible(
  250. os.path.join(self.tempdir, 'allowed')))
  251. def test_is_accessible_static_and_not_accessible(self):
  252. stubs.FakeFile.set_allowed_paths(os.path.join(self.tempdir, 'allowed'), [])
  253. stubs.FakeFile.set_static_files('^.*%s.*$' % 'not_allowed')
  254. self.assertFalse(stubs.FakeFile.is_file_accessible(
  255. os.path.join(self.tempdir, 'not_allowed')))
  256. def test_is_accessible_skipped_and_static_root_appdir(self):
  257. stubs.FakeFile.set_allowed_paths(self.tempdir, [])
  258. self.assertTrue(stubs.FakeFile.is_file_accessible(self.tempdir))
  259. def test_is_accessible_none_filename(self):
  260. self.assertRaises(TypeError, stubs.FakeFile.is_file_accessible, None)
  261. if __name__ == '__main__':
  262. unittest.main()