2014-07-29 18:23:14 +00:00
|
|
|
/*
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
Copyright 2014 The Perkeep Authors
|
2014-07-29 18:23:14 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package netutil
|
|
|
|
|
|
|
|
import (
|
2014-09-15 13:15:59 +00:00
|
|
|
"net"
|
|
|
|
"strconv"
|
2014-07-29 18:23:14 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHostPort(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
baseURL string
|
|
|
|
wantNetAddr string
|
|
|
|
}{
|
|
|
|
// IPv4, no prefix
|
|
|
|
{
|
|
|
|
baseURL: "http://foo.com/",
|
|
|
|
wantNetAddr: "foo.com:80",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
baseURL: "https://foo.com/",
|
|
|
|
wantNetAddr: "foo.com:443",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
baseURL: "http://foo.com:8080/",
|
|
|
|
wantNetAddr: "foo.com:8080",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
baseURL: "https://foo.com:8080/",
|
|
|
|
wantNetAddr: "foo.com:8080",
|
|
|
|
},
|
|
|
|
|
|
|
|
// IPv4, with prefix
|
|
|
|
{
|
|
|
|
baseURL: "http://foo.com/pics/",
|
|
|
|
wantNetAddr: "foo.com:80",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
baseURL: "https://foo.com/pics/",
|
|
|
|
wantNetAddr: "foo.com:443",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
baseURL: "http://foo.com:8080/pics/",
|
|
|
|
wantNetAddr: "foo.com:8080",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
baseURL: "https://foo.com:8080/pics/",
|
|
|
|
wantNetAddr: "foo.com:8080",
|
|
|
|
},
|
|
|
|
|
|
|
|
// IPv6, no prefix
|
|
|
|
{
|
|
|
|
baseURL: "http://[::1]/",
|
|
|
|
wantNetAddr: "[::1]:80",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
baseURL: "https://[::1]/",
|
|
|
|
wantNetAddr: "[::1]:443",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
baseURL: "http://[::1]:8080/",
|
|
|
|
wantNetAddr: "[::1]:8080",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
baseURL: "https://[::1]:8080/",
|
|
|
|
wantNetAddr: "[::1]:8080",
|
|
|
|
},
|
|
|
|
|
|
|
|
// IPv6, with prefix
|
|
|
|
{
|
|
|
|
baseURL: "http://[::1]/pics/",
|
|
|
|
wantNetAddr: "[::1]:80",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
baseURL: "https://[::1]/pics/",
|
|
|
|
wantNetAddr: "[::1]:443",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
baseURL: "http://[::1]:8080/pics/",
|
|
|
|
wantNetAddr: "[::1]:8080",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
baseURL: "https://[::1]:8080/pics/",
|
|
|
|
wantNetAddr: "[::1]:8080",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, v := range tests {
|
|
|
|
got, err := HostPort(v.baseURL)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if got != v.wantNetAddr {
|
|
|
|
t.Errorf("got: %v for %v, want: %v", got, v.baseURL, v.wantNetAddr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-15 13:15:59 +00:00
|
|
|
|
2015-12-26 21:32:49 +00:00
|
|
|
func TestListenHostPort(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
in string
|
|
|
|
want string // or "ERR:"
|
|
|
|
}{
|
|
|
|
{":80", "localhost:80"},
|
|
|
|
{"0.0.0.0:80", "localhost:80"},
|
|
|
|
{"foo:80", "foo:80"},
|
|
|
|
{"foo:0", "foo:0"},
|
|
|
|
{"", "ERR"},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
got, err := ListenHostPort(tt.in)
|
|
|
|
if err != nil {
|
|
|
|
got = "ERR"
|
|
|
|
}
|
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("ListenHostPort(%q) = %q, %v; want %q", tt.in, got, err, tt.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-09-15 13:15:59 +00:00
|
|
|
func testLocalhostResolver(t *testing.T, resolve func() net.IP) {
|
|
|
|
ip := resolve()
|
|
|
|
if ip == nil {
|
|
|
|
t.Fatal("no ip found.")
|
|
|
|
}
|
|
|
|
if !ip.IsLoopback() {
|
|
|
|
t.Errorf("expected a loopback address: %s", ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-05 16:45:17 +00:00
|
|
|
func TestLoopbackIP(t *testing.T) {
|
2014-09-15 13:15:59 +00:00
|
|
|
testLocalhostResolver(t, loopbackIP)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocalhost(t *testing.T) {
|
|
|
|
_, err := Localhost()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListenOnLocalRandomPort(t *testing.T) {
|
|
|
|
l, err := ListenOnLocalRandomPort()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error %v", err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
_, port, err := net.SplitHostPort(l.Addr().String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if p, _ := strconv.Atoi(port); p < 1 {
|
|
|
|
t.Fatalf("expected port(%d) to be > 0", p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLocalhostLookup(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if ip := localhostLookup(); ip == nil {
|
|
|
|
b.Fatal("no ip found.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLoopbackIP(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if ip := loopbackIP(); ip == nil {
|
|
|
|
b.Fatal("no ip found.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|