public void TestOptionInvalidCastException() { var option = new CoapOption(0); Assert.Throws <InvalidCastException>(() => _ = option.ValueString); Assert.Throws <InvalidCastException>(() => _ = option.ValueString = "test"); Assert.Throws <InvalidCastException>(() => _ = option.ValueOpaque); Assert.Throws <InvalidCastException>(() => _ = option.ValueOpaque = Encoding.UTF8.GetBytes("test")); Assert.Throws <InvalidCastException>(() => _ = option.ValueUInt); Assert.Throws <InvalidCastException>(() => _ = option.ValueUInt = 1234); Assert.Throws <InvalidCastException>(() => _ = option.DefaultString); Assert.Throws <InvalidCastException>(() => _ = option.DefaultOpaque); Assert.Throws <InvalidCastException>(() => _ = option.DefaultUInt); option.FromBytes(null); // No-op option.FromBytes(new byte[] {}); // No-op Assert.Throws <InvalidCastException>(() => option.FromBytes(new byte[] { 0x12 })); }
public void TestOpaqueOption() { var option = new CoapOption(0, type: OptionType.Opaque); option.ValueOpaque = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xF0 }; Assert.AreEqual(8, option.Length); Assert.AreEqual(new byte[] { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xF0 }, option.ValueOpaque); Assert.AreEqual(new byte[] { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xF0 }, option.GetBytes()); option.FromBytes(new byte[] { 0xAA, 0x55, 0x11 }); Assert.AreEqual(3, option.Length); Assert.AreEqual(new byte[] { 0xAA, 0x55, 0x11 }, option.ValueOpaque); }
public void TestValueOption(uint value, int length, byte[] expected) { var optionToBytes = new CoapOption(0, type: OptionType.UInt, maxLength: 4); optionToBytes.ValueUInt = value; Assert.AreEqual(length, optionToBytes.Length); Assert.AreEqual(expected, optionToBytes.GetBytes()); var optionFromBytes = new CoapOption(0, type: OptionType.UInt, maxLength: 4); optionFromBytes.FromBytes(expected); Assert.AreEqual(length, optionFromBytes.Length); Assert.AreEqual(value, optionFromBytes.ValueUInt); }