def test_capability_exchange(self): """Test that we can respond to capability exchange requests""" msg = message.Message() msg.header.command_code = base.BaseApplicationCommands.CAPABILITIES_EXCHANGE msg.header.request = True msg.append_avp(avp.AVP('Host-IP-Address', '127.0.0.1')) msg.append_avp(avp.AVP('Inband-Security-Id', 0)) msg.append_avp(avp.AVP('Supported-Vendor-Id', 0)) msg.append_avp(avp.AVP('Vendor-Id', 0)) msg.append_avp(avp.AVP('Vendor-Specific-Application-Id', [])) # Encode request message into buffer req_buf = bytearray(msg.length) msg.encode(req_buf, 0) msg = message.Message() msg.header.command_code = base.BaseApplicationCommands.CAPABILITIES_EXCHANGE msg.header.request = False msg.append_avp(avp.AVP('Result-Code', avp.ResultCode.DIAMETER_SUCCESS)) msg.append_avp(avp.AVP('Origin-Host', self._server.host)) msg.append_avp(avp.AVP('Origin-Realm', self._server.realm)) msg.append_avp(avp.AVP('Origin-State-Id', self._server.state_id)) msg.append_avp(avp.AVP('Host-IP-Address', self.HOST_ADDR)) msg.append_avp(avp.AVP('Vendor-Id', 0)) msg.append_avp(avp.AVP('Supported-Vendor-Id', avp.VendorId.TGPP)) msg.append_avp( avp.AVP('Vendor-Specific-Application-Id', [ avp.AVP('Auth-Application-Id', s6a.S6AApplication.APP_ID), avp.AVP('Vendor-Id', avp.VendorId.TGPP) ])) msg.append_avp(avp.AVP('Product-Name', 'magma')) # Encode response message into buffer resp_buf = bytearray(msg.length) msg.encode(resp_buf, 0) self._check_reply(req_buf, resp_buf)
def test_auth_unknown_subscriber(self): """ Test that we reject auth requests if the subscriber is unknown """ msg = message.Message() msg.header.application_id = s6a.S6AApplication.APP_ID msg.header.command_code = s6a.S6AApplicationCommands.AUTHENTICATION_INFORMATION msg.header.request = True msg.append_avp( avp.AVP( 'Session-Id', 'enb-Lenovo-Product.openair4G.eur;1475864727;1;apps6a', ), ) msg.append_avp(avp.AVP('Auth-Session-State', 1)) msg.append_avp(avp.AVP('User-Name', '3')) msg.append_avp(avp.AVP('Visited-PLMN-Id', b'(Y')) msg.append_avp( avp.AVP( 'Requested-EUTRAN-Authentication-Info', [ avp.AVP('Number-Of-Requested-Vectors', 1), avp.AVP('Immediate-Response-Preferred', 0), ], ), ) # Encode request message into buffer req_buf = bytearray(msg.length) msg.encode(req_buf, 0) msg = message.Message() msg.header.application_id = s6a.S6AApplication.APP_ID msg.header.command_code = \ s6a.S6AApplicationCommands.AUTHENTICATION_INFORMATION msg.header.request = False msg.append_avp( avp.AVP( 'Session-Id', 'enb-Lenovo-Product.openair4G.eur;1475864727;1;apps6a', ), ) msg.append_avp(avp.AVP('Auth-Session-State', 1)) # Host identifiers msg.append_avp(avp.AVP('Origin-Host', self._server.host)) msg.append_avp(avp.AVP('Origin-Realm', self._server.realm)) msg.append_avp(avp.AVP('Origin-State-Id', self._server.state_id)) # Response result msg.append_avp( avp.AVP( 'Result-Code', avp.ResultCode.DIAMETER_ERROR_USER_UNKNOWN, ), ) # Encode response into buffer resp_buf = bytearray(msg.length) msg.encode(resp_buf, 0) self._check_reply(req_buf, resp_buf)
def test_auth_success(self): """ Test that we can respond to auth requests with an auth vector """ msg = message.Message() msg.header.application_id = s6a.S6AApplication.APP_ID msg.header.command_code = s6a.S6AApplicationCommands.AUTHENTICATION_INFORMATION msg.header.request = True msg.append_avp(avp.AVP('Session-Id', 'enb-Lenovo-Product.openair4G.eur;1475864727;1;apps6a')) msg.append_avp(avp.AVP('Auth-Session-State', 1)) msg.append_avp(avp.AVP('User-Name', '1')) msg.append_avp(avp.AVP('Visited-PLMN-Id', b'(Y')) msg.append_avp(avp.AVP('Requested-EUTRAN-Authentication-Info', [ avp.AVP('Number-Of-Requested-Vectors', 1), avp.AVP('Immediate-Response-Preferred', 0), ])) # Encode request message into buffer req_buf = bytearray(msg.length) msg.encode(req_buf, 0) msg = message.Message() msg.header.application_id = s6a.S6AApplication.APP_ID msg.header.command_code = \ s6a.S6AApplicationCommands.AUTHENTICATION_INFORMATION msg.header.request = False msg.append_avp(avp.AVP('Session-Id', 'enb-Lenovo-Product.openair4G.eur;1475864727;1;apps6a')) msg.append_avp(avp.AVP('Authentication-Info', [ avp.AVP('E-UTRAN-Vector', [ avp.AVP('RAND', b'\x00\x01\x02\x03\x04\x05' b'\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'), avp.AVP('XRES', b'\x2d\xaf\x87\x3d\x73\xf3\x10\xc6'), avp.AVP('AUTN', b'\x6f\xbf\xa3\x80\x1f\x57\x80' b'\x00\x7b\xde\x59\x88\x6e\x96\xe4\xfe'), avp.AVP('KASME', b'\x87\x48\xc1\xc0\xa2\x82' b'\x6f\xa4\x05\xb1\xe2\x7e\xa1\x04\x43\x4a' b'\xe5\x56\xc7\x65\xe8\xf0\x61\xeb\xdb\x8a' b'\xe2\x86\xc4\x46\x16\xc2')])])) msg.append_avp(avp.AVP('Auth-Session-State', 1)) # Host identifiers msg.append_avp(avp.AVP('Origin-Host', self._server.host)) msg.append_avp(avp.AVP('Origin-Realm', self._server.realm)) msg.append_avp(avp.AVP('Origin-State-Id', self._server.state_id)) # Response result msg.append_avp(avp.AVP('Result-Code', avp.ResultCode.DIAMETER_SUCCESS)) # Encode response into buffer resp_buf = bytearray(msg.length) msg.encode(resp_buf, 0) self._check_reply(req_buf, resp_buf)
def _auth_req( user_name, visited_plmn_id, num_request_vectors, immediate_response_preferred, resync_info, ): msg = message.Message() msg.header.application_id = s6a.S6AApplication.APP_ID msg.header.command_code = s6a.S6AApplicationCommands.AUTHENTICATION_INFORMATION msg.header.request = True msg.append_avp( avp.AVP( 'Session-Id', 'enb-Lenovo-Product.openair4G.eur;1475864727;1;apps6a', ), ) msg.append_avp(avp.AVP('Auth-Session-State', 1)) msg.append_avp(avp.AVP('User-Name', user_name)) msg.append_avp(avp.AVP('Visited-PLMN-Id', visited_plmn_id)) msg.append_avp( avp.AVP( 'Requested-EUTRAN-Authentication-Info', [ avp.AVP('Number-Of-Requested-Vectors', num_request_vectors), avp.AVP( 'Immediate-Response-Preferred', 1 if immediate_response_preferred else 0, ), avp.AVP('Re-Synchronization-Info', resync_info), ], ), ) return msg
def test_respond(self): """ Tests that we can use the convenience clone constructor and create_response_header method for copying fields from header """ # Set some non-defaults we will clone header = message.MessageHeader() header.version = 0x2 header.command_code = 0x3 header.application_id = 0x4 header.hop_by_hop_id = 0x5 header.end_to_end_id = 0x6 header.request = True header.retransmitted = True header.error = True header.proxiable = True msg = message.Message(header) msg.append_avp(avp.AVP('User-Name', '')) # A clone should be almost equal resp = message.Message.create_response_msg(msg) # These fields are reset self.assertFalse(resp.header.request) self.assertFalse(resp.header.retransmitted) self.assertFalse(resp.header.error) self.assertEqual(len(resp._avps), 0) # Now they should be equal resp.header.request = msg.header.request resp.header.retransmitted = msg.header.retransmitted resp.header.error = msg.header.error self.assertEqual(msg.header, resp.header)
def test_resync(self): """ Test that we can respond to auth requests with an auth vector """ msg = message.Message() msg.header.application_id = s6a.S6AApplication.APP_ID msg.header.command_code = s6a.S6AApplicationCommands.AUTHENTICATION_INFORMATION msg.header.request = True msg.append_avp( avp.AVP('Session-Id', 'enb-Lenovo-Product.openair4G.eur;1475864727;1;apps6a')) msg.append_avp(avp.AVP('Auth-Session-State', 1)) msg.append_avp(avp.AVP('User-Name', '1')) msg.append_avp(avp.AVP('Visited-PLMN-Id', b'(Y')) msg.append_avp( avp.AVP('Requested-EUTRAN-Authentication-Info', [ avp.AVP('Number-Of-Requested-Vectors', 1), avp.AVP('Immediate-Response-Preferred', 0), avp.AVP('Re-Synchronization-Info', 30 * b'\x00') ])) # Encode request message into buffer req_buf = bytearray(msg.length) msg.encode(req_buf, 0) processor = self._server._s6a_manager.lte_processor with unittest.mock.patch.object(processor, 'resync_lte_auth_seq'): self._server.data_received(req_buf) processor.resync_lte_auth_seq.assert_called_once_with( '1', 16 * b'\x00', 14 * b'\x00')
def test_application_dispatch(self): """Check that we can decode an inbound message and call the appropriate handler""" msg = message.Message() msg.header.application_id = 0xfac3b00c msg.header.request = True req_buf = bytearray(msg.length) msg.encode(req_buf, 0) self._check_handler(req_buf, 0xfac3b00c)
def test_send_msg(self): """Test that the writer will encode a message and write it to the transport""" msg = message.Message() self.writer.send_msg(msg) self._writes.assert_called_once_with( b'\x01\x00\x00\x14' b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' b'\x00\x00\x00', ) self._writes.reset_mock()
def test_invalid_command(self): """Test that we ignore invalid commands""" msg = message.Message() msg.header.command_code = 0xfa4e msg.header.request = True # Encode request message into buffer req_buf = bytearray(msg.length) msg.encode(req_buf, 0) self._check_reply(req_buf, b'')
def test_unsolicited_response(self): """Test that we ignore unsolicited responses""" msg = message.Message() msg.header.command_code = base.BaseApplicationCommands.CAPABILITIES_EXCHANGE msg.header.request = False # Encode request message into buffer req_buf = bytearray(msg.length) msg.encode(req_buf, 0) self._check_reply(req_buf, b'')
def test_empty_message(self): """ Tests that we can encode and decode an empty message """ # A message with no arguments is just an empty header msg = message.Message() self._compare_msg( msg, b'\x01' + # Version b'\x00\x00\x14' + # Length 20 b'\x00' * (message.HEADER_LEN - 4))
def test_watchdog(self): """Test that we can respond to watchdog requests""" msg = message.Message() msg.header.command_code = base.BaseApplicationCommands.DEVICE_WATCHDOG msg.header.request = True # Encode request message into buffer req_buf = bytearray(msg.length) msg.encode(req_buf, 0) msg = message.Message() msg.header.command_code = base.BaseApplicationCommands.DEVICE_WATCHDOG msg.header.request = False msg.append_avp(avp.AVP('Result-Code', avp.ResultCode.DIAMETER_SUCCESS)) msg.append_avp(avp.AVP('Origin-Host', self._server.host)) msg.append_avp(avp.AVP('Origin-Realm', self._server.realm)) msg.append_avp(avp.AVP('Origin-State-Id', self._server.state_id)) # Encode response into buffer resp_buf = bytearray(msg.length) msg.encode(resp_buf, 0) self._check_reply(req_buf, resp_buf)
def test_message_with_avp(self): """ Adding an AVP to the message is just tacking on the AVP encoded output """ msg = message.Message() msg.append_avp(avp.AVP('User-Name', 'hello')) self._compare_msg( msg, b'\x01' + # Version b'\x00\x00\x24' + # Length 36 b'\x00' * (message.HEADER_LEN - 4) + b'\x00\x00\x00\x01@\x00\x00\rhello\x00\x00\x00')
def _update_location_req(user_name, visited_plmn_id, ulr_flags): msg = message.Message() msg.header.application_id = s6a.S6AApplication.APP_ID msg.header.command_code = s6a.S6AApplicationCommands.UPDATE_LOCATION msg.header.request = True msg.append_avp( avp.AVP('Session-Id', 'enb-Lenovo-Product.openair4G.eur;1475864727;1;apps6a')) msg.append_avp(avp.AVP('Auth-Session-State', 1)) msg.append_avp(avp.AVP('User-Name', user_name)) msg.append_avp(avp.AVP('Visited-PLMN-Id', visited_plmn_id)) msg.append_avp(avp.AVP('ULR-Flags', ulr_flags)) msg.append_avp(avp.AVP('RAT-Type', 1004)) return msg
def test_gen_buf(self): """Test that the writer will generate a buffer of the length of the message""" msg = message.Message() buf = self.writer._get_write_buf(msg) self.assertEqual(len(buf), msg.length)
def setUpClass(cls): cls.msg = message.Message() cls.msg.append_avp(avp.AVP('User-Name', 'hello')) cls.msg.append_avp(avp.AVP('User-Name', 'world')) cls.msg.append_avp(avp.AVP('Host-IP-Address', '127.0.0.1'))
def test_location_update(self): """ Test that we can respond to update location request with subscriber data """ msg = message.Message() msg.header.application_id = s6a.S6AApplication.APP_ID msg.header.command_code = s6a.S6AApplicationCommands.UPDATE_LOCATION msg.header.request = True msg.append_avp( avp.AVP( 'Session-Id', 'enb-Lenovo-Product.openair4G.eur;1475864727;1;apps6a', ), ) msg.append_avp(avp.AVP('Auth-Session-State', 1)) msg.append_avp(avp.AVP('User-Name', '208950000000001')) msg.append_avp(avp.AVP('Visited-PLMN-Id', b'(Y')) msg.append_avp(avp.AVP('RAT-Type', 1004)) msg.append_avp(avp.AVP('ULR-Flags', 34)) # Encode request message into buffer req_buf = bytearray(msg.length) msg.encode(req_buf, 0) msg = message.Message() msg.header.application_id = s6a.S6AApplication.APP_ID msg.header.command_code = s6a.S6AApplicationCommands.UPDATE_LOCATION msg.header.request = False msg.append_avp( avp.AVP( 'Session-Id', 'enb-Lenovo-Product.openair4G.eur;1475864727;1;apps6a', ), ) msg.append_avp(avp.AVP('ULA-Flags', 1)) msg.append_avp( avp.AVP( 'Subscription-Data', [ avp.AVP('MSISDN', b'333608050011'), avp.AVP('Access-Restriction-Data', 47), avp.AVP('Subscriber-Status', 0), avp.AVP('Network-Access-Mode', 2), avp.AVP( 'AMBR', [ avp.AVP('Max-Requested-Bandwidth-UL', 10000), avp.AVP('Max-Requested-Bandwidth-DL', 50000), ], ), avp.AVP( 'APN-Configuration-Profile', [ avp.AVP('Context-Identifier', 0), avp.AVP( 'All-APN-Configurations-Included-Indicator', 0), avp.AVP( 'APN-Configuration', [ avp.AVP('Context-Identifier', 0), avp.AVP('PDN-Type', 0), avp.AVP('Service-Selection', 'oai.ipv4'), avp.AVP( 'EPS-Subscribed-QoS-Profile', [ avp.AVP('QoS-Class-Identifier', 9), avp.AVP( 'Allocation-Retention-Priority', [ avp.AVP( 'Priority-Level', 15), avp.AVP( 'Pre-emption-Capability', 1), avp.AVP( 'Pre-emption-Vulnerability', 0), ], ), ], ), avp.AVP( 'AMBR', [ avp.AVP( 'Max-Requested-Bandwidth-UL', 10000), avp.AVP( 'Max-Requested-Bandwidth-DL', 50000), ], ), ], ), ], ), ], ), ) msg.append_avp(avp.AVP('Auth-Session-State', 1)) # Host identifiers msg.append_avp(avp.AVP('Origin-Host', self._server.host)) msg.append_avp(avp.AVP('Origin-Realm', self._server.realm)) msg.append_avp(avp.AVP('Origin-State-Id', self._server.state_id)) # Response result msg.append_avp(avp.AVP('Result-Code', avp.ResultCode.DIAMETER_SUCCESS)) # Encode response into buffer resp_buf = bytearray(msg.length) msg.encode(resp_buf, 0) self._check_reply(req_buf, resp_buf)