gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/net/idna/idna_test.go (about)

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package idna
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  var idnaTestCases = [...]struct {
    12  	ascii, unicode string
    13  }{
    14  	// Labels.
    15  	{"books", "books"},
    16  	{"xn--bcher-kva", "bücher"},
    17  
    18  	// Domains.
    19  	{"foo--xn--bar.org", "foo--xn--bar.org"},
    20  	{"golang.org", "golang.org"},
    21  	{"example.xn--p1ai", "example.рф"},
    22  	{"xn--czrw28b.tw", "商業.tw"},
    23  	{"www.xn--mller-kva.de", "www.müller.de"},
    24  }
    25  
    26  func TestIDNA(t *testing.T) {
    27  	for _, tc := range idnaTestCases {
    28  		if a, err := ToASCII(tc.unicode); err != nil {
    29  			t.Errorf("ToASCII(%q): %v", tc.unicode, err)
    30  		} else if a != tc.ascii {
    31  			t.Errorf("ToASCII(%q): got %q, want %q", tc.unicode, a, tc.ascii)
    32  		}
    33  
    34  		if u, err := ToUnicode(tc.ascii); err != nil {
    35  			t.Errorf("ToUnicode(%q): %v", tc.ascii, err)
    36  		} else if u != tc.unicode {
    37  			t.Errorf("ToUnicode(%q): got %q, want %q", tc.ascii, u, tc.unicode)
    38  		}
    39  	}
    40  }
    41  
    42  func TestIDNASeparators(t *testing.T) {
    43  	type subCase struct {
    44  		unicode   string
    45  		wantASCII string
    46  		wantErr   bool
    47  	}
    48  
    49  	testCases := []struct {
    50  		name     string
    51  		profile  *Profile
    52  		subCases []subCase
    53  	}{
    54  		{
    55  			name: "Punycode", profile: Punycode,
    56  			subCases: []subCase{
    57  				{"example\u3002jp", "xn--examplejp-ck3h", false},
    58  				{"東京\uFF0Ejp", "xn--jp-l92cn98g071o", false},
    59  				{"大阪\uFF61jp", "xn--jp-ku9cz72u463f", false},
    60  			},
    61  		},
    62  		{
    63  			name: "Lookup", profile: Lookup,
    64  			subCases: []subCase{
    65  				{"example\u3002jp", "example.jp", false},
    66  				{"東京\uFF0Ejp", "xn--1lqs71d.jp", false},
    67  				{"大阪\uFF61jp", "xn--pssu33l.jp", false},
    68  			},
    69  		},
    70  		{
    71  			name: "Display", profile: Display,
    72  			subCases: []subCase{
    73  				{"example\u3002jp", "example.jp", false},
    74  				{"東京\uFF0Ejp", "xn--1lqs71d.jp", false},
    75  				{"大阪\uFF61jp", "xn--pssu33l.jp", false},
    76  			},
    77  		},
    78  		{
    79  			name: "Registration", profile: Registration,
    80  			subCases: []subCase{
    81  				{"example\u3002jp", "", true},
    82  				{"東京\uFF0Ejp", "", true},
    83  				{"大阪\uFF61jp", "", true},
    84  			},
    85  		},
    86  	}
    87  	for _, tc := range testCases {
    88  		t.Run(tc.name, func(t *testing.T) {
    89  			for _, c := range tc.subCases {
    90  				gotA, err := tc.profile.ToASCII(c.unicode)
    91  				if c.wantErr {
    92  					if err == nil {
    93  						t.Errorf("ToASCII(%q): got no error, but an error expected", c.unicode)
    94  					}
    95  				} else {
    96  					if err != nil {
    97  						t.Errorf("ToASCII(%q): got err=%v, but no error expected", c.unicode, err)
    98  					} else if gotA != c.wantASCII {
    99  						t.Errorf("ToASCII(%q): got %q, want %q", c.unicode, gotA, c.wantASCII)
   100  					}
   101  				}
   102  			}
   103  		})
   104  	}
   105  }
   106  
   107  // TODO(nigeltao): test errors, once we've specified when ToASCII and ToUnicode
   108  // return errors.