1- namespace AngleSharp . Io . Tests
2- {
3- using NUnit . Framework ;
4- using System ;
5- using System . Net . NetworkInformation ;
6-
7- /// <summary>
8- /// Small (but quite useable) code to enable / disable some
9- /// test(s) depending on the current network status.
10- /// Taken from
11- /// http://stackoverflow.com/questions/520347/c-sharp-how-do-i-check-for-a-network-connection
12- /// </summary>
13- static class Helper
14- {
15- /// <summary>
16- /// Indicates whether any network connection is available
17- /// Filter connections below a specified speed, as well as virtual network cards.
18- /// Additionally writes an inconclusive message if no network is available.
19- /// </summary>
20- /// <returns>True if a network connection is available; otherwise false.</returns>
21- public static Boolean IsNetworkAvailable ( )
22- {
23- if ( IsNetworkAvailable ( 0 ) )
24- {
25- return true ;
26- }
27-
28- Assert . Inconclusive ( "No network has been detected. Test skipped." ) ;
29- return false ;
30- }
31-
32- /// <summary>
33- /// Indicates whether any network connection is available.
34- /// Filter connections below a specified speed, as well as virtual network cards.
35- /// </summary>
36- /// <param name="minimumSpeed">The minimum speed required. Passing 0 will not filter connection using speed.</param>
37- /// <returns>True if a network connection is available; otherwise false.</returns>
38- public static Boolean IsNetworkAvailable ( Int64 minimumSpeed )
39- {
40- if ( NetworkInterface . GetIsNetworkAvailable ( ) )
41- {
42- foreach ( var ni in NetworkInterface . GetAllNetworkInterfaces ( ) )
43- {
44- // discard because of standard reasons
45- if ( ( ni . OperationalStatus != OperationalStatus . Up ) ||
46- ( ni . NetworkInterfaceType == NetworkInterfaceType . Loopback ) ||
47- ( ni . NetworkInterfaceType == NetworkInterfaceType . Tunnel ) )
48- continue ;
49-
50- // this allow to filter modems, serial, etc.
51- // I use 10000000 as a minimum speed for most cases
52- if ( ni . Speed < minimumSpeed )
53- continue ;
54-
55- // discard virtual cards (virtual box, virtual pc, etc.)
56- if ( ( ni . Description . IndexOf ( "virtual" , StringComparison . OrdinalIgnoreCase ) >= 0 ) ||
57- ( ni . Name . IndexOf ( "virtual" , StringComparison . OrdinalIgnoreCase ) >= 0 ) )
58- continue ;
59-
60- // discard "Microsoft Loopback Adapter", it will not show as NetworkInterfaceType.Loopback but as Ethernet Card.
61- if ( ni . Description . Equals ( "Microsoft Loopback Adapter" , StringComparison . OrdinalIgnoreCase ) )
62- continue ;
63-
64- return true ;
65- }
66- }
67-
68- return false ;
69- }
70- }
1+ namespace AngleSharp . Io . Tests
2+ {
3+ using NUnit . Framework ;
4+ using System ;
5+ using System . Net . NetworkInformation ;
6+
7+ /// <summary>
8+ /// Small (but quite useable) code to enable / disable some
9+ /// test(s) depending on the current network status.
10+ /// Taken from
11+ /// http://stackoverflow.com/questions/520347/c-sharp-how-do-i-check-for-a-network-connection
12+ /// </summary>
13+ static class Helper
14+ {
15+ /// <summary>
16+ /// Indicates whether any network connection is available
17+ /// Filter connections below a specified speed, as well as virtual network cards.
18+ /// Additionally writes an inconclusive message if no network is available.
19+ /// </summary>
20+ /// <returns>True if a network connection is available; otherwise false.</returns>
21+ public static Boolean IsNetworkAvailable ( )
22+ {
23+ if ( IsNetworkAvailable ( 0 ) )
24+ {
25+ return true ;
26+ }
27+
28+ Assert . Inconclusive ( "No network has been detected. Test skipped." ) ;
29+ return false ;
30+ }
31+
32+ /// <summary>
33+ /// Indicates whether any network connection is available.
34+ /// Filter connections below a specified speed, as well as virtual network cards.
35+ /// </summary>
36+ /// <param name="minimumSpeed">The minimum speed required. Passing 0 will not filter connection using speed.</param>
37+ /// <returns>True if a network connection is available; otherwise false.</returns>
38+ public static Boolean IsNetworkAvailable ( Int64 minimumSpeed )
39+ {
40+ if ( NetworkInterface . GetIsNetworkAvailable ( ) )
41+ {
42+ foreach ( var ni in NetworkInterface . GetAllNetworkInterfaces ( ) )
43+ {
44+ // discard because of standard reasons
45+ if ( ( ni . OperationalStatus != OperationalStatus . Up ) ||
46+ ( ni . NetworkInterfaceType == NetworkInterfaceType . Loopback ) ||
47+ ( ni . NetworkInterfaceType == NetworkInterfaceType . Tunnel ) )
48+ continue ;
49+
50+ // this allow to filter modems, serial, etc.
51+ // I use 10000000 as a minimum speed for most cases
52+ if ( ni . Speed < minimumSpeed )
53+ continue ;
54+
55+ // discard virtual cards (virtual box, virtual pc, etc.)
56+ if ( ( ni . Description . IndexOf ( "virtual" , StringComparison . OrdinalIgnoreCase ) >= 0 ) ||
57+ ( ni . Name . IndexOf ( "virtual" , StringComparison . OrdinalIgnoreCase ) >= 0 ) )
58+ continue ;
59+
60+ // discard "Microsoft Loopback Adapter", it will not show as NetworkInterfaceType.Loopback but as Ethernet Card.
61+ if ( ni . Description . Equals ( "Microsoft Loopback Adapter" , StringComparison . OrdinalIgnoreCase ) )
62+ continue ;
63+
64+ return true ;
65+ }
66+ }
67+
68+ return false ;
69+ }
70+ }
7171}
0 commit comments