-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_test.go
60 lines (51 loc) · 1.13 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
package systemd
import (
"bufio"
"bytes"
"encoding/hex"
"io"
"os"
"strconv"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestAuthExternal(t *testing.T) {
const authResp = "OK eb50e12940d90495b897de9f64090a3e\r\n"
got := bytes.Buffer{}
w := bufio.NewWriter(&got)
rw := bufio.NewReadWriter(
bufio.NewReader(bytes.NewBufferString(authResp)),
w,
)
if err := authExternal(rw); err != nil {
t.Fatal(err)
}
w.Flush()
var want bytes.Buffer
{
uid := strconv.Itoa(os.Geteuid())
want.WriteByte(0)
want.WriteString("AUTH EXTERNAL ")
want.WriteString(hex.EncodeToString([]byte(uid)))
want.WriteString("\r\n")
want.WriteString("BEGIN\r\n")
}
if diff := cmp.Diff(want.String(), got.String()); diff != "" {
t.Fatal(diff)
}
}
func BenchmarkAuthExternal(b *testing.B) {
authResp := bytes.NewReader([]byte("OK eb50e12940d90495b897de9f64090a3e\r\n"))
r := bufio.NewReader(authResp)
got := bytes.Buffer{}
w := bufio.NewWriter(&got)
rw := bufio.NewReadWriter(r, w)
b.ResetTimer()
for i := 0; i < b.N; i++ {
authResp.Seek(0, io.SeekStart)
got.Reset()
if err := authExternal(rw); err != nil {
b.Fatal(err)
}
}
}