github.com/kaydxh/golang@v0.0.131/go/net/resolver/resolver_test.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package resolver_test
    23  
    24  import (
    25  	"fmt"
    26  	"testing"
    27  
    28  	"context"
    29  
    30  	"github.com/kaydxh/golang/go/net/resolver"
    31  	resolver_ "github.com/kaydxh/golang/go/net/resolver"
    32  	_ "github.com/kaydxh/golang/go/net/resolver/dns"
    33  	_ "github.com/kaydxh/golang/go/net/resolver/passthrough"
    34  	resolve_ "github.com/kaydxh/golang/go/net/resolver/resolve"
    35  )
    36  
    37  func TestResolveOne(t *testing.T) {
    38  	testCases := []struct {
    39  		target   string
    40  		iptype   resolver.Resolver_IPType
    41  		expected string
    42  	}{
    43  		{
    44  			target:   "dns:///www.baidu.com:10000",
    45  			iptype:   resolver.Resolver_ip_type_v4,
    46  			expected: "",
    47  		},
    48  		{
    49  			target:   "dns:///www.google.com",
    50  			iptype:   resolver.Resolver_ip_type_v4,
    51  			expected: "",
    52  		},
    53  		{
    54  			target:   "dns:///198.121.11.1",
    55  			iptype:   resolver.Resolver_ip_type_v4,
    56  			expected: "",
    57  		},
    58  		{
    59  			target:   "passthrough:///198.121.11.2",
    60  			iptype:   resolver.Resolver_ip_type_v4,
    61  			expected: "",
    62  		},
    63  		{
    64  			target:   "198.121.11.2",
    65  			iptype:   resolver.Resolver_ip_type_v4,
    66  			expected: "",
    67  		},
    68  	}
    69  
    70  	for i, testCase := range testCases {
    71  		t.Run(fmt.Sprintf("%d-test", i), func(t *testing.T) {
    72  			addr, err := resolve_.ResolveOne(context.Background(), testCase.target, resolver_.WithPickMode(resolver_.Resolver_pick_mode_first))
    73  			if err != nil {
    74  				t.Fatalf("failed to resolve target: %v, err: %v", testCase.target, err)
    75  			}
    76  			t.Logf("resolve one addr %v for target %v", addr, testCase.target)
    77  		})
    78  	}
    79  }
    80  
    81  func TestResolveAll(t *testing.T) {
    82  	testCases := []struct {
    83  		target   string
    84  		iptype   resolver.Resolver_IPType
    85  		expected string
    86  	}{
    87  		{
    88  			target:   "dns:///www.baidu.com",
    89  			iptype:   resolver.Resolver_ip_type_v4,
    90  			expected: "",
    91  		},
    92  		{
    93  			target:   "dns:///www.google.com",
    94  			iptype:   resolver.Resolver_ip_type_v4,
    95  			expected: "",
    96  		},
    97  	}
    98  
    99  	for i, testCase := range testCases {
   100  		t.Run(fmt.Sprintf("%d-test", i), func(t *testing.T) {
   101  			addrs, err := resolve_.ResolveAll(context.Background(), testCase.target, resolver.WithIPTypeForResolveAll(testCase.iptype))
   102  			if err != nil {
   103  				t.Fatalf("failed to resolve target: %v, err: %v", testCase.target, err)
   104  			}
   105  			t.Logf("resolve all addrs %v for target %v", addrs, testCase.target)
   106  		})
   107  	}
   108  }