github.com/jcmturner/gokrb5/v8@v8.4.4/pac/client_claims_test.go (about)

     1  package pac
     2  
     3  import (
     4  	"encoding/hex"
     5  	"testing"
     6  
     7  	"github.com/jcmturner/gokrb5/v8/test/testdata"
     8  	"github.com/jcmturner/rpc/v2/mstypes"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  const (
    13  	ClaimsEntryIDStr            = "ad://ext/sAMAccountName:88d5d9085ea5c0c0"
    14  	ClaimsEntryValueStr         = "testuser1"
    15  	ClaimsEntryIDInt64          = "ad://ext/msDS-SupportedE:88d5dea8f1af5f19"
    16  	ClaimsEntryValueInt64 int64 = 28
    17  	ClaimsEntryIDUInt64         = "ad://ext/objectClass:88d5de791e7b27e6"
    18  )
    19  
    20  func TestPAC_ClientClaimsInfoStr_Unmarshal(t *testing.T) {
    21  	t.Parallel()
    22  	b, err := hex.DecodeString(testdata.MarshaledPAC_ClientClaimsInfoStr)
    23  	if err != nil {
    24  		t.Fatal("Could not decode test data hex string")
    25  	}
    26  	var k ClientClaimsInfo
    27  	err = k.Unmarshal(b)
    28  	if err != nil {
    29  		t.Fatalf("Error unmarshaling test data: %v", err)
    30  	}
    31  	assert.Equal(t, uint32(1), k.ClaimsSet.ClaimsArrayCount, "claims array count not as expected")
    32  	assert.Equal(t, mstypes.ClaimsSourceTypeAD, k.ClaimsSet.ClaimsArrays[0].ClaimsSourceType, "claims source type not as expected")
    33  	assert.Equal(t, uint32(1), k.ClaimsSet.ClaimsArrays[0].ClaimsCount, "claims count not as expected")
    34  	assert.Equal(t, uint16(3), k.ClaimsSet.ClaimsArrays[0].ClaimEntries[0].Type, "claims entry type not as expected")
    35  	assert.Equal(t, uint32(1), k.ClaimsSet.ClaimsArrays[0].ClaimEntries[0].TypeString.ValueCount, "claims value count not as expected")
    36  	assert.Equal(t, ClaimsEntryIDStr, k.ClaimsSet.ClaimsArrays[0].ClaimEntries[0].ID, "claims entry ID not as expected")
    37  	assert.Equal(t, []mstypes.LPWSTR{{Value: ClaimsEntryValueStr}}, k.ClaimsSet.ClaimsArrays[0].ClaimEntries[0].TypeString.Value, "claims value not as expected")
    38  	assert.Equal(t, mstypes.CompressionFormatNone, k.ClaimsSetMetadata.CompressionFormat, "compression format not as expected")
    39  }
    40  
    41  func TestPAC_ClientClaimsMultiValueUint_Unmarshal(t *testing.T) {
    42  	t.Parallel()
    43  	b, err := hex.DecodeString(testdata.MarshaledPAC_ClientClaimsInfoMultiUint)
    44  	if err != nil {
    45  		t.Fatal("Could not decode test data hex string")
    46  	}
    47  	var k ClientClaimsInfo
    48  	err = k.Unmarshal(b)
    49  	if err != nil {
    50  		t.Fatalf("Error unmarshaling test data: %v", err)
    51  	}
    52  	assert.Equal(t, uint32(1), k.ClaimsSet.ClaimsArrayCount, "claims array count not as expected")
    53  	assert.Equal(t, mstypes.ClaimsSourceTypeAD, k.ClaimsSet.ClaimsArrays[0].ClaimsSourceType, "claims source type not as expected")
    54  	assert.Equal(t, uint32(1), k.ClaimsSet.ClaimsArrays[0].ClaimsCount, "claims count not as expected")
    55  	assert.Equal(t, mstypes.ClaimTypeIDUInt64, k.ClaimsSet.ClaimsArrays[0].ClaimEntries[0].Type, "claims entry type not as expected")
    56  	assert.Equal(t, uint32(4), k.ClaimsSet.ClaimsArrays[0].ClaimEntries[0].TypeUInt64.ValueCount, "claims value count not as expected")
    57  	assert.Equal(t, ClaimsEntryIDUInt64, k.ClaimsSet.ClaimsArrays[0].ClaimEntries[0].ID, "claims entry ID not as expected")
    58  	assert.Equal(t, []uint64{655369, 65543, 65542, 65536}, k.ClaimsSet.ClaimsArrays[0].ClaimEntries[0].TypeUInt64.Value, "claims value not as expected")
    59  	assert.Equal(t, mstypes.CompressionFormatNone, k.ClaimsSetMetadata.CompressionFormat, "compression format not as expected")
    60  }
    61  
    62  func TestPAC_ClientClaimsInt_Unmarshal(t *testing.T) {
    63  	t.Parallel()
    64  	b, err := hex.DecodeString(testdata.MarshaledPAC_ClientClaimsInfoInt)
    65  	if err != nil {
    66  		t.Fatal("Could not decode test data hex string")
    67  	}
    68  	var k ClientClaimsInfo
    69  	err = k.Unmarshal(b)
    70  	if err != nil {
    71  		t.Fatalf("Error unmarshaling test data: %v", err)
    72  	}
    73  	assert.Equal(t, uint32(1), k.ClaimsSet.ClaimsArrayCount, "claims array count not as expected")
    74  	assert.Equal(t, mstypes.ClaimsSourceTypeAD, k.ClaimsSet.ClaimsArrays[0].ClaimsSourceType, "claims source type not as expected")
    75  	assert.Equal(t, uint32(1), k.ClaimsSet.ClaimsArrays[0].ClaimsCount, "claims count not as expected")
    76  	assert.Equal(t, mstypes.ClaimTypeIDInt64, k.ClaimsSet.ClaimsArrays[0].ClaimEntries[0].Type, "claims entry type not as expected")
    77  	assert.Equal(t, uint32(1), k.ClaimsSet.ClaimsArrays[0].ClaimEntries[0].TypeInt64.ValueCount, "claims value count not as expected")
    78  	assert.Equal(t, ClaimsEntryIDInt64, k.ClaimsSet.ClaimsArrays[0].ClaimEntries[0].ID, "claims entry ID not as expected")
    79  	assert.Equal(t, []int64{ClaimsEntryValueInt64}, k.ClaimsSet.ClaimsArrays[0].ClaimEntries[0].TypeInt64.Value, "claims value not as expected")
    80  	assert.Equal(t, mstypes.CompressionFormatNone, k.ClaimsSetMetadata.CompressionFormat, "compression format not as expected")
    81  }
    82  
    83  func TestPAC_ClientClaimsMultiValueStr_Unmarshal(t *testing.T) {
    84  	t.Parallel()
    85  	b, err := hex.DecodeString(testdata.MarshaledPAC_ClientClaimsInfoMultiStr)
    86  	if err != nil {
    87  		t.Fatal("Could not decode test data hex string")
    88  	}
    89  	var k ClientClaimsInfo
    90  	err = k.Unmarshal(b)
    91  	if err != nil {
    92  		t.Fatalf("Error unmarshaling test data: %v", err)
    93  	}
    94  	assert.Equal(t, uint32(1), k.ClaimsSet.ClaimsArrayCount, "claims array count not as expected")
    95  	assert.Equal(t, mstypes.ClaimsSourceTypeAD, k.ClaimsSet.ClaimsArrays[0].ClaimsSourceType, "claims source type not as expected")
    96  	assert.Equal(t, uint32(1), k.ClaimsSet.ClaimsArrays[0].ClaimsCount, "claims count not as expected")
    97  	assert.Equal(t, mstypes.ClaimTypeIDString, k.ClaimsSet.ClaimsArrays[0].ClaimEntries[0].Type, "claims entry type not as expected")
    98  	assert.Equal(t, uint32(4), k.ClaimsSet.ClaimsArrays[0].ClaimEntries[0].TypeString.ValueCount, "claims value count not as expected")
    99  	assert.Equal(t, "ad://ext/otherIpPhone:88d5de9f6b4af985", k.ClaimsSet.ClaimsArrays[0].ClaimEntries[0].ID, "claims entry ID not as expected")
   100  	assert.Equal(t, []mstypes.LPWSTR{{Value: "str1"}, {Value: "str2"}, {Value: "str3"}, {Value: "str4"}}, k.ClaimsSet.ClaimsArrays[0].ClaimEntries[0].TypeString.Value, "claims value not as expected")
   101  	assert.Equal(t, mstypes.CompressionFormatNone, k.ClaimsSetMetadata.CompressionFormat, "compression format not as expected")
   102  }
   103  
   104  func TestPAC_ClientClaimsInfoMultiEntry_Unmarshal(t *testing.T) {
   105  	// Has an int and a str claim type
   106  	t.Parallel()
   107  	b, err := hex.DecodeString(testdata.MarshaledPAC_ClientClaimsInfoMulti)
   108  	if err != nil {
   109  		t.Fatal("Could not decode test data hex string")
   110  	}
   111  	var k ClientClaimsInfo
   112  	err = k.Unmarshal(b)
   113  	if err != nil {
   114  		t.Fatalf("Error unmarshaling test data: %v", err)
   115  	}
   116  	assert.Equal(t, uint32(1), k.ClaimsSet.ClaimsArrayCount, "claims array count not as expected")
   117  	assert.Equal(t, mstypes.ClaimsSourceTypeAD, k.ClaimsSet.ClaimsArrays[0].ClaimsSourceType, "claims source type not as expected")
   118  	assert.Equal(t, uint32(2), k.ClaimsSet.ClaimsArrays[0].ClaimsCount, "claims count not as expected")
   119  	assert.Equal(t, uint16(1), k.ClaimsSet.ClaimsArrays[0].ClaimEntries[0].Type, "claims entry type not as expected")
   120  	assert.Equal(t, uint32(1), k.ClaimsSet.ClaimsArrays[0].ClaimEntries[0].TypeInt64.ValueCount, "claims value count not as expected")
   121  	assert.Equal(t, ClaimsEntryIDInt64, k.ClaimsSet.ClaimsArrays[0].ClaimEntries[0].ID, "claims entry ID not as expected")
   122  	assert.Equal(t, []int64{int64(28)}, k.ClaimsSet.ClaimsArrays[0].ClaimEntries[0].TypeInt64.Value, "claims value not as expected")
   123  	assert.Equal(t, uint16(3), k.ClaimsSet.ClaimsArrays[0].ClaimEntries[1].Type, "claims entry type not as expected")
   124  	assert.Equal(t, uint32(1), k.ClaimsSet.ClaimsArrays[0].ClaimEntries[1].TypeString.ValueCount, "claims value count not as expected")
   125  	assert.Equal(t, ClaimsEntryIDStr, k.ClaimsSet.ClaimsArrays[0].ClaimEntries[1].ID, "claims entry ID not as expected")
   126  	assert.Equal(t, []mstypes.LPWSTR{{Value: ClaimsEntryValueStr}}, k.ClaimsSet.ClaimsArrays[0].ClaimEntries[1].TypeString.Value, "claims value not as expected")
   127  	assert.Equal(t, mstypes.CompressionFormatNone, k.ClaimsSetMetadata.CompressionFormat, "compression format not as expected")
   128  }
   129  
   130  // Compressed claims not yet supported.
   131  //func TestPAC_ClientClaimsInfo_Unmarshal_UnsupportedCompression(t *testing.T) {
   132  //	t.Parallel()
   133  //	b, err := hex.DecodeString(testdata.MarshaledPAC_ClientClaimsInfo_XPRESS_HUFF)
   134  //	if err != nil {
   135  //		t.Fatal("Could not decode test data hex string")
   136  //	}
   137  //	var k ClientClaimsInfo
   138  //	err = k.Unmarshal(b)
   139  //	if err != nil {
   140  //		t.Fatalf("Error unmarshaling test data: %v", err)
   141  //	}
   142  //	assert.Equal(t, mstypes.CompressionFormatXPressHuff, k.ClaimsSetMetadata.CompressionFormat, "compression format not as expected")
   143  //}