3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-22 10:42:52 +01:00

Add tests to subpackages

This commit is contained in:
Daniel Oaks 2017-10-07 22:19:37 +10:00
parent 10949a434a
commit 378d55af65
7 changed files with 227 additions and 17 deletions

View File

@ -49,3 +49,13 @@ const (
func (capability Capability) Name() string { func (capability Capability) Name() string {
return string(capability) return string(capability)
} }
// Version is used to select which max version of CAP the client supports.
type Version uint
const (
// Cap301 refers to the base CAP spec.
Cap301 Version = 301
// Cap302 refers to the IRCv3.2 CAP spec.
Cap302 Version = 302
)

46
irc/caps/set_test.go Normal file
View File

@ -0,0 +1,46 @@
// Copyright (c) 2017 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package caps
import "testing"
import "reflect"
func TestSets(t *testing.T) {
s1 := NewSet()
s1.Enable(AccountTag, EchoMessage, UserhostInNames)
if !s1.Has(AccountTag, EchoMessage, UserhostInNames) {
t.Error("Did not have the tags we expected")
}
if s1.Has(AccountTag, EchoMessage, STS, UserhostInNames) {
t.Error("Has() returned true when we don't have all the given capabilities")
}
s1.Disable(AccountTag)
if s1.Has(AccountTag) {
t.Error("Disable() did not correctly disable the given capability")
}
enabledCaps := make(map[Capability]bool)
for _, capab := range s1.List() {
enabledCaps[capab] = true
}
expectedCaps := map[Capability]bool{
EchoMessage: true,
UserhostInNames: true,
}
if !reflect.DeepEqual(enabledCaps, expectedCaps) {
t.Errorf("Enabled and expected capability lists do not match: %v, %v", enabledCaps, expectedCaps)
}
// make sure re-enabling doesn't add to the count or something weird like that
s1.Enable(EchoMessage)
if s1.Count() != 2 {
t.Error("Count() did not match expected capability count")
}
}

View File

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

View File

@ -9,6 +9,34 @@ import (
) )
func TestISUPPORT(t *testing.T) { func TestISUPPORT(t *testing.T) {
// test multiple output replies
tListLong := NewList()
tListLong.AddNoValue("1")
tListLong.AddNoValue("2")
tListLong.AddNoValue("3")
tListLong.AddNoValue("4")
tListLong.AddNoValue("5")
tListLong.AddNoValue("6")
tListLong.AddNoValue("7")
tListLong.AddNoValue("8")
tListLong.AddNoValue("9")
tListLong.AddNoValue("A")
tListLong.AddNoValue("B")
tListLong.AddNoValue("C")
tListLong.AddNoValue("D")
tListLong.AddNoValue("E")
tListLong.AddNoValue("F")
tListLong.RegenerateCachedReply()
longReplies := [][]string{
{"1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "are supported by this server"},
{"E", "F", "are supported by this server"},
}
if !reflect.DeepEqual(tListLong.CachedReply, longReplies) {
t.Errorf("Multiple output replies did not match, got [%v]", longReplies)
}
// create first list // create first list
tList1 := NewList() tList1 := NewList()
tList1.Add("SASL", "yes") tList1.Add("SASL", "yes")

View File

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

86
irc/passwd/salted_test.go Normal file
View File

@ -0,0 +1,86 @@
// Copyright (c) 2016 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package passwd
import (
"encoding/base64"
"testing"
)
type SaltedPasswordTest struct {
ManagerSalt string
Salt string
Hash string
Password string
}
var SaltedPasswords = []SaltedPasswordTest{
{
ManagerSalt: "3TPITDVf/NGb4OlCyV1uZNW1H7zy3BFos+Dsu7dj",
Salt: "b6oVqshJUfcm1zWEtqwKqUVylqLONAZfqt17ns+Y",
Hash: "JDJhJDE0JFYuT28xOFFNZldaaTI1UWpzNENMeHVKdm5vS1lkL2tFL1lFVkQ2a0loUEY2Vzk3UTZSVDVP",
Password: "test",
},
{
ManagerSalt: "iNGeNEfuPihM8kYDZ/C6qAJ0JERKeKkUYp6wYDU0",
Salt: "U7TA6k6VLSLHfdjSsQH0vc3Jqq6cUezJNyd0DC9c",
Hash: "JDJhJDE0JEguY2Rva3VOTVRrNm1VeGdXWjAwamViMGNvV0xYZFdHcTZjenFCRWE3Ymt2N1JiSFJDZlYy",
Password: "test2",
},
{
ManagerSalt: "ghKJaaSNTjuFmgLRqrgY4FGfx8wXEGOBE02PZvbv",
Salt: "NO/mtrMhGjX1FGDGdpGrDJIi4jxsb0aFa7ybId7r",
Hash: "JDJhJDE0JEI0M055Z2NDcjNUanB5ZEJ5MzUybi5FT3o4Y1MyNXp2c1NDVS9hS0hOcUxSRDZTWmUxTnN5",
Password: "supermono",
},
}
func TestSaltedPassword(t *testing.T) {
// check newly-generated password
managerSalt, err := NewSalt()
if err != nil {
t.Error("Could not generate manager salt")
}
salt, err := NewSalt()
if err != nil {
t.Error("Could not generate salt")
}
manager := NewSaltedManager(managerSalt)
passHash, err := manager.GenerateFromPassword(salt, "this is a test password")
if err != nil {
t.Error("Could not generate from password")
}
if manager.CompareHashAndPassword(passHash, salt, "this is a test password") != nil {
t.Error("Generated password does not match")
}
// check our stored passwords
for i, info := range SaltedPasswords {
// decode strings to bytes
managerSalt, err = base64.StdEncoding.DecodeString(info.ManagerSalt)
if err != nil {
t.Errorf("Could not decode manager salt for test %d", i)
}
salt, err := base64.StdEncoding.DecodeString(info.Salt)
if err != nil {
t.Errorf("Could not decode salt for test %d", i)
}
hash, err := base64.StdEncoding.DecodeString(info.Hash)
if err != nil {
t.Errorf("Could not decode hash for test %d", i)
}
// make sure our test values are still correct
manager := NewSaltedManager(managerSalt)
if manager.CompareHashAndPassword(hash, salt, info.Password) != nil {
t.Errorf("Password does not match for [%s]", info.Password)
}
}
}

View File

@ -0,0 +1,54 @@
// Copyright (c) 2016 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package passwd
import (
"testing"
)
var UnsaltedPasswords = map[string]string{
"test1": "JDJhJDA0JFFwZ1V0RWZTMFVaMkFrdlRrTG9FZk9FNEZWbWkvVEhsdGFnSXlIUC5wVmpYTkNERFJPNlcu",
"test2": "JDJhJDA0JHpQTGNqczlIanc3V2NFQ3JEOVlTM09aNkRTbGRsQzRyNmt3Q01aSUs2Y2xyWURVODZ1V0px",
"supernomo": "JDJhJDA0JHdJekhnQmk1VXQ4WUphL0pIL0tXQWVKVXJ6dXcvRDJ3WFljWW9XOGhzNllIbW1DRlFkL1VL",
}
func TestUnsaltedPassword(t *testing.T) {
for password, hash := range UnsaltedPasswords {
generatedHash, err := GenerateEncodedPassword(password)
if err != nil {
t.Errorf("Could not hash password for [%s]: %s", password, err.Error())
}
hashBytes, err := DecodePasswordHash(hash)
if err != nil {
t.Errorf("Could not decode hash for [%s]: %s", password, err.Error())
}
generatedHashBytes, err := DecodePasswordHash(generatedHash)
if err != nil {
t.Errorf("Could not decode generated hash for [%s]: %s", password, err.Error())
}
passwordBytes := []byte(password)
if ComparePassword(hashBytes, passwordBytes) != nil {
t.Errorf("Stored hash for [%s] did not match", password)
}
if ComparePassword(generatedHashBytes, passwordBytes) != nil {
t.Errorf("Generated hash for [%s] did not match", password)
}
}
}
func TestUnsaltedPasswordFailures(t *testing.T) {
_, err := GenerateEncodedPassword("")
if err != ErrEmptyPassword {
t.Error("Generating empty password did not fail as expected!")
}
_, err = DecodePasswordHash("")
if err != ErrEmptyPassword {
t.Error("Decoding empty password hash did not fail as expected!")
}
}