Source file src/cmd/trace/main_test.go

     1  // Copyright 2026 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 main
     6  
     7  import "testing"
     8  
     9  func TestListenAddr(t *testing.T) {
    10  	tests := []struct {
    11  		name     string
    12  		addr     string
    13  		wantAddr string
    14  		wantErr  bool
    15  	}{
    16  		{
    17  			name:     "empty host",
    18  			addr:     ":8080",
    19  			wantAddr: "localhost:8080",
    20  		},
    21  		{
    22  			name:     "with host",
    23  			addr:     "localhost:8080",
    24  			wantAddr: "localhost:8080",
    25  		},
    26  		{
    27  			name:     "with IP",
    28  			addr:     "127.0.0.1:8080",
    29  			wantAddr: "127.0.0.1:8080",
    30  		},
    31  		{
    32  			name:     "unspecified host",
    33  			addr:     "0.0.0.0:8080",
    34  			wantAddr: "0.0.0.0:8080",
    35  		},
    36  		{
    37  			name:    "host only",
    38  			addr:    "127.0.0.1",
    39  			wantErr: true,
    40  		},
    41  		{
    42  			name:    "port only",
    43  			addr:    "8080",
    44  			wantErr: true,
    45  		},
    46  	}
    47  
    48  	for _, tt := range tests {
    49  		t.Run(tt.name, func(t *testing.T) {
    50  			got, err := listenAddr(tt.addr)
    51  			if tt.wantErr && err == nil {
    52  				t.Errorf("listenAddr(%q) got nil err want non-nil", tt.addr)
    53  			} else if !tt.wantErr && err != nil {
    54  				t.Errorf("listenAddr(%q) got err %v want nil", tt.addr, err)
    55  			} else if got != tt.wantAddr {
    56  				t.Errorf("listenAddr(%q) = %q, want %q", tt.addr, got, tt.wantAddr)
    57  			}
    58  		})
    59  	}
    60  }
    61  
    62  func TestAddrURL(t *testing.T) {
    63  	tests := []struct {
    64  		name           string
    65  		addr           string
    66  		wantURL        string
    67  		wantSimplified bool
    68  	}{
    69  		{
    70  			name:           "empty host",
    71  			addr:           ":8080",
    72  			wantURL:        "http://localhost:8080",
    73  			wantSimplified: true,
    74  		},
    75  		{
    76  			name:           "with host",
    77  			addr:           "localhost:8080",
    78  			wantURL:        "http://localhost:8080",
    79  			wantSimplified: false,
    80  		},
    81  		{
    82  			name:           "with ip",
    83  			addr:           "10.10.10.10:8080",
    84  			wantURL:        "http://10.10.10.10:8080",
    85  			wantSimplified: false,
    86  		},
    87  		{
    88  			name:           "unspecified ipv4",
    89  			addr:           "0.0.0.0:8080",
    90  			wantURL:        "http://localhost:8080",
    91  			wantSimplified: true,
    92  		},
    93  		{
    94  			name:           "unspecified ipv6",
    95  			addr:           "[::]:8080",
    96  			wantURL:        "http://localhost:8080",
    97  			wantSimplified: true,
    98  		},
    99  	}
   100  
   101  	for _, tt := range tests {
   102  		t.Run(tt.name, func(t *testing.T) {
   103  			gotURL, gotSimplified, err := addrURL(tt.addr)
   104  			if err != nil {
   105  				t.Fatalf("addrURL(%q) got err %v want nil", tt.addr, err)
   106  			}
   107  			if gotURL != tt.wantURL {
   108  				t.Errorf("addrURL(%q) = %q, want %q", tt.addr, gotURL, tt.wantURL)
   109  			}
   110  			if gotSimplified != tt.wantSimplified {
   111  				t.Errorf("addrURL(%q) simplified = %v, want %v", tt.addr, gotSimplified, tt.wantSimplified)
   112  			}
   113  		})
   114  	}
   115  }
   116  

View as plain text