def __init__(self, name, keyid_restr=None, hash_restr=None):
        """

        :param name: A CCNxName
        :param keyid_restr: list or array of bytes
        :param hash_restr:  list or array of bytes
        :return: a CCNxInterest
        """
        length = name.length
        if keyid_restr is not None:
            keyid_tlv = CCNxTlv(T_KEYIDREST, len(keyid_restr), keyid_restr)
            length += 4 + keyid_tlv.length
        else:
            keyid_tlv = None

        if hash_restr is not None:
            hash_tlv = CCNxTlv(T_OBJHASHREST, len(hash_restr), hash_restr)
            length += 4 + hash_tlv.length
        else:
            hash_tlv = None

        interest_tlv = CCNxTlv(T_INTEREST, length, None)
        body_tlvs = [interest_tlv] + name.tlv_list()
        if keyid_tlv is not None:
            body_tlvs += [keyid_tlv]
        if hash_tlv is not None:
            body_tlvs += [hash_tlv]

        super(CCNxInterest,self).__init__([], body_tlvs)
        self.name = name
        self.__keyid_restr = keyid_restr
        self.__hash_restr = hash_restr
Beispiel #2
0
    def __init__(self, name, expiry_time, *args):
        """

        :param name: A CCNxName
        :return: a CCNxInterest
        """
        length = name.length
        expiry_tlv = None

        if expiry_time is not None:
            expiry_array = CCNxTlv.number_to_array(expiry_time)
            expiry_tlv = CCNxTlv(T_EXPIRY, len(expiry_array), expiry_array)
            length += 4 + expiry_tlv.length

        for arg in args:
            if arg is not None:
                if type(arg) != CCNxTlv:
                    raise ValueError("arg {} is not a CCNxTlv".format(arg))
                length += 4
                if arg.value is not None:
                    length += arg.length

        co_tlv = CCNxTlv(T_OBJECT, length, None)
        body_tlvs = [co_tlv] + name.tlv_list()
        if expiry_tlv is not None:
            body_tlvs += [expiry_tlv]
        body_tlvs += [a for a in args if a is not None]

        super(CCNxContentObject, self).__init__([], body_tlvs)
        self.name = name
Beispiel #3
0
    def test_pattern_4_9(self):
        tlv = CCNxTlv(0x0001, 0x01FF, "apple pie...")
        truth = [0xC3, 0xFF]
        encoded = CCNxCompressorVariableLength.compress(tlv)

        self.assertTrue(encoded is not None)
        self.assertTrue(encoded == truth)
Beispiel #4
0
    def test_sub_match(self):
        #     pairs.append(Pair([0x00, 0x03, 0x00, 0x04], 0x83))
        #     pairs.append(Pair([0x00, 0x03, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04], 0x84))
        #
        # The string 0x00030004 will match both 0x83 and the prefix of 0x84.
        # Test with the string 0x00030004000200FF.  It should return 83 and only remove one item
        # from the TLV list

        cmpr = CCNxCompressorFixedLength()

        tlv1 = CCNxTlv(0x0003, 0x0004, None)
        tlv2 = CCNxTlv(0x0002, 0x00FF, None)
        tlv_list = [tlv1, tlv2]

        encoded = cmpr.compress(tlv_list)
        self.assertTrue(encoded is not None)
        self.assertTrue(encoded == [0x83])

        # make sure we consumed the token off the list
        self.assertTrue(len(tlv_list) == 1)
Beispiel #5
0
    def test_simple_nomatch(self):
        cmpr = CCNxCompressorFixedLength()

        # This will match out to 3 of 4 bytes

        tlv = CCNxTlv(0x0005, 0x00FF, "foo")
        tlv_list = [tlv]

        encoded = cmpr.compress(tlv_list)
        self.assertTrue(encoded is None)

        # make sure we did not consume the token off the list
        self.assertTrue(len(tlv_list) == 1)
Beispiel #6
0
    def test_longest_match(self):
        #     pairs.append(Pair([0x00, 0x03, 0x00, 0x04], 0x83))
        #     pairs.append(Pair([0x00, 0x03, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04], 0x84))
        #
        # The string 0x00030004 will match both 0x83 and the prefix of 0x84.
        # Test with the full string match.  It should return 84 and more 3 items from list
        # from the TLV list

        cmpr = CCNxCompressorFixedLength()

        tlv1 = CCNxTlv(0x0003, 0x0004, None)
        tlv2 = CCNxTlv(0x0002, 0x0000, None)
        tlv3 = CCNxTlv(0x0004, 0x0004, "foo")
        tlv4 = CCNxTlv(0x00FF, 0x00FF, None)

        tlv_list = [tlv1, tlv2, tlv3, tlv4]

        encoded = cmpr.compress(tlv_list)
        self.assertTrue(encoded is not None)
        self.assertTrue(encoded == [ 0x84, 'f', 'o', 'o' ])

        # make sure we consumed the token off the list
        self.assertTrue(len(tlv_list) == 1)
Beispiel #7
0
    def test_simple_match(self):
        cmpr = CCNxCompressorFixedLength()

        # This entry has no extension.  It's a simple match
        # pairs.append(Pair([0x00, 0x05, 0x00, 0x01], 0x8F))

        tlv = CCNxTlv(0x0005, 0x0001, "foo")
        tlv_list = [tlv]

        encoded = cmpr.compress(tlv_list)
        self.assertTrue(encoded is not None)
        self.assertTrue(encoded == [ 0x8F, 'f', 'o', 'o' ])

        # make sure we consumed the token off the list
        self.assertTrue(len(tlv_list) == 0)
Beispiel #8
0
 def test_pattern_16_16(self):
     tlv = CCNxTlv(0x8686, 0x83FF, "apple pie...")
     truth = [0xFF, 0x86, 0x86, 0x83, 0xFF]
     encoded = CCNxCompressorVariableLength.compact(tlv)
     self.assertTrue(encoded == truth)
Beispiel #9
0
    def test_pattern_15_5(self):
        tlv = CCNxTlv(0x7FFE, 0x001F, "apple pie...")
        truth = [0xEF, 0xFF, 0xDF]

        encoded = CCNxCompressorVariableLength.compact(tlv)
        self.assertTrue(encoded == truth)
Beispiel #10
0
 def test_compress_16_10(self):
     """16 bit T's not compressed, they are compacted"""
     tlv = CCNxTlv(0x8000, 0x0001, "apple pie...")
     encoded = CCNxCompressorVariableLength.compress(tlv)
     self.assertTrue(encoded is None)
Beispiel #11
0
 def test_compress_15_5(self):
     """15-bit T with length over 5 bits compacted, not compressed"""
     tlv = CCNxTlv(0x4000, 0x0020, "apple pie...")
     encoded = CCNxCompressorVariableLength.compress(tlv)
     self.assertTrue(encoded is None)
Beispiel #12
0
    def test_pattern_3_4(self):
        tlv = CCNxTlv(0x0001, 0x000F, "0123456789abcdef")

        encoded = CCNxCompressorVariableLength.compress(tlv)
        self.assertTrue(encoded is not None)
        self.assertTrue(encoded == [0x1F])