Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Link.mtu_bytes #722

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions RNS/Link.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from .vendor import umsgpack as umsgpack
import threading
import inspect
import struct
import math
import time
import RNS
Expand Down Expand Up @@ -110,7 +109,7 @@ class Link:

@staticmethod
def mtu_bytes(mtu):
return struct.pack(">I", mtu & 0xFFFFFF)[1:]
return mtu.to_bytes(3, byteorder='big', signed=False)

@staticmethod
def mtu_from_lr_packet(packet):
Expand Down
18 changes: 18 additions & 0 deletions tests/test_link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Tests for RNS.Link

import pytest

from RNS.Link import Link

def test_mtu_bytes():
data = [
(0, bytes([0, 0, 0])),
(5, bytes([0, 0, 5])),
(256, bytes([0, 1, 0])),
(65536, bytes([1, 0, 0])),
(16777215, bytes([255, 255, 255])),
]
for inp, outp in data:
assert Link.mtu_bytes(inp) == outp, f"Error:{inp} packed wrong"
pytest.raises(OverflowError, Link.mtu_bytes, -5) # Out of range
pytest.raises(OverflowError, Link.mtu_bytes, 20_000_000) # Out of range