github.com/puellanivis/breton@v0.2.16/lib/files/socketfiles/tcp_test.go (about)

     1  package socketfiles
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  	"net/url"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  func TestTCPName(t *testing.T) {
    12  	sock := &socket{
    13  		qaddr: &net.TCPAddr{
    14  			IP:   []byte{127, 0, 0, 1},
    15  			Port: 65535,
    16  		},
    17  		addr: &net.TCPAddr{
    18  			IP:   []byte{127, 0, 0, 2},
    19  			Port: 80,
    20  		},
    21  
    22  		packetSize: 188, // should not show up
    23  		bufferSize: 1024,
    24  
    25  		ttl: 100,
    26  		tos: 0x80,
    27  
    28  		throttler: throttler{
    29  			bitrate: 2048,
    30  		},
    31  	}
    32  
    33  	uri := sock.uri()
    34  	expected := "tcp://127.0.0.2:80?buffer_size=1024&localaddr=127.0.0.1&localport=65535&max_bitrate=2048&tos=0x80&ttl=100"
    35  
    36  	if s := uri.String(); s != expected {
    37  		t.Errorf("got a bad URI, was expecting, but got:\n\t%v\n\t%v", expected, s)
    38  	}
    39  
    40  	sock = &socket{
    41  		qaddr: &net.TCPAddr{
    42  			IP:   []byte{127, 0, 0, 1},
    43  			Port: 65534,
    44  		},
    45  		addr: &net.TCPAddr{
    46  			IP:   []byte{127, 0, 0, 2},
    47  			Port: 443,
    48  		},
    49  	}
    50  
    51  	uri = sock.uri()
    52  	expected = "tcp://127.0.0.2:443?localaddr=127.0.0.1&localport=65534"
    53  
    54  	if s := uri.String(); s != expected {
    55  		t.Errorf("got a bad URI, was expecting, but got:\n\t%v\n\t%v", expected, s)
    56  	}
    57  
    58  	sock = &socket{
    59  		addr: &net.TCPAddr{
    60  			IP:   []byte{127, 0, 0, 2},
    61  			Port: 8080,
    62  		},
    63  	}
    64  
    65  	uri = sock.uri()
    66  	expected = "tcp://127.0.0.2:8080"
    67  
    68  	if s := uri.String(); s != expected {
    69  		t.Errorf("got a bad URI, was expecting, but got:\n\t%v\n\t%v", expected, s)
    70  	}
    71  }
    72  
    73  func TestTCPNoSlashSlash(t *testing.T) {
    74  	uri, err := url.Parse("tcp:0.0.0.0:0")
    75  	if err != nil {
    76  		t.Fatal("unexpected error parsing constant URL")
    77  	}
    78  
    79  	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
    80  
    81  	r, err := (&tcpHandler{}).Open(ctx, uri)
    82  	if err == nil {
    83  		r.Close()
    84  		t.Fatal("expected Open(\"tcp:0.0.0.0:0\") to error, it did not")
    85  	}
    86  	cancel()
    87  
    88  	ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second)
    89  
    90  	w, err := (&tcpHandler{}).Create(ctx, uri)
    91  	if err == nil {
    92  		w.Close()
    93  		t.Fatal("expected Create(\"tcp:0.0.0.0:0\") to error, it did not")
    94  	}
    95  	cancel()
    96  }
    97  
    98  func TestTCPEmptyURL(t *testing.T) {
    99  	uri, err := url.Parse("tcp:")
   100  	if err != nil {
   101  		t.Fatal("unexpected error parsing constant URL")
   102  	}
   103  
   104  	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
   105  
   106  	r, err := (&tcpHandler{}).Open(ctx, uri)
   107  	if err == nil {
   108  		r.Close()
   109  		t.Fatal("expected Open(\"tcp:\") to error, it did not")
   110  	}
   111  	cancel()
   112  
   113  	ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second)
   114  
   115  	w, err := (&tcpHandler{}).Create(ctx, uri)
   116  	if err == nil {
   117  		w.Close()
   118  		t.Fatal("expected Create(\"tcp:\") to error, it did not")
   119  	}
   120  	cancel()
   121  }
   122  
   123  func TestTCPBadLocalAddr(t *testing.T) {
   124  	uri, err := url.Parse("tcp://0.0.0.0:0?localaddr=invalid")
   125  	if err != nil {
   126  		t.Fatal("unexpected error parsing constant URL")
   127  	}
   128  
   129  	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
   130  
   131  	w, err := (&tcpHandler{}).Create(ctx, uri)
   132  	if err == nil {
   133  		w.Close()
   134  		t.Fatal("exepcted Create(\"tcp://0.0.0.0:0?localaddr=invalid\") to error, it did not")
   135  	}
   136  	cancel()
   137  }