File: browserpass_test.go

package info (click to toggle)
browserpass 2.0.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 568 kB
  • sloc: sh: 128; makefile: 112
file content (61 lines) | stat: -rw-r--r-- 1,307 bytes parent folder | download
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
package browserpass

import (
	"strings"
	"testing"

	"github.com/gokyle/twofactor"
)

func TestParseLogin(t *testing.T) {
	r := strings.NewReader("password\n\nfoo\nlogin: bar")

	login, err := parseLogin(r)
	if err != nil {
		t.Fatal(err)
	}

	if login.Password != "password" {
		t.Errorf("Password is %s, expected %s", login.Password, "password")
	}
	if login.Username != "bar" {
		t.Errorf("Username is %s, expected %s", login.Username, "bar")
	}
}

func TestOtp(t *testing.T) {
	r := strings.NewReader("password\n\nfoo\nlogin: bar\notpauth://totp/totp-secret?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ&issuer=test")

	login, err := parseLogin(r)
	if err != nil {
		t.Fatal(err)
	}

	if login.OTPLabel != "totp-secret" {
		t.Errorf("OTPLabel is '%s', expected '%s'", login.OTPLabel, "totp-secret")
	}

	o, err := twofactor.NewGoogleTOTP("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ")
	if err != nil {
		t.Error(err)
	}

	otp := o.OTP()

	if login.OTP != otp {
		t.Errorf("OTP is %s, expected %s", login.OTP, otp)
	}
}

func TestGuessUsername(t *testing.T) {
	tests := map[string]string{
		"foo":     "",
		"foo/bar": "bar",
	}

	for input, expected := range tests {
		if username := guessUsername(input); username != expected {
			t.Errorf("guessUsername(%s): expected %s, got %s", input, expected, username)
		}
	}
}