Skip to content

Commit dc4c767

Browse files
committed
add missing unit test, fix some missing units.
1 parent 812d817 commit dc4c767

File tree

5 files changed

+80
-8
lines changed

5 files changed

+80
-8
lines changed

ccnpy/core/KeyLink.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class KeyLink(TlvType):
2525
def class_type(cls):
2626
return cls.__T_KEYLINK
2727

28-
def __init__(self, link):
28+
def __init__(self, link: Link):
2929
TlvType.__init__(self)
3030
self._link = link
3131
self._tlv = Tlv(self.class_type(), link)

ccnpy/core/Link.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Link:
3131
"""
3232
Serves as the base class for KeyLink and is used in LocatorList
3333
"""
34-
def __init__(self, name=None, keyid=None, digest=None):
34+
def __init__(self, name: Optional[Name | str] = None, keyid: Optional[HashValue] = None, digest: Optional[HashValue] = None):
3535
"""
3636
The Link will serialize as a list of TLVs. It has no surrounding "link" type. That context must be
3737
provided by the concrete class, like KeyLink.
@@ -55,6 +55,8 @@ def __init__(self, name=None, keyid=None, digest=None):
5555
self._name = name
5656
self._keyid = keyid
5757
self._digest = digest
58+
# for iteration
59+
self._offset = 0
5860

5961
self._wire_format = array.array("B", [])
6062
if self._name is not None:
@@ -72,6 +74,21 @@ def __eq__(self, other):
7274
def __repr__(self):
7375
return "Link(%r, %r, %r)" % (self._name, self._keyid, self._digest)
7476

77+
def __len__(self):
78+
return len(self._wire_format)
79+
80+
def __iter__(self):
81+
self._offset = 0
82+
return self
83+
84+
def __next__(self):
85+
if self._offset == len(self._wire_format):
86+
raise StopIteration
87+
88+
output = self._wire_format[self._offset]
89+
self._offset += 1
90+
return output
91+
7592
def name(self) -> Optional[Name]:
7693
return self._name
7794

tests/core/test_ContentObject.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@
1818
from datetime import datetime, UTC
1919

2020
from ccnpy.core.ContentObject import ContentObject
21+
from ccnpy.core.HashValue import HashValue
2122
from ccnpy.core.Name import Name
2223
from ccnpy.core.Payload import Payload
2324
from ccnpy.core.Tlv import Tlv
25+
from ccnpy.flic.tlvs.HashGroup import HashGroup
26+
from ccnpy.flic.tlvs.Manifest import Manifest
27+
from ccnpy.flic.tlvs.Node import Node
28+
from ccnpy.flic.tlvs.Pointers import Pointers
2429

2530

2631
class ContentObjectTest(unittest.TestCase):
@@ -61,6 +66,15 @@ def test_deserialize(self):
6166
expected = ContentObject.create_data(name=name, payload=payload, expiry_time=dt, final_chunk_id=9)
6267
self.assertEqual(expected, actual, "Incorrect deserialization")
6368

69+
def test_create_manifest(self):
70+
dt = datetime.fromtimestamp(1560252745.906, UTC)
71+
manifest = Manifest(node=Node(hash_groups=[HashGroup(pointers=Pointers([HashValue.create_sha256([0])]))]))
72+
actual = ContentObject.create_manifest(manifest=manifest, name=Name.from_uri('ccnx:/foo'), expiry_time=dt)
73+
encoded = actual.serialize()
74+
decoded = ContentObject.parse(Tlv.deserialize(encoded))
75+
self.assertEqual(actual, decoded)
76+
decoded_manifest = Manifest.from_content_object(decoded)
77+
self.assertEqual(manifest, decoded_manifest)
6478

6579

6680
if __name__ == '__main__':

tests/core/test_KeyLink.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2024 Marc Mosko
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import array
17+
import unittest
18+
19+
from ccnpy.core.HashValue import HashValue
20+
from ccnpy.core.KeyLink import KeyLink
21+
from ccnpy.core.Link import Link
22+
from ccnpy.core.Name import Name
23+
from ccnpy.core.Tlv import Tlv
24+
25+
26+
class KeyLinkTest(unittest.TestCase):
27+
def test_serialize(self):
28+
name = Name.from_uri('ccnx:/a/b')
29+
keyid = HashValue(1, b'ab')
30+
digest = HashValue(2, b'ABCD')
31+
link = KeyLink(Link(name=name, keyid=keyid, digest=digest))
32+
actual = link.serialize()
33+
expected = array.array("B", [
34+
0, 14, 0, 36,
35+
0, 0, 0, 10, 0, 1, 0, 1, 97, 0, 1, 0, 1, 98,
36+
0, 2, 0, 6, 0, 1, 0, 2, 97, 98,
37+
0, 3, 0, 8, 0, 2, 0, 4, 65, 66, 67, 68])
38+
self.assertEqual(expected, actual)
39+
40+
decoded = KeyLink.parse(Tlv.deserialize(actual))
41+
self.assertEqual(link, decoded)

tests/flic/tree/test_TreeBuilder.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ def test_nary_4_3_61(self):
132132
```
133133
Example:
134134
DDDDMMM
135-
_/ \\_____________________________
136-
/ \___ \
137-
/ \ \
135+
_/ ||_____________________________
136+
/ |___ |
137+
/ | |
138138
DDDDDDD DDDDMMM DDDDMMM
139-
__/ \\ __/ \\
140-
/ \\________ / \\________
141-
/ \ \ / \ \
139+
__/ || __/ ||
140+
/ ||________ / ||________
141+
/ | | / | |
142142
DDDDDDD DDDDDDD DDDDDDD DDDDDDD DDDDDDD DDDDDDD
143143
144144
3 * 4 + 7 * 7 = 12 + 49 = 61

0 commit comments

Comments
 (0)