Skip to content
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

Fix IPv4 and IPv6 Regex #2285

Merged
merged 17 commits into from
Feb 5, 2021
4 changes: 2 additions & 2 deletions src/neo/SmartContract/Native/NameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public sealed class NameService : NonfungibleToken<NameService.NameState>
private const uint OneYear = 365 * 24 * 3600;
private static readonly Regex rootRegex = new Regex("^[a-z][a-z0-9]{0,15}$", RegexOptions.Singleline);
private static readonly Regex nameRegex = new Regex("^(?=.{3,255}$)([a-z0-9]{1,62}\\.)+[a-z][a-z0-9]{0,15}$", RegexOptions.Singleline);
private static readonly Regex ipv4Regex = new Regex("^(2(5[0-5]|[0-4]\\d))|1?\\d{1,2}(\\.((2(5[0-5]|[0-4]\\d))|1?\\d{1,2})){3}$", RegexOptions.Singleline);
private static readonly Regex ipv6Regex = new Regex("^([a-f0-9]{1,4}:){7}[a-f0-9]{1,4}$", RegexOptions.Singleline | RegexOptions.IgnoreCase);
private static readonly Regex ipv4Regex = new Regex("^(?=\\d+\\.\\d+\\.\\d+\\.\\d+$)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\\.?){4}$", RegexOptions.Singleline);
private static readonly Regex ipv6Regex = new Regex("(?:^)(([0-9a-f]{1,4}:){7,7}[0-9a-f]{1,4}|([0-9a-f]{1,4}:){1,7}:|([0-9a-f]{1,4}:){1,6}:[0-9a-f]{1,4}|([0-9a-f]{1,4}:){1,5}(:[0-9a-f]{1,4}){1,2}|([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,3}|([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,4}|([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,5}|[0-9a-f]{1,4}:((:[0-9a-f]{1,4}){1,6})|:((:[0-9a-f]{1,4}){1,7}|:))(?=$)", RegexOptions.Singleline);

