File: compact_test.go

package info (click to toggle)
golang-github-openpubkey-openpubkey 0.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 964 kB
  • sloc: makefile: 10
file content (153 lines) | stat: -rw-r--r-- 6,646 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2024 OpenPubkey
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

package pktoken

import (
	"testing"

	"github.com/stretchr/testify/require"
)

func TestBuildCompact(t *testing.T) {
	testCases := []struct {
		name         string
		tokens       [][]byte
		freshIDToken []byte
		expError     string
		expPktCom    []byte
	}{
		{name: "happy case one tokens",
			tokens: [][]byte{
				// base64(one fake protected).base64(fake payload).base64(one fake sig)
				[]byte(`MWZha2Vwcm90ZWN0ZWQ.ZmFrZSBwYXlsb2Fk.b25lIGZha2Ugc2ln`)},
			expPktCom: []byte(`ZmFrZSBwYXlsb2Fk:MWZha2Vwcm90ZWN0ZWQ:b25lIGZha2Ugc2ln`),
		},
		{name: "happy case two tokens",
			tokens: [][]byte{
				// base64(one fake protected).base64(fake payload).base64(one fake sig)
				[]byte(`MWZha2Vwcm90ZWN0ZWQ.ZmFrZSBwYXlsb2Fk.b25lIGZha2Ugc2ln`),
				// base64(two fake protected).base64(fake payload).base64(two fake sig)
				[]byte(`dHdvIGZha2UgcHJvdGVjdGVk.ZmFrZSBwYXlsb2Fk.dHdvIGZha2Ugc2ln`)},
			expPktCom: []byte(`ZmFrZSBwYXlsb2Fk:MWZha2Vwcm90ZWN0ZWQ:b25lIGZha2Ugc2ln:dHdvIGZha2UgcHJvdGVjdGVk:dHdvIGZha2Ugc2ln`),
		},
		{name: "happy case two tokens and refreshed ID Token",
			tokens: [][]byte{
				// base64(one fake protected).base64(fake payload).base64(one fake sig)
				[]byte(`MWZha2Vwcm90ZWN0ZWQ.ZmFrZSBwYXlsb2Fk.b25lIGZha2Ugc2ln`),
				// base64(two fake protected).base64(fake payload).base64(two fake sig)
				[]byte(`dHdvIGZha2UgcHJvdGVjdGVk.ZmFrZSBwYXlsb2Fk.dHdvIGZha2Ugc2ln`)},
			// base64(refreshed protected).base64(refreshed payload).base64(refreshed sig)
			freshIDToken: []byte(`cmVmcmVzaGVkIHByb3RlY3RlZA.cmVmcmVzaGVkIHBheWxvYWQ.cmVmcmVzaGVkIHNpZw`),
			expPktCom:    []byte(`ZmFrZSBwYXlsb2Fk:MWZha2Vwcm90ZWN0ZWQ:b25lIGZha2Ugc2ln:dHdvIGZha2UgcHJvdGVjdGVk:dHdvIGZha2Ugc2ln.cmVmcmVzaGVkIHByb3RlY3RlZA.cmVmcmVzaGVkIHBheWxvYWQ.cmVmcmVzaGVkIHNpZw`),
		},
		{name: "different payloads",
			expError: "payloads in tokens are not the same",
			tokens: [][]byte{
				// base64(one fake protected).base64(fake payload).base64(one fake sig)
				[]byte(`MWZha2Vwcm90ZWN0ZWQ.ZmFrZSBwYXlsb2Fk.b25lIGZha2Ugc2ln`),
				// base64(two fake protected).base64(different payload).base64(two fake sig)
				[]byte(`dHdvIGZha2UgcHJvdGVjdGVk.ZGlmZmVyZW50IHBheWxvYWQ.dHdvIGZha2Ugc2ln`)},
		},
		{name: "malformed Token",
			expError: "invalid number of segments",
			tokens: [][]byte{
				// base64(one fake protected).base64(fake payload).base64(one fake sig)
				[]byte(`MWZha2Vwcm90ZWN0ZWQ.ZmFrZSBwYXlsb2Fk.b25lIGZha2Ugc2ln`),
				// malformed token
				[]byte(`..ZmFrZSBwYXl.sb2Fk.dHdvIGZha2Ugc2ln`)},
		},
		{name: "malformed Refreshed ID Token",
			expError: "invalid refreshed ID Token",
			tokens: [][]byte{
				// base64(one fake protected).base64(fake payload).base64(one fake sig)
				[]byte(`MWZha2Vwcm90ZWN0ZWQ.ZmFrZSBwYXlsb2Fk.b25lIGZha2Ugc2ln`),
				// base64(two fake protected).base64(fake payload).base64(two fake sig)
				[]byte(`dHdvIGZha2UgcHJvdGVjdGVk.ZmFrZSBwYXlsb2Fk.dHdvIGZha2Ugc2ln`)},
			// base64(refreshed protected).base64(refreshed payload).base64(refreshed sig)
			freshIDToken: []byte(`***=BAD!!!###`),
		},
	}
	for _, tc := range testCases {

		t.Run(tc.name, func(t *testing.T) {
			pktCom, err := CompactPKToken(tc.tokens, tc.freshIDToken)
			if tc.expError != "" {
				require.ErrorContains(t, err, tc.expError)
			} else {
				require.NoError(t, err)
				require.Equal(t, string(tc.expPktCom), string(pktCom))
			}
		})
	}
}

