-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcommon_test.go
63 lines (54 loc) · 1.5 KB
/
common_test.go
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
package gose
import (
"encoding/base64"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"strings"
"testing"
)
const (
ModeEncrypt = iota // blockModeCloser is in encrypt mode
ModeDecrypt // blockModeCloser is in decrypt mode
)
const (
mockExpectedCleartext = "decrypted"
mockExpectedCiphertext = "encrypted"
)
type MockBlockMode struct {
mock.Mock
mode int
}
func (mbm *MockBlockMode) BlockSize() int {
args := mbm.Called()
return args.Get(0).(int)
}
// In order to simulate a behavior tangible for tests, i.e encrypt or decrypt according to the mode, we simply return a
// string that inform us if we properly were in the encrypt or decrypt mode
func (mbm *MockBlockMode) CryptBlocks(dst, src []byte) {
switch mbm.mode {
case ModeDecrypt:
copy(dst, mockExpectedCleartext)
case ModeEncrypt:
copy(dst, mockExpectedCiphertext)
default:
panic("unexpected mode")
}
}
func VerifyJWEStructure(t *testing.T, jwe string) {
require.NotEmpty(t, jwe)
// verify the structure
splits := strings.Split(jwe, ".")
require.Equal(t, 5, len(splits))
// For direct encryption, the encrypted key is nil
// we expected an empty string for the second part of the JWE
require.Empty(t, splits[1])
// other parts should not be empty
require.NotEmpty(t, splits[0])
require.NotEmpty(t, splits[2])
require.NotEmpty(t, splits[3])
require.NotEmpty(t, splits[4])
// verify IV
iv, err := base64.RawURLEncoding.DecodeString(splits[2])
require.NoError(t, err)
require.NotEmpty(t, iv)
}