breakpad_unittest.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env python
  2. # Copyright (c) 2011 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. """Unit tests for breakpad.py."""
  6. import os
  7. import sys
  8. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  9. from testing_support.super_mox import SuperMoxTestBase
  10. import breakpad
  11. class Breakpad(SuperMoxTestBase):
  12. """Setups and tear downs the mocks but doesn't test anything as-is."""
  13. def setUp(self):
  14. super(Breakpad, self).setUp()
  15. self.mox.StubOutWithMock(breakpad.atexit, 'register')
  16. self.mox.StubOutWithMock(breakpad.getpass, 'getuser')
  17. self.mox.StubOutWithMock(breakpad.urllib2, 'urlopen')
  18. breakpad._HOST_NAME = 'bozo'
  19. self._old_sys_argv = breakpad.sys.argv
  20. breakpad.sys.argv = ['my_test']
  21. self._old_sys_version = breakpad.sys.version
  22. breakpad.sys.version = 'random python'
  23. def tearDown(self):
  24. breakpad.sys.version = self._old_sys_version
  25. breakpad.sys.argv = self._old_sys_argv
  26. super(Breakpad, self).tearDown()
  27. def testMembersChanged(self):
  28. members = [
  29. 'CheckForException', 'DEFAULT_URL', 'FormatException', 'Register',
  30. 'SendProfiling', 'SendStack',
  31. 'atexit', 'getpass', 'os', 'post', 'socket', 'sys', 'time', 'traceback',
  32. 'urllib', 'urllib2',
  33. ]
  34. # If this test fails, you should add the relevant test.
  35. self.compareMembers(breakpad, members)
  36. def _part_1_setup_mocks(self, exception):
  37. breakpad.os.getcwd().AndReturn('/tmp/asdf')
  38. breakpad.getpass.getuser().AndReturn('georges')
  39. obj = self.mox.CreateMockAnything()
  40. kwargs = {}
  41. if (breakpad.sys.version_info[0] * 10 + breakpad.sys.version_info[1]) >= 26:
  42. kwargs['timeout'] = 4
  43. breakpad.urllib2.urlopen(
  44. 'https://chromium-status.appspot.com/breakpad',
  45. breakpad.urllib.urlencode([('exception', exception)]) + (
  46. '&args=%5B%27my_test%27%5D'
  47. '&stack=bar'
  48. '&host=bozo'
  49. '&version=random+python'
  50. '&user=georges'
  51. '&cwd=%2Ftmp%2Fasdf'),
  52. **kwargs).AndReturn(obj)
  53. obj.read().AndReturn('ok')
  54. obj.close()
  55. def _part_2_verify_stdout(self, exception):
  56. self.checkstdout(
  57. ( "Sending crash report ...\n"
  58. " args: ['my_test']\n"
  59. " cwd: /tmp/asdf\n"
  60. " exception: %s\n"
  61. " host: bozo\n"
  62. " stack: bar\n"
  63. " user: georges\n"
  64. " version: random python\n"
  65. "ok\n") % exception)
  66. def _check(self, obj, result):
  67. self._part_1_setup_mocks(result)
  68. self.mox.ReplayAll()
  69. breakpad.SendStack(obj, 'bar')
  70. self._part_2_verify_stdout(result)
  71. def testSendBase(self):
  72. self._check('foo', 'foo')
  73. def testSendReprThrows(self):
  74. class Throws(object):
  75. def __repr__(self):
  76. raise NotImplementedError()
  77. def __str__(self):
  78. return '[foo]'
  79. self._check(Throws(), '[foo]')
  80. def testSendStrThrows(self):
  81. class Throws(object):
  82. def __repr__(self):
  83. return '[foo]'
  84. def __str__(self):
  85. raise NotImplementedError()
  86. self._check(Throws(), '[foo]')
  87. def testSendBoth(self):
  88. class Both(object):
  89. def __repr__(self):
  90. return '[foo]'
  91. def __str__(self):
  92. return '[bar]'
  93. self._check(Both(), '[bar]')
  94. def testSendException(self):
  95. obj = Exception('foo')
  96. obj.msg = 'a message'
  97. self._check(obj, 'foo\nMsg: a message')
  98. if __name__ == '__main__':
  99. import unittest
  100. unittest.main()