"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "internal/home/auth_test.go" between
AdGuardHome-0.105.1.tar.gz and AdGuardHome-0.105.2.tar.gz

About: AdGuard Home is a network-wide ads & trackers blocking DNS server. It operates as a DNS server that re-routes tracking domains to a "black hole," thus preventing your devices from connecting to those servers.

auth_test.go  (AdGuardHome-0.105.1):auth_test.go  (AdGuardHome-0.105.2)
package home package home
import ( import (
"bytes"
"crypto/rand"
"encoding/hex" "encoding/hex"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
"time" "time"
"github.com/AdguardTeam/AdGuardHome/internal/aghtest" "github.com/AdguardTeam/AdGuardHome/internal/aghtest"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
aghtest.DiscardLogOutput(m) aghtest.DiscardLogOutput(m)
} }
func prepareTestDir() string { func prepareTestDir() string {
const dir = "./agh-test" const dir = "./agh-test"
_ = os.RemoveAll(dir) _ = os.RemoveAll(dir)
_ = os.MkdirAll(dir, 0o755) _ = os.MkdirAll(dir, 0o755)
return dir return dir
} }
func TestNewSessionToken(t *testing.T) {
// Successful case.
token, err := newSessionToken()
require.Nil(t, err)
assert.Len(t, token, sessionTokenSize)
// Break the rand.Reader.
prevReader := rand.Reader
t.Cleanup(func() {
rand.Reader = prevReader
})
rand.Reader = &bytes.Buffer{}
// Unsuccessful case.
token, err = newSessionToken()
require.NotNil(t, err)
assert.Empty(t, token)
}
func TestAuth(t *testing.T) { func TestAuth(t *testing.T) {
dir := prepareTestDir() dir := prepareTestDir()
defer func() { _ = os.RemoveAll(dir) }() t.Cleanup(func() { _ = os.RemoveAll(dir) })
fn := filepath.Join(dir, "sessions.db") fn := filepath.Join(dir, "sessions.db")
users := []User{ users := []User{{
{Name: "name", PasswordHash: "$2y$05$..vyzAECIhJPfaQiOK17IukcQnqE Name: "name",
gKJHy0iETyYqxn3YXJl8yZuo2"}, PasswordHash: "$2y$05$..vyzAECIhJPfaQiOK17IukcQnqEgKJHy0iETyYqxn3
} YXJl8yZuo2",
}}
a := InitAuth(fn, nil, 60) a := InitAuth(fn, nil, 60)
s := session{} s := session{}
user := User{Name: "name"} user := User{Name: "name"}
a.UserAdd(&user, "password") a.UserAdd(&user, "password")
assert.Equal(t, checkSessionNotFound, a.checkSession("notfound")) assert.Equal(t, checkSessionNotFound, a.checkSession("notfound"))
a.RemoveSession("notfound") a.RemoveSession("notfound")
sess, err := getSession(&users[0]) sess, err := newSessionToken()
assert.Nil(t, err) assert.Nil(t, err)
sessStr := hex.EncodeToString(sess) sessStr := hex.EncodeToString(sess)
now := time.Now().UTC().Unix() now := time.Now().UTC().Unix()
// check expiration // check expiration
s.expire = uint32(now) s.expire = uint32(now)
a.addSession(sess, &s) a.addSession(sess, &s)
assert.Equal(t, checkSessionExpired, a.checkSession(sessStr)) assert.Equal(t, checkSessionExpired, a.checkSession(sessStr))
// add session with TTL = 2 sec // add session with TTL = 2 sec
 End of changes. 6 change blocks. 
6 lines changed or deleted 29 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)