func TestFromCompact(t *testing.T) {
	testCases := []struct {
		name            string
		expTokens       [][]byte
		expFreshIDToken []byte
		expError        string
		pktCom          []byte
	}{
		{name: "happy case one tokens",
			expTokens: [][]byte{
				// base64(one fake protected).base64(fake payload).base64(one fake sig)
				[]byte(`MWZha2Vwcm90ZWN0ZWQ.ZmFrZSBwYXlsb2Fk.b25lIGZha2Ugc2ln`)},
			pktCom: []byte(`ZmFrZSBwYXlsb2Fk:MWZha2Vwcm90ZWN0ZWQ:b25lIGZha2Ugc2ln`),
		},
		{name: "happy case two tokens",
			// base64(one fake protected).base64(fake payload).base64(one fake sig)
			expTokens: [][]byte{
				// base64(one fake protected).base64(fake payload).base64(one fake sig)
				[]byte(`MWZha2Vwcm90ZWN0ZWQ.ZmFrZSBwYXlsb2Fk.b25lIGZha2Ugc2ln`),
				// base64(two fake protected).base64(fake payload).base64(two fake sig)
				[]byte(`dHdvIGZha2UgcHJvdGVjdGVk.ZmFrZSBwYXlsb2Fk.dHdvIGZha2Ugc2ln`)},
			pktCom: []byte(`ZmFrZSBwYXlsb2Fk:MWZha2Vwcm90ZWN0ZWQ:b25lIGZha2Ugc2ln:dHdvIGZha2UgcHJvdGVjdGVk:dHdvIGZha2Ugc2ln`),
		},
		{name: "happy case two tokens and refreshed ID Token",
			expTokens: [][]byte{
				// base64(one fake protected).base64(fake payload).base64(one fake sig)
				[]byte(`MWZha2Vwcm90ZWN0ZWQ.ZmFrZSBwYXlsb2Fk.b25lIGZha2Ugc2ln`),
				// base64(two fake protected).base64(fake payload).base64(two fake sig)
				[]byte(`dHdvIGZha2UgcHJvdGVjdGVk.ZmFrZSBwYXlsb2Fk.dHdvIGZha2Ugc2ln`)},
			// base64(refreshed protected).base64(refreshed payload).base64(refreshed sig)
			expFreshIDToken: []byte(`cmVmcmVzaGVkIHByb3RlY3RlZA.cmVmcmVzaGVkIHBheWxvYWQ.cmVmcmVzaGVkIHNpZw`),
			pktCom:          []byte(`ZmFrZSBwYXlsb2Fk:MWZha2Vwcm90ZWN0ZWQ:b25lIGZha2Ugc2ln:dHdvIGZha2UgcHJvdGVjdGVk:dHdvIGZha2Ugc2ln.cmVmcmVzaGVkIHByb3RlY3RlZA.cmVmcmVzaGVkIHBheWxvYWQ.cmVmcmVzaGVkIHNpZw`),
		},
		{name: "malformed Compact PK Token (invalid number of segments)",
			expError: "invalid number of segments",
			pktCom:   []byte(`dHdvIGZha2Ugc2ln:dHdvIGZha2Ugc2ln.cmVmcmVzaGVkIHByb3RlY3RlZA.cmVmcmVzaGVkIHBheWxvYWQ.cmVmcmVzaGVkIHNpZw`),
		},
		{name: "malformed Compact PK Token (invalid refreshed ID Token)",
			expError: "invalid refreshed ID Token",
			pktCom:   []byte(`ZmFrZSBwYXlsb2Fk:MWZha2Vwcm90ZWN0ZWQ:b25lIGZha2Ugc2ln:dHdvIGZha2UgcHJvdGVjdGVk:dHdvIGZha2Ugc2ln.BAD.REFRESHED.ID.TOKEN`),
		},
	}
	for _, tc := range testCases {

		t.Run(tc.name, func(t *testing.T) {
			tokens, freshIDToken, err := SplitCompactPKToken(tc.pktCom)
			if tc.expError != "" {
				require.ErrorContains(t, err, tc.expError)
			} else {
				require.NoError(t, err)
				require.Equal(t, tc.expTokens, tokens)
				require.Equal(t, tc.expFreshIDToken, freshIDToken)
			}
		})
	}
}