internal NameService()
{
Expand Down
122 changes: 55 additions & 67 deletions tests/neo.UnitTests/SmartContract/Native/UT_NNS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void TestSetup()
public void Check_Symbol() => NativeContract.NameService.Symbol(_snapshot).Should().Be("NNS");

[TestMethod]
public void Test_SetRecord_IPV4()
public void Test_SetRecord_A_AAAA()
{
var snapshot = _snapshot.CreateSnapshot();
// Fake blockchain
Expand All @@ -53,7 +53,7 @@ public void Test_SetRecord_IPV4()

// committee member,add a new root and then register, setrecord
string validroot = "testroot";
var ret = Check_AddRoot(snapshot, committeeAddress, validroot, persistingBlock);
Check_AddRoot(snapshot, committeeAddress, validroot, persistingBlock).Should().BeTrue();

string name = "testname";
string domain = name + "." + validroot;
Expand All @@ -69,76 +69,64 @@ public void Test_SetRecord_IPV4()
register_ret.State.Should().BeTrue();

//check NFT token
Assert.AreEqual(NativeContract.NameService.BalanceOf(snapshot, from), (BigInteger)1);
Assert.AreEqual(NativeContract.NameService.BalanceOf(snapshot, from), 1);

//after register
checkAvail_ret = Check_IsAvailable(snapshot, UInt160.Zero, domain, persistingBlock);
checkAvail_ret.Result.Should().BeTrue();
checkAvail_ret.State.Should().BeFalse();

//set As IPv4 Address
string testA = "10.10.10.10";
var setRecord_ret = Check_SetRecord(snapshot, domain, RecordType.A, testA, from, persistingBlock);
setRecord_ret.Should().BeTrue();

var getRecord_ret = Check_GetRecord(snapshot, domain, RecordType.A, persistingBlock);
getRecord_ret.State.Should().BeTrue();
getRecord_ret.Result.Should().Equals(testA);
//test As IPv4 Address
bool setRecord_ret;
(bool State, string Result) getRecord_ret;

testA = "0.0.0.0";
setRecord_ret = Check_SetRecord(snapshot, domain, RecordType.A, testA, from, persistingBlock);
setRecord_ret.Should().BeTrue();
getRecord_ret = Check_GetRecord(snapshot, domain, RecordType.A, persistingBlock);
getRecord_ret.State.Should().BeTrue();
getRecord_ret.Result.Should().Equals(testA);

testA = "255.255.255.255";
setRecord_ret = Check_SetRecord(snapshot, domain, RecordType.A, testA, from, persistingBlock);
setRecord_ret.Should().BeTrue();
getRecord_ret = Check_GetRecord(snapshot, domain, RecordType.A, persistingBlock);
getRecord_ret.State.Should().BeTrue();
getRecord_ret.Result.Should().Equals(testA);

//invalid case
setRecord_ret = Check_SetRecord(snapshot, domain, RecordType.A, "1a", from, persistingBlock);
setRecord_ret.Should().BeFalse();

setRecord_ret = Check_SetRecord(snapshot, domain, RecordType.A, "256.0.0.0", from, persistingBlock);
setRecord_ret.Should().BeFalse();

setRecord_ret = Check_SetRecord(snapshot, domain, RecordType.A, "0.0.0.-1", from, persistingBlock);
setRecord_ret.Should().BeFalse();

setRecord_ret = Check_SetRecord(snapshot, domain, RecordType.A, "0.0.0.0.1", from, persistingBlock);
setRecord_ret.Should().BeFalse();

setRecord_ret = Check_SetRecord(snapshot, domain, RecordType.A, "11111111.11111111.11111111.11111111", from, persistingBlock);
setRecord_ret.Should().BeFalse();

setRecord_ret = Check_SetRecord(snapshot, domain, RecordType.A, "11111111.11111111.11111111.11111111", from, persistingBlock);
setRecord_ret.Should().BeFalse();

setRecord_ret = Check_SetRecord(snapshot, domain, RecordType.A, "ff.ff.ff.ff", from, persistingBlock);
setRecord_ret.Should().BeFalse();

setRecord_ret = Check_SetRecord(snapshot, domain, RecordType.A, "0.0.256", from, persistingBlock);
setRecord_ret.Should().BeFalse();

setRecord_ret = Check_SetRecord(snapshot, domain, RecordType.A, "0.0.0", from, persistingBlock);
setRecord_ret.Should().BeFalse();

setRecord_ret = Check_SetRecord(snapshot, domain, RecordType.A, "0.257", from, persistingBlock);
setRecord_ret.Should().BeFalse();

setRecord_ret = Check_SetRecord(snapshot, domain, RecordType.A, "1.1", from, persistingBlock);
setRecord_ret.Should().BeFalse();
foreach (var testTrue in new string[] {
"0.0.0.0", "10.10.10.10", "255.255.255.255","192.168.1.1"
})
{
setRecord_ret = Check_SetRecord(snapshot, domain, RecordType.A, testTrue, from, persistingBlock);
setRecord_ret.Should().BeTrue();
getRecord_ret = Check_GetRecord(snapshot, domain, RecordType.A, persistingBlock);
getRecord_ret.State.Should().BeTrue();
getRecord_ret.Result.Should().Equals(testTrue);
}

setRecord_ret = Check_SetRecord(snapshot, domain, RecordType.A, "257", from, persistingBlock);
setRecord_ret.Should().BeFalse();
//test As IPv6 Address
foreach (var testTrue in new string[] {
"2001:db8::8:800:200c:417a", "ff01::101", "ff01::101", "::1", "::",
"2001:db8:0:0:8:800:200c:417a", "ff01:0:0:0:0:0:0:101", "0:0:0:0:0:0:0:1", "0:0:0:0:0:0:0:0"
})
{
setRecord_ret = Check_SetRecord(snapshot, domain, RecordType.AAAA, testTrue, from, persistingBlock);
setRecord_ret.Should().BeTrue();
getRecord_ret = Check_GetRecord(snapshot, domain, RecordType.AAAA, persistingBlock);
getRecord_ret.State.Should().BeTrue();
getRecord_ret.Result.Should().Equals(testTrue);
}

setRecord_ret = Check_SetRecord(snapshot, domain, RecordType.A, "1", from, persistingBlock);
setRecord_ret.Should().BeFalse();
//invalid A case
foreach (var testFalse in new string[] {
"1a", "256.0.0.0", "01.01.01.01", "00.0.0.0", "0.0.0.-1",
"0.0.0.0.1", "11111111.11111111.11111111.11111111",
"11111111.11111111.11111111.11111111", "ff.ff.ff.ff", "0.0.256",
"0.0.0", "0.257", "1.1", "257",
"1"
})
{
setRecord_ret = Check_SetRecord(snapshot, domain, RecordType.A, testFalse, from, persistingBlock);
setRecord_ret.Should().BeFalse();
}

//invalid AAAA case
foreach (var testFalse in new string[] {
"2001:DB8::8:800:200C:417A", "FF01::101", "fF01::101",
"2001:DB8:0:0:8:800:200C:417A", "FF01:0:0:0:0:0:0:101",
"::ffff:1.01.1.01", "2001:DB8:0:0:8:800:200C:4Z", "::13.1.68.3",
})
{
setRecord_ret = Check_SetRecord(snapshot, domain, RecordType.AAAA, testFalse, from, persistingBlock);
setRecord_ret.Should().BeFalse();
}
}

[TestMethod]
Expand All @@ -152,7 +140,7 @@ public void Test_SetRecord_CNAME()

// committee member,add a new root and then register, setrecord
string validroot = "testroot";
var ret = Check_AddRoot(snapshot, committeeAddress, validroot, persistingBlock);
Check_AddRoot(snapshot, committeeAddress, validroot, persistingBlock).Should().BeTrue();

string name = "testname";

Expand Down Expand Up @@ -229,7 +217,7 @@ public void Test_SetRecord_TxT()

// committee member,add a new root and then register, setrecord
string validroot = "testroot";
var ret = Check_AddRoot(snapshot, committeeAddress, validroot, persistingBlock);
Check_AddRoot(snapshot, committeeAddress, validroot, persistingBlock).Should().BeTrue();

string name = "testname";
string domain = name + "." + validroot;
Expand Down Expand Up @@ -320,7 +308,7 @@ public void Test_SetRecord_IPV6()

// committee member,add a new root and then register, setrecord
string validroot = "testroot";
var ret = Check_AddRoot(snapshot, committeeAddress, validroot, persistingBlock);
Check_AddRoot(snapshot, committeeAddress, validroot, persistingBlock).Should().BeTrue();

string name = "testname";
string domain = name + "." + validroot;
Expand All @@ -336,7 +324,7 @@ public void Test_SetRecord_IPV6()
register_ret.State.Should().BeTrue();

//set as IPV6 address
string testAAAA = "2001:0000:1F1F:0000:0000:0100:11A0:ADDF";
string testAAAA = "2001:0000:1f1f:0000:0000:0100:11a0:addf";
var setRecord_ret = Check_SetRecord(snapshot, domain, RecordType.AAAA, testAAAA, from, persistingBlock);
setRecord_ret.Should().BeTrue();

Expand Down Expand Up @@ -452,7 +440,7 @@ public void Test_SetAdmin()

// committee member,add a new root and then register, setrecord
string validroot = "testroot";
var ret = Check_AddRoot(snapshot, committeeAddress, validroot, persistingBlock);
Check_AddRoot(snapshot, committeeAddress, validroot, persistingBlock).Should().BeTrue();

string name = "testname";
string domain = name + "." + validroot;
Expand Down