-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc13_test.go
62 lines (52 loc) · 1.22 KB
/
c13_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
package main
import (
"crypto/aes"
"net/url"
"testing"
)
func TestProfileFor(t *testing.T) {
const email = "foo@bar.com"
if got, err := profileFor(email); err != nil {
t.Errorf("unexpected error: %s", err)
} else {
// it encodes the '@' char as '%40'
t.Log(got)
}
}
func TestCreateAdminUser(t *testing.T) {
key, err := randomBytes(aes.BlockSize, aes.BlockSize)
if err != nil {
t.Fatalf("generating random AES key: %s", err)
}
encryptionOracle := func(email []byte) ([]byte, error) {
userProfile, err := profileFor(string(email))
if err != nil {
return nil, err
}
return encryptAesEcb([]byte(userProfile), key)
}
adminOracle := func(cipherText []byte) (bool, error) {
const (
roleKey = "role"
adminRole = "admin"
)
plainText, err := decryptAesEcb(cipherText, key)
if err != nil {
return false, err
}
adminProfile := delPadPkcs7(plainText)
v, err := url.ParseQuery(string(adminProfile))
if err != nil {
return false, err
}
t.Log(string(adminProfile))
return v.Get(roleKey) == adminRole, nil
}
isAdmin, err := createAdminProfile(encryptionOracle, adminOracle)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !isAdmin {
t.Fatalf("Profile is not admin")
}
}