...

Source file src/crypto/x509/parser_test.go

Documentation: crypto/x509

     1  // Copyright 2021 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 x509
     6  
     7  import (
     8  	"encoding/asn1"
     9  	"testing"
    10  
    11  	cryptobyte_asn1 "golang.org/x/crypto/cryptobyte/asn1"
    12  )
    13  
    14  func TestParseASN1String(t *testing.T) {
    15  	tests := []struct {
    16  		name        string
    17  		tag         cryptobyte_asn1.Tag
    18  		value       []byte
    19  		expected    string
    20  		expectedErr string
    21  	}{
    22  		{
    23  			name:     "T61String",
    24  			tag:      cryptobyte_asn1.T61String,
    25  			value:    []byte{80, 81, 82},
    26  			expected: string("PQR"),
    27  		},
    28  		{
    29  			name:     "PrintableString",
    30  			tag:      cryptobyte_asn1.PrintableString,
    31  			value:    []byte{80, 81, 82},
    32  			expected: string("PQR"),
    33  		},
    34  		{
    35  			name:        "PrintableString (invalid)",
    36  			tag:         cryptobyte_asn1.PrintableString,
    37  			value:       []byte{1, 2, 3},
    38  			expectedErr: "invalid PrintableString",
    39  		},
    40  		{
    41  			name:     "UTF8String",
    42  			tag:      cryptobyte_asn1.UTF8String,
    43  			value:    []byte{80, 81, 82},
    44  			expected: string("PQR"),
    45  		},
    46  		{
    47  			name:        "UTF8String (invalid)",
    48  			tag:         cryptobyte_asn1.UTF8String,
    49  			value:       []byte{255},
    50  			expectedErr: "invalid UTF-8 string",
    51  		},
    52  		{
    53  			name:     "BMPString",
    54  			tag:      cryptobyte_asn1.Tag(asn1.TagBMPString),
    55  			value:    []byte{80, 81},
    56  			expected: string("偑"),
    57  		},
    58  		{
    59  			name:        "BMPString (invalid length)",
    60  			tag:         cryptobyte_asn1.Tag(asn1.TagBMPString),
    61  			value:       []byte{255},
    62  			expectedErr: "invalid BMPString",
    63  		},
    64  		{
    65  			name:     "IA5String",
    66  			tag:      cryptobyte_asn1.IA5String,
    67  			value:    []byte{80, 81},
    68  			expected: string("PQ"),
    69  		},
    70  		{
    71  			name:        "IA5String (invalid)",
    72  			tag:         cryptobyte_asn1.IA5String,
    73  			value:       []byte{255},
    74  			expectedErr: "invalid IA5String",
    75  		},
    76  		{
    77  			name:     "NumericString",
    78  			tag:      cryptobyte_asn1.Tag(asn1.TagNumericString),
    79  			value:    []byte{49, 50},
    80  			expected: string("12"),
    81  		},
    82  		{
    83  			name:        "NumericString (invalid)",
    84  			tag:         cryptobyte_asn1.Tag(asn1.TagNumericString),
    85  			value:       []byte{80},
    86  			expectedErr: "invalid NumericString",
    87  		},
    88  	}
    89  
    90  	for _, tc := range tests {
    91  		t.Run(tc.name, func(t *testing.T) {
    92  			out, err := parseASN1String(tc.tag, tc.value)
    93  			if err != nil && err.Error() != tc.expectedErr {
    94  				t.Fatalf("parseASN1String returned unexpected error: got %q, want %q", err, tc.expectedErr)
    95  			} else if err == nil && tc.expectedErr != "" {
    96  				t.Fatalf("parseASN1String didn't fail, expected: %s", tc.expectedErr)
    97  			}
    98  			if out != tc.expected {
    99  				t.Fatalf("parseASN1String returned unexpected value: got %q, want %q", out, tc.expected)
   100  			}
   101  		})
   102  	}
   103  }
   104  

View as plain text