mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-18 03:18:29 -07:00
dfdbfee4fd
Merge in DNS/adguard-home from 2639-testify-require-4 to master Closes #2639. Squashed commit of the following: commit 0bb9125f42ab6d2511c1b8e481112aa5edd581d9 Merge: 0e9e9ed12c9992e0
Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Mar 11 15:47:21 2021 +0300 Merge branch 'master' into 2639-testify-require-4 commit 0e9e9ed16ae13ce648b5e1da6ffd123df911c2d7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Mar 10 12:43:15 2021 +0300 home: rm deletion error check commit 6bfbbcd2b7f9197a06856f9e6b959c2e1c4b8353 Merge: c8ebe5418811c881
Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Mar 10 12:30:07 2021 +0300 Merge branch 'master' into 2639-testify-require-4 commit c8ebe54142bba780226f76ddb72e33664ed28f30 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Mar 10 12:28:43 2021 +0300 home: imp tests commit f0e1db456f02df5f5f56ca93e7bd40a48475b38c Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Mar 5 14:06:41 2021 +0300 dnsforward: imp tests commit 4528246105ed06471a8778abbe8e5c30fc5483d5 Merge: 54b08d9c90ebc4d8
Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Mar 4 18:17:52 2021 +0300 Merge branch 'master' into 2639-testify-require-4 commit 54b08d9c980b8d69d019a1a1b3931aa048275691 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Feb 11 13:17:05 2021 +0300 dnsfilter: imp tests
215 lines
4.9 KiB
Go
215 lines
4.9 KiB
Go
package home
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
aghtest.DiscardLogOutput(m)
|
|
}
|
|
|
|
func prepareTestDir(t *testing.T) string {
|
|
t.Helper()
|
|
|
|
const dir = "./agh-test"
|
|
|
|
require.Nil(t, os.RemoveAll(dir))
|
|
// TODO(e.burkov): Replace with testing.TempDir after updating Go
|
|
// version to 1.16.
|
|
require.Nil(t, os.MkdirAll(dir, 0o755))
|
|
|
|
t.Cleanup(func() { require.Nil(t, os.RemoveAll(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) {
|
|
dir := prepareTestDir(t)
|
|
fn := filepath.Join(dir, "sessions.db")
|
|
|
|
users := []User{{
|
|
Name: "name",
|
|
PasswordHash: "$2y$05$..vyzAECIhJPfaQiOK17IukcQnqEgKJHy0iETyYqxn3YXJl8yZuo2",
|
|
}}
|
|
a := InitAuth(fn, nil, 60)
|
|
s := session{}
|
|
|
|
user := User{Name: "name"}
|
|
a.UserAdd(&user, "password")
|
|
|
|
assert.Equal(t, checkSessionNotFound, a.checkSession("notfound"))
|
|
a.RemoveSession("notfound")
|
|
|
|
sess, err := newSessionToken()
|
|
assert.Nil(t, err)
|
|
sessStr := hex.EncodeToString(sess)
|
|
|
|
now := time.Now().UTC().Unix()
|
|
// check expiration
|
|
s.expire = uint32(now)
|
|
a.addSession(sess, &s)
|
|
assert.Equal(t, checkSessionExpired, a.checkSession(sessStr))
|
|
|
|
// add session with TTL = 2 sec
|
|
s = session{}
|
|
s.expire = uint32(time.Now().UTC().Unix() + 2)
|
|
a.addSession(sess, &s)
|
|
assert.Equal(t, checkSessionOK, a.checkSession(sessStr))
|
|
|
|
a.Close()
|
|
|
|
// load saved session
|
|
a = InitAuth(fn, users, 60)
|
|
|
|
// the session is still alive
|
|
assert.Equal(t, checkSessionOK, a.checkSession(sessStr))
|
|
// reset our expiration time because checkSession() has just updated it
|
|
s.expire = uint32(time.Now().UTC().Unix() + 2)
|
|
a.storeSession(sess, &s)
|
|
a.Close()
|
|
|
|
u := a.UserFind("name", "password")
|
|
assert.NotEmpty(t, u.Name)
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
// load and remove expired sessions
|
|
a = InitAuth(fn, users, 60)
|
|
assert.Equal(t, checkSessionNotFound, a.checkSession(sessStr))
|
|
|
|
a.Close()
|
|
}
|
|
|
|
// implements http.ResponseWriter
|
|
type testResponseWriter struct {
|
|
hdr http.Header
|
|
statusCode int
|
|
}
|
|
|
|
func (w *testResponseWriter) Header() http.Header {
|
|
return w.hdr
|
|
}
|
|
|
|
func (w *testResponseWriter) Write([]byte) (int, error) {
|
|
return 0, nil
|
|
}
|
|
|
|
func (w *testResponseWriter) WriteHeader(statusCode int) {
|
|
w.statusCode = statusCode
|
|
}
|
|
|
|
func TestAuthHTTP(t *testing.T) {
|
|
dir := prepareTestDir(t)
|
|
fn := filepath.Join(dir, "sessions.db")
|
|
|
|
users := []User{
|
|
{Name: "name", PasswordHash: "$2y$05$..vyzAECIhJPfaQiOK17IukcQnqEgKJHy0iETyYqxn3YXJl8yZuo2"},
|
|
}
|
|
Context.auth = InitAuth(fn, users, 60)
|
|
|
|
handlerCalled := false
|
|
handler := func(_ http.ResponseWriter, _ *http.Request) {
|
|
handlerCalled = true
|
|
}
|
|
handler2 := optionalAuth(handler)
|
|
w := testResponseWriter{}
|
|
w.hdr = make(http.Header)
|
|
r := http.Request{}
|
|
r.Header = make(http.Header)
|
|
r.Method = http.MethodGet
|
|
|
|
// get / - we're redirected to login page
|
|
r.URL = &url.URL{Path: "/"}
|
|
handlerCalled = false
|
|
handler2(&w, &r)
|
|
assert.Equal(t, http.StatusFound, w.statusCode)
|
|
assert.NotEmpty(t, w.hdr.Get("Location"))
|
|
assert.False(t, handlerCalled)
|
|
|
|
// go to login page
|
|
loginURL := w.hdr.Get("Location")
|
|
r.URL = &url.URL{Path: loginURL}
|
|
handlerCalled = false
|
|
handler2(&w, &r)
|
|
assert.True(t, handlerCalled)
|
|
|
|
// perform login
|
|
cookie, err := Context.auth.httpCookie(loginJSON{Name: "name", Password: "password"})
|
|
assert.Nil(t, err)
|
|
assert.NotEmpty(t, cookie)
|
|
|
|
// get /
|
|
handler2 = optionalAuth(handler)
|
|
w.hdr = make(http.Header)
|
|
r.Header.Set("Cookie", cookie)
|
|
r.URL = &url.URL{Path: "/"}
|
|
handlerCalled = false
|
|
handler2(&w, &r)
|
|
assert.True(t, handlerCalled)
|
|
r.Header.Del("Cookie")
|
|
|
|
// get / with basic auth
|
|
handler2 = optionalAuth(handler)
|
|
w.hdr = make(http.Header)
|
|
r.URL = &url.URL{Path: "/"}
|
|
r.SetBasicAuth("name", "password")
|
|
handlerCalled = false
|
|
handler2(&w, &r)
|
|
assert.True(t, handlerCalled)
|
|
r.Header.Del("Authorization")
|
|
|
|
// get login page with a valid cookie - we're redirected to /
|
|
handler2 = optionalAuth(handler)
|
|
w.hdr = make(http.Header)
|
|
r.Header.Set("Cookie", cookie)
|
|
r.URL = &url.URL{Path: loginURL}
|
|
handlerCalled = false
|
|
handler2(&w, &r)
|
|
assert.NotEmpty(t, w.hdr.Get("Location"))
|
|
assert.False(t, handlerCalled)
|
|
r.Header.Del("Cookie")
|
|
|
|
// get login page with an invalid cookie
|
|
handler2 = optionalAuth(handler)
|
|
w.hdr = make(http.Header)
|
|
r.Header.Set("Cookie", "bad")
|
|
r.URL = &url.URL{Path: loginURL}
|
|
handlerCalled = false
|
|
handler2(&w, &r)
|
|
assert.True(t, handlerCalled)
|
|
r.Header.Del("Cookie")
|
|
|
|
Context.auth.Close()
|
|
}
|