From b7108939cb48b3e0c9564fb1831d8acd5ee88724 Mon Sep 17 00:00:00 2001 From: Mircea-Pavel ANTON Date: Sat, 26 Oct 2024 16:38:36 +0000 Subject: [PATCH] improve structure --- internal/mikrotik/record_test.go | 201 ++++++++++++++++++++----------- 1 file changed, 133 insertions(+), 68 deletions(-) diff --git a/internal/mikrotik/record_test.go b/internal/mikrotik/record_test.go index 91109e1..2d7d921 100644 --- a/internal/mikrotik/record_test.go +++ b/internal/mikrotik/record_test.go @@ -176,7 +176,9 @@ func TestDNSRecordToExternalDNSEndpoint(t *testing.T) { expected *endpoint.Endpoint expectError bool }{ - // Basic Record Types + // =============================================================== + // A RECORD TEST CASES + // =============================================================== { name: "Valid A record", record: &DNSRecord{ @@ -193,6 +195,43 @@ func TestDNSRecordToExternalDNSEndpoint(t *testing.T) { }, expectError: false, }, + { + name: "Invalid A record (empty address)", + record: &DNSRecord{ + Name: "invalid.example.com", + Type: "A", + Address: "", + TTL: "1h", + }, + expected: nil, + expectError: true, + }, + { + name: "Invalid A record (malformed address)", + record: &DNSRecord{ + Name: "invalid.example.com", + Type: "A", + Address: "999.999.999.999", + TTL: "1h", + }, + expected: nil, + expectError: true, + }, + { + name: "Invalid A record (IPv6 address)", + record: &DNSRecord{ + Name: "invalid.example.com", + Type: "A", + Address: "2001:db8::1", + TTL: "1h", + }, + expected: nil, + expectError: true, + }, + + // =============================================================== + // AAAA RECORD TEST CASES + // =============================================================== { name: "Valid AAAA record", record: &DNSRecord{ @@ -209,6 +248,43 @@ func TestDNSRecordToExternalDNSEndpoint(t *testing.T) { }, expectError: false, }, + { + name: "Invalid AAAA record (empty address)", + record: &DNSRecord{ + Name: "invalid.example.com", + Type: "AAAA", + Address: "", + TTL: "1h", + }, + expected: nil, + expectError: true, + }, + { + name: "Invalid AAAA record (IPv4 address)", + record: &DNSRecord{ + Name: "invalid.example.com", + Type: "AAAA", + Address: "1.2.3.4", + TTL: "1h", + }, + expected: nil, + expectError: true, + }, + { + name: "Invalid AAAA record (malformed address)", + record: &DNSRecord{ + Name: "invalid.example.com", + Type: "AAAA", + Address: "1200:0000:AB00:1234:0000:2552:7777:1313:3:31", + TTL: "1h", + }, + expected: nil, + expectError: true, + }, + + // =============================================================== + // CNAME RECORD TEST CASES + // =============================================================== { name: "Valid CNAME record", record: &DNSRecord{ @@ -225,6 +301,32 @@ func TestDNSRecordToExternalDNSEndpoint(t *testing.T) { }, expectError: false, }, + { + name: "Invalid CNAME record (empty cname)", + record: &DNSRecord{ + Name: "invalid.example.com", + Type: "CNAME", + CName: "", + TTL: "30m", + }, + expected: nil, + expectError: true, + }, + { + name: "Invalid CNAME record (malformed domain)", + record: &DNSRecord{ + Name: "invalid.example.com", + Type: "CNAME", + CName: "sub......domain...here-", + TTL: "30m", + }, + expected: nil, + expectError: true, + }, + + // =============================================================== + // TXT RECORD TEST CASES + // =============================================================== { name: "Valid TXT record", record: &DNSRecord{ @@ -241,10 +343,23 @@ func TestDNSRecordToExternalDNSEndpoint(t *testing.T) { }, expectError: false, }, + { + name: "Invalid TXT record (empty text)", + record: &DNSRecord{ + Name: "invalid.example.com", + Type: "TXT", + Text: "", + TTL: "10m", + }, + expected: nil, + expectError: true, + }, - // Provider-specific stuff + // =============================================================== + // PROVIDER-SPECIFIC DATA TEST CASES + // =============================================================== { - name: "Provider-specific properties", + name: "Valid Provider-specific properties", record: &DNSRecord{ Name: "example.com", Type: "TXT", @@ -269,8 +384,11 @@ func TestDNSRecordToExternalDNSEndpoint(t *testing.T) { }, expectError: false, }, + // TODO: invalid provider specific - // Edge Cases + // =============================================================== + // DEFAULT VALUES FOR UNSET FIELDS TEST CASES + // =============================================================== { name: "Empty Type (should default to 'A')", record: &DNSRecord{ @@ -304,7 +422,9 @@ func TestDNSRecordToExternalDNSEndpoint(t *testing.T) { expectError: false, }, - // Error Cases + // =============================================================== + // GENERIC ERROR CASES + // =============================================================== { name: "Invalid TTL in DNSRecord", record: &DNSRecord{ @@ -317,77 +437,22 @@ func TestDNSRecordToExternalDNSEndpoint(t *testing.T) { expectError: true, }, { - name: "Unsupported record type", - record: &DNSRecord{ - Name: "example.com", - Type: "FWD", - TTL: "1h", - }, - expected: nil, - expectError: true, - }, - { - name: "Invalid A record (empty address)", - record: &DNSRecord{ - Name: "invalid.example.com", - Type: "A", - Address: "", - TTL: "1h", - }, - expected: nil, - expectError: true, - }, - { - name: "Invalid A record (malformed address)", + name: "Invalid TTL in DNSRecord", record: &DNSRecord{ - Name: "invalid.example.com", + Name: "example.com", Type: "A", - Address: "2001:db8::1", - TTL: "1h", - }, - expected: nil, - expectError: true, - }, - { - name: "Invalid AAAA record (empty address)", - record: &DNSRecord{ - Name: "invalid.example.com", - Type: "AAAA", - Address: "", - TTL: "1h", - }, - expected: nil, - expectError: true, - }, - { - name: "Invalid AAAA record (malformed address)", - record: &DNSRecord{ - Name: "invalid.example.com", - Type: "AAAA", - Address: "1.2.3.4", - TTL: "1h", - }, - expected: nil, - expectError: true, - }, - { - name: "Invalid CNAME record (empty cname)", - record: &DNSRecord{ - Name: "invalid.example.com", - Type: "CNAME", - CName: "", - TTL: "30m", + Address: "192.0.2.1", + TTL: "invalid", }, expected: nil, expectError: true, }, { - name: "Invalid TXT record (empty text)", + name: "Unsupported record type", record: &DNSRecord{ - Name: "invalid.example.com", - Type: "TXT", - Text: "", - TTL: "10m", + Name: "example.com", + Type: "FWD", + TTL: "1h", }, expected: nil, expectError: true,