forked from jsimonetti/go-spice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_test.go
66 lines (53 loc) · 1.2 KB
/
auth_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
64
65
66
package spice
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"io"
"log"
"testing"
)
func TestAuthSpiceSave(t *testing.T) {
auth := newAuthSpice(t)
auth.SaveAddress("123456")
if auth.LoadAddress() != "123456" {
t.Errorf("address saved and loaded mismatch")
}
auth.SaveToken("123456")
if auth.LoadToken() != "123456" {
t.Errorf("tokens saved and loaded mismatch")
}
}
func TestAuthSpiceToken(t *testing.T) {
auth := newAuthSpice(t)
password := "123456"
// crypto/rand.Reader is a good source of entropy for randomizing the
// encryption function.
rng := rand.Reader
pubkey := auth.privateKey.Public().(*rsa.PublicKey)
ciphertext, err := rsa.EncryptOAEP(sha1.New(), rng, pubkey, []byte(password), []byte{})
if err != nil {
panic(err)
}
auth.tenant.(io.Writer).Write(ciphertext)
token, err := auth.Token()
if err != nil {
log.Fatalf("unexpected error %#v", err)
}
if token != password {
log.Fatalf("wrong password received")
}
}
func newAuthSpice(t *testing.T) *authSpice {
t.Helper()
key, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil {
panic(err)
}
a := &authSpice{
tenant: bytes.NewBuffer(make([]byte, 0, 0)),
privateKey: key,
}
return a
}