Sergiusz Bazanski | 6f773e0 | 2019-10-02 20:46:48 +0200 | [diff] [blame] | 1 | package pki |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/go-test/deep" |
| 7 | ) |
| 8 | |
| 9 | func TestParseClient(t *testing.T) { |
| 10 | flagPKIRealm = "hswaw.net" |
| 11 | |
| 12 | tests := []struct { |
| 13 | name string |
| 14 | want *ClientInfo |
| 15 | }{ |
| 16 | // Local cluster |
| 17 | {"foo.bar.svc.k0.hswaw.net", &ClientInfo{Cluster: "k0.hswaw.net", Principal: "bar.svc", Job: "foo"}}, |
| 18 | {"foo.bar.k0.hswaw.net", nil}, |
| 19 | |
| 20 | // Foreign cluster |
| 21 | {"foo.bar.svc.k1.hswaw.net", &ClientInfo{Cluster: "k1.hswaw.net", Principal: "bar.svc", Job: "foo"}}, |
| 22 | {"foo.bar.k1.hswaw.net", nil}, |
| 23 | |
| 24 | // Human admins (admins, as we know, don't have a real job) |
| 25 | {"q3k.person.hswaw.net", &ClientInfo{Cluster: "person.hswaw.net", Principal: "q3k", Job: ""}}, |
| 26 | |
| 27 | // External services |
| 28 | {"kasownik.external.hswaw.net", &ClientInfo{Cluster: "external.hswaw.net", Principal: "kasownik", Job: ""}}, |
| 29 | |
| 30 | // Broken. |
| 31 | {"foo.hswaw.net", nil}, |
| 32 | {"ldap.hackerspace.pl", nil}, |
| 33 | {"", nil}, |
| 34 | {"..what..plz...don.t.hack.me.hswaw.net", nil}, |
| 35 | } |
| 36 | |
| 37 | for i, te := range tests { |
| 38 | res, err := parseClientName(te.name) |
| 39 | if err != nil { |
| 40 | if te.want != nil { |
| 41 | t.Errorf("#%d: wanted result, got err %v", i, err) |
| 42 | } |
| 43 | continue |
| 44 | } |
| 45 | |
| 46 | if te.want == nil { |
| 47 | t.Errorf("#%d: wanted err, got %+v", i, res) |
| 48 | continue |
| 49 | } |
| 50 | |
| 51 | if diff := deep.Equal(*te.want, *res); diff != nil { |
| 52 | t.Errorf("#%d: res diff: %v", i, diff) |
| 53 | continue |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func TestCheckPerson(t *testing.T) { |
| 59 | flagPKIRealm = "hswaw.net" |
| 60 | |
| 61 | res, err := parseClientName("q3k.person.hswaw.net") |
| 62 | if err != nil { |
| 63 | t.Fatalf("err: %v", err) |
| 64 | } |
| 65 | |
| 66 | if want, got := "q3k", res.Person(); want != got { |
| 67 | t.Fatalf("wanted %q, got %q", want, got) |
| 68 | } |
| 69 | } |