当前位置: 首页>>代码示例>>Python>>正文


Python Controller.get_version方法代码示例

本文整理汇总了Python中stem.control.Controller.get_version方法的典型用法代码示例。如果您正苦于以下问题:Python Controller.get_version方法的具体用法?Python Controller.get_version怎么用?Python Controller.get_version使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在stem.control.Controller的用法示例。


在下文中一共展示了Controller.get_version方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestControl

# 需要导入模块: from stem.control import Controller [as 别名]
# 或者: from stem.control.Controller import get_version [as 别名]
class TestControl(unittest.TestCase):
  def setUp(self):
    socket = stem.socket.ControlSocket()

    with patch('stem.control.Controller.add_event_listener', Mock()):
      self.controller = Controller(socket)

  @patch('stem.control.Controller.get_info')
  def test_get_version(self, get_info_mock):
    """
    Exercises the get_version() method.
    """

    try:
      # Use one version for first check.
      version_2_1 = "0.2.1.32"
      version_2_1_object = stem.version.Version(version_2_1)
      get_info_mock.return_value = version_2_1

      # Return a version with a cold cache.
      self.assertEqual(version_2_1_object, self.controller.get_version())

      # Use a different version for second check.
      version_2_2 = "0.2.2.39"
      version_2_2_object = stem.version.Version(version_2_2)
      get_info_mock.return_value = version_2_2

      # Return a version with a hot cache, so it will be the old version.
      self.assertEqual(version_2_1_object, self.controller.get_version())

      # Turn off caching.
      self.controller._is_caching_enabled = False
      # Return a version without caching, so it will be the new version.
      self.assertEqual(version_2_2_object, self.controller.get_version())

      # Raise an exception in the get_info() call.
      get_info_mock.side_effect = InvalidArguments

      # Get a default value when the call fails.
      self.assertEqual(
        "default returned",
        self.controller.get_version(default = "default returned")
      )

      # No default value, accept the error.
      self.assertRaises(InvalidArguments, self.controller.get_version)

      # Give a bad version.  The stem.version.Version ValueError should bubble up.
      version_A_42 = "0.A.42.spam"
      get_info_mock.return_value = version_A_42
      get_info_mock.side_effect = None
      self.assertRaises(ValueError, self.controller.get_version)
    finally:
      # Turn caching back on before we leave.
      self.controller._is_caching_enabled = True

  @patch('stem.control.Controller.get_info')
  @patch('stem.control.Controller.get_conf')
  def test_get_exit_policy(self, get_conf_mock, get_info_mock):
    """
    Exercises the get_exit_policy() method.
    """

    get_conf_mock.side_effect = lambda param, **kwargs: {
      "ExitPolicyRejectPrivate": "1",
      "ExitPolicy": ["accept *:80,   accept *:443", "accept 43.5.5.5,reject *:22"],
    }[param]

    get_info_mock.side_effect = lambda param, default = None: {
      "address": "123.45.67.89",
      "exit-policy/default": "reject *:25,reject *:119,reject *:135-139,reject *:445,reject *:563,reject *:1214,reject *:4661-4666,reject *:6346-6429,reject *:6699,reject *:6881-6999,accept *:*",
    }[param]

    expected = ExitPolicy(
      'reject 0.0.0.0/8:*',  # private entries
      'reject 169.254.0.0/16:*',
      'reject 127.0.0.0/8:*',
      'reject 192.168.0.0/16:*',
      'reject 10.0.0.0/8:*',
      'reject 172.16.0.0/12:*',
      'reject 123.45.67.89:*',  # relay's public address
      'accept *:80',  # finally we get to our ExitPolicy
      'accept *:443',
      'accept 43.5.5.5:*',
      'reject *:22',
      'reject *:25',  # default policy
      'reject *:119',
      'reject *:135-139',
      'reject *:445',
      'reject *:563',
      'reject *:1214',
      'reject *:4661-4666',
      'reject *:6346-6429',
      'reject *:6699',
      'reject *:6881-6999',
      'accept *:*',
    )

    self.assertEqual(expected, self.controller.get_exit_policy())

