Beispiel #1
0
    def setUp(self):
        unittest.TestCase.setUp(self)
        self.cx = self.MockCx()
        stack = mock.MagicMock()
        stack.getDictionary.return_value = self.cx
        self.app = mock.MagicMock()

        self.peer_listener = HSSPeerListener(self.app, "domain", stack)
        self.peer = mock.MagicMock()
        self.peer_listener.connected(self.peer)
        self.assertEquals(self.peer, self.peer_listener.peer)
        settings.SPROUT_HOSTNAME = "sprout"
        settings.SPROUT_PORT = 1234
Beispiel #2
0
    def setUp(self):
        unittest.TestCase.setUp(self)
        self.cx = self.MockCx()
        stack = mock.MagicMock()
        stack.getDictionary.return_value = self.cx
        self.app = mock.MagicMock()

        self.peer_listener = HSSPeerListener(self.app, "domain", stack)
        self.peer = mock.MagicMock()
        self.peer_listener.connected(self.peer)
        self.assertEquals(self.peer, self.peer_listener.peer)
        settings.SPROUT_HOSTNAME = "sprout"
        settings.SPROUT_PORT = 1234
Beispiel #3
0
class TestHSSPeerListener(unittest.TestCase):
    class MockRequest(mock.MagicMock):
        def __init__(self):
            mock.MagicMock.__init__(self)
            self.avps = []

        def addAVP(self, avp):
            self.avps.append(avp)

    class MockCx(mock.MagicMock):
        class MockAVP(mock.MagicMock):
            def __init__(self, avp, value=None):
                mock.MagicMock.__init__(self)
                self.avp = avp
                self.value = value

            def withOctetString(self, s):
                return {self.avp: s}

            def withInteger32(self, i):
                return {self.avp: i}

            def withAVP(self, avp):
                return {self.avp: avp}

        def getAVP(self, avp):
            if avp == 'Vendor-Specific-Application-Id':
                return {avp: None}
            return self.MockAVP(avp)

    def setUp(self):
        unittest.TestCase.setUp(self)
        self.cx = self.MockCx()
        stack = mock.MagicMock()
        stack.getDictionary.return_value = self.cx
        self.app = mock.MagicMock()

        self.peer_listener = HSSPeerListener(self.app, "domain", stack)
        self.peer = mock.MagicMock()
        self.peer_listener.connected(self.peer)
        self.assertEquals(self.peer, self.peer_listener.peer)
        settings.SPROUT_HOSTNAME = "sprout"
        settings.SPROUT_PORT = 1234

    def test_get_diameter_error_code(self):
        mock_error = mock.MagicMock()
        mock_error.getInteger32.return_value = 1234
        mock_exp = mock.MagicMock()
        mock_exp.getGroup.return_value = [None, mock_error]
        self.cx.findFirstAVP.return_value = mock_exp
        request = mock.MagicMock()

        error_code = self.peer_listener.get_diameter_error_code(request)
        self.cx.findFirstAVP.assert_called_once_with(request, "Experimental-Result")
        mock_exp.getGroup.assert_called_once_with()
        mock_error.getInteger32.assert_called_once_with()
        self.assertEquals(1234, error_code)

        self.cx.findFirstAVP.return_value = None
        error_code = self.peer_listener.get_diameter_error_code(request)
        self.assertEquals(None, error_code)

    def test_fetch_multimedia_auth(self):
        mock_req = self.MockRequest()
        self.cx.getCommandRequest.return_value = mock_req
        deferred = self.peer_listener.fetch_multimedia_auth("priv", "pub")
        self.cx.getCommandRequest.assert_called_once_with(self.peer.stack, "Multimedia-Auth", True)
        self.assertEquals(mock_req.avps,
                          [{'User-Name': 'priv'},
                           {'Public-Identity': 'pub'},
                           {'Server-Name': 'sip:sprout:1234'},
                           {'SIP-Number-Auth-Items': 1},
                           {'SIP-Auth-Data-Item': {'SIP-Authentication-Scheme': 'SIP Digest'}}])
        self.peer.stack.sendByPeer.assert_called_once_with(self.peer, mock_req)
        inner_deferred = self.app.add_pending_response.call_args[0][1]
        # Now mimic returning a value from the HSS
        mock_answer = mock.MagicMock()
        self.cx.findFirstAVP.return_value = mock.MagicMock()
        self.cx.findFirstAVP.return_value.getOctetString.return_value = "digest"
        deferred_callback = mock.MagicMock()
        deferred.addCallback(deferred_callback)
        inner_deferred.callback(mock_answer)
        self.cx.findFirstAVP.assert_called_once_with(mock_answer, "SIP-Auth-Data-Item", "SIP-Digest-Authenticate AVP", "Digest-HA1")
        self.assertEquals(deferred_callback.call_args[0][0], "digest")

    def test_fetch_multimedia_auth_fail(self):
        mock_req = self.MockRequest()
        self.cx.getCommandRequest.return_value = mock_req
        deferred = self.peer_listener.fetch_multimedia_auth("priv", "pub")
        inner_deferred = self.app.add_pending_response.call_args[0][1]
        # Now mimic an error returning a value from the HSS
        mock_answer = mock.MagicMock()
        self.cx.findFirstAVP.return_value = None
        deferred_errback = mock.MagicMock()
        deferred.addErrback(deferred_errback)
        inner_deferred.callback(mock_answer)
        self.assertEquals(deferred_errback.call_args[0][0].type, HSSNotFound)

    def test_fetch_server_assignment(self):
        mock_req = self.MockRequest()
        self.cx.getCommandRequest.return_value = mock_req
        deferred = self.peer_listener.fetch_server_assignment("priv", "pub")
        self.cx.getCommandRequest.assert_called_once_with(self.peer.stack, "Server-Assignment", True)
        self.assertEquals(mock_req.avps,
                          [{'User-Name': 'priv'},
                           {'Public-Identity': 'pub'},
                           {'Server-Name': 'sip:sprout:1234'},
                           {'Server-Assignment-Type': 1},
                           {'Destination-Realm': 'domain'},
                           {'User-Data-Already-Available': 0},
                           {'Vendor-Specific-Application-Id': None},
                           {'Auth-Session-State': 0}])
        self.peer.stack.sendByPeer.assert_called_once_with(self.peer, mock_req)
        inner_deferred = self.app.add_pending_response.call_args[0][1]
        # Now mimic returning a value from the HSS
        mock_answer = mock.MagicMock()
        ifc_xml = (
        "<ServiceProfile>"
          "<PublicIdentity>"
            "<Identity>pub</Identity>"
            "<Extension>"
              "<IdentityType>0</IdentityType>"
            "</Extension>"
          "</PublicIdentity>"
          "<InitialFilterCriteria>"
            "ifc"
          "</InitialFilterCriteria>"
        "</ServiceProfile>")
        self.cx.findFirstAVP.return_value = mock.MagicMock()
        self.cx.findFirstAVP.return_value.getOctetString.return_value = (
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
        "<IMSSubscription>"
          "<PrivateID>priv</PrivateID>"
          "%s"
        "</IMSSubscription>") % ifc_xml
        deferred_callback = mock.MagicMock()
        deferred.addCallback(deferred_callback)
        inner_deferred.callback(mock_answer)
        self.cx.findFirstAVP.assert_called_once_with(mock_answer, "User-Data")
        self.assertEquals(deferred_callback.call_args[0][0], (['pub'], ifc_xml))

    def test_fetch_server_assignment_multi(self):
        mock_req = self.MockRequest()
        self.cx.getCommandRequest.return_value = mock_req
        deferred = self.peer_listener.fetch_server_assignment("priv", "pub")
        self.cx.getCommandRequest.assert_called_once_with(self.peer.stack, "Server-Assignment", True)
        self.assertEquals(mock_req.avps,
                          [{'User-Name': 'priv'},
                           {'Public-Identity': 'pub'},
                           {'Server-Name': 'sip:sprout:1234'},
                           {'Server-Assignment-Type': 1},
                           {'Destination-Realm': 'domain'},
                           {'User-Data-Already-Available': 0},
                           {'Vendor-Specific-Application-Id': None},
                           {'Auth-Session-State': 0}])
        self.peer.stack.sendByPeer.assert_called_once_with(self.peer, mock_req)
        inner_deferred = self.app.add_pending_response.call_args[0][1]
        # Now mimic returning a value from the HSS
        mock_answer = mock.MagicMock()
        ifc_xml = (
        "<ServiceProfile>"
          "<PublicIdentity>"
            "<Identity>pub</Identity>"
            "<Extension>"
              "<IdentityType>0</IdentityType>"
            "</Extension>"
          "</PublicIdentity>"
          "<PublicIdentity>"
            "<Identity>pub2</Identity>"
            "<Extension>"
              "<IdentityType>0</IdentityType>"
            "</Extension>"
          "</PublicIdentity>"
          "<InitialFilterCriteria>"
            "ifc"
          "</InitialFilterCriteria>"
        "</ServiceProfile>")
        self.cx.findFirstAVP.return_value = mock.MagicMock()
        self.cx.findFirstAVP.return_value.getOctetString.return_value = (
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
        "<IMSSubscription>"
          "<PrivateID>priv</PrivateID>"
          "%s"
        "</IMSSubscription>") % ifc_xml
        deferred_callback = mock.MagicMock()
        deferred.addCallback(deferred_callback)
        inner_deferred.callback(mock_answer)
        self.cx.findFirstAVP.assert_called_once_with(mock_answer, "User-Data")
        self.assertEquals(deferred_callback.call_args[0][0], (['pub', 'pub2'], ifc_xml))

    @mock.patch("xml.etree.ElementTree.tostring")
    @mock.patch("xml.etree.ElementTree.fromstring")
    def test_fetch_server_assignment_error(self, fromstring, tostring):
        mock_req = self.MockRequest()
        self.cx.getCommandRequest.return_value = mock_req
        deferred = self.peer_listener.fetch_server_assignment("priv", "pub")
        inner_deferred = self.app.add_pending_response.call_args[0][1]
        # Now mimic error returning a value from the HSS
        mock_answer = mock.MagicMock()
        self.cx.findFirstAVP.return_value = None
        deferred_errback = mock.MagicMock()
        deferred.addErrback(deferred_errback)
        inner_deferred.callback(mock_answer)
        self.assertEquals(deferred_errback.call_args[0][0].type, HSSNotFound)

    @mock.patch("xml.etree.ElementTree.tostring")
    @mock.patch("xml.etree.ElementTree.fromstring")
    def test_fetch_server_assignment_incorrect_public_id(self, fromstring, tostring):
        mock_req = self.MockRequest()
        self.cx.getCommandRequest.return_value = mock_req
        deferred = self.peer_listener.fetch_server_assignment("priv", "pub")
        inner_deferred = self.app.add_pending_response.call_args[0][1]
        # Now mimic returning a value from the HSS (but with the incorrect public id)
        mock_answer = mock.MagicMock()
        self.cx.findFirstAVP.return_value = mock.MagicMock()
        self.cx.findFirstAVP.return_value.getOctetString.return_value = "user_data"
        deferred_errback = mock.MagicMock()
        deferred.addErrback(deferred_errback)
        # IFC is more complex than MM auth, we have to mock out the xml parsing
        mock_xml = mock.MagicMock()
        fromstring.return_value = mock_xml
        mock_sp = mock.MagicMock()
        mock_sp.find.return_value.text = "Humpty Dumpty"
        mock_xml.iterfind.return_value = [mock_sp]
        tostring.return_value = "ifc"
        inner_deferred.callback(mock_answer)
        self.assertEquals(deferred_errback.call_args[0][0].type, HSSNotFound)

    def test_disconnected(self):
        self.assertEquals(self.peer_listener.peer, self.peer)
        self.peer_listener.disconnected(mock.MagicMock())
        self.assertEquals(self.peer_listener.peer, None)
