mirror of
https://github.com/xcp-ng/xenadmin.git
synced 2025-01-20 07:19:18 +01:00
Merge pull request #124 from GaborApatiNagy/netmask
CA-116005: validation of the SN mask doesn't appear to be working correctly on the last two values of the mask.
This commit is contained in:
commit
9dfeba8be3
@ -151,7 +151,7 @@ namespace XenAdmin.Dialogs
|
||||
NetworkComboBox.SelectedIndex != -1 &&
|
||||
(DHCPIPRadioButton.Checked ||
|
||||
((StringUtility.IsIPAddress(IPAddressTextBox.Text)) &&
|
||||
StringUtility.IsIPAddress(SubnetTextBox.Text) && IsOptionalIPAddress(GatewayTextBox.Text))) &&
|
||||
StringUtility.IsValidNetmask(SubnetTextBox.Text) && IsOptionalIPAddress(GatewayTextBox.Text))) &&
|
||||
(type == Type.SECONDARY || ((IsOptionalIPAddress(PreferredDNSTextBox.Text)
|
||||
&& IsOptionalIPAddress(AlternateDNS1TextBox.Text)
|
||||
&& IsOptionalIPAddress(AlternateDNS2TextBox.Text))));
|
||||
|
91
XenAdminTests/UnitTests/SubnetworkMaskValidatorTest.cs
Normal file
91
XenAdminTests/UnitTests/SubnetworkMaskValidatorTest.cs
Normal file
@ -0,0 +1,91 @@
|
||||
/* Copyright (c) Citrix Systems Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms,
|
||||
* with or without modification, are permitted provided
|
||||
* that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above
|
||||
* copyright notice, this list of conditions and the
|
||||
* following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above
|
||||
* copyright notice, this list of conditions and the
|
||||
* following disclaimer in the documentation and/or other
|
||||
* materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using XenAdmin.Core;
|
||||
|
||||
namespace XenAdminTests.UnitTests
|
||||
{
|
||||
[TestFixture, Category(TestCategories.Unit)]
|
||||
class SubnetworkMaskValidatorTest
|
||||
{
|
||||
[Test, Sequential]
|
||||
public void TestValidNetmasks(
|
||||
[Values(
|
||||
"255.255.0.0",
|
||||
"128.0.0.0",
|
||||
"255.240.0.0",
|
||||
"255.128.0.0",
|
||||
"255.255.128.0",
|
||||
"255.255.255.255",
|
||||
"0.0.0.0",
|
||||
"128.0.0.0",
|
||||
"240.0.0.0"
|
||||
)] string validValues)
|
||||
|
||||
{
|
||||
Assert.AreEqual(true, StringUtility.IsValidNetmask(validValues));
|
||||
}
|
||||
|
||||
[Test, Sequential]
|
||||
public void TestInvalidNetmasks(
|
||||
[Values(
|
||||
"Gabor was here",
|
||||
"",
|
||||
null,
|
||||
"255.255,255.255",
|
||||
"255.0.255.0",
|
||||
"240.128.0.0",
|
||||
"255.00.0.0",
|
||||
"128.128.0.0",
|
||||
"192.168.1.1",
|
||||
"....",
|
||||
".255.255.0.0",
|
||||
"255.255.-0.0",
|
||||
"255.255.0.255",
|
||||
"255.128.0.1",
|
||||
"257.0.0.0",
|
||||
"888.0.0.0",
|
||||
"99999999999999999999999999999999999999999999999999999999999999999999999999",
|
||||
"0e0,0.0.0",
|
||||
"5e888",
|
||||
"255 .240.0.0",
|
||||
"255. 128.0.0 ",
|
||||
" 255.255.128.0"
|
||||
)] string invalidValues)
|
||||
{
|
||||
Assert.AreEqual(false, StringUtility.IsValidNetmask(invalidValues));
|
||||
}
|
||||
}
|
||||
}
|
@ -64,6 +64,7 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="UnitTests\SubnetworkMaskValidatorTest.cs" />
|
||||
<Compile Include="UnitTests\ExceptionSerializationTest.cs" />
|
||||
<Compile Include="XenModelTests\ActionTests\ActionTest.cs" />
|
||||
<Compile Include="XenModelTests\ActionTests\VMActionsTest.cs" />
|
||||
|
@ -33,6 +33,8 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Linq;
|
||||
using System.Collections;
|
||||
|
||||
namespace XenAdmin.Core
|
||||
{
|
||||
@ -150,6 +152,59 @@ namespace XenAdmin.Core
|
||||
private static readonly Regex IPRegex = new Regex(@"^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$");
|
||||
private static readonly Regex IPRegex0 = new Regex(@"^([0]{1,3})\.([0]{1,3})\.([0]{1,3})\.([0]{1,3})$");
|
||||
|
||||
/// <summary>
|
||||
/// Validates IPv4 subnet mask string (strict)
|
||||
/// Eg. 255.255.240.0 is valid
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Logically valid values with trailing zeros will not pass validation
|
||||
/// Eg. 255.255.240.00 will return NOT valid
|
||||
/// </remarks>
|
||||
/// <param name="netmask"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsValidNetmask(string netmask)
|
||||
{
|
||||
if (netmask == null)
|
||||
return false;
|
||||
|
||||
var netmaskBytes = new List<byte>();
|
||||
var parts = netmask.Split('.').ToList();
|
||||
|
||||
if (parts.Count != 4 || parts.Any(p => p.Length > 3))
|
||||
return false;
|
||||
|
||||
foreach (var part in parts)
|
||||
{
|
||||
byte byteValue;
|
||||
|
||||
//Converting value to byte if possible. The second check is to make sure that valid, but server side not supported values not get through (eg. 000, 00)
|
||||
if (!byte.TryParse(part, System.Globalization.NumberStyles.None, null, out byteValue) || byteValue.ToString() != part)
|
||||
return false;
|
||||
|
||||
netmaskBytes.Add(byteValue);
|
||||
}
|
||||
|
||||
var bits = new BitArray(netmaskBytes.ToArray());
|
||||
if (bits.Count != 32)
|
||||
return false;
|
||||
|
||||
bool wasZero = false;
|
||||
for (int octetNo = 0; octetNo < 4; octetNo ++)
|
||||
for (int relPos = 7; relPos >=0 ; relPos --) //less significant bit is on the left
|
||||
{
|
||||
bool val = bits[octetNo * 8 + relPos];
|
||||
|
||||
//if there is 1 again and there has been any 0 before, netmask is invalid. All other cases (if we get here) are valid.
|
||||
if (wasZero && val)
|
||||
return false;
|
||||
|
||||
if (!val)
|
||||
wasZero = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool IsIPAddress(string s)
|
||||
{
|
||||
if (string.IsNullOrEmpty(s))
|
||||
|
Loading…
Reference in New Issue
Block a user