#.........这里部分代码省略.........
开发者ID:ouzel,项目名称:stem,代码行数:103,代码来源:controller.py

示例2: TestControl

# 需要导入模块: from stem.control import Controller [as 别名]
# 或者: from stem.control.Controller import get_version [as 别名]
class TestControl(unittest.TestCase):
    def setUp(self):
        socket = stem.socket.ControlSocket()

        with patch("stem.control.Controller.add_event_listener", Mock()):
            self.controller = Controller(socket)

    @patch("stem.control.Controller.get_info")
    def test_get_version(self, get_info_mock):
        """
    Exercises the get_version() method.
    """

        try:
            # Use one version for first check.
            version_2_1 = "0.2.1.32"
            version_2_1_object = stem.version.Version(version_2_1)
            get_info_mock.return_value = version_2_1

            # Return a version with a cold cache.
            self.assertEqual(version_2_1_object, self.controller.get_version())

            # Use a different version for second check.
            version_2_2 = "0.2.2.39"
            version_2_2_object = stem.version.Version(version_2_2)
            get_info_mock.return_value = version_2_2

            # Return a version with a hot cache, so it will be the old version.
            self.assertEqual(version_2_1_object, self.controller.get_version())

            # Turn off caching.
            self.controller._is_caching_enabled = False
            # Return a version without caching, so it will be the new version.
            self.assertEqual(version_2_2_object, self.controller.get_version())

            # Raise an exception in the get_info() call.
            get_info_mock.side_effect = InvalidArguments

            # Get a default value when the call fails.
            self.assertEqual("default returned", self.controller.get_version(default="default returned"))

            # No default value, accept the error.
            self.assertRaises(InvalidArguments, self.controller.get_version)

            # Give a bad version.  The stem.version.Version ValueError should bubble up.
            version_A_42 = "0.A.42.spam"
            get_info_mock.return_value = version_A_42
            get_info_mock.side_effect = None
            self.assertRaises(ValueError, self.controller.get_version)
        finally:
            # Turn caching back on before we leave.
            self.controller._is_caching_enabled = True

    @patch("stem.control.Controller.get_info")
    @patch("stem.control.Controller.get_conf")
    def test_get_exit_policy(self, get_conf_mock, get_info_mock):
        """
    Exercises the get_exit_policy() method.
    """

        get_conf_mock.side_effect = lambda param, **kwargs: {
            "ExitPolicyRejectPrivate": "1",
            "ExitPolicy": ["accept *:80,   accept *:443", "accept 43.5.5.5,reject *:22"],
        }[param]

        get_info_mock.side_effect = lambda param, default=None: {
            "address": "123.45.67.89",
            "exit-policy/default": "reject *:25,reject *:119,reject *:135-139,reject *:445,reject *:563,reject *:1214,reject *:4661-4666,reject *:6346-6429,reject *:6699,reject *:6881-6999,accept *:*",
        }[param]

        expected = ExitPolicy(
            "reject 0.0.0.0/8:*",  # private entries
            "reject 169.254.0.0/16:*",
            "reject 127.0.0.0/8:*",
            "reject 192.168.0.0/16:*",
            "reject 10.0.0.0/8:*",
            "reject 172.16.0.0/12:*",
            "reject 123.45.67.89:*",  # relay's public address
            "accept *:80",  # finally we get to our ExitPolicy
            "accept *:443",
            "accept 43.5.5.5:*",
            "reject *:22",
            "reject *:25",  # default policy
            "reject *:119",
            "reject *:135-139",
            "reject *:445",
            "reject *:563",
            "reject *:1214",
            "reject *:4661-4666",
            "reject *:6346-6429",
            "reject *:6699",
            "reject *:6881-6999",
            "accept *:*",
        )

        self.assertEqual(expected, self.controller.get_exit_policy())

    @patch("stem.control.Controller.get_info")
    @patch("stem.control.Controller.get_conf")
    def test_get_ports(self, get_conf_mock, get_info_mock):
#.........这里部分代码省略.........
开发者ID:Fuzew,项目名称:sharp-stem,代码行数:103,代码来源:controller.py

示例3: TestControl

# 需要导入模块: from stem.control import Controller [as 别名]
# 或者: from stem.control.Controller import get_version [as 别名]
class TestControl(unittest.TestCase):
  def setUp(self):
    socket = stem.socket.ControlSocket()

    mocking.mock_method(Controller, "add_event_listener", mocking.no_op())
    self.controller = Controller(socket, enable_caching = True)
    mocking.revert_mocking()

  def tearDown(self):
    mocking.revert_mocking()

  def test_get_version(self):
    """
    Exercises the get_version() method.
    """

    try:
      # Use one version for first check.
      version_2_1 = "0.2.1.32"
      version_2_1_object = stem.version.Version(version_2_1)
      mocking.mock_method(Controller, "get_info", mocking.return_value(version_2_1))

      # Return a version with a cold cache.
      self.assertEqual(version_2_1_object, self.controller.get_version())

      # Use a different version for second check.
      version_2_2 = "0.2.2.39"
      version_2_2_object = stem.version.Version(version_2_2)
      mocking.mock_method(Controller, "get_info", mocking.return_value(version_2_2))

      # Return a version with a hot cache, so it will be the old version.
      self.assertEqual(version_2_1_object, self.controller.get_version())

      # Turn off caching.
      self.controller._is_caching_enabled = False
      # Return a version without caching, so it will be the new version.
      self.assertEqual(version_2_2_object, self.controller.get_version())

      # Raise an exception in the get_info() call.
      mocking.mock_method(Controller, "get_info", mocking.raise_exception(InvalidArguments))

      # Get a default value when the call fails.
      self.assertEqual(
        "default returned",
        self.controller.get_version(default = "default returned")
      )

      # No default value, accept the error.
      self.assertRaises(InvalidArguments, self.controller.get_version)

      # Give a bad version.  The stem.version.Version ValueError should bubble up.
      version_A_42 = "0.A.42.spam"
      mocking.mock_method(Controller, "get_info", mocking.return_value(version_A_42))
      self.assertRaises(ValueError, self.controller.get_version)
    finally:
      # Turn caching back on before we leave.
      self.controller._is_caching_enabled = True

  def test_get_exit_policy(self):
    """
    Exercises the get_exit_policy() method.
    """

    mocking.mock_method(Controller, "get_conf", mocking.return_for_args({
      ("ExitPolicyRejectPrivate",): "1",
      ("ExitPolicy", "multiple=True"): ["accept *:80,   accept *:443", "accept 43.5.5.5,reject *:22"]
    }, is_method = True))

    mocking.mock_method(Controller, "get_info", mocking.return_for_args({
      ("address", None): "123.45.67.89",
      ("exit-policy/default",): "reject *:25,reject *:119,reject *:135-139,reject *:445,reject *:563,reject *:1214,reject *:4661-4666,reject *:6346-6429,reject *:6699,reject *:6881-6999,accept *:*"
    }, is_method = True))

    expected = ExitPolicy(
      'reject 0.0.0.0/8:*',  # private entries
      'reject 169.254.0.0/16:*',
      'reject 127.0.0.0/8:*',
      'reject 192.168.0.0/16:*',
      'reject 10.0.0.0/8:*',
      'reject 172.16.0.0/12:*',
      'reject 123.45.67.89:*',  # relay's public address
      'accept *:80',  # finally we get to our ExitPolicy
      'accept *:443',
      'accept 43.5.5.5:*',
      'reject *:22',
      'reject *:25',  # default policy
      'reject *:119',
      'reject *:135-139',
      'reject *:445',
      'reject *:563',
      'reject *:1214',
      'reject *:4661-4666',
      'reject *:6346-6429',
      'reject *:6699',
      'reject *:6881-6999',
      'accept *:*',
    )

    self.assertEqual(expected, self.controller.get_exit_policy())

#.........这里部分代码省略.........
开发者ID:gsathya,项目名称:stem,代码行数:103,代码来源:controller.py

示例4: TestControl

