github.com/lestrrat-go/jwx/v2@v2.0.21/jwa/jwa_test.go (about)

     1  package jwa_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/lestrrat-go/jwx/v2/jwa"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  type stringer struct {
    12  	src string
    13  }
    14  
    15  func (s stringer) String() string {
    16  	return s.src
    17  }
    18  
    19  func TestSanity(t *testing.T) {
    20  	var k1 jwa.KeyAlgorithm = jwa.RS256
    21  	if _, ok := k1.(jwa.SignatureAlgorithm); !assert.True(t, ok, `converting k1 to jws.SignatureAlgorithm should succeed`) {
    22  		return
    23  	}
    24  	if _, ok := k1.(jwa.KeyEncryptionAlgorithm); !assert.False(t, ok, `converting k1 to jws.KeyEncryptionAlgorithm should fail`) {
    25  		return
    26  	}
    27  	var k2 jwa.KeyAlgorithm = jwa.DIRECT
    28  	if _, ok := k2.(jwa.SignatureAlgorithm); !assert.False(t, ok, `converting k2 to jws.SignatureAlgorithm should fail`) {
    29  		return
    30  	}
    31  	if _, ok := k2.(jwa.KeyEncryptionAlgorithm); !assert.True(t, ok, `converting k2 to jws.KeyEncryptionAlgorithm should succeed`) {
    32  		return
    33  	}
    34  }
    35  
    36  func TestKeyAlgorithmFrom(t *testing.T) {
    37  	testcases := []struct {
    38  		Input interface{}
    39  		Error bool
    40  	}{
    41  		{
    42  			Input: jwa.RS256,
    43  		},
    44  		{
    45  			Input: jwa.DIRECT,
    46  		},
    47  		{
    48  			Input: jwa.A128CBC_HS256,
    49  			Error: true,
    50  		},
    51  	}
    52  
    53  	for _, tc := range testcases {
    54  		tc := tc
    55  		t.Run(fmt.Sprintf("%T", tc.Input), func(t *testing.T) {
    56  			alg := jwa.KeyAlgorithmFrom(tc.Input)
    57  			if tc.Error {
    58  				if !assert.IsType(t, alg, jwa.InvalidKeyAlgorithm(""), `key should be invalid`) {
    59  					return
    60  				}
    61  			} else {
    62  				if !assert.Equal(t, alg, tc.Input, `key should be valid`) {
    63  					return
    64  				}
    65  			}
    66  		})
    67  	}
    68  }