Explorar el Código

Add tests to subpackages

tags/v0.9.2-beta
Daniel Oaks hace 6 años
padre
commit
378d55af65

+ 10
- 0
irc/caps/constants.go Ver fichero

@@ -49,3 +49,13 @@ const (
49 49
 func (capability Capability) Name() string {
50 50
 	return string(capability)
51 51
 }
52
+
53
+// Version is used to select which max version of CAP the client supports.
54
+type Version uint
55
+
56
+const (
57
+	// Cap301 refers to the base CAP spec.
58
+	Cap301 Version = 301
59
+	// Cap302 refers to the IRCv3.2 CAP spec.
60
+	Cap302 Version = 302
61
+)

+ 46
- 0
irc/caps/set_test.go Ver fichero

@@ -0,0 +1,46 @@
1
+// Copyright (c) 2017 Daniel Oaks <daniel@danieloaks.net>
2
+// released under the MIT license
3
+
4
+package caps
5
+
6
+import "testing"
7
+import "reflect"
8
+
9
+func TestSets(t *testing.T) {
10
+	s1 := NewSet()
11
+
12
+	s1.Enable(AccountTag, EchoMessage, UserhostInNames)
13
+
14
+	if !s1.Has(AccountTag, EchoMessage, UserhostInNames) {
15
+		t.Error("Did not have the tags we expected")
16
+	}
17
+
18
+	if s1.Has(AccountTag, EchoMessage, STS, UserhostInNames) {
19
+		t.Error("Has() returned true when we don't have all the given capabilities")
20
+	}
21
+
22
+	s1.Disable(AccountTag)
23
+
24
+	if s1.Has(AccountTag) {
25
+		t.Error("Disable() did not correctly disable the given capability")
26
+	}
27
+
28
+	enabledCaps := make(map[Capability]bool)
29
+	for _, capab := range s1.List() {
30
+		enabledCaps[capab] = true
31
+	}
32
+	expectedCaps := map[Capability]bool{
33
+		EchoMessage:     true,
34
+		UserhostInNames: true,
35
+	}
36
+	if !reflect.DeepEqual(enabledCaps, expectedCaps) {
37
+		t.Errorf("Enabled and expected capability lists do not match: %v, %v", enabledCaps, expectedCaps)
38
+	}
39
+
40
+	// make sure re-enabling doesn't add to the count or something weird like that
41
+	s1.Enable(EchoMessage)
42
+
43
+	if s1.Count() != 2 {
44
+		t.Error("Count() did not match expected capability count")
45
+	}
46
+}

+ 0
- 14
irc/caps/version.go Ver fichero

@@ -1,14 +0,0 @@
1
-// Copyright (c) 2017 Daniel Oaks <daniel@danieloaks.net>
2
-// released under the MIT license
3
-
4
-package caps
5
-
6
-// Version is used to select which max version of CAP the client supports.
7
-type Version uint
8
-
9
-const (
10
-	// Cap301 refers to the base CAP spec.
11
-	Cap301 Version = 301
12
-	// Cap302 refers to the IRCv3.2 CAP spec.
13
-	Cap302 Version = 302
14
-)

+ 28
- 0
irc/isupport/list_test.go Ver fichero

@@ -9,6 +9,34 @@ import (
9 9
 )
10 10
 
11 11
 func TestISUPPORT(t *testing.T) {
12
+	// test multiple output replies
13
+	tListLong := NewList()
14
+	tListLong.AddNoValue("1")
15
+	tListLong.AddNoValue("2")
16
+	tListLong.AddNoValue("3")
17
+	tListLong.AddNoValue("4")
18
+	tListLong.AddNoValue("5")
19
+	tListLong.AddNoValue("6")
20
+	tListLong.AddNoValue("7")
21
+	tListLong.AddNoValue("8")
22
+	tListLong.AddNoValue("9")
23
+	tListLong.AddNoValue("A")
24
+	tListLong.AddNoValue("B")
25
+	tListLong.AddNoValue("C")
26
+	tListLong.AddNoValue("D")
27
+	tListLong.AddNoValue("E")
28
+	tListLong.AddNoValue("F")
29
+	tListLong.RegenerateCachedReply()
30
+
31
+	longReplies := [][]string{
32
+		{"1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "are supported by this server"},
33
+		{"E", "F", "are supported by this server"},
34
+	}
35
+
36
+	if !reflect.DeepEqual(tListLong.CachedReply, longReplies) {
37
+		t.Errorf("Multiple output replies did not match, got [%v]", longReplies)
38
+	}
39
+
12 40
 	// create first list
13 41
 	tList1 := NewList()
14 42
 	tList1.Add("SASL", "yes")

+ 3
- 3
irc/passwd/salted.go Ver fichero

@@ -36,9 +36,9 @@ type SaltedManager struct {
36 36
 
37 37
 // NewSaltedManager returns a new SaltedManager with the given salt.
38 38
 func NewSaltedManager(salt []byte) SaltedManager {
39
-	var sm SaltedManager
40
-	sm.salt = salt
41
-	return sm
39
+	return SaltedManager{
40
+		salt: salt,
41
+	}
42 42
 }
43 43
 
44 44
 // assemblePassword returns an assembled slice of bytes for the given password details.

+ 86
- 0
irc/passwd/salted_test.go Ver fichero

@@ -0,0 +1,86 @@
1
+// Copyright (c) 2016 Daniel Oaks <daniel@danieloaks.net>
2
+// released under the MIT license
3
+
4
+package passwd
5
+
6
+import (
7
+	"encoding/base64"
8
+	"testing"
9
+)
10
+
11
+type SaltedPasswordTest struct {
12
+	ManagerSalt string
13
+	Salt        string
14
+	Hash        string
15
+	Password    string
16
+}
17
+
18
+var SaltedPasswords = []SaltedPasswordTest{
19
+	{
20
+		ManagerSalt: "3TPITDVf/NGb4OlCyV1uZNW1H7zy3BFos+Dsu7dj",
21
+		Salt:        "b6oVqshJUfcm1zWEtqwKqUVylqLONAZfqt17ns+Y",
22
+		Hash:        "JDJhJDE0JFYuT28xOFFNZldaaTI1UWpzNENMeHVKdm5vS1lkL2tFL1lFVkQ2a0loUEY2Vzk3UTZSVDVP",
23
+		Password:    "test",
24
+	},
25
+	{
26
+		ManagerSalt: "iNGeNEfuPihM8kYDZ/C6qAJ0JERKeKkUYp6wYDU0",
27
+		Salt:        "U7TA6k6VLSLHfdjSsQH0vc3Jqq6cUezJNyd0DC9c",
28
+		Hash:        "JDJhJDE0JEguY2Rva3VOTVRrNm1VeGdXWjAwamViMGNvV0xYZFdHcTZjenFCRWE3Ymt2N1JiSFJDZlYy",
29
+		Password:    "test2",
30
+	},
31
+	{
32
+		ManagerSalt: "ghKJaaSNTjuFmgLRqrgY4FGfx8wXEGOBE02PZvbv",
33
+		Salt:        "NO/mtrMhGjX1FGDGdpGrDJIi4jxsb0aFa7ybId7r",
34
+		Hash:        "JDJhJDE0JEI0M055Z2NDcjNUanB5ZEJ5MzUybi5FT3o4Y1MyNXp2c1NDVS9hS0hOcUxSRDZTWmUxTnN5",
35
+		Password:    "supermono",
36
+	},
37
+}
38
+
39
+func TestSaltedPassword(t *testing.T) {
40
+	// check newly-generated password
41
+	managerSalt, err := NewSalt()
42
+	if err != nil {
43
+		t.Error("Could not generate manager salt")
44
+	}
45
+
46
+	salt, err := NewSalt()
47
+	if err != nil {
48
+		t.Error("Could not generate salt")
49
+	}
50
+
51
+	manager := NewSaltedManager(managerSalt)
52
+
53
+	passHash, err := manager.GenerateFromPassword(salt, "this is a test password")
54
+	if err != nil {
55
+		t.Error("Could not generate from password")
56
+	}
57
+
58
+	if manager.CompareHashAndPassword(passHash, salt, "this is a test password") != nil {
59
+		t.Error("Generated password does not match")
60
+	}
61
+
62
+	// check our stored passwords
63
+	for i, info := range SaltedPasswords {
64
+		// decode strings to bytes
65
+		managerSalt, err = base64.StdEncoding.DecodeString(info.ManagerSalt)
66
+		if err != nil {
67
+			t.Errorf("Could not decode manager salt for test %d", i)
68
+		}
69
+
70
+		salt, err := base64.StdEncoding.DecodeString(info.Salt)
71
+		if err != nil {
72
+			t.Errorf("Could not decode salt for test %d", i)
73
+		}
74
+
75
+		hash, err := base64.StdEncoding.DecodeString(info.Hash)
76
+		if err != nil {
77
+			t.Errorf("Could not decode hash for test %d", i)
78
+		}
79
+
80
+		// make sure our test values are still correct
81
+		manager := NewSaltedManager(managerSalt)
82
+		if manager.CompareHashAndPassword(hash, salt, info.Password) != nil {
83
+			t.Errorf("Password does not match for [%s]", info.Password)
84
+		}
85
+	}
86
+}

+ 54
- 0
irc/passwd/unsalted_test.go Ver fichero

@@ -0,0 +1,54 @@
1
+// Copyright (c) 2016 Daniel Oaks <daniel@danieloaks.net>
2
+// released under the MIT license
3
+
4
+package passwd
5
+
6
+import (
7
+	"testing"
8
+)
9
+
10
+var UnsaltedPasswords = map[string]string{
11
+	"test1":     "JDJhJDA0JFFwZ1V0RWZTMFVaMkFrdlRrTG9FZk9FNEZWbWkvVEhsdGFnSXlIUC5wVmpYTkNERFJPNlcu",
12
+	"test2":     "JDJhJDA0JHpQTGNqczlIanc3V2NFQ3JEOVlTM09aNkRTbGRsQzRyNmt3Q01aSUs2Y2xyWURVODZ1V0px",
13
+	"supernomo": "JDJhJDA0JHdJekhnQmk1VXQ4WUphL0pIL0tXQWVKVXJ6dXcvRDJ3WFljWW9XOGhzNllIbW1DRlFkL1VL",
14
+}
15
+
16
+func TestUnsaltedPassword(t *testing.T) {
17
+	for password, hash := range UnsaltedPasswords {
18
+		generatedHash, err := GenerateEncodedPassword(password)
19
+		if err != nil {
20
+			t.Errorf("Could not hash password for [%s]: %s", password, err.Error())
21
+		}
22
+
23
+		hashBytes, err := DecodePasswordHash(hash)
24
+		if err != nil {
25
+			t.Errorf("Could not decode hash for [%s]: %s", password, err.Error())
26
+		}
27
+
28
+		generatedHashBytes, err := DecodePasswordHash(generatedHash)
29
+		if err != nil {
30
+			t.Errorf("Could not decode generated hash for [%s]: %s", password, err.Error())
31
+		}
32
+
33
+		passwordBytes := []byte(password)
34
+
35
+		if ComparePassword(hashBytes, passwordBytes) != nil {
36
+			t.Errorf("Stored hash for [%s] did not match", password)
37
+		}
38
+		if ComparePassword(generatedHashBytes, passwordBytes) != nil {
39
+			t.Errorf("Generated hash for [%s] did not match", password)
40
+		}
41
+	}
42
+}
43
+
44
+func TestUnsaltedPasswordFailures(t *testing.T) {
45
+	_, err := GenerateEncodedPassword("")
46
+	if err != ErrEmptyPassword {
47
+		t.Error("Generating empty password did not fail as expected!")
48
+	}
49
+
50
+	_, err = DecodePasswordHash("")
51
+	if err != ErrEmptyPassword {
52
+		t.Error("Decoding empty password hash did not fail as expected!")
53
+	}
54
+}

Loading…
Cancelar
Guardar