Skip to content

Commit

Permalink
✅ Test(test_bgpprefixsid.py): add unittest and format result
Browse files Browse the repository at this point in the history
  • Loading branch information
yuangezhizao committed Mar 5, 2024
1 parent 19a8d88 commit ca0c3ff
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 21 deletions.
11 changes: 2 additions & 9 deletions yabgp/message/attribute/sr/bgpprefixsid.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,6 @@ def decorator(klass):

return decorator

def dict(self):
"""
:return:
"""
return {self.ID: self.value}

@classmethod
def unpack(cls, data):
"""
Expand All @@ -80,11 +73,11 @@ def unpack(cls, data):
value = data[3: 3 + length]

if type_code in cls.registered_tlvs:
tlvs.append(cls.registered_tlvs[type_code].unpack(value).dict())
tlvs.append(cls.registered_tlvs[type_code].unpack(value))
else:
tlvs.append({
'type': type_code,
'value': str(binascii.b2a_hex(value))
})
data = data[3 + length:]
return cls(value=tlvs)
return tlvs
4 changes: 2 additions & 2 deletions yabgp/message/attribute/sr/srv6/l3service.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def unpack(cls, data):
value = data[3: 3 + srv6_service_sub_tlv_length]

if srv6_service_sub_tlv_type_code in cls.registered_tlvs:
tlvs.append(cls.registered_tlvs[srv6_service_sub_tlv_type_code].unpack(value).dict())
tlvs.append(cls.registered_tlvs[srv6_service_sub_tlv_type_code].unpack(value))
else:
tlvs.append({
'type': srv6_service_sub_tlv_type_code,
Expand All @@ -93,4 +93,4 @@ def unpack(cls, data):
value = {
'srv6_service_sub_tlvs': tlvs
}
return cls(value=value)
return {cls.TYPE_STR: value}
4 changes: 2 additions & 2 deletions yabgp/message/attribute/sr/srv6/sidinformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def unpack(cls, data):
value = data[3: 3 + srv6_service_data_sub_sub_tlv_length]

if srv6_service_data_sub_sub_tlv_type_code in cls.registered_tlvs:
tlvs.append(cls.registered_tlvs[srv6_service_data_sub_sub_tlv_type_code].unpack(value).dict())
tlvs.append(cls.registered_tlvs[srv6_service_data_sub_sub_tlv_type_code].unpack(value))
else:
tlvs.append({
'type': srv6_service_data_sub_sub_tlv_type_code,
Expand All @@ -108,4 +108,4 @@ def unpack(cls, data):

'srv6_service_data_sub_sub_tlvs': tlvs
}
return cls(value=value)
return {cls.TYPE_STR: value}
14 changes: 7 additions & 7 deletions yabgp/message/attribute/sr/srv6/sidstructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ def unpack(cls, data):
:param data:
:return:
"""
locator_block_length = ord(data[0])
locator_node_length = ord(data[1])
function_length = ord(data[2])
argument_length = ord(data[3])
transposition_length = ord(data[4])
transposition_offset = ord(data[5])
locator_block_length = data[0]
locator_node_length = data[1]
function_length = data[2]
argument_length = data[3]
transposition_length = data[4]
transposition_offset = data[5]
value = {
'locator_block_length': locator_block_length,
'locator_node_length': locator_node_length,
Expand All @@ -65,4 +65,4 @@ def unpack(cls, data):
'transposition_length': transposition_length,
'transposition_offset': transposition_offset
}
return cls(value=value)
return {cls.TYPE_STR: value}
2 changes: 1 addition & 1 deletion yabgp/message/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ def parse_attributes(data, asn4=False):
continue

elif type_code == bgp_cons.BGPTYPE_BGP_PREFIX_SID:
decode_value = BGPPrefixSID.unpack(data=attr_value).dict()
decode_value = BGPPrefixSID.unpack(data=attr_value)

else:
decode_value = binascii.b2a_hex(attr_value).decode('utf-8')
Expand Down
Empty file.
55 changes: 55 additions & 0 deletions yabgp/tests/unit/message/attribute/sr/test_bgpprefixsid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2024 Cisco Systems, Inc.
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

""" Test BGP Prefix SID TLV """

import unittest

from yabgp.message.update import BGPPrefixSID


class TestBGPPrefixSID(unittest.TestCase):

def test_unpack(self):
"""
:return:
"""
data_bin = b'\xc0\x28\x25' \
b'\x05\x00\x22\x00' \
b'\x01\x00\x1e\x00\x20\x05\xbb\x00\x01\x12\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x3f\x00' \
b'\x01\x00\x06\x20\x10\x10\x00\x10\x30'
data_list = [{
'srv6_l3_service': {
'srv6_service_sub_tlvs': [{
'srv6_sid_information': {
'srv6_sid_value': '2005:bb00:112::',
'srv6_service_sid_flags': 0,
'srv6_endpoint_behavior': 63,
'srv6_service_data_sub_sub_tlvs': [{
'srv6_sid_structure': {
'locator_block_length': 32,
'locator_node_length': 16,
'function_length': 16,
'argument_length': 0,
'transposition_length': 16,
'transposition_offset': 48
}
}]
}
}]
}
}]
self.assertEqual(data_list, BGPPrefixSID.unpack(data=data_bin[3:]))

0 comments on commit ca0c3ff

Please sign in to comment.