-
Notifications
You must be signed in to change notification settings - Fork 0
/
gs2_header_test.go
66 lines (58 loc) · 1.7 KB
/
gs2_header_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 scramauth
import (
"bytes"
"encoding/base64"
"fmt"
"testing"
)
func TestEncode(t *testing.T) {
p := NewParams()
p.Append([]Param{
{Key: []byte{'n'}, Val: []byte("helloworld")},
{Key: []byte{'r'}, Val: []byte("123456")}}...)
header := Gs2Header{Authzid: []byte("123456"), CB: TlsUnique, Params: p}
var buf bytes.Buffer
if err := header.Encode(&buf); err != nil {
t.Fatalf("encoding gs2 header error: %s", err.Error())
}
res := base64.StdEncoding.EncodeToString([]byte("p=tls-unique,a=123456,n=helloworld,r=123456"))
if buf.String() != res {
fmt.Printf("%s - %s\n", buf.String(), res)
t.Fatalf("encode gs2 header error")
}
}
func TestEncode_noAuthId(t *testing.T) {
str := base64.StdEncoding.EncodeToString([]byte("p=tls-unique,,n=helloworld,r=123456"))
header := Gs2Header{}
buf1 := bytes.NewBuffer([]byte(str))
if err := header.Decode(buf1); err != nil {
t.Fatalf(err.Error())
}
p := header.Params.All()
if CB(string(header.CB)) != TlsUnique ||
header.Params.Len() != 2 ||
string(p[0].Key) != "n" ||
string(p[0].Val) != "helloworld" ||
string(p[1].Key) != "r" ||
string(p[1].Val) != "123456" {
t.Fatalf("decode gs2 header error")
}
}
func TestDecode(t *testing.T) {
str := "cD10bHMtdW5pcXVlLGE9MTIzNDU2LG49aGVsbG93b3JsZCxyPTEyMzQ1Ng=="
header := Gs2Header{}
buf1 := bytes.NewBuffer([]byte(str))
if err := header.Decode(buf1); err != nil {
t.Fatalf(err.Error())
}
p := header.Params.All()
if string(header.Authzid) != "123456" ||
CB(string(header.CB)) != TlsUnique ||
header.Params.Len() != 2 ||
string(p[0].Key) != "n" ||
string(p[0].Val) != "helloworld" ||
string(p[1].Key) != "r" ||
string(p[1].Val) != "123456" {
t.Fatalf("decode gs2 header error")
}
}