-
Notifications
You must be signed in to change notification settings - Fork 555
[Tests] Add missing tests for NWProtocolStack and fix NWParameters tests. #4793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
spouliot
merged 2 commits into
dotnet:xcode10
from
mandel-macaque:nwprotocolstack-tests
Sep 12, 2018
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,144 @@ | ||
| #if !__WATCHOS__ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| #if XAMCORE_2_0 | ||
| using CoreFoundation; | ||
| using Foundation; | ||
| using Network; | ||
| using ObjCRuntime; | ||
| using Security; | ||
| #else | ||
| using MonoTouch.CoreFoundation; | ||
| using MonoTouch.Foundation; | ||
| using MonoTouch.Network; | ||
| using MonoTouch.Security; | ||
| #endif | ||
|
|
||
| using NUnit.Framework; | ||
|
|
||
| namespace MonoTouchFixtures.Network { | ||
|
|
||
| [TestFixture] | ||
| [Preserve (AllMembers = true)] | ||
| public class NWProtocolStackTest { | ||
|
|
||
| AutoResetEvent connectedEvent; // used to let us know when the connection was established so that we can access the NWPath | ||
| string host; | ||
| NWConnection connection; | ||
| NWProtocolStack stack; | ||
| List<NWProtocolOptions> options; | ||
|
|
||
| [TestFixtureSetUp] | ||
| public void Init () | ||
| { | ||
| TestRuntime.AssertXcodeVersion (10, 0); | ||
| // we want to use a single connection, since it is expensive | ||
| connectedEvent = new AutoResetEvent(false); | ||
| host = "www.google.com"; | ||
| using (var parameters = NWParameters.CreateUdp ()) | ||
| using (var endpoint = NWEndpoint.Create (host, "80")) { | ||
| connection = new NWConnection (endpoint, parameters); | ||
| connection.SetQueue (DispatchQueue.DefaultGlobalQueue); // important, else we will get blocked | ||
| connection.SetStateChangeHandler (ConnectionStateHandler); | ||
| connection.Start (); | ||
| Assert.True (connectedEvent.WaitOne (20000), "Connection timed out."); | ||
| stack = parameters.ProtocolStack; | ||
| using (var ipOptions = stack.InternetProtocol) { | ||
| ipOptions.IPSetVersion (NWIPVersion.Version4); | ||
| stack.PrependApplicationProtocol (ipOptions); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [TestFixtureTearDown] | ||
| public void Dispose() | ||
| { | ||
| connection?.Cancel (); | ||
| } | ||
|
|
||
| [SetUp] | ||
| public void SetUp () | ||
| { | ||
| options = new List<NWProtocolOptions> (); | ||
| } | ||
|
|
||
| void ConnectionStateHandler (NWConnectionState state, NWError error) | ||
| { | ||
| switch (state){ | ||
| case NWConnectionState.Ready: | ||
| connectedEvent.Set (); | ||
| break; | ||
| case NWConnectionState.Cancelled: | ||
| connection?.Dispose (); | ||
| connection = null; | ||
| stack?.Dispose (); | ||
| stack = null; | ||
| foreach (var o in options) | ||
| o.Dispose (); | ||
| break; | ||
| case NWConnectionState.Invalid: | ||
| case NWConnectionState.Failed: | ||
| Assert.Inconclusive ("Network connection could not be performed."); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void PrependApplicationProtocolNullOptionsTest () | ||
| { | ||
| // not need to test the method with a valid argument since it is part of the setup. | ||
| Assert.Throws <ArgumentNullException> (() => stack.PrependApplicationProtocol (null)); | ||
| } | ||
|
|
||
| // handler to iterate over the app protocols | ||
| public void InterateProtocolsHandler (NWProtocolOptions o) | ||
| { | ||
| options.Add (o); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ClearApplicationProtocolsTest () | ||
| { | ||
| // test the clean and the iterate | ||
| stack.IterateProtocols (InterateProtocolsHandler); | ||
| Assert.That (options.Count, Is.GreaterThan (0), "options.Count"); | ||
| // remove present ones | ||
| foreach (var o in options) | ||
| o.Dispose (); | ||
| options = new List<NWProtocolOptions> (); | ||
| stack.ClearApplicationProtocols (); | ||
| stack.IterateProtocols (InterateProtocolsHandler); | ||
| Assert.AreEqual (0, options.Count, "Cleared options"); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TransportProtocolPropertyTest () | ||
| { | ||
| using (var options = stack.TransportProtocol) | ||
| { | ||
| Assert.IsNotNull (options, "Transport protocol should not be null."); | ||
| } | ||
| using (var options = NWProtocolOptions.CreateUdp ()) | ||
| { | ||
| stack.TransportProtocol = options; | ||
| using (var copyOptions = stack.TransportProtocol) | ||
| { | ||
| copyOptions.IPSetUseMinimumMtu (true); // should not crash | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void InternetProtocolTest () | ||
| { | ||
| using (var o = stack.InternetProtocol) | ||
| { | ||
| o.IPSetUseMinimumMtu (true); // should not crash | ||
| } | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
| #endif | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: missing EOL for last line.