github.com/jcmturner/gokrb5/v8@v8.4.4/spnego/negotiationToken_test.go (about)

     1  package spnego
     2  
     3  import (
     4  	"encoding/hex"
     5  	"testing"
     6  
     7  	"github.com/jcmturner/gofork/encoding/asn1"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  const (
    12  	testNegTokenInit = "a08202aa308202a6a027302506092a864886f71201020206052b0501050206092a864882f71201020206062b0601050205a2820279048202756082027106092a864886f71201020201006e8202603082025ca003020105a10302010ea20703050000000000a38201706182016c30820168a003020105a10d1b0b544553542e474f4b524235a2233021a003020103a11a30181b04485454501b10686f73742e746573742e676f6b726235a382012b30820127a003020112a103020102a282011904820115d4bd890abc456f44e2e7a2e8111bd6767abf03266dfcda97c629af2ece450a5ae1f145e4a4d1bc2c848e66a6c6b31d9740b26b03cdbd2570bfcf126e90adf5f5ebce9e283ff5086da47b129b14fc0aabd4d1df9c1f3c72b80cc614dfc28783450b2c7b7749651f432b47aaa2ff158c0066b757f3fb00dd7b4f63d68276c76373ecdd3f19c66ebc43a81e577f3c263b878356f57e8d6c4eccd587b81538e70392cf7e73fc12a6f7c537a894a7bb5566c83ac4d69757aa320a51d8d690017aebf952add1889adfc3307b0e6cd8c9b57cf8589fbe52800acb6461c25473d49faa1bdceb8bce3f61db23f9cd6a09d5adceb411e1c4546b30b33331e570fd6bc50aa403557e75f488e759750ea038aab6454667d9b64f41a481d23081cfa003020112a281c70481c4d67ba2ae4cf5d917caab1d863605249320e90482563662ed92408a543b6ad5edeb8f9375e9060a205491df082fd2a5fec93dfb76f41012bb60cae20f07adbb77a1aa56f0521f36e1ea10dc9fb762902b254dd7664d0bcc6f751f2003e41990af1b4330d10477bfad638b9f0b704ac80cc47731f8ec8d801762bad8884b8de90adb1dbe7fc7b0ffafd38fb5eb8b6547cee30d89873281ce63ad70042a13478b1a7c2bdde0f223ace62dbb84e2d06f1070f4265f66e0544449335e2fcc4d0aee5bf81c5999"
    13  	testNegTokenResp = "a1143012a0030a0100a10b06092a864886f712010202"
    14  )
    15  
    16  func TestUnmarshal_negTokenInit(t *testing.T) {
    17  	t.Parallel()
    18  	b, err := hex.DecodeString(testNegTokenInit)
    19  	if err != nil {
    20  		t.Fatalf("Error converting hex string test data to bytes: %v", err)
    21  	}
    22  	isInit, nt, err := UnmarshalNegToken(b)
    23  	if err != nil {
    24  		t.Fatalf("Error unmarshalling negotiation token: %v", err)
    25  	}
    26  	assert.IsType(t, NegTokenInit{}, nt, "Not the expected type NegTokenInit")
    27  	assert.True(t, isInit, "Boolean indicating type is negTokenInit is not true")
    28  	nInit := nt.(NegTokenInit)
    29  	assert.Equal(t, 4, len(nInit.MechTypes))
    30  	expectMechTypes := []asn1.ObjectIdentifier{
    31  		[]int{1, 2, 840, 113554, 1, 2, 2},
    32  		[]int{1, 3, 5, 1, 5, 2},
    33  		[]int{1, 2, 840, 48018, 1, 2, 2},
    34  		[]int{1, 3, 6, 1, 5, 2, 5},
    35  	}
    36  	assert.Equal(t, expectMechTypes, nInit.MechTypes, "MechTypes list in NegTokenInit not as expected")
    37  }
    38  
    39  func TestMarshal_negTokenInit(t *testing.T) {
    40  	t.Parallel()
    41  	b, err := hex.DecodeString(testNegTokenInit)
    42  	if err != nil {
    43  		t.Fatalf("Error converting hex string test data to bytes: %v", err)
    44  	}
    45  	_, nt, err := UnmarshalNegToken(b)
    46  	if err != nil {
    47  		t.Fatalf("Error unmarshalling negotiation token: %v", err)
    48  	}
    49  	nInit := nt.(NegTokenInit)
    50  	mb, err := nInit.Marshal()
    51  	if err != nil {
    52  		t.Fatalf("Error marshalling negotiation init token: %v", err)
    53  	}
    54  	assert.Equal(t, b, mb, "Marshalled bytes not as expected for NegTokenInit")
    55  }
    56  
    57  func TestUnmarshal_negTokenResp(t *testing.T) {
    58  	t.Parallel()
    59  	b, err := hex.DecodeString(testNegTokenResp)
    60  	if err != nil {
    61  		t.Fatalf("Error converting hex string test data to bytes: %v", err)
    62  	}
    63  	isInit, nt, err := UnmarshalNegToken(b)
    64  	if err != nil {
    65  		t.Fatalf("Error unmarshalling negotiation token: %v", err)
    66  	}
    67  	assert.IsType(t, NegTokenResp{}, nt, "Not the expected type NegTokenResp")
    68  	assert.False(t, isInit, "Boolean indicating type is negTokenInit is not false")
    69  	nResp := nt.(NegTokenResp)
    70  	assert.Equal(t, asn1.Enumerated(0), nResp.NegState)
    71  	assert.Equal(t, asn1.ObjectIdentifier{1, 2, 840, 113554, 1, 2, 2}, nResp.SupportedMech, "SupportedMech type not as expected.")
    72  }
    73  
    74  func TestMarshal_negTokenResp(t *testing.T) {
    75  	t.Parallel()
    76  	b, err := hex.DecodeString(testNegTokenResp)
    77  	if err != nil {
    78  		t.Fatalf("Error converting hex string test data to bytes: %v", err)
    79  	}
    80  	_, nt, err := UnmarshalNegToken(b)
    81  	if err != nil {
    82  		t.Fatalf("Error unmarshalling negotiation token: %v", err)
    83  	}
    84  	nResp := nt.(NegTokenResp)
    85  	mb, err := nResp.Marshal()
    86  	if err != nil {
    87  		t.Fatalf("Error marshalling negotiation init token: %v", err)
    88  	}
    89  	assert.Equal(t, b, mb, "Marshalled bytes not as expected for NegTokenResp")
    90  }
    91  
    92  func TestUnmarshal_negTokenInitWithReqFlags(t *testing.T) {
    93  	mhex := "a01e301ca00d300b06092a864886f712010202a10403020176a2050403010203"
    94  	mb, err := hex.DecodeString(mhex)
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  
    99  	var m NegTokenInit
   100  	err = m.Unmarshal(mb)
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	if len(m.MechTokenBytes) != 3 {
   105  		t.Errorf("unmarshal did not return the correct number of mechToken bytes")
   106  	}
   107  }