<commit_before>// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package net

import (
	"reflect"
	"runtime"
	"testing"
)

var parseIPTests = []struct {
	in  string
	out IP
}{
	{"127.0.1.2", IPv4(127, 0, 1, 2)},
	{"127.0.0.1", IPv4(127, 0, 0, 1)},
	{"127.001.002.003", IPv4(127, 1, 2, 3)},
	{"::ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
	{"::ffff:127.001.002.003", IPv4(127, 1, 2, 3)},
	{"::ffff:7f01:0203", IPv4(127, 1, 2, 3)},
	{"0:0:0:0:0000:ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
	{"0:0:0:0:000000:ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
	{"fdf8:f53e:61e4::18:127.1.2.3", IPv4(127, 1, 2, 3)},

	{"fc00:db20:35b:7399::5", IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01, 0, 0, 0, 0, 0, 0, 0x00, 0x68}},
	{"fdf8:f53e:61e4::18", IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01, 0, 0, 0, 0, 0, 0, 0x00, 0x68}},

	{"127.0.0.256", nil},
	{"abc", nil},
	{"123:", nil},
	{"fe80::1%lo0", nil},
	{"fe80::1%911", nil},
	{"", nil},
	{"fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b:b2:b3:b4", nil}, // Issue 6628
}

func TestParseIP(t *testing.T) {
	for _, tt := range parseIPTests {
		if out := ParseIP(tt.in); !reflect.DeepEqual(out, tt.out) {
			t.Errorf("ParseIP(%q) = %v, want %v", tt.in, out, tt.out)
		}
		if tt.in == "" {
			// Tested in TestMarshalEmptyIP below.
			continue
		}
		var out IP
		if err := out.UnmarshalText([]byte(tt.in)); !reflect.DeepEqual(out, tt.out) || (tt.out == nil) != (err != nil) {
			t.Errorf("IP.UnmarshalText(%q) = %v, %v, want %v", tt.in, out, err, tt.out)
		}
	}
}

func TestLookupWithIP(t *testing.T) {
	_, err := LookupIP("")
	if err == nil {
		t.Errorf(`LookupIP("") succeeded, should fail`)
	}
	_, err = LookupHost("")
	if err == nil {
		t.Errorf(`LookupIP("") succeeded, should fail`)
	}

	// Test that LookupHost and LookupIP, which normally
	// expect host names, work with IP addresses.
	for _, tt := range parseIPTests {
		addrs, err := LookupHost(tt.in)
		if tt.out != nil {
			if len(addrs) != 1 || addrs[0] != tt.in || err != nil {
				t.Errorf("LookupHost(%q) = %v, %v, want %v, nil", tt.in, addrs, err, []string{tt.in})
			}
		} else {
			// We can't control what the host resolver does; if it can resolve, say,
			// 127.0.0.256 or fe80::1%911 or a host named 'abc', who are we to judge?
			// Warn about these discrepancies but don't fail the test.
			if err == nil {
				t.Logf("warning: LookupHost(%q) = %v, want error", tt.in, addrs)
			}
		}

		ips, err := LookupIP(tt.in)
		if tt.out != nil {
			if len(ips) != 1 || !reflect.DeepEqual(ips[0], tt.out) || err != nil {
				t.Errorf("LookupIP(%q) = %v, %v, want %v, nil", tt.in, ips, err, []IP{tt.out})
			}
		} else {
			// We can't control what the host resolver does. See above.
			if err == nil {
				t.Logf("warning: LookupIP(%q) = %v, want error", tt.in, ips)
			}
		}
	}
}

func BenchmarkParseIP(b *testing.B) {
	testHookUninstaller.Do(uninstallTestHooks)

	for i := 0; i < b.N; i++ {
		for _, tt := range parseIPTests {<commit_msg>net: do not look up abc by default<commit_after>// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package net

import (
	"reflect"
	"runtime"
	"testing"
)

var parseIPTests = []struct {
	in  string
	out IP
}{
	{"127.0.1.2", IPv4(127, 0, 1, 2)},
	{"127.0.0.1", IPv4(127, 0, 0, 1)},
	{"127.001.002.003", IPv4(127, 1, 2, 3)},
	{"::ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
	{"::ffff:127.001.002.003", IPv4(127, 1, 2, 3)},
	{"::ffff:7f01:0203", IPv4(127, 1, 2, 3)},
	{"0:0:0:0:0000:ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
	{"0:0:0:0:000000:ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
	{"fdf8:f53e:61e4::18:127.1.2.3", IPv4(127, 1, 2, 3)},

	{"fc00:db20:35b:7399::5", IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01, 0, 0, 0, 0, 0, 0, 0x00, 0x68}},
	{"fdf8:f53e:61e4::18", IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01, 0, 0, 0, 0, 0, 0, 0x00, 0x68}},

	{"127.0.0.256", nil},
	{"abc", nil},
	{"123:", nil},
	{"fe80::1%lo0", nil},
	{"fe80::1%911", nil},
	{"", nil},
	{"fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b:b2:b3:b4", nil}, // Issue 6628
}

func TestParseIP(t *testing.T) {
	for _, tt := range parseIPTests {
		if out := ParseIP(tt.in); !reflect.DeepEqual(out, tt.out) {
			t.Errorf("ParseIP(%q) = %v, want %v", tt.in, out, tt.out)
		}
		if tt.in == "" {
			// Tested in TestMarshalEmptyIP below.
			continue
		}
		var out IP
		if err := out.UnmarshalText([]byte(tt.in)); !reflect.DeepEqual(out, tt.out) || (tt.out == nil) != (err != nil) {
			t.Errorf("IP.UnmarshalText(%q) = %v, %v, want %v", tt.in, out, err, tt.out)
		}
	}
}

func TestLookupWithIP(t *testing.T) {
	_, err := LookupIP("")
	if err == nil {
		t.Errorf(`LookupIP("") succeeded, should fail`)
	}
	_, err = LookupHost("")
	if err == nil {
		t.Errorf(`LookupIP("") succeeded, should fail`)
	}

	// Test that LookupHost and LookupIP, which normally
	// expect host names, work with IP addresses.
	for _, tt := range parseIPTests {
		if tt.out != nil {
			addrs, err := LookupHost(tt.in)
			if len(addrs) != 1 || addrs[0] != tt.in || err != nil {
				t.Errorf("LookupHost(%q) = %v, %v, want %v, nil", tt.in, addrs, err, []string{tt.in})
			}
		} else if !testing.Short() {
			// We can't control what the host resolver does; if it can resolve, say,
			// 127.0.0.256 or fe80::1%911 or a host named 'abc', who are we to judge?
			// Warn about these discrepancies but don't fail the test.
			addrs, err := LookupHost(tt.in)
			if err == nil {
				t.Logf("warning: LookupHost(%q) = %v, want error", tt.in, addrs)
			}
		}

		if tt.out != nil {
			ips, err := LookupIP(tt.in)
			if len(ips) != 1 || !reflect.DeepEqual(ips[0], tt.out) || err != nil {
				t.Errorf("LookupIP(%q) = %v, %v, want %v, nil", tt.in, ips, err, []IP{tt.out})
			}
		} else if !testing.Short() {
			ips, err := LookupIP(tt.in)
			// We can't control what the host resolver does. See above.
			if err == nil {
				t.Logf("warning: LookupIP(%q) = %v, want error", tt.in, ips)
			}
		}
	}
}

func BenchmarkParseIP(b *testing.B) {
	testHookUninstaller.Do(uninstallTestHooks)

	for i := 0; i < b.N; i++ {
		for _, tt := range parseIPTests {
{'fraction_non_alphanumeric': 0.21695518524510596, 'fraction_numerical': 0.0999838213881249, 'mean_word_length': 3.3535211267605636, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 20, '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': '1412819', 'n_tokens_mistral': 3234, 'n_tokens_neox': 2710, 'n_words': 713}