def test_dump(self): username = libradi.RadiusAvp("user-name", "johndoe") status = libradi.RadiusAvp("acct-status-type", 1) nas_ip = libradi.RadiusAvp("nas-ip-address", "127.0.0.1") framed_ip = libradi.RadiusAvp("framed-ip-address", "10.0.0.1") framed_mask = libradi.RadiusAvp("framed-ip-netmask", "255.255.255.255") framed_proto = libradi.RadiusAvp("framed-protocol", 1) calling_id = libradi.RadiusAvp("calling-station-id", "00441234987654") called_id = libradi.RadiusAvp("called-station-id", "web.apn") imsi = libradi.RadiusAvp("3gpp-imsi", "12345678901234") imei = libradi.RadiusAvp("3gpp-imeisv", "3456789012345678901234567890") self.request.add_avp(username) self.request.add_avp(status) self.request.add_avp(nas_ip) self.request.add_avp(framed_ip) self.request.add_avp(framed_mask) self.request.add_avp(framed_proto) self.request.add_avp(calling_id) self.request.add_avp(called_id) self.request.add_avp(imsi) self.request.add_avp(imei) # exported from the tcpdump exp_message = ( "04f5008ecf00f8a8355d79ff820361f2567a9e9501096a6f686e" "646f6528060000000104067f00000108060a0000010906ffffffff07060" "00000011f1030303434313233343938373635341e097765622e61706e1a" "16000028af011031323334353637383930313233341a24000028af141e3" "3343536373839303132333435363738393031323334353637383930") self.assertEquals(exp_message, self.request.dump().encode("hex"))
def test_authenticator(self): username = libradi.RadiusAvp("user-name", "johndoe") status = libradi.RadiusAvp("acct-status-type", 1) nas_ip = libradi.RadiusAvp("nas-ip-address", "127.0.0.1") framed_ip = libradi.RadiusAvp("framed-ip-address", "10.0.0.1") framed_mask = libradi.RadiusAvp("framed-ip-netmask", "255.255.255.255") framed_proto = libradi.RadiusAvp("framed-protocol", 1) calling_id = libradi.RadiusAvp("calling-station-id", "00441234987654") called_id = libradi.RadiusAvp("called-station-id", "web.apn") imsi = libradi.RadiusAvp("3gpp-imsi", "12345678901234") imei = libradi.RadiusAvp("3gpp-imeisv", "3456789012345678901234567890") self.request.add_avp(username) self.request.add_avp(status) self.request.add_avp(nas_ip) self.request.add_avp(framed_ip) self.request.add_avp(framed_mask) self.request.add_avp(framed_proto) self.request.add_avp(calling_id) self.request.add_avp(called_id) self.request.add_avp(imsi) self.request.add_avp(imei) avps = self.request.get_all_avps_contents() # the result is recieved from a tcpdump from a packet created # with radtool package. self.assertEquals( "cf00f8a8355d79ff820361f2567a9e95", self.request.compute_authenticator(avps).encode("hex"))
def test_get_all_avp_content(self): avp1 = libradi.RadiusAvp("called-station-id", "1234567890") self.assertIsNotNone(avp1) self.request.add_avp(avp1) self.assertEquals(avp1.dump(), self.request.get_all_avps_contents()) avp2 = libradi.RadiusAvp("framed-ip-address", "10.0.0.1") self.assertIsNotNone(avp2) self.request.add_avp(avp2) self.assertEquals("".join((avp1.dump(), avp2.dump())), self.request.get_all_avps_contents())
def test_avp_str(self): avp = libradi.RadiusAvp("Framed-IP-Address", "10.0.0.1") self.assertIsNotNone(avp) exp_str = ("AVP: Type:Framed-IP-Address(ipaddr) Length:6 " "Value:10.0.0.1") self.assertEquals(exp_str, str(avp)) avp = libradi.RadiusAvp("3GPP-IMSI", "123456789") self.assertIsNotNone(avp) exp_str = ("AVP: Type:Vendor-Specific(octets) Length:17 " "Value:10415\n `- AVP: Type:3GPP-IMSI(string) " "Length:11 Value:123456789") self.assertEquals(exp_str, str(avp))
def test_integer_value_avp(self): with self.assertRaises(ValueError): avp = libradi.RadiusAvp("F5-LTM-User-Console", 5) try: avp = libradi.RadiusAvp("F5-LTM-User-Console", 1) except ValueError as e: self.fail("raised {}: {}".format(type(e), e.message)) self.assertIsNotNone(avp) self.assertTrue(avp.has_sub_avps()) self.assertEquals(26, avp.avp_code.value) # vendor-specific self.assertEquals(3375, avp.avp_value.value) # F5 networks 3375 self.assertEquals(4, avp.avp_subavp[0].avp_code.value) self.assertEquals(1, avp.avp_subavp[0].avp_value.value)
def test_add_avp(self): req_length = self.request.length avp = libradi.RadiusAvp("called-station-id", "1234567890") self.assertIsNotNone(avp) self.assertEquals(0, len(self.request.avp_list)) self.request.add_avp(avp) self.assertEquals(req_length + len(avp), len(self.request)) req_length = len(self.request) self.assertEquals(1, len(self.request.avp_list)) avp = libradi.RadiusAvp("framed-ip-address", "10.0.0.1") self.assertIsNotNone(avp) self.request.add_avp(avp) self.assertEquals(req_length + len(avp), len(self.request)) self.assertEquals(2, len(self.request.avp_list))
def test_avp_dump(self): avp = libradi.RadiusAvp("Calling-Station-Id", "00441234987654") self.assertEquals(31, avp.avp_code.value) binary = avp.dump() # exported from a tcpdump with a libradi packet exp_bin = libradi.radtypes.get_type_instance( "byte", 0x1f103030343431323334393837363534, 16) self.assertEquals(exp_bin.dump(), binary) avp = libradi.RadiusAvp("Called-Station-Id", "web.apn") self.assertEquals(30, avp.avp_code.value) # exported from a tcpdump with a libradi packet exp_bin = libradi.radtypes.get_type_instance("byte", 0x1e097765622e61706e, len(avp)) self.assertEquals(exp_bin.dump(), avp.dump())
def test_request_string(self): username = libradi.RadiusAvp("user-name", "johndoe") status = libradi.RadiusAvp("acct-status-type", 1) self.request.add_avp(username) self.request.add_avp(status) self.assertEquals(2, len(self.request.avp_list)) avps = self.request.get_all_avps_contents() exp_str = ("REQUEST: Code:{} PID:{} Length:{} Auth:{}" "\n {}\n {}").format( self.request.code, self.request.pid, len(self.request), self.request.compute_authenticator(avps).encode("hex"), self.request.avp_list[0], self.request.avp_list[1]) self.assertEquals(exp_str, str(self.request))
def test_integer_value_avp(self): avp = libradi.RadiusAvp("F5-LTM-User-Console", 1) self.assertIsNotNone(avp) self.assertTrue(avp.has_sub_avps()) self.assertEquals(26, avp.avp_code.value) # vendor-specific self.assertEquals(3375, avp.avp_value.value) # F5 networks 3375 self.assertEquals(4, avp.avp_subavp[0].avp_code.value) self.assertEquals(1, avp.avp_subavp[0].avp_value.value)
def test_string_avp(self): avp = libradi.RadiusAvp("DHCP-Boot-File-Name", "hello_world") self.assertIsNotNone(avp) self.assertTrue(avp.has_sub_avps()) self.assertEquals(26, avp.avp_code.value) # vendor-specific self.assertEquals(54, avp.avp_value.value) # dhcp value 54 self.assertEquals(67, avp.avp_subavp[0].avp_code.value) self.assertEquals("hello_world", avp.avp_subavp[0].avp_value.value)
def test_integer_avp(self): avp = libradi.RadiusAvp("3gpp-ms-time-zone", 100) self.assertIsNotNone(avp) self.assertTrue(avp.has_sub_avps()) self.assertEquals(26, avp.avp_code.value) # vendor-specific self.assertEquals(10415, avp.avp_value.value) # 3gpp value 10415 # 3gpp-ms-time-zone self.assertEquals(23, avp.avp_subavp[0].avp_code.value) self.assertEquals(100, avp.avp_subavp[0].avp_value.value) # 100
def test_avp_dump_vendor(self): avp = libradi.RadiusAvp("3GPP-IMSI", "12345678901234") self.assertIsNotNone(avp) self.assertTrue(avp.has_sub_avps()) self.assertEquals(26, avp.avp_code.value) # vendor-specific self.assertEquals(10415, avp.avp_value.value) # 3GPP 10415 # exported from a tcpdump with a libradi packet exp_bin = libradi.radtypes.get_type_instance( "byte", 0x1a16000028af01103132333435363738393031323334, len(avp)) self.assertEquals(exp_bin.dump(), avp.dump())
def create_radius_request(config, action): """generate a binary version of the packet based on the current config""" rad = libradi.RadiusMessage(config.radius_secret) rad.add_avp(libradi.RadiusAvp("User-Name", config.username)) rad.add_avp(libradi.RadiusAvp("Acct-Status-Type", action)) if is_ipv6(config.radius_dest): rad.add_avp(libradi.RadiusAvp("NAS-IPv6-Address", config.radius_dest)) else: rad.add_avp(libradi.RadiusAvp("NAS-IP-Address", config.radius_dest)) if is_ipv6(config.framed_ip): if not config.framed_mask: config.framed_mask = 128 rad.add_avp( libradi.RadiusAvp( "Framed-IPv6-Prefix", "{}/{}".format(config.framed_ip, config.framed_mask))) else: if not config.framed_mask: config.framed_mask = 32 rad.add_avp(libradi.RadiusAvp("Framed-IP-Address", config.framed_ip)) rad.add_avp( libradi.RadiusAvp( "Framed-IP-Netmask", libradi.radtypes.bits_to_ip4mask(config.framed_mask))) rad.add_avp(libradi.RadiusAvp("Framed-Protocol", FRAMED_PROTO_PPP)) rad.add_avp(libradi.RadiusAvp("Calling-Station-Id", config.calling_id)) rad.add_avp(libradi.RadiusAvp("Called-Station-Id", config.called_id)) rad.add_avp( libradi.RadiusAvp("3GPP-Location-Info", "0x" + config.subs_loc_info.encode("hex"))) rad.add_avp(libradi.RadiusAvp("3GPP-IMSI", config.imsi)) rad.add_avp(libradi.RadiusAvp("3GPP-IMEISV", config.imei)) for name, value in config.avps: rad.add_avp(libradi.RadiusAvp(name, value)) debug(str(rad)) return rad
def test_string_no_vendor_avp(self): avp = libradi.RadiusAvp("Called-Station-Id", "1234567890") self.assertIsNotNone(avp) self.assertFalse(avp.has_sub_avps()) self.assertEquals(30, avp.avp_code.value) self.assertEquals("1234567890", avp.avp_value.value)