...

Source file src/github.com/google/s2a-go/fallback/s2a_fallback_test.go

Documentation: github.com/google/s2a-go/fallback

     1  /*
     2   *
     3   * Copyright 2023 Google LLC
     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   *     https://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 fallback
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"net"
    25  	"reflect"
    26  	"testing"
    27  )
    28  
    29  func TestDefaultFallbackClientHandshakeFunc(t *testing.T) {
    30  	testDialContextFunc := func(context.Context, string, string) (net.Conn, error) {
    31  		return nil, fmt.Errorf("testDialError")
    32  	}
    33  	for _, tc := range []struct {
    34  		desc              string
    35  		inputFallbackAddr string
    36  		funcInitialized   bool
    37  		expectedAddr      string
    38  		expectNilErr      bool
    39  	}{
    40  		{
    41  			"working case, fallback address has port suffix",
    42  			"example.com:443",
    43  			true,
    44  			"example.com:443",
    45  			true,
    46  		},
    47  		{
    48  			"working case, fallback address has no port suffix",
    49  			"example.com",
    50  			true,
    51  			"example.com:443",
    52  
    53  			true,
    54  		},
    55  		{
    56  			"working case, IP address, with port",
    57  			"192.168.1.1:443",
    58  			true,
    59  			"192.168.1.1:443",
    60  
    61  			true,
    62  		},
    63  		{
    64  			"working case, IP address, no port",
    65  			"192.168.1.1",
    66  			true,
    67  			"192.168.1.1:443",
    68  
    69  			true,
    70  		},
    71  		{
    72  			"working case, IPv6 address, with port",
    73  			"[2001:db8::1]:443",
    74  			true,
    75  			"[2001:db8::1]:443",
    76  			true,
    77  		},
    78  		{
    79  			"working case, IPv6 address, no port",
    80  			"2001:db8::1",
    81  			true,
    82  			"[2001:db8::1]:443",
    83  			true,
    84  		},
    85  		{
    86  			"test empty fallback address",
    87  			"",
    88  			false,
    89  			"",
    90  			false,
    91  		},
    92  	} {
    93  		fbFunc, err := defaultFallbackClientHandshakeFuncInternal(tc.inputFallbackAddr, testDialContextFunc)
    94  		if got, want := fbFunc != nil, tc.funcInitialized; got != want {
    95  			t.Errorf("%v: fallback handshake func is initialized=[%v], want [%v]", tc.desc, got, want)
    96  		}
    97  		if got, want := err == nil, tc.expectNilErr; got != want {
    98  			t.Errorf("%v: got error [%v], want nil error [%v]", tc.desc, err, want)
    99  		}
   100  		if err == nil {
   101  			_, _, err := fbFunc(context.TODO(), "", nil, fmt.Errorf("testS2AError"))
   102  			if err == nil {
   103  				t.Errorf("%v: expecting an error from the test dial function, got nil instead", tc.desc)
   104  			}
   105  			expectedErr := fmt.Sprintf("dialing to fallback server %s failed: testDialError; S2A client handshake with  error: testS2AError", tc.expectedAddr)
   106  			if got, want := err.Error(), expectedErr; got != want {
   107  				t.Errorf("%v: fallback handshake got error [%v], want error [%v]", tc.desc, got, want)
   108  			}
   109  		}
   110  	}
   111  }
   112  func TestDefaultFallbackDialerAndAddress(t *testing.T) {
   113  	for _, tc := range []struct {
   114  		desc              string
   115  		inputFallbackAddr string
   116  		dialerInitialized bool
   117  		expectedAddr      string
   118  		expectNilErr      bool
   119  	}{
   120  		{
   121  			"working case, fallback address has port suffix",
   122  			"example.com:443",
   123  			true,
   124  			"example.com:443",
   125  			true,
   126  		},
   127  		{
   128  			"working case, fallback address has no port suffix",
   129  			"example.com",
   130  			true,
   131  			"example.com:443",
   132  			true,
   133  		},
   134  		{
   135  			"working case, IP address, with port",
   136  			"192.168.1.1:443",
   137  			true,
   138  			"192.168.1.1:443",
   139  			true,
   140  		},
   141  		{
   142  			"working case, IP address, no port",
   143  			"192.168.1.1",
   144  			true,
   145  			"192.168.1.1:443",
   146  			true,
   147  		},
   148  		{
   149  			"working case, IPv6 address, with port",
   150  			"[2001:db8::1]:443",
   151  			true,
   152  			"[2001:db8::1]:443",
   153  			true,
   154  		},
   155  		{
   156  			"working case, IPv6 address, no port",
   157  			"2001:db8::1",
   158  			true,
   159  			"[2001:db8::1]:443",
   160  			true,
   161  		},
   162  		{
   163  			"test empty fallback address",
   164  			"",
   165  			false,
   166  			"",
   167  			false,
   168  		},
   169  	} {
   170  		fbDialer, fbAddr, err := DefaultFallbackDialerAndAddress(tc.inputFallbackAddr)
   171  		if got, want := fbDialer != nil, tc.dialerInitialized; got != want {
   172  			t.Errorf("%v: fallback dialer is initialized=[%v], want [%v]", tc.desc, got, want)
   173  		}
   174  		if got, want := fbAddr, tc.expectedAddr; got != want {
   175  			t.Errorf("%v: returned fallback address=[%v], want [%v]", tc.desc, got, want)
   176  		}
   177  		if got, want := err == nil, tc.expectNilErr; got != want {
   178  			t.Errorf("%v: got error [%v], want nil error [%v]", tc.desc, err, want)
   179  		}
   180  		if err == nil {
   181  			if !reflect.DeepEqual(fbDialer.Config, &FallbackTLSConfigHTTP) {
   182  				t.Errorf("%v: unexpected tls config from fallback dialer: [%v], expected: [%v]", tc.desc, fbDialer.Config, &FallbackTLSConfigHTTP)
   183  			}
   184  
   185  		}
   186  	}
   187  }
   188  

View as plain text