Beispiel #4
0
class TestHSSPeerListener(unittest.TestCase):
    class MockRequest(mock.MagicMock):
        def __init__(self):
            mock.MagicMock.__init__(self)
            self.avps = []

        def addAVP(self, avp):
            self.avps.append(avp)

    class MockCx(mock.MagicMock):
        class MockAVP(mock.MagicMock):
            def __init__(self, avp, value=None):
                mock.MagicMock.__init__(self)
                self.avp = avp
                self.value = value

            def withOctetString(self, s):
                return {self.avp: s}

            def withInteger32(self, i):
                return {self.avp: i}

            def withAVP(self, avp):
                return {self.avp: avp}

        def getAVP(self, avp):
            if avp == 'Vendor-Specific-Application-Id':
                return {avp: None}
            return self.MockAVP(avp)

    def setUp(self):
        unittest.TestCase.setUp(self)
        self.cx = self.MockCx()
        stack = mock.MagicMock()
        stack.getDictionary.return_value = self.cx
        self.app = mock.MagicMock()

        self.peer_listener = HSSPeerListener(self.app, "domain", stack)
        self.peer = mock.MagicMock()
        self.peer_listener.connected(self.peer)
        self.assertEquals(self.peer, self.peer_listener.peer)
        settings.SPROUT_HOSTNAME = "sprout"
        settings.SPROUT_PORT = 1234

    def test_get_diameter_error_code(self):
        mock_error = mock.MagicMock()
        mock_error.getInteger32.return_value = 1234
        mock_exp = mock.MagicMock()
        mock_exp.getGroup.return_value = [None, mock_error]
        self.cx.findFirstAVP.return_value = mock_exp
        request = mock.MagicMock()

        error_code = self.peer_listener.get_diameter_error_code(request)
        self.cx.findFirstAVP.assert_called_once_with(request,
                                                     "Experimental-Result")
        mock_exp.getGroup.assert_called_once_with()
        mock_error.getInteger32.assert_called_once_with()
        self.assertEquals(1234, error_code)

        self.cx.findFirstAVP.return_value = None
        error_code = self.peer_listener.get_diameter_error_code(request)
        self.assertEquals(None, error_code)

    def test_fetch_multimedia_auth(self):
        mock_req = self.MockRequest()
        self.cx.getCommandRequest.return_value = mock_req
        deferred = self.peer_listener.fetch_multimedia_auth("priv", "pub")
        self.cx.getCommandRequest.assert_called_once_with(
            self.peer.stack, "Multimedia-Auth", True)
        self.assertEquals(mock_req.avps, [{
            'User-Name': 'priv'
        }, {
            'Public-Identity': 'pub'
        }, {
            'Server-Name': 'sip:sprout:1234'
        }, {
            'SIP-Number-Auth-Items': 1
        }, {
            'SIP-Auth-Data-Item': {
                'SIP-Authentication-Scheme': 'SIP Digest'
            }
        }])
        self.peer.stack.sendByPeer.assert_called_once_with(self.peer, mock_req)
        inner_deferred = self.app.add_pending_response.call_args[0][1]
        # Now mimic returning a value from the HSS
        mock_answer = mock.MagicMock()
        self.cx.findFirstAVP.return_value = mock.MagicMock()
        self.cx.findFirstAVP.return_value.getOctetString.return_value = "digest"
        deferred_callback = mock.MagicMock()
        deferred.addCallback(deferred_callback)
        inner_deferred.callback(mock_answer)
        self.cx.findFirstAVP.assert_called_once_with(
            mock_answer, "SIP-Auth-Data-Item", "SIP-Digest-Authenticate AVP",
            "Digest-HA1")
        self.assertEquals(deferred_callback.call_args[0][0], "digest")

    def test_fetch_multimedia_auth_fail(self):
        mock_req = self.MockRequest()
        self.cx.getCommandRequest.return_value = mock_req
        deferred = self.peer_listener.fetch_multimedia_auth("priv", "pub")
        inner_deferred = self.app.add_pending_response.call_args[0][1]
        # Now mimic an error returning a value from the HSS
        mock_answer = mock.MagicMock()
        self.cx.findFirstAVP.return_value = None
        deferred_errback = mock.MagicMock()
        deferred.addErrback(deferred_errback)
        inner_deferred.callback(mock_answer)
        self.assertEquals(deferred_errback.call_args[0][0].type, HSSNotFound)

    def test_fetch_server_assignment(self):
        mock_req = self.MockRequest()
        self.cx.getCommandRequest.return_value = mock_req
        deferred = self.peer_listener.fetch_server_assignment("priv", "pub")
        self.cx.getCommandRequest.assert_called_once_with(
            self.peer.stack, "Server-Assignment", True)
        self.assertEquals(mock_req.avps,
                          [{
                              'User-Name': 'priv'
                          }, {
                              'Public-Identity': 'pub'
                          }, {
                              'Server-Name': 'sip:sprout:1234'
                          }, {
                              'Server-Assignment-Type': 1
                          }, {
                              'Destination-Realm': 'domain'
                          }, {
                              'User-Data-Already-Available': 0
                          }, {
                              'Vendor-Specific-Application-Id': None
                          }, {
                              'Auth-Session-State': 0
                          }])
        self.peer.stack.sendByPeer.assert_called_once_with(self.peer, mock_req)
        inner_deferred = self.app.add_pending_response.call_args[0][1]
        # Now mimic returning a value from the HSS
        mock_answer = mock.MagicMock()
        ifc_xml = ("<ServiceProfile>"
                   "<PublicIdentity>"
                   "<Identity>pub</Identity>"
                   "<Extension>"
                   "<IdentityType>0</IdentityType>"
                   "</Extension>"
                   "</PublicIdentity>"
                   "<InitialFilterCriteria>"
                   "ifc"
                   "</InitialFilterCriteria>"
                   "</ServiceProfile>")
        self.cx.findFirstAVP.return_value = mock.MagicMock()
        self.cx.findFirstAVP.return_value.getOctetString.return_value = (
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            "<IMSSubscription>"
            "<PrivateID>priv</PrivateID>"
            "%s"
            "</IMSSubscription>") % ifc_xml
        deferred_callback = mock.MagicMock()
        deferred.addCallback(deferred_callback)
        inner_deferred.callback(mock_answer)
        self.cx.findFirstAVP.assert_called_once_with(mock_answer, "User-Data")
        self.assertEquals(deferred_callback.call_args[0][0],
                          (['pub'], ifc_xml))

    def test_fetch_server_assignment_multi(self):
        mock_req = self.MockRequest()
        self.cx.getCommandRequest.return_value = mock_req
        deferred = self.peer_listener.fetch_server_assignment("priv", "pub")
        self.cx.getCommandRequest.assert_called_once_with(
            self.peer.stack, "Server-Assignment", True)
        self.assertEquals(mock_req.avps,
                          [{
                              'User-Name': 'priv'
                          }, {
                              'Public-Identity': 'pub'
                          }, {
                              'Server-Name': 'sip:sprout:1234'
                          }, {
                              'Server-Assignment-Type': 1
                          }, {
                              'Destination-Realm': 'domain'
                          }, {
                              'User-Data-Already-Available': 0
                          }, {
                              'Vendor-Specific-Application-Id': None
                          }, {
                              'Auth-Session-State': 0
                          }])
        self.peer.stack.sendByPeer.assert_called_once_with(self.peer, mock_req)
        inner_deferred = self.app.add_pending_response.call_args[0][1]
        # Now mimic returning a value from the HSS
        mock_answer = mock.MagicMock()
        ifc_xml = ("<ServiceProfile>"
                   "<PublicIdentity>"
                   "<Identity>pub</Identity>"
                   "<Extension>"
                   "<IdentityType>0</IdentityType>"
                   "</Extension>"
                   "</PublicIdentity>"
                   "<PublicIdentity>"
                   "<Identity>pub2</Identity>"
                   "<Extension>"
                   "<IdentityType>0</IdentityType>"
                   "</Extension>"
                   "</PublicIdentity>"
                   "<InitialFilterCriteria>"
                   "ifc"
                   "</InitialFilterCriteria>"
                   "</ServiceProfile>")
        self.cx.findFirstAVP.return_value = mock.MagicMock()
        self.cx.findFirstAVP.return_value.getOctetString.return_value = (
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            "<IMSSubscription>"
            "<PrivateID>priv</PrivateID>"
            "%s"
            "</IMSSubscription>") % ifc_xml
        deferred_callback = mock.MagicMock()
        deferred.addCallback(deferred_callback)
        inner_deferred.callback(mock_answer)
        self.cx.findFirstAVP.assert_called_once_with(mock_answer, "User-Data")
        self.assertEquals(deferred_callback.call_args[0][0],
                          (['pub', 'pub2'], ifc_xml))

    @mock.patch("xml.etree.ElementTree.tostring")
    @mock.patch("xml.etree.ElementTree.fromstring")
    def test_fetch_server_assignment_error(self, fromstring, tostring):
        mock_req = self.MockRequest()
        self.cx.getCommandRequest.return_value = mock_req
        deferred = self.peer_listener.fetch_server_assignment("priv", "pub")
        inner_deferred = self.app.add_pending_response.call_args[0][1]
        # Now mimic error returning a value from the HSS
        mock_answer = mock.MagicMock()
        self.cx.findFirstAVP.return_value = None
        deferred_errback = mock.MagicMock()
        deferred.addErrback(deferred_errback)
        inner_deferred.callback(mock_answer)
        self.assertEquals(deferred_errback.call_args[0][0].type, HSSNotFound)

    @mock.patch("xml.etree.ElementTree.tostring")
    @mock.patch("xml.etree.ElementTree.fromstring")
    def test_fetch_server_assignment_incorrect_public_id(
            self, fromstring, tostring):
        mock_req = self.MockRequest()
        self.cx.getCommandRequest.return_value = mock_req
        deferred = self.peer_listener.fetch_server_assignment("priv", "pub")
        inner_deferred = self.app.add_pending_response.call_args[0][1]
        # Now mimic returning a value from the HSS (but with the incorrect public id)
        mock_answer = mock.MagicMock()
        self.cx.findFirstAVP.return_value = mock.MagicMock()
        self.cx.findFirstAVP.return_value.getOctetString.return_value = "user_data"
        deferred_errback = mock.MagicMock()
        deferred.addErrback(deferred_errback)
        # IFC is more complex than MM auth, we have to mock out the xml parsing
        mock_xml = mock.MagicMock()
        fromstring.return_value = mock_xml
        mock_sp = mock.MagicMock()
        mock_sp.find.return_value.text = "Humpty Dumpty"
        mock_xml.iterfind.return_value = [mock_sp]
        tostring.return_value = "ifc"
        inner_deferred.callback(mock_answer)
        self.assertEquals(deferred_errback.call_args[0][0].type, HSSNotFound)

    def test_disconnected(self):
        self.assertEquals(self.peer_listener.peer, self.peer)
        self.peer_listener.disconnected(mock.MagicMock())
        self.assertEquals(self.peer_listener.peer, None)