github.com/slayercat/go@v0.0.0-20170428012452-c51559813f61/src/net/port_test.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package net
     6  
     7  import "testing"
     8  
     9  var parsePortTests = []struct {
    10  	service     string
    11  	port        int
    12  	needsLookup bool
    13  }{
    14  	{"", 0, false},
    15  
    16  	// Decimal number literals
    17  	{"-1073741825", -1 << 30, false},
    18  	{"-1073741824", -1 << 30, false},
    19  	{"-1073741823", -(1<<30 - 1), false},
    20  	{"-123456789", -123456789, false},
    21  	{"-1", -1, false},
    22  	{"-0", 0, false},
    23  	{"0", 0, false},
    24  	{"+0", 0, false},
    25  	{"+1", 1, false},
    26  	{"65535", 65535, false},
    27  	{"65536", 65536, false},
    28  	{"123456789", 123456789, false},
    29  	{"1073741822", 1<<30 - 2, false},
    30  	{"1073741823", 1<<30 - 1, false},
    31  	{"1073741824", 1<<30 - 1, false},
    32  	{"1073741825", 1<<30 - 1, false},
    33  
    34  	// Others
    35  	{"abc", 0, true},
    36  	{"9pfs", 0, true},
    37  	{"123badport", 0, true},
    38  	{"bad123port", 0, true},
    39  	{"badport123", 0, true},
    40  	{"123456789badport", 0, true},
    41  	{"-2147483649badport", 0, true},
    42  	{"2147483649badport", 0, true},
    43  }
    44  
    45  func TestParsePort(t *testing.T) {
    46  	// The following test cases are cribbed from the strconv
    47  	for _, tt := range parsePortTests {
    48  		if port, needsLookup := parsePort(tt.service); port != tt.port || needsLookup != tt.needsLookup {
    49  			t.Errorf("parsePort(%q) = %d, %t; want %d, %t", tt.service, port, needsLookup, tt.port, tt.needsLookup)
    50  		}
    51  	}
    52  }