github.com/jcmturner/gokrb5/v8@v8.4.4/spnego/spnego_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  	testGSSAPIInit = "608202b606062b0601050502a08202aa308202a6a027302506092a864886f71201020206052b0501050206092a864882f71201020206062b0601050205a2820279048202756082027106092a864886f71201020201006e8202603082025ca003020105a10302010ea20703050000000000a38201706182016c30820168a003020105a10d1b0b544553542e474f4b524235a2233021a003020103a11a30181b04485454501b10686f73742e746573742e676f6b726235a382012b30820127a003020112a103020102a282011904820115d4bd890abc456f44e2e7a2e8111bd6767abf03266dfcda97c629af2ece450a5ae1f145e4a4d1bc2c848e66a6c6b31d9740b26b03cdbd2570bfcf126e90adf5f5ebce9e283ff5086da47b129b14fc0aabd4d1df9c1f3c72b80cc614dfc28783450b2c7b7749651f432b47aaa2ff158c0066b757f3fb00dd7b4f63d68276c76373ecdd3f19c66ebc43a81e577f3c263b878356f57e8d6c4eccd587b81538e70392cf7e73fc12a6f7c537a894a7bb5566c83ac4d69757aa320a51d8d690017aebf952add1889adfc3307b0e6cd8c9b57cf8589fbe52800acb6461c25473d49faa1bdceb8bce3f61db23f9cd6a09d5adceb411e1c4546b30b33331e570fd6bc50aa403557e75f488e759750ea038aab6454667d9b64f41a481d23081cfa003020112a281c70481c4eb593beb5afcb1a2a669d54cb85a3772231559f2d40c9f8f053f218ba6eb084ed7efc467d94b88bcd189dda920d6e675ec001a6a2bca11f0a1de37f2f7ae9929f94a86d625b2ec1b213a88cbae6099dda7b172cd3bd1802cb177ae4554d59277004bfd3435248f55044fe7af7b2c9c5a3c43763278c585395aebe2856cdff9f2569d8b823564ce6be2d19748b910ec06bd3c0a9bc5de51ddcf7d875f1108ca6ad935f52d90cb62a18197d9b8e796bef0fbe1463f61df61cfbce6008ae9e1a2d2314a986d"
    13  	testGSSAPIResp = "a1143012a0030a0100a10b06092a864886f712010202"
    14  )
    15  
    16  func TestUnmarshal_SPNEGO_Init(t *testing.T) {
    17  	t.Parallel()
    18  	b, err := hex.DecodeString(testGSSAPIInit)
    19  	if err != nil {
    20  		t.Fatalf("Error converting hex string test data to bytes: %v", err)
    21  	}
    22  	var s SPNEGOToken
    23  	err = s.Unmarshal(b)
    24  	if err != nil {
    25  		t.Fatalf("Error unmarshalling SPNEGO with NegTokenInit: %v", err)
    26  	}
    27  	assert.True(t, s.Init, "SPNEGO does not indicate it contains NegTokenInit as expected")
    28  	assert.False(t, s.Resp, "SPNEGO indicates is contains a NegTokenResp but it shouldn't")
    29  	assert.Equal(t, 4, len(s.NegTokenInit.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, s.NegTokenInit.MechTypes, "MechTypes list in NegTokenInit not as expected")
    37  	assert.NotZero(t, len(s.NegTokenInit.MechTokenBytes), "MechToken is zero in length")
    38  }
    39  
    40  func TestUnMarshal_SPNEGO_Empty(t *testing.T) {
    41  	sp := new(SPNEGOToken)
    42  
    43  	// The following tests are intended to ensure we don't panic.
    44  	if err := sp.Unmarshal(nil); err == nil {
    45  		t.Fatal("should have errored, input is absent")
    46  	}
    47  	if err := sp.Unmarshal([]byte{}); err == nil {
    48  		t.Fatal("should have errored, input is empty")
    49  	}
    50  	if err := sp.Unmarshal([]byte{1}); err == nil {
    51  		t.Fatal("should have errored, input is too low")
    52  	}
    53  }
    54  
    55  func TestUnmarshal_SPNEGO_RespTarg(t *testing.T) {
    56  	t.Parallel()
    57  	b, err := hex.DecodeString(testGSSAPIResp)
    58  	if err != nil {
    59  		t.Fatalf("Error converting hex string test data to bytes: %v", err)
    60  	}
    61  	var s SPNEGOToken
    62  	err = s.Unmarshal(b)
    63  	if err != nil {
    64  		t.Fatalf("Error unmarshalling SPNEGO with NegTokenResp/NegTokenTarg: %v", err)
    65  	}
    66  	assert.True(t, s.Resp, "SPNEGO does not indicate it contains NegTokenResp/Targ as expected")
    67  	assert.False(t, s.Init, "SPNEGO indicates is contains a NegTokenInit but it shouldn't")
    68  	assert.Equal(t, asn1.Enumerated(0), s.NegTokenResp.NegState, "Negotiation state not as expected.")
    69  	assert.Equal(t, asn1.ObjectIdentifier{1, 2, 840, 113554, 1, 2, 2}, s.NegTokenResp.SupportedMech, "SupportedMech type not as expected.")
    70  }
    71  
    72  func TestMarshal_SPNEGO_Init(t *testing.T) {
    73  	t.Parallel()
    74  	b, err := hex.DecodeString(testGSSAPIInit)
    75  	if err != nil {
    76  		t.Fatalf("Error converting hex string test data to bytes: %v", err)
    77  	}
    78  	var s SPNEGOToken
    79  	err = s.Unmarshal(b)
    80  	if err != nil {
    81  		t.Fatalf("Error unmarshalling SPNEGO with NegTokenInit: %v", err)
    82  	}
    83  	mb, err := s.Marshal()
    84  	if err != nil {
    85  		t.Fatalf("Error marshalling SPNEGO containing NegTokenInit: %v", err)
    86  	}
    87  	assert.Equal(t, b, mb, "Marshaled bytes not as expected")
    88  }
    89  
    90  func TestMarshal_SPNEGO_RespTarg(t *testing.T) {
    91  	t.Parallel()
    92  	b, err := hex.DecodeString(testGSSAPIResp)
    93  	if err != nil {
    94  		t.Fatalf("Error converting hex string test data to bytes: %v", err)
    95  	}
    96  	var s SPNEGOToken
    97  	err = s.Unmarshal(b)
    98  	if err != nil {
    99  		t.Fatalf("Error unmarshalling SPNEGO with NegTokenResp: %v", err)
   100  	}
   101  	mb, err := s.Marshal()
   102  	if err != nil {
   103  		t.Fatalf("Error marshalling SPNEGO containing NegTokenResp: %v", err)
   104  	}
   105  	assert.Equal(t, b, mb, "Marshaled bytes not as expected")
   106  }