From 0cd02e2de8c57274c777b55204eb09718bb4db5f Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Wed, 17 Feb 2021 10:42:00 +0300 Subject: [PATCH] core: update protocols regexps --- pkg/core/native/name_service.go | 4 +- pkg/core/native/name_service_test.go | 56 ++++++++++++++++++++++++++++ pkg/core/native_name_service_test.go | 2 +- 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/pkg/core/native/name_service.go b/pkg/core/native/name_service.go index 5628865e9..f3c26cda4 100644 --- a/pkg/core/native/name_service.go +++ b/pkg/core/native/name_service.go @@ -75,8 +75,8 @@ var ( // Lookahead is not supported by Go, but it is simple `(?=.{3,255}$)`, // so we check name length explicitly. nameRegex = regexp.MustCompile("^([a-z0-9]{1,62}\\.)+[a-z][a-z0-9]{0,15}$") - ipv4Regex = regexp.MustCompile("^(2(5[0-5]|[0-4]\\d))|1?\\d{1,2}(\\.((2(5[0-5]|[0-4]\\d))|1?\\d{1,2})){3}$") - ipv6Regex = regexp.MustCompile("^([a-f0-9A-F]{1,4}:){7}[a-f0-9A-F]{1,4}$") + ipv4Regex = regexp.MustCompile("^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$") + ipv6Regex = regexp.MustCompile("(?:^)(([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}|:))$") rootRegex = regexp.MustCompile("^[a-z][a-z0-9]{0,15}$") ) diff --git a/pkg/core/native/name_service_test.go b/pkg/core/native/name_service_test.go index ec456a52e..978f294b3 100644 --- a/pkg/core/native/name_service_test.go +++ b/pkg/core/native/name_service_test.go @@ -30,3 +30,59 @@ func TestParseDomain(t *testing.T) { _, ok := domainFromString("nodots") require.False(t, ok) } + +func TestNameService_CheckName(t *testing.T) { + // tests are got from the C# implementation + testCases := []struct { + Type RecordType + Name string + ShouldFail bool + }{ + {Type: RecordTypeA, Name: "0.0.0.0"}, + {Type: RecordTypeA, Name: "10.10.10.10"}, + {Type: RecordTypeA, Name: "255.255.255.255"}, + {Type: RecordTypeA, Name: "192.168.1.1"}, + {Type: RecordTypeA, Name: "1a", ShouldFail: true}, + {Type: RecordTypeA, Name: "256.0.0.0", ShouldFail: true}, + {Type: RecordTypeA, Name: "01.01.01.01", ShouldFail: true}, + {Type: RecordTypeA, Name: "00.0.0.0", ShouldFail: true}, + {Type: RecordTypeA, Name: "0.0.0.-1", ShouldFail: true}, + {Type: RecordTypeA, Name: "0.0.0.0.1", ShouldFail: true}, + {Type: RecordTypeA, Name: "11111111.11111111.11111111.11111111", ShouldFail: true}, + {Type: RecordTypeA, Name: "11111111.11111111.11111111.11111111", ShouldFail: true}, + {Type: RecordTypeA, Name: "ff.ff.ff.ff", ShouldFail: true}, + {Type: RecordTypeA, Name: "0.0.256", ShouldFail: true}, + {Type: RecordTypeA, Name: "0.0.0", ShouldFail: true}, + {Type: RecordTypeA, Name: "0.257", ShouldFail: true}, + {Type: RecordTypeA, Name: "1.1", ShouldFail: true}, + {Type: RecordTypeA, Name: "257", ShouldFail: true}, + {Type: RecordTypeA, Name: "1", ShouldFail: true}, + {Type: RecordTypeAAAA, Name: "2001:db8::8:800:200c:417a"}, + {Type: RecordTypeAAAA, Name: "ff01::101"}, + {Type: RecordTypeAAAA, Name: "::1"}, + {Type: RecordTypeAAAA, Name: "::"}, + {Type: RecordTypeAAAA, Name: "2001:db8:0:0:8:800:200c:417a"}, + {Type: RecordTypeAAAA, Name: "ff01:0:0:0:0:0:0:101"}, + {Type: RecordTypeAAAA, Name: "0:0:0:0:0:0:0:1"}, + {Type: RecordTypeAAAA, Name: "0:0:0:0:0:0:0:0"}, + {Type: RecordTypeAAAA, Name: "2001:DB8::8:800:200C:417A", ShouldFail: true}, + {Type: RecordTypeAAAA, Name: "FF01::101", ShouldFail: true}, + {Type: RecordTypeAAAA, Name: "fF01::101", ShouldFail: true}, + {Type: RecordTypeAAAA, Name: "2001:DB8:0:0:8:800:200C:417A", ShouldFail: true}, + {Type: RecordTypeAAAA, Name: "FF01:0:0:0:0:0:0:101", ShouldFail: true}, + {Type: RecordTypeAAAA, Name: "::ffff:1.01.1.01", ShouldFail: true}, + {Type: RecordTypeAAAA, Name: "2001:DB8:0:0:8:800:200C:4Z", ShouldFail: true}, + {Type: RecordTypeAAAA, Name: "::13.1.68.3", ShouldFail: true}, + } + for _, testCase := range testCases { + if testCase.ShouldFail { + require.Panics(t, func() { + checkName(testCase.Type, testCase.Name) + }) + } else { + require.NotPanics(t, func() { + checkName(testCase.Type, testCase.Name) + }) + } + } +} diff --git a/pkg/core/native_name_service_test.go b/pkg/core/native_name_service_test.go index be7b15887..905d22c3c 100644 --- a/pkg/core/native_name_service_test.go +++ b/pkg/core/native_name_service_test.go @@ -205,7 +205,7 @@ func TestSetGetRecord(t *testing.T) { testNameServiceInvoke(t, bc, "getRecord", "1.2.3.4", "neo.com", int64(native.RecordTypeA)) testNameServiceInvoke(t, bc, "setRecord", stackitem.Null{}, "neo.com", int64(native.RecordTypeA), "1.2.3.4") testNameServiceInvoke(t, bc, "getRecord", "1.2.3.4", "neo.com", int64(native.RecordTypeA)) - testNameServiceInvoke(t, bc, "setRecord", stackitem.Null{}, "neo.com", int64(native.RecordTypeAAAA), "2001:0000:1F1F:0000:0000:0100:11A0:ADDF") + testNameServiceInvoke(t, bc, "setRecord", stackitem.Null{}, "neo.com", int64(native.RecordTypeAAAA), "2001:0000:1f1f:0000:0000:0100:11a0:addf") testNameServiceInvoke(t, bc, "setRecord", stackitem.Null{}, "neo.com", int64(native.RecordTypeCNAME), "nspcc.ru") testNameServiceInvoke(t, bc, "setRecord", stackitem.Null{}, "neo.com", int64(native.RecordTypeTXT), "sometext")