| package pki |
| |
| import ( |
| "testing" |
| |
| "github.com/go-test/deep" |
| ) |
| |
| func TestParseClient(t *testing.T) { |
| flagPKIRealm = "hswaw.net" |
| |
| tests := []struct { |
| name string |
| want *ClientInfo |
| }{ |
| // Local cluster |
| {"foo.bar.svc.k0.hswaw.net", &ClientInfo{Cluster: "k0.hswaw.net", Principal: "bar.svc", Job: "foo"}}, |
| {"foo.bar.k0.hswaw.net", nil}, |
| |
| // Foreign cluster |
| {"foo.bar.svc.k1.hswaw.net", &ClientInfo{Cluster: "k1.hswaw.net", Principal: "bar.svc", Job: "foo"}}, |
| {"foo.bar.k1.hswaw.net", nil}, |
| |
| // Human admins (admins, as we know, don't have a real job) |
| {"q3k.person.hswaw.net", &ClientInfo{Cluster: "person.hswaw.net", Principal: "q3k", Job: ""}}, |
| |
| // External services |
| {"kasownik.external.hswaw.net", &ClientInfo{Cluster: "external.hswaw.net", Principal: "kasownik", Job: ""}}, |
| |
| // Broken. |
| {"foo.hswaw.net", nil}, |
| {"ldap.hackerspace.pl", nil}, |
| {"", nil}, |
| {"..what..plz...don.t.hack.me.hswaw.net", nil}, |
| } |
| |
| for i, te := range tests { |
| res, err := parseClientName(te.name) |
| if err != nil { |
| if te.want != nil { |
| t.Errorf("#%d: wanted result, got err %v", i, err) |
| } |
| continue |
| } |
| |
| if te.want == nil { |
| t.Errorf("#%d: wanted err, got %+v", i, res) |
| continue |
| } |
| |
| if diff := deep.Equal(*te.want, *res); diff != nil { |
| t.Errorf("#%d: res diff: %v", i, diff) |
| continue |
| } |
| } |
| } |
| |
| func TestCheckPerson(t *testing.T) { |
| flagPKIRealm = "hswaw.net" |
| |
| res, err := parseClientName("q3k.person.hswaw.net") |
| if err != nil { |
| t.Fatalf("err: %v", err) |
| } |
| |
| if want, got := "q3k", res.Person(); want != got { |
| t.Fatalf("wanted %q, got %q", want, got) |
| } |
| } |