google.golang.org/grpc@v1.72.2/credentials/alts/internal/handshaker/service/service_test.go (about)

     1  /*
     2   *
     3   * Copyright 2018 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  package service
    20  
    21  import (
    22  	"testing"
    23  
    24  	"google.golang.org/grpc/internal/grpctest"
    25  )
    26  
    27  type s struct {
    28  	grpctest.Tester
    29  }
    30  
    31  func Test(t *testing.T) {
    32  	grpctest.RunSubTests(t, s{})
    33  }
    34  
    35  const (
    36  	testAddress1 = "some_address_1"
    37  	testAddress2 = "some_address_2"
    38  )
    39  
    40  // TestDial verifies the behaviour of alts handshake when there are multiple Dials.
    41  // If a connection has already been established, this function returns it.
    42  // Otherwise, a new connection is created.
    43  func (s) TestDial(t *testing.T) {
    44  	// First call to Dial, it should create a connection to the server running
    45  	// at the given address.
    46  	conn1, err := Dial(testAddress1)
    47  	if err != nil {
    48  		t.Fatalf("first call to Dial(%v) failed: %v", testAddress1, err)
    49  	}
    50  	defer conn1.Close()
    51  	if got, want := hsConnMap[testAddress1], conn1; got != want {
    52  		t.Fatalf("hsConnMap[%v]=%v, want %v", testAddress1, got, want)
    53  	}
    54  
    55  	// Second call to Dial should return conn1 above.
    56  	conn2, err := Dial(testAddress1)
    57  	if err != nil {
    58  		t.Fatalf("second call to Dial(%v) failed: %v", testAddress1, err)
    59  	}
    60  	defer conn2.Close()
    61  	if got, want := conn2, conn1; got != want {
    62  		t.Fatalf("second call to Dial(%v)=(%v, _), want (%v,. _)", testAddress1, got, want)
    63  	}
    64  	if got, want := hsConnMap[testAddress1], conn1; got != want {
    65  		t.Fatalf("hsConnMap[%v]=%v, want %v", testAddress1, got, want)
    66  	}
    67  
    68  	// Third call to Dial using a different address should create a new connection.
    69  	conn3, err := Dial(testAddress2)
    70  	if err != nil {
    71  		t.Fatalf("third call to Dial(%v) failed: %v", testAddress2, err)
    72  	}
    73  	defer conn3.Close()
    74  	if got, want := hsConnMap[testAddress2], conn3; got != want {
    75  		t.Fatalf("hsConnMap[%v]=%v, want %v", testAddress2, got, want)
    76  	}
    77  	if got, want := conn2 == conn3, false; got != want {
    78  		t.Fatalf("(conn2==conn3)=%v, want %v", got, want)
    79  	}
    80  }