istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/hbone/dialer_test.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package hbone
    16  
    17  import (
    18  	"net"
    19  	"testing"
    20  	"time"
    21  )
    22  
    23  func newTCPServer(t testing.TB, data string) string {
    24  	n, err := net.Listen("tcp", "127.0.0.1:0")
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  	t.Logf("opened listener on %v", n.Addr().String())
    29  	go func() {
    30  		for {
    31  			c, err := n.Accept()
    32  			if err != nil {
    33  				log.Info(err)
    34  				return
    35  			}
    36  			log.Info("accepted connection")
    37  			c.Write([]byte(data))
    38  			c.Close()
    39  		}
    40  	}()
    41  	t.Cleanup(func() {
    42  		n.Close()
    43  	})
    44  	return n.Addr().String()
    45  }
    46  
    47  func TestDialerError(t *testing.T) {
    48  	timeout := 500 * time.Millisecond
    49  	d := NewDialer(Config{
    50  		ProxyAddress: "127.0.0.10:1", // Random address that should fail to dial
    51  		Headers: map[string][]string{
    52  			"some-addition-metadata": {"test-value"},
    53  		},
    54  		TLS:     nil, // No TLS for simplification
    55  		Timeout: &timeout,
    56  	})
    57  
    58  	_, err := d.Dial("tcp", "fake")
    59  	if err == nil {
    60  		t.Fatal("expected error, got none.")
    61  	}
    62  }
    63  
    64  func TestDialer(t *testing.T) {
    65  	timeout := 500 * time.Millisecond
    66  	testAddr := newTCPServer(t, "hello")
    67  	proxy := newHBONEServer(t)
    68  	d := NewDialer(Config{
    69  		ProxyAddress: proxy,
    70  		Headers: map[string][]string{
    71  			"some-addition-metadata": {"test-value"},
    72  		},
    73  		TLS:     nil, // No TLS for simplification
    74  		Timeout: &timeout,
    75  	})
    76  	send := func() {
    77  		client, err := d.Dial("tcp", testAddr)
    78  		if err != nil {
    79  			t.Fatal(err)
    80  		}
    81  		defer client.Close()
    82  
    83  		go func() {
    84  			n, err := client.Write([]byte("hello world"))
    85  			log.Infof("wrote %v/%v", n, err)
    86  		}()
    87  
    88  		buf := make([]byte, 8)
    89  		n, err := client.Read(buf)
    90  		if err != nil {
    91  			t.Fatalf("err with %v: %v", n, err)
    92  		}
    93  		if string(buf[:n]) != "hello" {
    94  			t.Fatalf("got unexpected buffer: %v", string(buf[:n]))
    95  		}
    96  		t.Logf("Read %v", string(buf[:n]))
    97  	}
    98  	// Make sure we can create multiple connections
    99  	send()
   100  	send()
   101  }
   102  
   103  func newHBONEServer(t *testing.T) string {
   104  	s := NewServer()
   105  	l, err := net.Listen("tcp", "0.0.0.0:0")
   106  	if err != nil {
   107  		t.Fatal(err)
   108  	}
   109  	go func() {
   110  		_ = s.Serve(l)
   111  	}()
   112  	t.Cleanup(func() {
   113  		_ = l.Close()
   114  	})
   115  	return l.Addr().String()
   116  }