Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 5 | package handlers |
| 6 | |
| 7 | import ( |
Nicolas LaCasse | 4c0a4b1 | 2014-12-12 14:51:20 -0800 | [diff] [blame] | 8 | "encoding/base64" |
| 9 | "encoding/json" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 10 | "net/http" |
| 11 | "net/http/httptest" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 12 | "reflect" |
Nicolas LaCasse | 4c0a4b1 | 2014-12-12 14:51:20 -0800 | [diff] [blame] | 13 | "sort" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 14 | "testing" |
| 15 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 16 | "v.io/v23/security" |
Cosmos Nicolaou | f889c73 | 2014-10-16 20:46:54 -0700 | [diff] [blame] | 17 | |
Suharsh Sivakumar | c004811 | 2015-03-19 11:48:28 -0700 | [diff] [blame] | 18 | "v.io/x/ref/services/identity" |
Asim Shankar | 4a69828 | 2015-03-21 21:59:18 -0700 | [diff] [blame] | 19 | "v.io/x/ref/test/testutil" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 20 | ) |
| 21 | |
Nicolas LaCasse | 4c0a4b1 | 2014-12-12 14:51:20 -0800 | [diff] [blame] | 22 | func TestBlessingRoot(t *testing.T) { |
Asim Shankar | c351263 | 2015-04-02 20:33:34 -0700 | [diff] [blame^] | 23 | // TODO(ashankar,ataly): Handle multiple root names? |
| 24 | blessingNames := []string{"test-root"} |
Asim Shankar | 4a69828 | 2015-03-21 21:59:18 -0700 | [diff] [blame] | 25 | p := testutil.NewPrincipal(blessingNames...) |
Nicolas LaCasse | 4c0a4b1 | 2014-12-12 14:51:20 -0800 | [diff] [blame] | 26 | |
| 27 | ts := httptest.NewServer(BlessingRoot{p}) |
| 28 | defer ts.Close() |
| 29 | response, err := http.Get(ts.URL) |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | dec := json.NewDecoder(response.Body) |
Suharsh Sivakumar | c004811 | 2015-03-19 11:48:28 -0700 | [diff] [blame] | 34 | var res identity.BlessingRootResponse |
Nicolas LaCasse | 4c0a4b1 | 2014-12-12 14:51:20 -0800 | [diff] [blame] | 35 | if err := dec.Decode(&res); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | |
| 39 | // Check that the names are correct. |
| 40 | sort.Strings(blessingNames) |
| 41 | sort.Strings(res.Names) |
| 42 | if !reflect.DeepEqual(res.Names, blessingNames) { |
| 43 | t.Errorf("Response has incorrect name. Got %v, want %v", res.Names, blessingNames) |
| 44 | } |
| 45 | |
| 46 | // Check that the public key is correct. |
| 47 | gotMarshalled, err := base64.URLEncoding.DecodeString(res.PublicKey) |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | got, err := security.UnmarshalPublicKey(gotMarshalled) |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | if want := p.PublicKey(); !reflect.DeepEqual(got, want) { |
| 56 | t.Errorf("Response has incorrect public key. Got %v, want %v", got, want) |
| 57 | } |
| 58 | } |