blob: 353c579cf500b0a5b8ddd904eee1853d64e8c081 [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// 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 Simsa5293dcb2014-05-10 09:56:38 -07005package handlers
6
7import (
Nicolas LaCasse4c0a4b12014-12-12 14:51:20 -08008 "encoding/base64"
9 "encoding/json"
Jiri Simsa5293dcb2014-05-10 09:56:38 -070010 "net/http"
11 "net/http/httptest"
Jiri Simsa5293dcb2014-05-10 09:56:38 -070012 "reflect"
Nicolas LaCasse4c0a4b12014-12-12 14:51:20 -080013 "sort"
Jiri Simsa5293dcb2014-05-10 09:56:38 -070014 "testing"
15
Jiri Simsa6ac95222015-02-23 16:11:49 -080016 "v.io/v23/security"
Cosmos Nicolaouf889c732014-10-16 20:46:54 -070017
Suharsh Sivakumarc0048112015-03-19 11:48:28 -070018 "v.io/x/ref/services/identity"
Asim Shankar4a698282015-03-21 21:59:18 -070019 "v.io/x/ref/test/testutil"
Jiri Simsa5293dcb2014-05-10 09:56:38 -070020)
21
Nicolas LaCasse4c0a4b12014-12-12 14:51:20 -080022func TestBlessingRoot(t *testing.T) {
Asim Shankarc3512632015-04-02 20:33:34 -070023 // TODO(ashankar,ataly): Handle multiple root names?
24 blessingNames := []string{"test-root"}
Asim Shankar4a698282015-03-21 21:59:18 -070025 p := testutil.NewPrincipal(blessingNames...)
Nicolas LaCasse4c0a4b12014-12-12 14:51:20 -080026
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 Sivakumarc0048112015-03-19 11:48:28 -070034 var res identity.BlessingRootResponse
Nicolas LaCasse4c0a4b12014-12-12 14:51:20 -080035 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}