# 需要导入模块: from stem.control import Controller [as 别名]
# 或者: from stem.control.Controller import get_version [as 别名]
class TestControl(unittest.TestCase):
  def setUp(self):
    socket = stem.socket.ControlSocket()

    with patch('stem.control.Controller.add_event_listener', Mock()):
      self.controller = Controller(socket)

  def test_event_description(self):
    self.assertEqual("Logging at the debug runlevel. This is low level, high volume information about tor's internals that generally isn't useful to users.", stem.control.event_description('DEBUG'))
    self.assertEqual('Event emitted every second with the bytes sent and received by tor.', stem.control.event_description('BW'))
    self.assertEqual('Event emitted every second with the bytes sent and received by tor.', stem.control.event_description('bw'))

  def test_event_description_includes_all_events(self):
    self.assertEqual(None, stem.control.event_description('NO_SUCH_EVENT'))

    for event in stem.control.EventType:
      self.assertTrue(stem.control.event_description(event) is not None)

  @patch('stem.control.Controller.get_info')
  def test_get_version(self, get_info_mock):
    """
    Exercises the get_version() method.
    """

    try:
      # Use one version for first check.
      version_2_1 = '0.2.1.32'
      version_2_1_object = stem.version.Version(version_2_1)
      get_info_mock.return_value = version_2_1

      # Return a version with a cold cache.
      self.assertEqual(version_2_1_object, self.controller.get_version())

      # Use a different version for second check.
      version_2_2 = '0.2.2.39'
      version_2_2_object = stem.version.Version(version_2_2)
      get_info_mock.return_value = version_2_2

      # Return a version with a hot cache, so it will be the old version.
      self.assertEqual(version_2_1_object, self.controller.get_version())

      # Turn off caching.
      self.controller._is_caching_enabled = False
      # Return a version without caching, so it will be the new version.
      self.assertEqual(version_2_2_object, self.controller.get_version())

      # Spec says the getinfo response may optionally be prefixed by 'Tor '. In
      # practice it doesn't but we should accept that.
      get_info_mock.return_value = 'Tor 0.2.1.32'
      self.assertEqual(version_2_1_object, self.controller.get_version())

      # Raise an exception in the get_info() call.
      get_info_mock.side_effect = InvalidArguments

      # Get a default value when the call fails.
      self.assertEqual(
        'default returned',
        self.controller.get_version(default = 'default returned')
      )

      # No default value, accept the error.
      self.assertRaises(InvalidArguments, self.controller.get_version)

      # Give a bad version.  The stem.version.Version ValueError should bubble up.
      version_A_42 = '0.A.42.spam'
      get_info_mock.return_value = version_A_42
      get_info_mock.side_effect = None
      self.assertRaises(ValueError, self.controller.get_version)
    finally:
      # Turn caching back on before we leave.
      self.controller._is_caching_enabled = True

  @patch('stem.control.Controller.get_info')
  @patch('stem.control.Controller.get_conf')
  def test_get_exit_policy(self, get_conf_mock, get_info_mock):
    """
    Exercises the get_exit_policy() method.
    """

    get_conf_mock.side_effect = lambda param, **kwargs: {
      'ExitPolicyRejectPrivate': '1',
      'ExitPolicy': ['accept *:80,   accept *:443', 'accept 43.5.5.5,reject *:22'],
    }[param]

    get_info_mock.side_effect = lambda param, default = None: {
      'address': '123.45.67.89',
      'exit-policy/default': 'reject *:25,reject *:119,reject *:135-139,reject *:445,reject *:563,reject *:1214,reject *:4661-4666,reject *:6346-6429,reject *:6699,reject *:6881-6999,accept *:*',
    }[param]

    expected = ExitPolicy(
      'reject 0.0.0.0/8:*',  # private entries
      'reject 169.254.0.0/16:*',
      'reject 127.0.0.0/8:*',
      'reject 192.168.0.0/16:*',
      'reject 10.0.0.0/8:*',
      'reject 172.16.0.0/12:*',
      'reject 123.45.67.89:*',  # relay's public address
      'accept *:80',  # finally we get to our ExitPolicy
      'accept *:443',
      'accept 43.5.5.5:*',
#.........这里部分代码省略.........
开发者ID:patrickod,项目名称:stem,代码行数:103,代码来源:controller.py


注:本文中的stem.control.Controller.get_version方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。