• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lduchosal / ipnetwork / 743

30 Mar 2025 12:27AM UTC coverage: 93.641% (+0.1%) from 93.516%
743

push

appveyor

lduchosal
Chore: last cleanups

1561 of 1667 relevant lines covered (93.64%)

820053.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.44
/src/System.Net.IPNetwork/IPNetwork2InternalParse.cs
1
// <copyright file="IPNetwork2InternalParse.cs" company="IPNetwork">
2
// Copyright (c) IPNetwork. All rights reserved.
3
// </copyright>
4

5
namespace System.Net;
6

7
using Text.RegularExpressions;
8

9
/// <summary>
10
/// The InternalParse methodes.
11
/// </summary>
12
public partial class IPNetwork2
13
{
14
    /// <summary>
15
    /// 192.168.168.100 - 255.255.255.0
16
    ///
17
    /// Network   : 192.168.168.0
18
    /// Netmask   : 255.255.255.0
19
    /// Cidr      : 24
20
    /// Start     : 192.168.168.1
21
    /// End       : 192.168.168.254
22
    /// Broadcast : 192.168.168.255.
23
    /// </summary>
24
    /// <param name="tryParse">Whether to throw exception or not during conversion.</param>
25
    /// <param name="ipaddress">A string containing an ip address to convert.</param>
26
    /// <param name="netmask">A string containing a netmask to convert (255.255.255.0).</param>
27
    /// <param name="ipnetwork">The resulting IPNetwork.</param>
28
    private static void InternalParse(bool tryParse, string ipaddress, string netmask, out IPNetwork2 ipnetwork)
29
    {
81✔
30
        if (string.IsNullOrEmpty(ipaddress))
81✔
31
        {
6✔
32
            if (tryParse == false)
6✔
33
            {
3✔
34
                throw new ArgumentNullException(nameof(ipaddress));
3✔
35
            }
36

37
            ipnetwork = null;
3✔
38
            return;
3✔
39
        }
40

41
        if (string.IsNullOrEmpty(netmask))
75✔
42
        {
7✔
43
            if (tryParse == false)
7✔
44
            {
4✔
45
                throw new ArgumentNullException(nameof(netmask));
4✔
46
            }
47

48
            ipnetwork = null;
3✔
49
            return;
3✔
50
        }
51

52
        bool ipaddressParsed = IPAddress.TryParse(ipaddress, out IPAddress ip);
68✔
53
        if (ipaddressParsed == false)
68✔
54
        {
5✔
55
            if (tryParse == false)
5✔
56
            {
3✔
57
                throw new ArgumentException("ipaddress");
3✔
58
            }
59

60
            ipnetwork = null;
2✔
61
            return;
2✔
62
        }
63

64
        bool netmaskParsed = IPAddress.TryParse(netmask, out IPAddress mask);
63✔
65
        if (netmaskParsed == false)
63✔
66
        {
4✔
67
            if (tryParse == false)
4✔
68
            {
2✔
69
                throw new ArgumentException("netmask");
2✔
70
            }
71

72
            ipnetwork = null;
2✔
73
            return;
2✔
74
        }
75

76
        InternalParse(tryParse, ip, mask, out ipnetwork);
59✔
77
    }
68✔
78

79
    /// <summary>
80
    /// Internal parse an IPNetwork2.
81
    /// </summary>
82
    /// <param name="tryParse">Prevent exception.</param>
83
    /// <param name="network">The network to parse.</param>
84
    /// <param name="cidrGuess">The way to guess CIDR.</param>
85
    /// <param name="sanitanize">Clean up the network.</param>
86
    /// <param name="ipnetwork">The resulting IPNetwork.</param>
87
    /// <exception cref="ArgumentNullException">When network is null.</exception>
88
    /// <exception cref="ArgumentException">When network is not valid.</exception>
89
    private static void InternalParse(bool tryParse, string network, ICidrGuess cidrGuess, bool sanitanize, out IPNetwork2 ipnetwork)
90
    {
4,000,715✔
91
        if (string.IsNullOrEmpty(network))
4,000,715✔
92
        {
4✔
93
            if (tryParse == false)
4✔
94
            {
2✔
95
                throw new ArgumentNullException(nameof(network));
2✔
96
            }
97

98
            ipnetwork = null;
2✔
99
            return;
2✔
100
        }
101

102
        if (sanitanize)
4,000,711✔
103
        {
4,000,705✔
104
            network = Regex.Replace(network, @"[^0-9a-fA-F\.\/\s\:]+", string.Empty, RegexOptions.None, TimeSpan.FromMilliseconds(100));
4,000,705✔
105
            network = Regex.Replace(network, @"\s{2,}", " ", RegexOptions.None, TimeSpan.FromMilliseconds(100));
4,000,705✔
106
            network = network.Trim();
4,000,705✔
107
        }
4,000,705✔
108

109
        StringSplitOptions splitOptions = sanitanize ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None;
4,000,711✔
110
        string[] args = network.Split([' ', '/'], splitOptions);
4,000,711✔
111

112
        if (args.Length == 0)
4,000,711✔
113
        {
1✔
114
            if (tryParse == false)
1✔
115
            {
×
116
                throw new ArgumentNullException(nameof(network));
×
117
            }
118

119
            ipnetwork = null;
1✔
120
            return;
1✔
121
        }
122

123
        if (args.Length == 1)
4,000,710✔
124
        {
46✔
125
            string cidrlessNetwork = args[0];
46✔
126
            if (cidrGuess.TryGuessCidr(cidrlessNetwork, out byte cidr))
46✔
127
            {
41✔
128
                InternalParse(tryParse, cidrlessNetwork, cidr, out ipnetwork);
41✔
129
                return;
41✔
130
            }
131

132
            if (tryParse == false)
5✔
133
            {
2✔
134
                throw new ArgumentException("network");
2✔
135
            }
136

137
            ipnetwork = null;
3✔
138
            return;
3✔
139
        }
140

141
        if (byte.TryParse(args[1], out byte cidr1))
4,000,664✔
142
        {
4,000,617✔
143
            InternalParse(tryParse, args[0], cidr1, out ipnetwork);
4,000,617✔
144
            return;
4,000,617✔
145
        }
146

147
        InternalParse(tryParse, args[0], args[1], out ipnetwork);
47✔
148
    }
4,000,709✔
149

150
    /// <summary>
151
    /// 192.168.168.100 255.255.255.0
152
    ///
153
    /// Network   : 192.168.168.0
154
    /// Netmask   : 255.255.255.0
155
    /// Cidr      : 24
156
    /// Start     : 192.168.168.1
157
    /// End       : 192.168.168.254
158
    /// Broadcast : 192.168.168.255.
159
    /// </summary>
160
    /// <param name="tryParse">Whether to throw exception or not during conversion.</param>
161
    /// <param name="ipaddress">An ip address to convert.</param>
162
    /// <param name="netmask">A netmask to convert (255.255.255.0).</param>
163
    /// <param name="ipnetwork">The resulting IPNetwork.</param>
164
    private static void InternalParse(bool tryParse, IPAddress ipaddress, IPAddress netmask, out IPNetwork2 ipnetwork)
165
    {
4,000,773✔
166
        if (ipaddress == null)
4,000,773✔
167
        {
4✔
168
            if (tryParse == false)
4✔
169
            {
2✔
170
                throw new ArgumentNullException(nameof(ipaddress));
2✔
171
            }
172

173
            ipnetwork = null;
2✔
174
            return;
2✔
175
        }
176

177
        if (netmask == null)
4,000,769✔
178
        {
4✔
179
            if (tryParse == false)
4✔
180
            {
2✔
181
                throw new ArgumentNullException(nameof(netmask));
2✔
182
            }
183

184
            ipnetwork = null;
2✔
185
            return;
2✔
186
        }
187

188
        var uintIpAddress = ToBigInteger(ipaddress);
4,000,765✔
189
        bool parsed = TryToCidr(netmask, out byte? cidr2);
4,000,765✔
190
        if (parsed == false)
4,000,765✔
191
        {
3✔
192
            if (tryParse == false)
3✔
193
            {
1✔
194
                throw new ArgumentException("netmask");
1✔
195
            }
196

197
            ipnetwork = null;
2✔
198
            return;
2✔
199
        }
200

201
        byte cidr = (byte)cidr2!;
4,000,762✔
202

203
        var ipnet = new IPNetwork2(uintIpAddress, ipaddress.AddressFamily, cidr);
4,000,762✔
204
        ipnetwork = ipnet;
4,000,762✔
205
    }
4,000,768✔
206

207
    /// <summary>
208
    /// 192.168.168.100/24
209
    ///
210
    /// Network   : 192.168.168.0
211
    /// Netmask   : 255.255.255.0
212
    /// Cidr      : 24
213
    /// Start     : 192.168.168.1
214
    /// End       : 192.168.168.254
215
    /// Broadcast : 192.168.168.255.
216
    /// </summary>
217
    /// <param name="tryParse">Whether to throw exception or not during conversion.</param>
218
    /// <param name="ipaddress">A string containing an ip address to convert.</param>
219
    /// <param name="cidr">A byte representing the CIDR to be used in conversion (/24).</param>
220
    /// <param name="ipnetwork">The resulting IPNetwork.</param>
221
    private static void InternalParse(bool tryParse, string ipaddress, byte cidr, out IPNetwork2 ipnetwork)
222
    {
4,000,750✔
223
        if (string.IsNullOrEmpty(ipaddress))
4,000,750✔
224
        {
6✔
225
            if (tryParse == false)
6✔
226
            {
2✔
227
                throw new ArgumentNullException(nameof(ipaddress));
2✔
228
            }
229

230
            ipnetwork = null;
4✔
231
            return;
4✔
232
        }
233

234
        bool ipaddressParsed = IPAddress.TryParse(ipaddress, out IPAddress ip);
4,000,744✔
235
        if (ipaddressParsed == false)
4,000,744✔
236
        {
37✔
237
            if (tryParse == false)
37✔
238
            {
1✔
239
                throw new ArgumentException("ipaddress");
1✔
240
            }
241

242
            ipnetwork = null;
36✔
243
            return;
36✔
244
        }
245

246
        bool parsedNetmask = TryToNetmask(cidr, ip.AddressFamily, out IPAddress mask);
4,000,707✔
247
        if (parsedNetmask == false)
4,000,707✔
248
        {
3✔
249
            if (tryParse == false)
3✔
250
            {
1✔
251
                throw new ArgumentException("cidr");
1✔
252
            }
253

254
            ipnetwork = null;
2✔
255
            return;
2✔
256
        }
257

258
        InternalParse(tryParse, ip, mask, out ipnetwork);
4,000,704✔
259
    }
4,000,746✔
260
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc