github.com/DataDog/datadog-agent/pkg/security/secl@v0.55.0-devel.0.20240517055856-10c4965fea94/model/dns_helpers_linux_test.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the Apache License Version 2.0.
     3  // This product includes software developed at Datadog (https://www.datadoghq.com/).
     4  // Copyright 2016-present Datadog, Inc.
     5  
     6  //go:build linux
     7  
     8  // Package model holds model related files
     9  package model
    10  
    11  import (
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestDnsHelpers_validateDNSName(t *testing.T) {
    18  	type test struct {
    19  		testName string
    20  		dnsName  string
    21  		isValid  bool
    22  	}
    23  
    24  	tests := []test{
    25  		// test valid dns names
    26  		{
    27  			testName: "test_ok_1",
    28  			dnsName:  "foo.bar",
    29  			isValid:  true,
    30  		},
    31  		{
    32  			testName: "test_ok_2",
    33  			dnsName:  "a.b",
    34  			isValid:  true,
    35  		},
    36  		{
    37  			testName: "test_ok_3",
    38  			dnsName:  "a.b.c",
    39  			isValid:  true,
    40  		},
    41  		{
    42  			testName: "test_ok_max_total_length",
    43  			dnsName:  "0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.253",
    44  			isValid:  true,
    45  		},
    46  		{
    47  			testName: "test_ok_max_domain_length",
    48  			dnsName:  "012345678901234567890123456789012345678901234567890123456789012.com",
    49  			isValid:  true,
    50  		},
    51  		{
    52  			testName: "test_ok_single_char",
    53  			dnsName:  "a",
    54  			isValid:  true,
    55  		},
    56  		{
    57  			testName: "test_ok_hostname",
    58  			dnsName:  "localhost",
    59  			isValid:  true,
    60  		},
    61  
    62  		// test invalid dns names
    63  		{
    64  			testName: "test_ko_2",
    65  			dnsName:  "a.b.",
    66  			isValid:  false,
    67  		},
    68  		{
    69  			testName: "test_ko_3",
    70  			dnsName:  ".a.b",
    71  			isValid:  false,
    72  		},
    73  		{
    74  			testName: "test_ko_4",
    75  			dnsName:  ".a.b.",
    76  			isValid:  false,
    77  		},
    78  		{
    79  			testName: "test_ko_5",
    80  			dnsName:  "a..b",
    81  			isValid:  false,
    82  		},
    83  		{
    84  			testName: "test_ko_7",
    85  			dnsName:  "...",
    86  			isValid:  false,
    87  		},
    88  		{
    89  			testName: "test_ko_8",
    90  			dnsName:  "a.b.c..d.e.f",
    91  			isValid:  false,
    92  		},
    93  		{
    94  			testName: "test_ko_max_total_length",
    95  			dnsName:  "0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.0.2.4.6.8.2530",
    96  			isValid:  false,
    97  		},
    98  		{
    99  			testName: "test_ko_max_domain_length",
   100  			dnsName:  "0123456789012345678901234567890123456789012345678901234567890123.com",
   101  			isValid:  false,
   102  		},
   103  	}
   104  
   105  	for _, test := range tests {
   106  		t.Run(test.testName, func(t *testing.T) {
   107  			err := validateDNSName(test.dnsName)
   108  			assert.Equal(t, test.isValid, err == nil)
   109  		})
   110  	}
   111  }