github.com/teknogeek/dnscontrol@v0.2.8/pkg/spflib/flatten_test.go (about)

     1  package spflib
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  func TestFlatten(t *testing.T) {
     9  	res, err := NewCache("testdata-dns1.json")
    10  	if err != nil {
    11  		t.Fatal(err)
    12  	}
    13  	rec, err := Parse(strings.Join([]string{"v=spf1",
    14  		"ip4:198.252.206.0/24",
    15  		"ip4:192.111.0.0/24",
    16  		"include:_spf.google.com",
    17  		"include:mailgun.org",
    18  		"include:spf-basic.fogcreek.com",
    19  		"include:mail.zendesk.com",
    20  		"include:servers.mcsv.net",
    21  		"include:sendgrid.net",
    22  		"include:spf.mtasv.net",
    23  		"~all"}, " "), res)
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  	t.Log(rec.Print())
    28  	rec = rec.Flatten("mailgun.org")
    29  	t.Log(rec.Print())
    30  }
    31  
    32  // each test is array of strings.
    33  // first item is unsplit input
    34  // next is @ spf record
    35  // after that is alternating record fqdn and value
    36  var splitTests = [][]string{
    37  	{
    38  		"simple",
    39  		"v=spf1 -all",
    40  		"v=spf1 -all",
    41  	},
    42  	{
    43  		"longsimple",
    44  		"v=spf1 include:a01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.com -all",
    45  		"v=spf1 include:a01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.com -all",
    46  	},
    47  	{
    48  		"long simple multipart",
    49  		"v=spf1 include:a.com include:b.com include:12345678901234567890123456789000000000000000123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.com -all",
    50  		"v=spf1 include:a.com include:b.com include:12345678901234567890123456789000000000000000123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.com -all",
    51  	},
    52  	{
    53  		"overflow",
    54  		"v=spf1 include:a.com include:b.com include:X12345678901234567890123456789000000000000000123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.com -all",
    55  		"v=spf1 include:a.com include:b.com include:_spf1.stackex.com -all",
    56  		"_spf1.stackex.com",
    57  		"v=spf1 include:X12345678901234567890123456789000000000000000123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.com -all",
    58  	},
    59  	{
    60  		"overflow all sign carries",
    61  		"v=spf1 include:a.com include:b.com include:X12345678901234567890123456789000000000000000123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.com ~all",
    62  		"v=spf1 include:a.com include:b.com include:_spf1.stackex.com ~all",
    63  		"_spf1.stackex.com",
    64  		"v=spf1 include:X12345678901234567890123456789000000000000000123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.com ~all",
    65  	},
    66  	{
    67  		"really big",
    68  		"v=spf1 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178" +
    69  			" ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178" +
    70  			" ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 -all",
    71  		"v=spf1 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 include:_spf1.stackex.com -all",
    72  		"_spf1.stackex.com",
    73  		"v=spf1 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 include:_spf2.stackex.com -all",
    74  		"_spf2.stackex.com",
    75  		"v=spf1 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 include:_spf3.stackex.com -all",
    76  		"_spf3.stackex.com",
    77  		"v=spf1 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 ip4:200.192.169.178 -all",
    78  	},
    79  	{
    80  		"too long to split",
    81  		"v=spf1 include:a0123456789012345678901234567890123456789012345sssss6789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.com -all",
    82  		"v=spf1 include:a0123456789012345678901234567890123456789012345sssss6789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.com -all",
    83  	},
    84  }
    85  
    86  func TestSplit(t *testing.T) {
    87  	for _, tst := range splitTests {
    88  		t.Run(tst[0], func(t *testing.T) {
    89  			rec, err := Parse(tst[1], nil)
    90  			if err != nil {
    91  				t.Fatal(err)
    92  			}
    93  			res := rec.TXTSplit("_spf%d.stackex.com")
    94  			if res["@"] != tst[2] {
    95  				t.Fatalf("Root record wrong. \nExp %s\ngot %s", tst[2], res["@"])
    96  			}
    97  			for i := 3; i < len(tst); i += 2 {
    98  				fqdn := tst[i]
    99  				exp := tst[i+1]
   100  				if res[fqdn] != exp {
   101  					t.Fatalf("Record %s.\nExp %s\ngot %s", fqdn, exp, res[fqdn])
   102  				}
   103  			}
   104  		})
   105  	}
   106  }