1package pb
2
3import (
4	"errors"
5	"reflect"
6	"testing"
7	"time"
8
9	"github.com/hashicorp/vault/sdk/helper/errutil"
10	"github.com/hashicorp/vault/sdk/helper/wrapping"
11	"github.com/hashicorp/vault/sdk/logical"
12)
13
14func TestTranslation_Errors(t *testing.T) {
15	errs := []error{
16		nil,
17		errors.New("test"),
18		errutil.UserError{Err: "test"},
19		errutil.InternalError{Err: "test"},
20		logical.CodedError(403, "test"),
21		&logical.StatusBadRequest{Err: "test"},
22		logical.ErrUnsupportedOperation,
23		logical.ErrUnsupportedPath,
24		logical.ErrInvalidRequest,
25		logical.ErrPermissionDenied,
26		logical.ErrMultiAuthzPending,
27	}
28
29	for _, err := range errs {
30		pe := ErrToProtoErr(err)
31		e := ProtoErrToErr(pe)
32
33		if !reflect.DeepEqual(e, err) {
34			t.Fatalf("Errs did not match: %#v, %#v", e, err)
35		}
36	}
37}
38
39func TestTranslation_StorageEntry(t *testing.T) {
40	tCases := []*logical.StorageEntry{
41		nil,
42		&logical.StorageEntry{Key: "key", Value: []byte("value")},
43		&logical.StorageEntry{Key: "key1", Value: []byte("value1"), SealWrap: true},
44		&logical.StorageEntry{Key: "key1", SealWrap: true},
45	}
46
47	for _, c := range tCases {
48		p := LogicalStorageEntryToProtoStorageEntry(c)
49		e := ProtoStorageEntryToLogicalStorageEntry(p)
50
51		if !reflect.DeepEqual(c, e) {
52			t.Fatalf("Entries did not match: %#v, %#v", e, c)
53		}
54	}
55}
56
57func TestTranslation_Request(t *testing.T) {
58	tCases := []*logical.Request{
59		nil,
60		&logical.Request{
61			ID:                       "ID",
62			ReplicationCluster:       "RID",
63			Operation:                logical.CreateOperation,
64			Path:                     "test/foo",
65			ClientToken:              "token",
66			ClientTokenAccessor:      "accessor",
67			DisplayName:              "display",
68			MountPoint:               "test",
69			MountType:                "secret",
70			MountAccessor:            "test-231234",
71			ClientTokenRemainingUses: 1,
72			EntityID:                 "tester",
73			PolicyOverride:           true,
74			Unauthenticated:          true,
75			Connection: &logical.Connection{
76				RemoteAddr: "localhost",
77			},
78		},
79		&logical.Request{
80			ID:                 "ID",
81			ReplicationCluster: "RID",
82			Operation:          logical.CreateOperation,
83			Path:               "test/foo",
84			Data: map[string]interface{}{
85				"string": "string",
86				"bool":   true,
87				"array":  []interface{}{"1", "2"},
88				"map": map[string]interface{}{
89					"key": "value",
90				},
91			},
92			Secret: &logical.Secret{
93				LeaseOptions: logical.LeaseOptions{
94					TTL:       time.Second,
95					MaxTTL:    time.Second,
96					Renewable: true,
97					Increment: time.Second,
98					IssueTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
99				},
100				InternalData: map[string]interface{}{
101					"role": "test",
102				},
103				LeaseID: "LeaseID",
104			},
105			Auth: &logical.Auth{
106				LeaseOptions: logical.LeaseOptions{
107					TTL:       time.Second,
108					MaxTTL:    time.Second,
109					Renewable: true,
110					Increment: time.Second,
111					IssueTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
112				},
113				InternalData: map[string]interface{}{
114					"role": "test",
115				},
116				DisplayName: "test",
117				Policies:    []string{"test", "Test"},
118				Metadata: map[string]string{
119					"test": "test",
120				},
121				ClientToken: "token",
122				Accessor:    "accessor",
123				Period:      5 * time.Second,
124				NumUses:     1,
125				EntityID:    "id",
126				Alias: &logical.Alias{
127					MountType:     "type",
128					MountAccessor: "accessor",
129					Name:          "name",
130				},
131				GroupAliases: []*logical.Alias{
132					&logical.Alias{
133						MountType:     "type",
134						MountAccessor: "accessor",
135						Name:          "name",
136					},
137				},
138			},
139			Headers: map[string][]string{
140				"X-Vault-Test": []string{"test"},
141			},
142			ClientToken:         "token",
143			ClientTokenAccessor: "accessor",
144			DisplayName:         "display",
145			MountPoint:          "test",
146			MountType:           "secret",
147			MountAccessor:       "test-231234",
148			WrapInfo: &logical.RequestWrapInfo{
149				TTL:      time.Second,
150				Format:   "token",
151				SealWrap: true,
152			},
153			ClientTokenRemainingUses: 1,
154			EntityID:                 "tester",
155			PolicyOverride:           true,
156			Unauthenticated:          true,
157		},
158	}
159
160	for _, c := range tCases {
161		p, err := LogicalRequestToProtoRequest(c)
162		if err != nil {
163			t.Fatal(err)
164		}
165		r, err := ProtoRequestToLogicalRequest(p)
166		if err != nil {
167			t.Fatal(err)
168		}
169
170		if !reflect.DeepEqual(c, r) {
171			t.Fatalf("Requests did not match: \n%#v, \n%#v", c, r)
172		}
173	}
174}
175
176func TestTranslation_Response(t *testing.T) {
177	tCases := []*logical.Response{
178		nil,
179		&logical.Response{
180			Data: map[string]interface{}{
181				"data": "blah",
182			},
183			Warnings: []string{"warning"},
184		},
185		&logical.Response{
186			Data: map[string]interface{}{
187				"string": "string",
188				"bool":   true,
189				"array":  []interface{}{"1", "2"},
190				"map": map[string]interface{}{
191					"key": "value",
192				},
193			},
194			Secret: &logical.Secret{
195				LeaseOptions: logical.LeaseOptions{
196					TTL:       time.Second,
197					MaxTTL:    time.Second,
198					Renewable: true,
199					Increment: time.Second,
200					IssueTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
201				},
202				InternalData: map[string]interface{}{
203					"role": "test",
204				},
205				LeaseID: "LeaseID",
206			},
207			Auth: &logical.Auth{
208				LeaseOptions: logical.LeaseOptions{
209					TTL:       time.Second,
210					MaxTTL:    time.Second,
211					Renewable: true,
212					Increment: time.Second,
213					IssueTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
214				},
215				InternalData: map[string]interface{}{
216					"role": "test",
217				},
218				DisplayName: "test",
219				Policies:    []string{"test", "Test"},
220				Metadata: map[string]string{
221					"test": "test",
222				},
223				ClientToken: "token",
224				Accessor:    "accessor",
225				Period:      5 * time.Second,
226				NumUses:     1,
227				EntityID:    "id",
228				Alias: &logical.Alias{
229					MountType:     "type",
230					MountAccessor: "accessor",
231					Name:          "name",
232				},
233				GroupAliases: []*logical.Alias{
234					&logical.Alias{
235						MountType:     "type",
236						MountAccessor: "accessor",
237						Name:          "name",
238					},
239				},
240			},
241			WrapInfo: &wrapping.ResponseWrapInfo{
242				TTL:             time.Second,
243				Token:           "token",
244				Accessor:        "accessor",
245				CreationTime:    time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
246				WrappedAccessor: "wrapped-accessor",
247				WrappedEntityID: "id",
248				Format:          "token",
249				CreationPath:    "test/foo",
250				SealWrap:        true,
251			},
252		},
253	}
254
255	for _, c := range tCases {
256		p, err := LogicalResponseToProtoResponse(c)
257		if err != nil {
258			t.Fatal(err)
259		}
260		r, err := ProtoResponseToLogicalResponse(p)
261		if err != nil {
262			t.Fatal(err)
263		}
264
265		if !reflect.DeepEqual(c, r) {
266			t.Fatalf("Requests did not match: \n%#v, \n%#v", c, r)
267		}
268	}
269}
270