|
| 1 | +using System.Text; |
| 2 | +using NetMQ.Core.Mechanisms; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace NetMQ.Tests |
| 6 | +{ |
| 7 | + public class MechanismTests |
| 8 | + { |
| 9 | + private Msg CreateMsg(string data, int lengthDiff) |
| 10 | + { |
| 11 | + Assert.NotNull(data); |
| 12 | + Assert.True(data.Length > 1); |
| 13 | + var length = data.Length + lengthDiff; |
| 14 | + Assert.True(length > 0); |
| 15 | + Assert.True(length < byte.MaxValue - 1); |
| 16 | + |
| 17 | + var msg = new Msg(); |
| 18 | + msg.InitGC(new byte[data.Length + 1], 0, data.Length + 1); |
| 19 | + msg.SetFlags(MsgFlags.Command); |
| 20 | + msg.Put((byte)length); |
| 21 | + msg.Put(Encoding.ASCII, data, 1); |
| 22 | + return msg; |
| 23 | + } |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public void IsCommandShouldReturnTrueForValidCommand() |
| 27 | + { |
| 28 | + var mechanism = new NullMechanism(null!, null!); |
| 29 | + var msg = CreateMsg("READY", 0); |
| 30 | + Assert.True(mechanism.IsCommand("READY", ref msg)); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public void IsCommandShouldReturnFalseForInvalidCommand() |
| 35 | + { |
| 36 | + var mechanism = new NullMechanism(null!, null!); |
| 37 | + var msg = CreateMsg("READY", -1); |
| 38 | + Assert.False(mechanism.IsCommand("READY", ref msg)); |
| 39 | + msg = CreateMsg("READY", 1); |
| 40 | + Assert.False(mechanism.IsCommand("READY", ref msg)); |
| 41 | + // this test case would fail due to an exception being throw (in 4.0.1.10 and prior) |
| 42 | + msg = CreateMsg("READY", 2); |
| 43 | + Assert.False(mechanism.IsCommand("READY", ref msg)); |
| 44 | + } |
| 45 | + |
| 46 | + // this test was used to validate the behavior prior to changing the validation logic in Mechanism.IsCommand |
| 47 | + // [Fact] |
| 48 | + // public void IsCommandShouldThrowWhenLengthByteExceedsSize() |
| 49 | + // { |
| 50 | + // var mechanism = new NullMechanism(null, null); |
| 51 | + // var msg = CreateMsg("READY", 2); |
| 52 | + // Assert.Throws<ArgumentOutOfRangeException>(() => mechanism.IsCommand("READY", ref msg)); |
| 53 | + // } |
| 54 | + } |
| 55 | +} |
0 commit comments