-
Notifications
You must be signed in to change notification settings - Fork 743
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue where message is assumed to contain correct length byte (le…
…ading to process exit) (#1050) Do not assume that message contains correct length to prevent calling GetString with invalid parameters.
- Loading branch information
Showing
2 changed files
with
59 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Text; | ||
using NetMQ.Core.Mechanisms; | ||
using Xunit; | ||
|
||
namespace NetMQ.Tests | ||
{ | ||
public class MechanismTests | ||
{ | ||
private Msg CreateMsg(string data, int lengthDiff) | ||
{ | ||
Assert.NotNull(data); | ||
Assert.True(data.Length > 1); | ||
var length = data.Length + lengthDiff; | ||
Assert.True(length > 0); | ||
Assert.True(length < byte.MaxValue - 1); | ||
|
||
var msg = new Msg(); | ||
msg.InitGC(new byte[data.Length + 1], 0, data.Length + 1); | ||
msg.SetFlags(MsgFlags.Command); | ||
msg.Put((byte)length); | ||
msg.Put(Encoding.ASCII, data, 1); | ||
return msg; | ||
} | ||
|
||
[Fact] | ||
public void IsCommandShouldReturnTrueForValidCommand() | ||
{ | ||
var mechanism = new NullMechanism(null!, null!); | ||
var msg = CreateMsg("READY", 0); | ||
Assert.True(mechanism.IsCommand("READY", ref msg)); | ||
} | ||
|
||
[Fact] | ||
public void IsCommandShouldReturnFalseForInvalidCommand() | ||
{ | ||
var mechanism = new NullMechanism(null!, null!); | ||
var msg = CreateMsg("READY", -1); | ||
Assert.False(mechanism.IsCommand("READY", ref msg)); | ||
msg = CreateMsg("READY", 1); | ||
Assert.False(mechanism.IsCommand("READY", ref msg)); | ||
// this test case would fail due to an exception being throw (in 4.0.1.10 and prior) | ||
msg = CreateMsg("READY", 2); | ||
Assert.False(mechanism.IsCommand("READY", ref msg)); | ||
} | ||
|
||
// this test was used to validate the behavior prior to changing the validation logic in Mechanism.IsCommand | ||
// [Fact] | ||
// public void IsCommandShouldThrowWhenLengthByteExceedsSize() | ||
// { | ||
// var mechanism = new NullMechanism(null, null); | ||
// var msg = CreateMsg("READY", 2); | ||
// Assert.Throws<ArgumentOutOfRangeException>(() => mechanism.IsCommand("READY", ref msg)); | ||
// } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters