<commit_before>package mailck

import (
	"fmt"
	"github.com/siebenmann/smtpd"
	"github.com/stretchr/testify/assert"
	"net"
	"testing"
	"time"
	"context"
)

func TestCheckSyntax(t *testing.T) {
	tests := []struct {
		mail  string
		valid bool
	}{
		{"", false},
		{"xxx", false},
		{"s.mancket<EMAIL>.de", false},
		{"<EMAIL>", false},
		{"<EMAIL>", false},
		{"<EMAIL>", true},
		{"<EMAIL>", true},
	}

	for _, test := range tests {
		t.Run(test.mail, func(t *testing.T) {
			result := CheckSyntax(test.mail)
			assert.Equal(t, test.valid, result)
		})
	}
}

func TestCheck(t *testing.T) {
	tests := []struct {
		mail   string
		result Result
		err    error
	}{
		{"xxx", InvalidSyntax, nil},
		{"<EMAIL>", InvalidDomain, nil},
		{"<EMAIL>", InvalidDomain, nil},
		{"<EMAIL>", Disposable, nil},
	}

	for _, test := range tests {
		t.Run(test.mail, func(t *testing.T) {
			start := time.Now()
			result, err := Check("<EMAIL>", test.mail)
			assert.Equal(t, test.result, result)
			assert.Equal(t, test.err, err)
			fmt.Printf("check for %30v: %-15v => %-10v (%v)\n", test.mail, time.Since(start), test.result.Result, test.result.ResultDetail)
		})
	}
}

func Test_checkMailbox(t *testing.T) {
	tests := []struct {
		stopAt      smtpd.Command
		result      Result
		expectError bool
	}{
		{smtpd.QUIT, Valid, false},
		{smtpd.RCPTTO, MailboxUnavailable, false},
		{smtpd.MAILFROM, MailserverError, true},
		{smtpd.HELO, MailserverError, true},
	}

	for _, test := range tests {
		t.Run(fmt.Sprintf("stop at: %v", test.stopAt), func(t *testing.T) {
			dummyServer := NewDummySMTPServer("localhost:2525", test.stopAt, 0)
			defer dummyServer.Close()
			result, err := checkMailbox(noContext,"<EMAIL>", "<EMAIL>", []*net.MX{{Host: "localhost"}}, 2525)
			assert.Equal(t, test.result, result)
			if test.expectError {
				assert.Error(t, err)
			} else {
				assert.NoError(t, err)
			}
		})
	}
}

func Test_checkMailbox_NetworkError(t *testing.T) {
	result, err := checkMailbox(noContext,"<EMAIL>", "<EMAIL>", []*net.MX{{Host: "localhost"}}, 6666)
	assert.Equal(t, NetworkError, result)
	assert.Error(t, err)
}

