class TestOptionMisc(object): EQ_NOMINAL_CASES = [ (protocol.Option("a", "b"), protocol.Option("a", "b"), True), (protocol.Option("a", "b"), protocol.Option("a", "c"), False), (protocol.Option("a", "b"), protocol.Option("x", "b"), False), (protocol.Option("a", "b"), protocol.Option("x", "y"), False), ] @pytest.mark.parametrize("a,b,expected", EQ_NOMINAL_CASES) def test_eq(self, a, b, expected): """Tests the Option.__eq__() function when both self and other are Options. """ assert a.__eq__(b) == expected @pytest.mark.parametrize("a,b,expected", EQ_NOMINAL_CASES) def test_ne(self, a, b, expected): """Tests the Option.__ne__() function when both self and other are Options. """ assert a.__ne__(b) == (not expected) EQ_NOTIMPLEMENTED_CASES = [ (protocol.Option("a", "b"), 3), (protocol.Option("a", "b"), ("a", "b")), ] @pytest.mark.parametrize("a,b", EQ_NOTIMPLEMENTED_CASES) def test_eq_notimplemented(self, a, b): """Tests the Option.__eq__() function when other is not an Option. """ assert a.__eq__(b) is NotImplemented @pytest.mark.parametrize("a,b", EQ_NOTIMPLEMENTED_CASES) def test_ne_notimplemented(self, a, b): """Tests the Option.__ne__() function when other is not an Option. """ assert a.__ne__(b) is NotImplemented def test_repr_coverage_only(self): """Runs the Option.__repr__() function just to ensure that it doesn't raise exceptions or cause parsing errors. """ assert repr(protocol.Option("hello", "world"))
class TestDecodeOption(object): NOMINAL_CASES = [ (b"windowsize\x00512\x00", 0, (protocol.Option("windowsize", "512"), 15)), (b"octet\x00tsize\x002048\x00", 6, (protocol.Option("tsize", "2048"), 17)), ] @pytest.mark.parametrize("input_bytes,offset,exp_output", NOMINAL_CASES) def test_nominal(self, input_bytes, offset, exp_output): """Tests nominal uses of Option.decode(). """ assert protocol.Option.decode(input_bytes, offset) == exp_output MISSING_NULL_CASES = [ (b"windowsize\x00512", 0), (b"windowsize 512\x00", 0), (b"windowsize 512", 0), (b"test\x00512\x00", 5), ] @pytest.mark.parametrize("input_bytes,offset", MISSING_NULL_CASES) def test_missing_null(self, input_bytes, offset): """Tests cases where null terminators were left out. """ with pytest.raises(primitives.NullTerminatorNotFoundError): protocol.Option.decode(input_bytes, offset) BAD_ASCII_CASES = [ (b"some option\x00" + u"an em dash\u2014quite dashing\x00".encode("utf-8"), 0), (b"oh\x82bother\x00val\x00", 0), (b"why\xffme\x00" + u"everything is \u00e4wful\x00".encode("utf-8"), 0), ] @pytest.mark.parametrize("input_bytes,offset", BAD_ASCII_CASES) def test_bad_ascii(self, input_bytes, offset): """Tests cases where the option name or value contain invalid ASCII. """ with pytest.raises(ValueError): protocol.Option.decode(input_bytes, offset)
def test_bad_ascii(self, name, value): """Tests cases where the option name or value contain invalid ASCII. """ with pytest.raises(ValueError): protocol.Option(name, value).encode()
def test_null(self, name, value): """Tests cases where the option name or value have a null character embedded in them. """ with pytest.raises(ValueError): protocol.Option(name, value).encode()
def test_nominal(self, name, value, exp_output): """Tests nominal uses of Option.encode(). """ assert protocol.Option(name, value).encode() == exp_output
def test_endtoend(self, name, value): """Tests end-to-end encoding and decoding of options. """ opt = protocol.Option(name, value) exp_offset = len(name) + len(value) + 2 assert protocol.Option.decode(opt.encode()) == (opt, exp_offset)
def test_repr_coverage_only(self): """Runs the Option.__repr__() function just to ensure that it doesn't raise exceptions or cause parsing errors. """ assert repr(protocol.Option("hello", "world"))