| 1 | package gssapi |
| 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 SPNEGO |
| 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 | MechTypeOIDKRB5, |
| 32 | []int{1, 3, 5, 1, 5, 2}, |
| 33 | MechTypeOIDMSLegacyKRB5, |
| 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.MechToken), "MechToken is zero in length") |
| 38 | } |
| 39 | |
| 40 | func TestUnmarshal_SPNEGO_RespTarg(t *testing.T) { |
| 41 | t.Parallel() |
| 42 | b, err := hex.DecodeString(testGSSAPIResp) |
| 43 | if err != nil { |
| 44 | t.Fatalf("Error converting hex string test data to bytes: %v", err) |
| 45 | } |
| 46 | var s SPNEGO |
| 47 | err = s.Unmarshal(b) |
| 48 | if err != nil { |
| 49 | t.Fatalf("Error unmarshalling SPNEGO with NegTokenResp/NegTokenTarg: %v", err) |
| 50 | } |
| 51 | assert.True(t, s.Resp, "SPNEGO does not indicate it contains NegTokenResp/Targ as expected") |
| 52 | assert.False(t, s.Init, "SPNEGO indicates is contains a NegTokenInit but it shouldn't") |
| 53 | assert.Equal(t, asn1.Enumerated(0), s.NegTokenResp.NegState, "Negotiation state not as expected.") |
| 54 | assert.Equal(t, MechTypeOIDKRB5, s.NegTokenResp.SupportedMech, "SupportedMech type not as expected.") |
| 55 | } |
| 56 | |
| 57 | func TestMarshal_SPNEGO_Init(t *testing.T) { |
| 58 | t.Parallel() |
| 59 | b, err := hex.DecodeString(testGSSAPIInit) |
| 60 | if err != nil { |
| 61 | t.Fatalf("Error converting hex string test data to bytes: %v", err) |
| 62 | } |
| 63 | var s SPNEGO |
| 64 | err = s.Unmarshal(b) |
| 65 | if err != nil { |
| 66 | t.Fatalf("Error unmarshalling SPNEGO with NegTokenInit: %v", err) |
| 67 | } |
| 68 | mb, err := s.Marshal() |
| 69 | if err != nil { |
| 70 | t.Fatalf("Error marshalling SPNEGO containing NegTokenInit: %v", err) |
| 71 | } |
| 72 | assert.Equal(t, b, mb, "Marshaled bytes not as expected") |
| 73 | } |
| 74 | |
| 75 | func TestMarshal_SPNEGO_RespTarg(t *testing.T) { |
| 76 | t.Parallel() |
| 77 | b, err := hex.DecodeString(testGSSAPIResp) |
| 78 | if err != nil { |
| 79 | t.Fatalf("Error converting hex string test data to bytes: %v", err) |
| 80 | } |
| 81 | var s SPNEGO |
| 82 | err = s.Unmarshal(b) |
| 83 | if err != nil { |
| 84 | t.Fatalf("Error unmarshalling SPNEGO with NegTokenResp: %v", err) |
| 85 | } |
| 86 | mb, err := s.Marshal() |
| 87 | if err != nil { |
| 88 | t.Fatalf("Error marshalling SPNEGO containing NegTokenResp: %v", err) |
| 89 | } |
| 90 | assert.Equal(t, b, mb, "Marshaled bytes not as expected") |
| 91 | } |