| 1 | package packets |
| 2 | |
| 3 | import ( |
| 4 | "encoding/asn1" |
| 5 | "errors" |
| 6 | "reflect" |
| 7 | "testing" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | func TestKrb5Contants(t *testing.T) { |
| 12 | var units = []struct { |
| 13 | got interface{} |
| 14 | exp interface{} |
| 15 | }{ |
| 16 | {Krb5AsRequestType, 10}, |
| 17 | {Krb5Krb5PrincipalNameType, 1}, |
| 18 | {Krb5CryptDesCbcMd4, 2}, |
| 19 | {Krb5CryptDescCbcMd5, 3}, |
| 20 | {Krb5CryptRc4Hmac, 23}, |
| 21 | } |
| 22 | for _, u := range units { |
| 23 | if !reflect.DeepEqual(u.exp, u.got) { |
| 24 | t.Fatalf("expected '%v', got '%v'", u.exp, u.got) |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | func TestKrb5Vars(t *testing.T) { |
| 30 | var units = []struct { |
| 31 | got interface{} |
| 32 | exp interface{} |
| 33 | }{ |
| 34 | {ErrNoCrypt, errors.New("No crypt alg found")}, |
| 35 | {ErrReqData, errors.New("Failed to extract pnData from as-req")}, |
| 36 | {ErrNoCipher, errors.New("No encryption type or cipher found")}, |
| 37 | {Krb5AsReqParam, "application,explicit,tag:10"}, |
| 38 | } |
| 39 | for _, u := range units { |
| 40 | if !reflect.DeepEqual(u.exp, u.got) { |
| 41 | t.Fatalf("expected '%v', got '%v'", u.exp, u.got) |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func TestKrb5PrincipalName(t *testing.T) { |
| 47 | str := []string{"example"} |
| 48 | name := Krb5PrincipalName{NameString: str} |
| 49 | var units = []struct { |
| 50 | got interface{} |
| 51 | exp interface{} |
| 52 | }{ |
| 53 | {name.NameType, 0}, |
| 54 | {name.NameString, str}, |
| 55 | } |
| 56 | for _, u := range units { |
| 57 | if !reflect.DeepEqual(u.exp, u.got) { |
| 58 | t.Fatalf("expected '%v', got '%v'", u.exp, u.got) |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestKrb5EncryptedData(t *testing.T) { |
| 64 | cipher := []byte{} |
| 65 | data := Krb5EncryptedData{Cipher: cipher} |
| 66 | var units = []struct { |
| 67 | got interface{} |
| 68 | exp interface{} |
| 69 | }{ |
| 70 | {data.Cipher, cipher}, |
| 71 | {data.Etype, 0}, |
| 72 | {data.Kvno, 0}, |
| 73 | } |
| 74 | for _, u := range units { |
| 75 | if !reflect.DeepEqual(u.exp, u.got) { |
| 76 | t.Fatalf("expected '%v', got '%v'", u.exp, u.got) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestKrb5Ticket(t *testing.T) { |
| 82 | v := 0 |
| 83 | r := "picat" |
| 84 | s := Krb5PrincipalName{} |
| 85 | e := Krb5EncryptedData{} |
| 86 | |
| 87 | ticket := Krb5Ticket{ |
| 88 | TktVno: v, |
| 89 | Realm: r, |
| 90 | Sname: s, |
| 91 | EncPart: e, |
| 92 | } |
| 93 | var units = []struct { |
| 94 | got interface{} |
| 95 | exp interface{} |
| 96 | }{ |
| 97 | {ticket.TktVno, v}, |
| 98 | {ticket.Realm, r}, |
| 99 | {ticket.Sname, s}, |
| 100 | {ticket.EncPart, e}, |
| 101 | } |
| 102 | for _, u := range units { |
| 103 | if !reflect.DeepEqual(u.exp, u.got) { |
| 104 | t.Fatalf("expected '%v', got '%v'", u.exp, u.got) |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func TestKrb5Address(t *testing.T) { |
| 110 | x := 0 |
| 111 | y := []byte{} |
| 112 | addr := Krb5Address{ |
| 113 | AddrType: x, |
| 114 | Krb5Address: y, |
| 115 | } |
| 116 | var units = []struct { |
| 117 | got interface{} |
| 118 | exp interface{} |
| 119 | }{ |
| 120 | {addr.AddrType, x}, |
| 121 | {addr.Krb5Address, y}, |
| 122 | } |
| 123 | for _, u := range units { |
| 124 | if !reflect.DeepEqual(u.exp, u.got) { |
| 125 | t.Fatalf("expected '%v', got '%v'", u.exp, u.got) |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func TestKrb5PnData(t *testing.T) { |
| 131 | x := 0 |
| 132 | y := []byte{} |
| 133 | addr := Krb5PnData{ |
| 134 | Krb5PnDataType: x, |
| 135 | Krb5PnDataValue: y, |
| 136 | } |
| 137 | var units = []struct { |
| 138 | got interface{} |
| 139 | exp interface{} |
| 140 | }{ |
| 141 | {addr.Krb5PnDataType, x}, |
| 142 | {addr.Krb5PnDataValue, y}, |
| 143 | } |
| 144 | for _, u := range units { |
| 145 | if !reflect.DeepEqual(u.exp, u.got) { |
| 146 | t.Fatalf("expected '%v', got '%v'", u.exp, u.got) |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | func TestKrb5ReqBody(t *testing.T) { |
| 152 | e := []int{} |
| 153 | a := []Krb5Address{} |
| 154 | k := []Krb5Ticket{} |
| 155 | req := Krb5ReqBody{ |
| 156 | Etype: e, |
| 157 | Krb5Addresses: a, |
| 158 | AdditionalKrb5Tickets: k, |
| 159 | } |
| 160 | var units = []struct { |
| 161 | got interface{} |
| 162 | exp interface{} |
| 163 | }{ |
| 164 | {req.KDCOptions, asn1.BitString{}}, |
| 165 | {req.Cname, Krb5PrincipalName{}}, |
| 166 | {req.Realm, ""}, |
| 167 | {req.Sname, Krb5PrincipalName{}}, |
| 168 | {req.From, time.Time{}}, |
| 169 | {req.Till, time.Time{}}, |
| 170 | {req.Rtime, time.Time{}}, |
| 171 | {req.Nonce, 0}, |
| 172 | {req.Etype, e}, |
| 173 | {req.Krb5Addresses, a}, |
| 174 | {req.EncAuthData, Krb5EncryptedData{}}, |
| 175 | {req.AdditionalKrb5Tickets, k}, |
| 176 | } |
| 177 | for _, u := range units { |
| 178 | if !reflect.DeepEqual(u.exp, u.got) { |
| 179 | t.Fatalf("expected '%v', got '%v'", u.exp, u.got) |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | } |
| 184 | |
| 185 | func TestKrb5Request(t *testing.T) { |
| 186 | p := []Krb5PnData{} |
| 187 | req := Krb5Request{ |
| 188 | Krb5PnData: p, |
| 189 | } |
| 190 | var units = []struct { |
| 191 | got interface{} |
| 192 | exp interface{} |
| 193 | }{ |
| 194 | {req.Pvno, 0}, |
| 195 | {req.MsgType, 0}, |
| 196 | {req.Krb5PnData, p}, |
| 197 | {req.ReqBody, Krb5ReqBody{}}, |
| 198 | } |
| 199 | for _, u := range units { |
| 200 | if !reflect.DeepEqual(u.exp, u.got) { |
| 201 | t.Fatalf("expected '%v', got '%v'", u.exp, u.got) |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | } |
| 206 | |
| 207 | // TODO: add test for func (kdc Krb5Request) String() |
| 208 | // TODO: add test for func (pd Krb5PnData) getParsedValue() |