github.com/pion/dtls/v2@v2.2.12/pkg/protocol/handshake/message_certificate_request_test.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  package handshake
     5  
     6  import (
     7  	"errors"
     8  	"reflect"
     9  	"testing"
    10  
    11  	"github.com/pion/dtls/v2/pkg/crypto/clientcertificate"
    12  	"github.com/pion/dtls/v2/pkg/crypto/hash"
    13  	"github.com/pion/dtls/v2/pkg/crypto/signature"
    14  	"github.com/pion/dtls/v2/pkg/crypto/signaturehash"
    15  )
    16  
    17  func TestHandshakeMessageCertificateRequest(t *testing.T) {
    18  	cases := map[string]struct {
    19  		rawCertificateRequest    []byte
    20  		parsedCertificateRequest *MessageCertificateRequest
    21  		expErr                   error
    22  	}{
    23  		"valid - with CertificateAuthoritiesNames": {
    24  			rawCertificateRequest: []byte{
    25  				0x02, 0x01, 0x40, 0x00, 0x0C, 0x04, 0x03, 0x04, 0x01, 0x05,
    26  				0x03, 0x05, 0x01, 0x06, 0x01, 0x02, 0x01, 0x00, 0x06, 0x00,
    27  				0x04, 0x74, 0x65, 0x73, 0x74,
    28  			},
    29  			parsedCertificateRequest: &MessageCertificateRequest{
    30  				CertificateTypes: []clientcertificate.Type{
    31  					clientcertificate.RSASign,
    32  					clientcertificate.ECDSASign,
    33  				},
    34  				SignatureHashAlgorithms: []signaturehash.Algorithm{
    35  					{Hash: hash.SHA256, Signature: signature.ECDSA},
    36  					{Hash: hash.SHA256, Signature: signature.RSA},
    37  					{Hash: hash.SHA384, Signature: signature.ECDSA},
    38  					{Hash: hash.SHA384, Signature: signature.RSA},
    39  					{Hash: hash.SHA512, Signature: signature.RSA},
    40  					{Hash: hash.SHA1, Signature: signature.RSA},
    41  				},
    42  				CertificateAuthoritiesNames: [][]byte{[]byte("test")},
    43  			},
    44  		},
    45  		"valid - without CertificateAuthoritiesNames": {
    46  			rawCertificateRequest: []byte{
    47  				0x02, 0x01, 0x40, 0x00, 0x0C, 0x04, 0x03, 0x04, 0x01, 0x05,
    48  				0x03, 0x05, 0x01, 0x06, 0x01, 0x02, 0x01, 0x00, 0x00,
    49  			},
    50  			parsedCertificateRequest: &MessageCertificateRequest{
    51  				CertificateTypes: []clientcertificate.Type{
    52  					clientcertificate.RSASign,
    53  					clientcertificate.ECDSASign,
    54  				},
    55  				SignatureHashAlgorithms: []signaturehash.Algorithm{
    56  					{Hash: hash.SHA256, Signature: signature.ECDSA},
    57  					{Hash: hash.SHA256, Signature: signature.RSA},
    58  					{Hash: hash.SHA384, Signature: signature.ECDSA},
    59  					{Hash: hash.SHA384, Signature: signature.RSA},
    60  					{Hash: hash.SHA512, Signature: signature.RSA},
    61  					{Hash: hash.SHA1, Signature: signature.RSA},
    62  				},
    63  			},
    64  		},
    65  		"invalid - casLength CertificateAuthoritiesNames": {
    66  			rawCertificateRequest: []byte{
    67  				0x02, 0x01, 0x40, 0x00, 0x0C, 0x04, 0x03, 0x04, 0x01, 0x05,
    68  				0x03, 0x05, 0x01, 0x06, 0x01, 0x02, 0x01, 0x01,
    69  			},
    70  			expErr: errBufferTooSmall,
    71  		},
    72  	}
    73  
    74  	for name, testCase := range cases {
    75  		testCase := testCase
    76  		t.Run(name, func(t *testing.T) {
    77  			c := &MessageCertificateRequest{}
    78  			if err := c.Unmarshal(testCase.rawCertificateRequest); err != nil {
    79  				if testCase.expErr != nil {
    80  					if errors.Is(err, testCase.expErr) {
    81  						return
    82  					}
    83  				}
    84  				t.Error(err)
    85  			} else if !reflect.DeepEqual(c, testCase.parsedCertificateRequest) {
    86  				t.Errorf("parsedCertificateRequest unmarshal: got %#v, want %#v", c, testCase.parsedCertificateRequest)
    87  			}
    88  			raw, err := c.Marshal()
    89  			if err != nil {
    90  				t.Error(err)
    91  			} else if !reflect.DeepEqual(raw, testCase.rawCertificateRequest) {
    92  				t.Errorf("parsedCertificateRequest marshal: got %#v, want %#v", raw, testCase.rawCertificateRequest)
    93  			}
    94  		})
    95  	}
    96  }