From 7929fe33209d106f1060a866cc7812ff54ea3728 Mon Sep 17 00:00:00 2001 From: hubera01 Date: Mon, 25 Apr 2016 10:56:31 +0200 Subject: [PATCH] Add missing argument checks in Consumer<>.CreateAsync References #21 --- Lawo.EmberPlusSharp/Model/Consumer.cs | 20 ++++++++++++++++++- Lawo.EmberPlusSharpTest/Model/ConsumerTest.cs | 11 ++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Lawo.EmberPlusSharp/Model/Consumer.cs b/Lawo.EmberPlusSharp/Model/Consumer.cs index a367514a..e03a6cf2 100644 --- a/Lawo.EmberPlusSharp/Model/Consumer.cs +++ b/Lawo.EmberPlusSharp/Model/Consumer.cs @@ -174,7 +174,14 @@ public static Task> CreateAsync(S101Client client, int timeout, /// The slot to communicate with. All outgoing objects will have /// their property set to this value. Incoming messages are ignored, if their /// property does not match this value. - /// is less than -1. + /// + /// + /// is less than -1, and/or + /// is either less than + /// or greater than . + /// + /// + /// equals null. /// An exception was thrown from one of the callbacks passed to the /// constructor, see for more information. /// The model does either not match the data sent by the provider, or the @@ -201,6 +208,17 @@ public static Task> CreateAsync(S101Client client, int timeout, public static async Task> CreateAsync( S101Client client, int timeout, ChildrenRetrievalPolicy childrenRetrievalPolicy, byte slot) { + if (client == null) + { + throw new ArgumentNullException("client"); + } + + if ((childrenRetrievalPolicy < ChildrenRetrievalPolicy.None) || + (childrenRetrievalPolicy > ChildrenRetrievalPolicy.All)) + { + throw new ArgumentOutOfRangeException("childrenRetrievalPolicy"); + } + var result = new Consumer(client, timeout, childrenRetrievalPolicy, slot); await result.RetrieveChildrenAsync(); result.ReceiveLoop(); diff --git a/Lawo.EmberPlusSharpTest/Model/ConsumerTest.cs b/Lawo.EmberPlusSharpTest/Model/ConsumerTest.cs index f3ef7f2a..d2849df6 100644 --- a/Lawo.EmberPlusSharpTest/Model/ConsumerTest.cs +++ b/Lawo.EmberPlusSharpTest/Model/ConsumerTest.cs @@ -911,6 +911,16 @@ public void ExceptionTest() AsyncPump.Run( async () => { + await AssertThrowAsync(() => Consumer.CreateAsync(null)); + + using (var dummy = new S101Client(Stream.Null, Stream.Null.ReadAsync, Stream.Null.WriteAsync)) + { + await AssertThrowAsync( + () => Consumer.CreateAsync(dummy, -2), + () => Consumer.CreateAsync(dummy, 10000, ChildrenRetrievalPolicy.None - 1), + () => Consumer.CreateAsync(dummy, 10000, ChildrenRetrievalPolicy.All + 1)); + } + TestStandardExceptionConstructors(); await AssertThrowInCreateAsync( "IncompleteLog1.xml", @@ -1761,6 +1771,7 @@ private static int CountChildren(IEnumerable children) private static void CheckValues(MainRoot root) { Assert.IsNull(root.Parent); + Assert.AreEqual(0, root.Number); Assert.AreEqual(string.Empty, root.Identifier); Assert.IsNull(root.Description); Assert.IsTrue(root.GetPath() == "/");