github.com/jcmturner/gokrb5/v8@v8.4.4/types/PAData_test.go (about) 1 package types 2 3 import ( 4 "encoding/hex" 5 "fmt" 6 "testing" 7 "time" 8 9 "github.com/jcmturner/gokrb5/v8/iana/patype" 10 "github.com/jcmturner/gokrb5/v8/test/testdata" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestUnmarshalPADataSequence(t *testing.T) { 15 t.Parallel() 16 var a PADataSequence 17 b, err := hex.DecodeString(testdata.MarshaledKRB5padata_sequence) 18 if err != nil { 19 t.Fatalf("Test vector read error: %v", err) 20 } 21 err = a.Unmarshal(b) 22 if err != nil { 23 t.Fatalf("Unmarshal error: %v", err) 24 } 25 assert.Equal(t, 2, len(a), "Number of PAData items in the sequence not as expected") 26 for i, pa := range a { 27 assert.Equal(t, patype.PA_SAM_RESPONSE, pa.PADataType, fmt.Sprintf("PAData type for entry %d not as expected", i+1)) 28 assert.Equal(t, []byte(testdata.TEST_PADATA_VALUE), pa.PADataValue, fmt.Sprintf("PAData valye for entry %d not as expected", i+1)) 29 } 30 } 31 32 func TestUnmarshalPADataSequence_empty(t *testing.T) { 33 t.Parallel() 34 var a PADataSequence 35 b, err := hex.DecodeString(testdata.MarshaledKRB5padataSequenceEmpty) 36 if err != nil { 37 t.Fatalf("Test vector read error: %v", err) 38 } 39 err = a.Unmarshal(b) 40 if err != nil { 41 t.Fatalf("Unmarshal error: %v", err) 42 } 43 assert.Equal(t, 0, len(a), "Number of PAData items in the sequence not as expected") 44 } 45 46 func TestUnmarshalPAEncTSEnc(t *testing.T) { 47 t.Parallel() 48 //Parse the test time value into a time.Time type 49 tt, _ := time.Parse(testdata.TEST_TIME_FORMAT, testdata.TEST_TIME) 50 51 var a PAEncTSEnc 52 b, err := hex.DecodeString(testdata.MarshaledKRB5pa_enc_ts) 53 if err != nil { 54 t.Fatalf("Test vector read error of %s: %v\n", "MarshaledKRB5pa_enc_ts", err) 55 } 56 err = a.Unmarshal(b) 57 if err != nil { 58 t.Fatalf("Unmarshal error of %s: %v\n", "MarshaledKRB5pa_enc_ts", err) 59 } 60 assert.Equal(t, tt, a.PATimestamp, "PA timestamp not as expected") 61 assert.Equal(t, 123456, a.PAUSec, "PA microseconds not as expected") 62 } 63 64 func TestUnmarshalPAEncTSEnc_nousec(t *testing.T) { 65 t.Parallel() 66 //Parse the test time value into a time.Time type 67 tt, _ := time.Parse(testdata.TEST_TIME_FORMAT, testdata.TEST_TIME) 68 69 var a PAEncTSEnc 70 b, err := hex.DecodeString(testdata.MarshaledKRB5pa_enc_tsNoUsec) 71 if err != nil { 72 t.Fatalf("Test vector read error: %v", err) 73 } 74 err = a.Unmarshal(b) 75 if err != nil { 76 t.Fatalf("Unmarshal error: %v", err) 77 } 78 assert.Equal(t, tt, a.PATimestamp, "PA timestamp not as expected") 79 assert.Equal(t, 0, a.PAUSec, "PA microseconds not as expected") 80 } 81 82 func TestUnmarshalETypeInfo(t *testing.T) { 83 t.Parallel() 84 var a ETypeInfo 85 b, err := hex.DecodeString(testdata.MarshaledKRB5etype_info) 86 if err != nil { 87 t.Fatalf("Test vector read error of %s: %v\n", "MarshaledKRB5etype_info", err) 88 } 89 err = a.Unmarshal(b) 90 if err != nil { 91 t.Fatalf("Unmarshal error of %s: %v\n", "MarshaledKRB5etype_info", err) 92 } 93 assert.Equal(t, 3, len(a), "Number of EType info entries not as expected") 94 assert.Equal(t, int32(0), a[0].EType, "Etype of first etype info entry not as expected") 95 assert.Equal(t, []byte("Morton's #0"), a[0].Salt, "Salt of first etype info entry not as expected") 96 assert.Equal(t, int32(1), a[1].EType, "Etype of second etype info entry not as expected") 97 assert.Equal(t, 0, len(a[1].Salt), "Salt of second etype info entry not as expected") 98 assert.Equal(t, int32(2), a[2].EType, "Etype of third etype info entry not as expected") 99 assert.Equal(t, []byte("Morton's #2"), a[2].Salt, "Salt of third etype info entry not as expected") 100 } 101 102 func TestUnmarshalETypeInfo_only1(t *testing.T) { 103 t.Parallel() 104 var a ETypeInfo 105 b, err := hex.DecodeString(testdata.MarshaledKRB5etype_infoOnly1) 106 if err != nil { 107 t.Fatalf("Test vector read error: %v", err) 108 } 109 err = a.Unmarshal(b) 110 if err != nil { 111 t.Fatalf("Unmarshal error: %v", err) 112 } 113 assert.Equal(t, 1, len(a), "Number of EType info entries not as expected") 114 assert.Equal(t, int32(0), a[0].EType, "Etype of first etype info entry not as expected") 115 assert.Equal(t, []byte("Morton's #0"), a[0].Salt, "Salt of first etype info entry not as expected") 116 } 117 118 func TestUnmarshalETypeInfo_noinfo(t *testing.T) { 119 t.Parallel() 120 var a ETypeInfo 121 b, err := hex.DecodeString(testdata.MarshaledKRB5etype_infoNoInfo) 122 if err != nil { 123 t.Fatalf("Test vector read error of %s: %v\n", "MarshaledKRB5etype_infoNoInfo", err) 124 } 125 err = a.Unmarshal(b) 126 if err != nil { 127 t.Fatalf("Unmarshal error of %s: %v\n", "MarshaledKRB5etype_infoNoInfo", err) 128 } 129 assert.Equal(t, 0, len(a), "Number of EType info entries not as expected") 130 } 131 132 func TestUnmarshalETypeInfo2(t *testing.T) { 133 t.Parallel() 134 var a ETypeInfo2 135 b, err := hex.DecodeString(testdata.MarshaledKRB5etype_info2) 136 if err != nil { 137 t.Fatalf("Test vector read error: %v", err) 138 } 139 err = a.Unmarshal(b) 140 if err != nil { 141 t.Fatalf("Unmarshal error: %v", err) 142 } 143 assert.Equal(t, 3, len(a), "Number of EType info2 entries not as expected") 144 assert.Equal(t, int32(0), a[0].EType, "Etype of first etype info2 entry not as expected") 145 assert.Equal(t, "Morton's #0", a[0].Salt, "Salt of first etype info2 entry not as expected") 146 assert.Equal(t, []byte("s2k: 0"), a[0].S2KParams, "String to key params of first etype info2 entry not as expected") 147 assert.Equal(t, int32(1), a[1].EType, "Etype of second etype info2 entry not as expected") 148 assert.Equal(t, 0, len(a[1].Salt), "Salt of second etype info2 entry not as expected") 149 assert.Equal(t, []byte("s2k: 1"), a[1].S2KParams, "String to key params of second etype info2 entry not as expected") 150 assert.Equal(t, int32(2), a[2].EType, "Etype of third etype info2 entry not as expected") 151 assert.Equal(t, "Morton's #2", a[2].Salt, "Salt of third etype info2 entry not as expected") 152 assert.Equal(t, []byte("s2k: 2"), a[2].S2KParams, "String to key params of third etype info2 entry not as expected") 153 } 154 155 func TestUnmarshalETypeInfo2_only1(t *testing.T) { 156 t.Parallel() 157 var a ETypeInfo2 158 b, err := hex.DecodeString(testdata.MarshaledKRB5etype_info2Only1) 159 if err != nil { 160 t.Fatalf("Test vector read error of %s: %v\n", "MarshaledKRB5etype_info2Only1", err) 161 } 162 err = a.Unmarshal(b) 163 if err != nil { 164 t.Fatalf("Unmarshal error of %s: %v\n", "MarshaledKRB5etype_info2Only1", err) 165 } 166 assert.Equal(t, 1, len(a), "Number of EType info2 entries not as expected") 167 assert.Equal(t, int32(0), a[0].EType, "Etype of first etype info2 entry not as expected") 168 assert.Equal(t, "Morton's #0", a[0].Salt, "Salt of first etype info2 entry not as expected") 169 assert.Equal(t, []byte("s2k: 0"), a[0].S2KParams, "String to key params of first etype info2 entry not as expected") 170 }