func Test_checkMailboxContext(t *testing.T) {
	deltas := []struct{
		delayTime      time.Duration
		contextTime    time.Duration
		expectedResult Result
	}{
		{ 0, 0, TimeoutError },
		{ 0, time.Second, Valid },
		{ time.Millisecond * 1500, 200 * time.Millisecond, TimeoutError },
	}
	for _,d := range deltas {
		t.Run(fmt.Sprintf("context time %v delay %v expected %v", d.contextTime,d.delayTime,d.expectedResult.Result), func(t *testing.T) {
			dummyServer := NewDummySMTPServer("localhost:2528", smtpd.QUIT,d.delayTime)
			tt := time.Now()
			ctx, _ := context.WithTimeout(context.Background(), d.contextTime)
			result,err := checkMailbox(ctx, "<EMAIL>", "<EMAIL>", []*net.MX{{Host: "127.0.0.1"}}, 2528)
			if d.expectedResult == Valid {
				assert.NoError(t,err)
			} else {
				assert.Error(t,err)
			}
			assert.Equal(t,d.expectedResult, result)
			// confirm that we completed within requested time
			// add 10ms of wiggle room
			assert.WithinDuration(t, time.Now(), tt, d.contextTime + 10 * time.Millisecond)
			dummyServer.Close()
		})<commit_msg>Fix leaking context in test<commit_after>package mailck

import (
	"fmt"
	"github.com/siebenmann/smtpd"
	"github.com/stretchr/testify/assert"
	"net"
	"testing"
	"time"
	"context"
)

func TestCheckSyntax(t *testing.T) {
	tests := []struct {
		mail  string
		valid bool
	}{
		{"", false},
		{"xxx", false},
		{"s.mancketarent.de", false},
		{"<EMAIL>", false},
		{"<EMAIL>", false},
		{"<EMAIL>", true},
		{"<EMAIL>", true},
	}

	for _, test := range tests {
		t.Run(test.mail, func(t *testing.T) {
			result := CheckSyntax(test.mail)
			assert.Equal(t, test.valid, result)
		})
	}
}

func TestCheck(t *testing.T) {
	tests := []struct {
		mail   string
		result Result
		err    error
	}{
		{"xxx", InvalidSyntax, nil},
		{"<EMAIL>", InvalidDomain, nil},
		{"<EMAIL>", InvalidDomain, nil},
		{"<EMAIL>", Disposable, nil},
	}

	for _, test := range tests {
		t.Run(test.mail, func(t *testing.T) {
			start := time.Now()
			result, err := Check("<EMAIL>", test.mail)
			assert.Equal(t, test.result, result)
			assert.Equal(t, test.err, err)
			fmt.Printf("check for %30v: %-15v => %-10v (%v)\n", test.mail, time.Since(start), test.result.Result, test.result.ResultDetail)
		})
	}
}

func Test_checkMailbox(t *testing.T) {
	tests := []struct {
		stopAt      smtpd.Command
		result      Result
		expectError bool
	}{
		{smtpd.QUIT, Valid, false},
		{smtpd.RCPTTO, MailboxUnavailable, false},
		{smtpd.MAILFROM, MailserverError, true},
		{smtpd.HELO, MailserverError, true},
	}

	for _, test := range tests {
		t.Run(fmt.Sprintf("stop at: %v", test.stopAt), func(t *testing.T) {
			dummyServer := NewDummySMTPServer("localhost:2525", test.stopAt, 0)
			defer dummyServer.Close()
			result, err := checkMailbox(noContext,"<EMAIL>", "<EMAIL>", []*net.MX{{Host: "localhost"}}, 2525)
			assert.Equal(t, test.result, result)
			if test.expectError {
				assert.Error(t, err)
			} else {
				assert.NoError(t, err)
			}
		})
	}
}

func Test_checkMailbox_NetworkError(t *testing.T) {
	result, err := checkMailbox(noContext,"<EMAIL>", "<EMAIL>", []*net.MX{{Host: "localhost"}}, 6666)
	assert.Equal(t, NetworkError, result)
	assert.Error(t, err)
}

func Test_checkMailboxContext(t *testing.T) {
	deltas := []struct{
		delayTime      time.Duration
		contextTime    time.Duration
		expectedResult Result
	}{
		{ 0, 0, TimeoutError },
		{ 0, time.Second, Valid },
		{ time.Millisecond * 1500, 200 * time.Millisecond, TimeoutError },
	}
	for _,d := range deltas {
		t.Run(fmt.Sprintf("context time %v delay %v expected %v", d.contextTime,d.delayTime,d.expectedResult.Result), func(t *testing.T) {
			dummyServer := NewDummySMTPServer("localhost:2528", smtpd.QUIT,d.delayTime)
			tt := time.Now()
			ctx, cancel := context.WithTimeout(context.Background(), d.contextTime)
			result,err := checkMailbox(ctx, "<EMAIL>", "<EMAIL>", []*net.MX{{Host: "127.0.0.1"}}, 2528)
			if d.expectedResult == Valid {
				assert.NoError(t,err)
			} else {
				assert.Error(t,err)
			}
			assert.Equal(t,d.expectedResult, result)
			// confirm that we completed within requested time
			// add 10ms of wiggle room
			assert.WithinDuration(t, time.Now(), tt, d.contextTime + 10 * time.Millisecond)
			dummyServer.Close()
			cancel()
		})
{'fraction_non_alphanumeric': 0.17947509036617948, 'fraction_numerical': 0.014772905862014773, 'mean_word_length': 3.8543096872616323, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3569425', 'n_tokens_mistral': 2650, 'n_tokens_neox': 2373, 'n_words': 496}