2020-07-24 21:39:38 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
2020-07-30 20:33:49 +00:00
|
|
|
"os/exec"
|
2021-02-01 18:04:15 +00:00
|
|
|
"path/filepath"
|
2020-07-30 20:33:49 +00:00
|
|
|
"runtime"
|
2020-07-24 21:39:38 +00:00
|
|
|
"strings"
|
2020-08-11 21:56:10 +00:00
|
|
|
"sync/atomic"
|
2020-07-24 21:39:38 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-04-25 07:38:33 +00:00
|
|
|
"github.com/alexedwards/argon2id"
|
2020-07-24 21:39:38 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-12-05 12:48:13 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-04-25 07:38:33 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
2020-12-05 12:48:13 +00:00
|
|
|
"github.com/drakkan/sftpgo/kms"
|
2021-02-01 18:04:15 +00:00
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2020-08-22 08:12:00 +00:00
|
|
|
"github.com/drakkan/sftpgo/vfs"
|
2020-07-24 21:39:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-07-29 19:56:56 +00:00
|
|
|
logSenderTest = "common_test"
|
2020-07-24 21:39:38 +00:00
|
|
|
httpAddr = "127.0.0.1:9999"
|
|
|
|
configDir = ".."
|
|
|
|
osWindows = "windows"
|
|
|
|
userTestUsername = "common_test_username"
|
|
|
|
)
|
|
|
|
|
|
|
|
type fakeConnection struct {
|
|
|
|
*BaseConnection
|
2020-08-11 21:56:10 +00:00
|
|
|
command string
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
func (c *fakeConnection) AddUser(user dataprovider.User) error {
|
2021-03-21 18:15:47 +00:00
|
|
|
_, err := user.GetFilesystem(c.GetID())
|
2020-07-29 19:56:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.BaseConnection.User = user
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
func (c *fakeConnection) Disconnect() error {
|
2020-07-29 19:56:56 +00:00
|
|
|
Connections.Remove(c.GetID())
|
2020-07-24 21:39:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fakeConnection) GetClientVersion() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fakeConnection) GetCommand() string {
|
2020-08-11 21:56:10 +00:00
|
|
|
return c.command
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fakeConnection) GetRemoteAddress() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-09-18 16:15:28 +00:00
|
|
|
type customNetConn struct {
|
|
|
|
net.Conn
|
|
|
|
id string
|
|
|
|
isClosed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *customNetConn) Close() error {
|
|
|
|
Connections.RemoveSSHConnection(c.id)
|
|
|
|
c.isClosed = true
|
|
|
|
return c.Conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSSHConnections(t *testing.T) {
|
|
|
|
conn1, conn2 := net.Pipe()
|
|
|
|
now := time.Now()
|
|
|
|
sshConn1 := NewSSHConnection("id1", conn1)
|
|
|
|
sshConn2 := NewSSHConnection("id2", conn2)
|
2020-11-07 17:05:47 +00:00
|
|
|
sshConn3 := NewSSHConnection("id3", conn2)
|
2020-09-18 16:15:28 +00:00
|
|
|
assert.Equal(t, "id1", sshConn1.GetID())
|
|
|
|
assert.Equal(t, "id2", sshConn2.GetID())
|
2020-11-07 17:05:47 +00:00
|
|
|
assert.Equal(t, "id3", sshConn3.GetID())
|
2020-09-18 16:15:28 +00:00
|
|
|
sshConn1.UpdateLastActivity()
|
|
|
|
assert.GreaterOrEqual(t, sshConn1.GetLastActivity().UnixNano(), now.UnixNano())
|
|
|
|
Connections.AddSSHConnection(sshConn1)
|
|
|
|
Connections.AddSSHConnection(sshConn2)
|
2020-11-07 17:05:47 +00:00
|
|
|
Connections.AddSSHConnection(sshConn3)
|
2020-09-18 16:15:28 +00:00
|
|
|
Connections.RLock()
|
2020-11-07 17:05:47 +00:00
|
|
|
assert.Len(t, Connections.sshConnections, 3)
|
2020-09-18 16:15:28 +00:00
|
|
|
Connections.RUnlock()
|
|
|
|
Connections.RemoveSSHConnection(sshConn1.id)
|
|
|
|
Connections.RLock()
|
2020-11-07 17:05:47 +00:00
|
|
|
assert.Len(t, Connections.sshConnections, 2)
|
|
|
|
assert.Equal(t, sshConn3.id, Connections.sshConnections[0].id)
|
|
|
|
assert.Equal(t, sshConn2.id, Connections.sshConnections[1].id)
|
2020-09-18 16:15:28 +00:00
|
|
|
Connections.RUnlock()
|
|
|
|
Connections.RemoveSSHConnection(sshConn1.id)
|
|
|
|
Connections.RLock()
|
2020-11-07 17:05:47 +00:00
|
|
|
assert.Len(t, Connections.sshConnections, 2)
|
|
|
|
assert.Equal(t, sshConn3.id, Connections.sshConnections[0].id)
|
|
|
|
assert.Equal(t, sshConn2.id, Connections.sshConnections[1].id)
|
2020-09-18 16:15:28 +00:00
|
|
|
Connections.RUnlock()
|
|
|
|
Connections.RemoveSSHConnection(sshConn2.id)
|
|
|
|
Connections.RLock()
|
2020-11-07 17:05:47 +00:00
|
|
|
assert.Len(t, Connections.sshConnections, 1)
|
|
|
|
assert.Equal(t, sshConn3.id, Connections.sshConnections[0].id)
|
|
|
|
Connections.RUnlock()
|
|
|
|
Connections.RemoveSSHConnection(sshConn3.id)
|
|
|
|
Connections.RLock()
|
2020-09-18 16:15:28 +00:00
|
|
|
assert.Len(t, Connections.sshConnections, 0)
|
|
|
|
Connections.RUnlock()
|
|
|
|
assert.NoError(t, sshConn1.Close())
|
|
|
|
assert.NoError(t, sshConn2.Close())
|
2020-11-07 17:05:47 +00:00
|
|
|
assert.NoError(t, sshConn3.Close())
|
2020-09-18 16:15:28 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 13:05:09 +00:00
|
|
|
func TestDefenderIntegration(t *testing.T) {
|
|
|
|
// by default defender is nil
|
|
|
|
configCopy := Config
|
|
|
|
|
|
|
|
ip := "127.1.1.1"
|
|
|
|
|
2021-01-04 16:52:14 +00:00
|
|
|
assert.Nil(t, ReloadDefender())
|
|
|
|
|
2021-01-02 13:05:09 +00:00
|
|
|
AddDefenderEvent(ip, HostEventNoLoginTried)
|
|
|
|
assert.False(t, IsBanned(ip))
|
|
|
|
|
2021-01-02 18:33:24 +00:00
|
|
|
assert.Nil(t, GetDefenderBanTime(ip))
|
|
|
|
assert.False(t, Unban(ip))
|
|
|
|
assert.Equal(t, 0, GetDefenderScore(ip))
|
|
|
|
|
2021-01-02 13:05:09 +00:00
|
|
|
Config.DefenderConfig = DefenderConfig{
|
|
|
|
Enabled: true,
|
|
|
|
BanTime: 10,
|
|
|
|
BanTimeIncrement: 50,
|
|
|
|
Threshold: 0,
|
|
|
|
ScoreInvalid: 2,
|
|
|
|
ScoreValid: 1,
|
|
|
|
ObservationTime: 15,
|
|
|
|
EntriesSoftLimit: 100,
|
|
|
|
EntriesHardLimit: 150,
|
|
|
|
}
|
|
|
|
err := Initialize(Config)
|
|
|
|
assert.Error(t, err)
|
|
|
|
Config.DefenderConfig.Threshold = 3
|
|
|
|
err = Initialize(Config)
|
|
|
|
assert.NoError(t, err)
|
2021-01-04 16:52:14 +00:00
|
|
|
assert.Nil(t, ReloadDefender())
|
2021-01-02 13:05:09 +00:00
|
|
|
|
|
|
|
AddDefenderEvent(ip, HostEventNoLoginTried)
|
|
|
|
assert.False(t, IsBanned(ip))
|
2021-01-02 18:33:24 +00:00
|
|
|
assert.Equal(t, 2, GetDefenderScore(ip))
|
|
|
|
assert.False(t, Unban(ip))
|
|
|
|
assert.Nil(t, GetDefenderBanTime(ip))
|
|
|
|
|
2021-01-02 13:05:09 +00:00
|
|
|
AddDefenderEvent(ip, HostEventLoginFailed)
|
|
|
|
assert.True(t, IsBanned(ip))
|
2021-01-02 18:33:24 +00:00
|
|
|
assert.Equal(t, 0, GetDefenderScore(ip))
|
|
|
|
assert.NotNil(t, GetDefenderBanTime(ip))
|
|
|
|
assert.True(t, Unban(ip))
|
|
|
|
assert.Nil(t, GetDefenderBanTime(ip))
|
|
|
|
assert.False(t, Unban(ip))
|
2021-01-02 13:05:09 +00:00
|
|
|
|
|
|
|
Config = configCopy
|
|
|
|
}
|
|
|
|
|
2021-04-18 10:31:06 +00:00
|
|
|
func TestRateLimitersIntegration(t *testing.T) {
|
|
|
|
// by default defender is nil
|
|
|
|
configCopy := Config
|
|
|
|
|
|
|
|
Config.RateLimitersConfig = []RateLimiterConfig{
|
|
|
|
{
|
|
|
|
Average: 100,
|
|
|
|
Period: 10,
|
|
|
|
Burst: 5,
|
|
|
|
Type: int(rateLimiterTypeGlobal),
|
|
|
|
Protocols: rateLimiterProtocolValues,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Average: 1,
|
|
|
|
Period: 1000,
|
|
|
|
Burst: 1,
|
|
|
|
Type: int(rateLimiterTypeSource),
|
|
|
|
Protocols: []string{ProtocolWebDAV, ProtocolWebDAV, ProtocolFTP},
|
|
|
|
GenerateDefenderEvents: true,
|
|
|
|
EntriesSoftLimit: 100,
|
|
|
|
EntriesHardLimit: 150,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := Initialize(Config)
|
|
|
|
assert.Error(t, err)
|
|
|
|
Config.RateLimitersConfig[0].Period = 1000
|
|
|
|
err = Initialize(Config)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-04-19 06:14:04 +00:00
|
|
|
assert.Len(t, rateLimiters, 4)
|
2021-04-18 10:31:06 +00:00
|
|
|
assert.Len(t, rateLimiters[ProtocolSSH], 1)
|
|
|
|
assert.Len(t, rateLimiters[ProtocolFTP], 2)
|
|
|
|
assert.Len(t, rateLimiters[ProtocolWebDAV], 2)
|
2021-04-19 06:14:04 +00:00
|
|
|
assert.Len(t, rateLimiters[ProtocolHTTP], 1)
|
2021-04-18 10:31:06 +00:00
|
|
|
|
|
|
|
source1 := "127.1.1.1"
|
|
|
|
source2 := "127.1.1.2"
|
|
|
|
|
2021-04-19 06:14:04 +00:00
|
|
|
_, err = LimitRate(ProtocolSSH, source1)
|
2021-04-18 10:31:06 +00:00
|
|
|
assert.NoError(t, err)
|
2021-04-19 06:14:04 +00:00
|
|
|
_, err = LimitRate(ProtocolFTP, source1)
|
2021-04-18 10:31:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
// sleep to allow the add configured burst to the token.
|
|
|
|
// This sleep is not enough to add the per-source burst
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
2021-04-19 06:14:04 +00:00
|
|
|
_, err = LimitRate(ProtocolWebDAV, source2)
|
2021-04-18 10:31:06 +00:00
|
|
|
assert.NoError(t, err)
|
2021-04-19 06:14:04 +00:00
|
|
|
_, err = LimitRate(ProtocolFTP, source1)
|
2021-04-18 10:31:06 +00:00
|
|
|
assert.Error(t, err)
|
2021-04-19 06:14:04 +00:00
|
|
|
_, err = LimitRate(ProtocolWebDAV, source2)
|
2021-04-18 10:31:06 +00:00
|
|
|
assert.Error(t, err)
|
2021-04-19 06:14:04 +00:00
|
|
|
_, err = LimitRate(ProtocolSSH, source1)
|
2021-04-18 10:31:06 +00:00
|
|
|
assert.NoError(t, err)
|
2021-04-19 06:14:04 +00:00
|
|
|
_, err = LimitRate(ProtocolSSH, source2)
|
2021-04-18 10:31:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
Config = configCopy
|
|
|
|
}
|
|
|
|
|
2020-12-15 18:29:30 +00:00
|
|
|
func TestMaxConnections(t *testing.T) {
|
|
|
|
oldValue := Config.MaxTotalConnections
|
2021-05-08 17:45:21 +00:00
|
|
|
perHost := Config.MaxPerHostConnections
|
|
|
|
|
|
|
|
Config.MaxPerHostConnections = 0
|
|
|
|
|
|
|
|
ipAddr := "192.168.7.8"
|
|
|
|
assert.True(t, Connections.IsNewConnectionAllowed(ipAddr))
|
|
|
|
|
2020-12-15 18:29:30 +00:00
|
|
|
Config.MaxTotalConnections = 1
|
2021-05-08 17:45:21 +00:00
|
|
|
Config.MaxPerHostConnections = perHost
|
2020-12-15 18:29:30 +00:00
|
|
|
|
2021-05-08 17:45:21 +00:00
|
|
|
assert.True(t, Connections.IsNewConnectionAllowed(ipAddr))
|
2021-03-21 18:15:47 +00:00
|
|
|
c := NewBaseConnection("id", ProtocolSFTP, dataprovider.User{})
|
2020-12-15 18:29:30 +00:00
|
|
|
fakeConn := &fakeConnection{
|
|
|
|
BaseConnection: c,
|
|
|
|
}
|
|
|
|
Connections.Add(fakeConn)
|
|
|
|
assert.Len(t, Connections.GetStats(), 1)
|
2021-05-08 17:45:21 +00:00
|
|
|
assert.False(t, Connections.IsNewConnectionAllowed(ipAddr))
|
2020-12-15 18:29:30 +00:00
|
|
|
|
|
|
|
res := Connections.Close(fakeConn.GetID())
|
|
|
|
assert.True(t, res)
|
|
|
|
assert.Eventually(t, func() bool { return len(Connections.GetStats()) == 0 }, 300*time.Millisecond, 50*time.Millisecond)
|
|
|
|
|
2021-05-08 17:45:21 +00:00
|
|
|
assert.True(t, Connections.IsNewConnectionAllowed(ipAddr))
|
|
|
|
Connections.AddClientConnection(ipAddr)
|
|
|
|
Connections.AddClientConnection(ipAddr)
|
|
|
|
assert.False(t, Connections.IsNewConnectionAllowed(ipAddr))
|
|
|
|
Connections.RemoveClientConnection(ipAddr)
|
|
|
|
assert.True(t, Connections.IsNewConnectionAllowed(ipAddr))
|
|
|
|
Connections.RemoveClientConnection(ipAddr)
|
2021-04-20 16:12:16 +00:00
|
|
|
|
2020-12-15 18:29:30 +00:00
|
|
|
Config.MaxTotalConnections = oldValue
|
|
|
|
}
|
|
|
|
|
2021-05-08 17:45:21 +00:00
|
|
|
func TestMaxConnectionPerHost(t *testing.T) {
|
|
|
|
oldValue := Config.MaxPerHostConnections
|
|
|
|
|
|
|
|
Config.MaxPerHostConnections = 2
|
|
|
|
|
|
|
|
ipAddr := "192.168.9.9"
|
|
|
|
Connections.AddClientConnection(ipAddr)
|
|
|
|
assert.True(t, Connections.IsNewConnectionAllowed(ipAddr))
|
|
|
|
|
|
|
|
Connections.AddClientConnection(ipAddr)
|
|
|
|
assert.True(t, Connections.IsNewConnectionAllowed(ipAddr))
|
|
|
|
|
|
|
|
Connections.AddClientConnection(ipAddr)
|
|
|
|
assert.False(t, Connections.IsNewConnectionAllowed(ipAddr))
|
|
|
|
|
|
|
|
Connections.RemoveClientConnection(ipAddr)
|
|
|
|
Connections.RemoveClientConnection(ipAddr)
|
|
|
|
|
|
|
|
Config.MaxPerHostConnections = oldValue
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
func TestIdleConnections(t *testing.T) {
|
|
|
|
configCopy := Config
|
|
|
|
|
|
|
|
Config.IdleTimeout = 1
|
2021-01-02 13:05:09 +00:00
|
|
|
err := Initialize(Config)
|
|
|
|
assert.NoError(t, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2020-09-18 16:15:28 +00:00
|
|
|
conn1, conn2 := net.Pipe()
|
|
|
|
customConn1 := &customNetConn{
|
|
|
|
Conn: conn1,
|
|
|
|
id: "id1",
|
|
|
|
}
|
|
|
|
customConn2 := &customNetConn{
|
|
|
|
Conn: conn2,
|
|
|
|
id: "id2",
|
|
|
|
}
|
|
|
|
sshConn1 := NewSSHConnection(customConn1.id, customConn1)
|
|
|
|
sshConn2 := NewSSHConnection(customConn2.id, customConn2)
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
username := "test_user"
|
|
|
|
user := dataprovider.User{
|
|
|
|
Username: username,
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
c := NewBaseConnection(sshConn1.id+"_1", ProtocolSFTP, user)
|
2020-07-24 21:39:38 +00:00
|
|
|
c.lastActivity = time.Now().Add(-24 * time.Hour).UnixNano()
|
|
|
|
fakeConn := &fakeConnection{
|
|
|
|
BaseConnection: c,
|
|
|
|
}
|
2020-09-18 16:15:28 +00:00
|
|
|
// both ssh connections are expired but they should get removed only
|
|
|
|
// if there is no associated connection
|
|
|
|
sshConn1.lastActivity = c.lastActivity
|
|
|
|
sshConn2.lastActivity = c.lastActivity
|
|
|
|
Connections.AddSSHConnection(sshConn1)
|
2020-07-24 21:39:38 +00:00
|
|
|
Connections.Add(fakeConn)
|
|
|
|
assert.Equal(t, Connections.GetActiveSessions(username), 1)
|
2021-03-21 18:15:47 +00:00
|
|
|
c = NewBaseConnection(sshConn2.id+"_1", ProtocolSSH, user)
|
2020-07-29 19:56:56 +00:00
|
|
|
fakeConn = &fakeConnection{
|
|
|
|
BaseConnection: c,
|
|
|
|
}
|
2020-09-18 16:15:28 +00:00
|
|
|
Connections.AddSSHConnection(sshConn2)
|
2020-07-29 19:56:56 +00:00
|
|
|
Connections.Add(fakeConn)
|
2020-09-18 16:15:28 +00:00
|
|
|
assert.Equal(t, Connections.GetActiveSessions(username), 2)
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
cFTP := NewBaseConnection("id2", ProtocolFTP, dataprovider.User{})
|
2020-09-18 16:15:28 +00:00
|
|
|
cFTP.lastActivity = time.Now().UnixNano()
|
|
|
|
fakeConn = &fakeConnection{
|
|
|
|
BaseConnection: cFTP,
|
|
|
|
}
|
|
|
|
Connections.Add(fakeConn)
|
|
|
|
assert.Equal(t, Connections.GetActiveSessions(username), 2)
|
|
|
|
assert.Len(t, Connections.GetStats(), 3)
|
|
|
|
Connections.RLock()
|
|
|
|
assert.Len(t, Connections.sshConnections, 2)
|
|
|
|
Connections.RUnlock()
|
2020-07-29 19:56:56 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
startIdleTimeoutTicker(100 * time.Millisecond)
|
2020-09-18 16:15:28 +00:00
|
|
|
assert.Eventually(t, func() bool { return Connections.GetActiveSessions(username) == 1 }, 1*time.Second, 200*time.Millisecond)
|
|
|
|
assert.Eventually(t, func() bool {
|
|
|
|
Connections.RLock()
|
|
|
|
defer Connections.RUnlock()
|
|
|
|
return len(Connections.sshConnections) == 1
|
|
|
|
}, 1*time.Second, 200*time.Millisecond)
|
2020-07-24 21:39:38 +00:00
|
|
|
stopIdleTimeoutTicker()
|
2020-09-18 16:15:28 +00:00
|
|
|
assert.Len(t, Connections.GetStats(), 2)
|
2020-07-29 19:56:56 +00:00
|
|
|
c.lastActivity = time.Now().Add(-24 * time.Hour).UnixNano()
|
2020-09-18 16:15:28 +00:00
|
|
|
cFTP.lastActivity = time.Now().Add(-24 * time.Hour).UnixNano()
|
|
|
|
sshConn2.lastActivity = c.lastActivity
|
2020-07-29 19:56:56 +00:00
|
|
|
startIdleTimeoutTicker(100 * time.Millisecond)
|
|
|
|
assert.Eventually(t, func() bool { return len(Connections.GetStats()) == 0 }, 1*time.Second, 200*time.Millisecond)
|
2020-09-18 16:15:28 +00:00
|
|
|
assert.Eventually(t, func() bool {
|
|
|
|
Connections.RLock()
|
|
|
|
defer Connections.RUnlock()
|
|
|
|
return len(Connections.sshConnections) == 0
|
|
|
|
}, 1*time.Second, 200*time.Millisecond)
|
2020-07-29 19:56:56 +00:00
|
|
|
stopIdleTimeoutTicker()
|
2020-09-18 16:15:28 +00:00
|
|
|
assert.True(t, customConn1.isClosed)
|
|
|
|
assert.True(t, customConn2.isClosed)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
Config = configCopy
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloseConnection(t *testing.T) {
|
2021-03-21 18:15:47 +00:00
|
|
|
c := NewBaseConnection("id", ProtocolSFTP, dataprovider.User{})
|
2020-07-24 21:39:38 +00:00
|
|
|
fakeConn := &fakeConnection{
|
|
|
|
BaseConnection: c,
|
|
|
|
}
|
2021-05-08 17:45:21 +00:00
|
|
|
assert.True(t, Connections.IsNewConnectionAllowed("127.0.0.1"))
|
2020-07-24 21:39:38 +00:00
|
|
|
Connections.Add(fakeConn)
|
|
|
|
assert.Len(t, Connections.GetStats(), 1)
|
|
|
|
res := Connections.Close(fakeConn.GetID())
|
|
|
|
assert.True(t, res)
|
|
|
|
assert.Eventually(t, func() bool { return len(Connections.GetStats()) == 0 }, 300*time.Millisecond, 50*time.Millisecond)
|
|
|
|
res = Connections.Close(fakeConn.GetID())
|
|
|
|
assert.False(t, res)
|
2020-07-29 19:56:56 +00:00
|
|
|
Connections.Remove(fakeConn.GetID())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSwapConnection(t *testing.T) {
|
2021-03-21 18:15:47 +00:00
|
|
|
c := NewBaseConnection("id", ProtocolFTP, dataprovider.User{})
|
2020-07-29 19:56:56 +00:00
|
|
|
fakeConn := &fakeConnection{
|
|
|
|
BaseConnection: c,
|
|
|
|
}
|
|
|
|
Connections.Add(fakeConn)
|
|
|
|
if assert.Len(t, Connections.GetStats(), 1) {
|
|
|
|
assert.Equal(t, "", Connections.GetStats()[0].Username)
|
|
|
|
}
|
|
|
|
c = NewBaseConnection("id", ProtocolFTP, dataprovider.User{
|
|
|
|
Username: userTestUsername,
|
2021-03-21 18:15:47 +00:00
|
|
|
})
|
2020-07-29 19:56:56 +00:00
|
|
|
fakeConn = &fakeConnection{
|
|
|
|
BaseConnection: c,
|
|
|
|
}
|
|
|
|
err := Connections.Swap(fakeConn)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, Connections.GetStats(), 1) {
|
|
|
|
assert.Equal(t, userTestUsername, Connections.GetStats()[0].Username)
|
|
|
|
}
|
|
|
|
res := Connections.Close(fakeConn.GetID())
|
|
|
|
assert.True(t, res)
|
|
|
|
assert.Eventually(t, func() bool { return len(Connections.GetStats()) == 0 }, 300*time.Millisecond, 50*time.Millisecond)
|
|
|
|
err = Connections.Swap(fakeConn)
|
|
|
|
assert.Error(t, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAtomicUpload(t *testing.T) {
|
|
|
|
configCopy := Config
|
|
|
|
|
|
|
|
Config.UploadMode = UploadModeStandard
|
|
|
|
assert.False(t, Config.IsAtomicUploadEnabled())
|
|
|
|
Config.UploadMode = UploadModeAtomic
|
|
|
|
assert.True(t, Config.IsAtomicUploadEnabled())
|
|
|
|
Config.UploadMode = UploadModeAtomicWithResume
|
|
|
|
assert.True(t, Config.IsAtomicUploadEnabled())
|
|
|
|
|
|
|
|
Config = configCopy
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConnectionStatus(t *testing.T) {
|
|
|
|
username := "test_user"
|
|
|
|
user := dataprovider.User{
|
|
|
|
Username: username,
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
fs := vfs.NewOsFs("", os.TempDir(), "")
|
|
|
|
c1 := NewBaseConnection("id1", ProtocolSFTP, user)
|
2020-07-24 21:39:38 +00:00
|
|
|
fakeConn1 := &fakeConnection{
|
|
|
|
BaseConnection: c1,
|
|
|
|
}
|
2020-08-22 08:12:00 +00:00
|
|
|
t1 := NewBaseTransfer(nil, c1, nil, "/p1", "/r1", TransferUpload, 0, 0, 0, true, fs)
|
2020-07-24 21:39:38 +00:00
|
|
|
t1.BytesReceived = 123
|
2020-08-22 08:12:00 +00:00
|
|
|
t2 := NewBaseTransfer(nil, c1, nil, "/p2", "/r2", TransferDownload, 0, 0, 0, true, fs)
|
2020-07-24 21:39:38 +00:00
|
|
|
t2.BytesSent = 456
|
2021-03-21 18:15:47 +00:00
|
|
|
c2 := NewBaseConnection("id2", ProtocolSSH, user)
|
2020-07-24 21:39:38 +00:00
|
|
|
fakeConn2 := &fakeConnection{
|
|
|
|
BaseConnection: c2,
|
2020-08-11 21:56:10 +00:00
|
|
|
command: "md5sum",
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
c3 := NewBaseConnection("id3", ProtocolWebDAV, user)
|
2020-08-11 21:56:10 +00:00
|
|
|
fakeConn3 := &fakeConnection{
|
|
|
|
BaseConnection: c3,
|
|
|
|
command: "PROPFIND",
|
|
|
|
}
|
2020-08-22 08:12:00 +00:00
|
|
|
t3 := NewBaseTransfer(nil, c3, nil, "/p2", "/r2", TransferDownload, 0, 0, 0, true, fs)
|
2020-07-24 21:39:38 +00:00
|
|
|
Connections.Add(fakeConn1)
|
|
|
|
Connections.Add(fakeConn2)
|
2020-08-11 21:56:10 +00:00
|
|
|
Connections.Add(fakeConn3)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
stats := Connections.GetStats()
|
2020-08-11 21:56:10 +00:00
|
|
|
assert.Len(t, stats, 3)
|
2020-07-24 21:39:38 +00:00
|
|
|
for _, stat := range stats {
|
|
|
|
assert.Equal(t, stat.Username, username)
|
|
|
|
assert.True(t, strings.HasPrefix(stat.GetConnectionInfo(), stat.Protocol))
|
|
|
|
assert.True(t, strings.HasPrefix(stat.GetConnectionDuration(), "00:"))
|
|
|
|
if stat.ConnectionID == "SFTP_id1" {
|
|
|
|
assert.Len(t, stat.Transfers, 2)
|
|
|
|
assert.Greater(t, len(stat.GetTransfersAsString()), 0)
|
|
|
|
for _, tr := range stat.Transfers {
|
|
|
|
if tr.OperationType == operationDownload {
|
|
|
|
assert.True(t, strings.HasPrefix(tr.getConnectionTransferAsString(), "DL"))
|
|
|
|
} else if tr.OperationType == operationUpload {
|
|
|
|
assert.True(t, strings.HasPrefix(tr.getConnectionTransferAsString(), "UL"))
|
|
|
|
}
|
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
} else if stat.ConnectionID == "DAV_id3" {
|
|
|
|
assert.Len(t, stat.Transfers, 1)
|
|
|
|
assert.Greater(t, len(stat.GetTransfersAsString()), 0)
|
2020-07-24 21:39:38 +00:00
|
|
|
} else {
|
|
|
|
assert.Equal(t, 0, len(stat.GetTransfersAsString()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := t1.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = t2.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-08-11 21:56:10 +00:00
|
|
|
err = fakeConn3.SignalTransfersAbort()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int32(1), atomic.LoadInt32(&t3.AbortTransfer))
|
|
|
|
err = t3.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = fakeConn3.SignalTransfersAbort()
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
Connections.Remove(fakeConn1.GetID())
|
2020-11-07 17:05:47 +00:00
|
|
|
stats = Connections.GetStats()
|
|
|
|
assert.Len(t, stats, 2)
|
|
|
|
assert.Equal(t, fakeConn3.GetID(), stats[0].ConnectionID)
|
|
|
|
assert.Equal(t, fakeConn2.GetID(), stats[1].ConnectionID)
|
2020-07-29 19:56:56 +00:00
|
|
|
Connections.Remove(fakeConn2.GetID())
|
2020-11-07 17:05:47 +00:00
|
|
|
stats = Connections.GetStats()
|
|
|
|
assert.Len(t, stats, 1)
|
|
|
|
assert.Equal(t, fakeConn3.GetID(), stats[0].ConnectionID)
|
2020-08-11 21:56:10 +00:00
|
|
|
Connections.Remove(fakeConn3.GetID())
|
2020-07-24 21:39:38 +00:00
|
|
|
stats = Connections.GetStats()
|
|
|
|
assert.Len(t, stats, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuotaScans(t *testing.T) {
|
|
|
|
username := "username"
|
|
|
|
assert.True(t, QuotaScans.AddUserQuotaScan(username))
|
|
|
|
assert.False(t, QuotaScans.AddUserQuotaScan(username))
|
|
|
|
if assert.Len(t, QuotaScans.GetUsersQuotaScans(), 1) {
|
|
|
|
assert.Equal(t, QuotaScans.GetUsersQuotaScans()[0].Username, username)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.True(t, QuotaScans.RemoveUserQuotaScan(username))
|
|
|
|
assert.False(t, QuotaScans.RemoveUserQuotaScan(username))
|
|
|
|
assert.Len(t, QuotaScans.GetUsersQuotaScans(), 0)
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
folderName := "folder"
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.True(t, QuotaScans.AddVFolderQuotaScan(folderName))
|
|
|
|
assert.False(t, QuotaScans.AddVFolderQuotaScan(folderName))
|
|
|
|
if assert.Len(t, QuotaScans.GetVFoldersQuotaScans(), 1) {
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Equal(t, QuotaScans.GetVFoldersQuotaScans()[0].Name, folderName)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.True(t, QuotaScans.RemoveVFolderQuotaScan(folderName))
|
|
|
|
assert.False(t, QuotaScans.RemoveVFolderQuotaScan(folderName))
|
|
|
|
assert.Len(t, QuotaScans.GetVFoldersQuotaScans(), 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProxyProtocolVersion(t *testing.T) {
|
|
|
|
c := Configuration{
|
|
|
|
ProxyProtocol: 1,
|
|
|
|
}
|
|
|
|
proxyListener, err := c.GetProxyListener(nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, proxyListener.Policy)
|
|
|
|
|
|
|
|
c.ProxyProtocol = 2
|
|
|
|
proxyListener, err = c.GetProxyListener(nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, proxyListener.Policy)
|
|
|
|
|
|
|
|
c.ProxyProtocol = 1
|
|
|
|
c.ProxyAllowed = []string{"invalid"}
|
|
|
|
_, err = c.GetProxyListener(nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
c.ProxyProtocol = 2
|
|
|
|
_, err = c.GetProxyListener(nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2021-04-05 08:07:59 +00:00
|
|
|
func TestStartupHook(t *testing.T) {
|
|
|
|
Config.StartupHook = ""
|
|
|
|
|
|
|
|
assert.NoError(t, Config.ExecuteStartupHook())
|
|
|
|
|
|
|
|
Config.StartupHook = "http://foo\x7f.com/startup"
|
|
|
|
assert.Error(t, Config.ExecuteStartupHook())
|
|
|
|
|
|
|
|
Config.StartupHook = "http://invalid:5678/"
|
|
|
|
assert.Error(t, Config.ExecuteStartupHook())
|
|
|
|
|
|
|
|
Config.StartupHook = fmt.Sprintf("http://%v", httpAddr)
|
|
|
|
assert.NoError(t, Config.ExecuteStartupHook())
|
|
|
|
|
|
|
|
Config.StartupHook = "invalidhook"
|
|
|
|
assert.Error(t, Config.ExecuteStartupHook())
|
|
|
|
|
|
|
|
if runtime.GOOS != osWindows {
|
|
|
|
hookCmd, err := exec.LookPath("true")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
Config.StartupHook = hookCmd
|
|
|
|
assert.NoError(t, Config.ExecuteStartupHook())
|
|
|
|
}
|
|
|
|
|
|
|
|
Config.StartupHook = ""
|
|
|
|
}
|
|
|
|
|
2020-07-30 20:33:49 +00:00
|
|
|
func TestPostConnectHook(t *testing.T) {
|
|
|
|
Config.PostConnectHook = ""
|
|
|
|
|
2021-01-02 13:05:09 +00:00
|
|
|
ipAddr := "127.0.0.1"
|
2020-07-30 20:33:49 +00:00
|
|
|
|
2021-01-02 13:05:09 +00:00
|
|
|
assert.NoError(t, Config.ExecutePostConnectHook(ipAddr, ProtocolFTP))
|
2020-07-30 20:33:49 +00:00
|
|
|
|
|
|
|
Config.PostConnectHook = "http://foo\x7f.com/"
|
2021-01-02 13:05:09 +00:00
|
|
|
assert.Error(t, Config.ExecutePostConnectHook(ipAddr, ProtocolSFTP))
|
2020-07-30 20:33:49 +00:00
|
|
|
|
|
|
|
Config.PostConnectHook = "http://invalid:1234/"
|
2021-01-02 13:05:09 +00:00
|
|
|
assert.Error(t, Config.ExecutePostConnectHook(ipAddr, ProtocolSFTP))
|
2020-07-30 20:33:49 +00:00
|
|
|
|
|
|
|
Config.PostConnectHook = fmt.Sprintf("http://%v/404", httpAddr)
|
2021-01-02 13:05:09 +00:00
|
|
|
assert.Error(t, Config.ExecutePostConnectHook(ipAddr, ProtocolFTP))
|
2020-07-30 20:33:49 +00:00
|
|
|
|
|
|
|
Config.PostConnectHook = fmt.Sprintf("http://%v", httpAddr)
|
2021-01-02 13:05:09 +00:00
|
|
|
assert.NoError(t, Config.ExecutePostConnectHook(ipAddr, ProtocolFTP))
|
2020-07-30 20:33:49 +00:00
|
|
|
|
|
|
|
Config.PostConnectHook = "invalid"
|
2021-01-02 13:05:09 +00:00
|
|
|
assert.Error(t, Config.ExecutePostConnectHook(ipAddr, ProtocolFTP))
|
2020-07-30 20:33:49 +00:00
|
|
|
|
|
|
|
if runtime.GOOS == osWindows {
|
|
|
|
Config.PostConnectHook = "C:\\bad\\command"
|
2021-01-02 13:05:09 +00:00
|
|
|
assert.Error(t, Config.ExecutePostConnectHook(ipAddr, ProtocolSFTP))
|
2020-07-30 20:33:49 +00:00
|
|
|
} else {
|
|
|
|
Config.PostConnectHook = "/invalid/path"
|
2021-01-02 13:05:09 +00:00
|
|
|
assert.Error(t, Config.ExecutePostConnectHook(ipAddr, ProtocolSFTP))
|
2020-07-30 20:33:49 +00:00
|
|
|
|
|
|
|
hookCmd, err := exec.LookPath("true")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
Config.PostConnectHook = hookCmd
|
2021-01-02 13:05:09 +00:00
|
|
|
assert.NoError(t, Config.ExecutePostConnectHook(ipAddr, ProtocolSFTP))
|
2020-07-30 20:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Config.PostConnectHook = ""
|
|
|
|
}
|
2020-12-05 12:48:13 +00:00
|
|
|
|
|
|
|
func TestCryptoConvertFileInfo(t *testing.T) {
|
|
|
|
name := "name"
|
2021-03-21 18:15:47 +00:00
|
|
|
fs, err := vfs.NewCryptFs("connID1", os.TempDir(), "", vfs.CryptFsConfig{Passphrase: kms.NewPlainSecret("secret")})
|
2020-12-05 12:48:13 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
cryptFs := fs.(*vfs.CryptFs)
|
|
|
|
info := vfs.NewFileInfo(name, true, 48, time.Now(), false)
|
|
|
|
assert.Equal(t, info, cryptFs.ConvertFileInfo(info))
|
|
|
|
info = vfs.NewFileInfo(name, false, 48, time.Now(), false)
|
|
|
|
assert.NotEqual(t, info.Size(), cryptFs.ConvertFileInfo(info).Size())
|
|
|
|
info = vfs.NewFileInfo(name, false, 33, time.Now(), false)
|
|
|
|
assert.Equal(t, int64(0), cryptFs.ConvertFileInfo(info).Size())
|
|
|
|
info = vfs.NewFileInfo(name, false, 1, time.Now(), false)
|
|
|
|
assert.Equal(t, int64(0), cryptFs.ConvertFileInfo(info).Size())
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
|
|
|
|
func TestFolderCopy(t *testing.T) {
|
|
|
|
folder := vfs.BaseVirtualFolder{
|
|
|
|
ID: 1,
|
|
|
|
Name: "name",
|
|
|
|
MappedPath: filepath.Clean(os.TempDir()),
|
|
|
|
UsedQuotaSize: 4096,
|
|
|
|
UsedQuotaFiles: 2,
|
|
|
|
LastQuotaUpdate: utils.GetTimeAsMsSinceEpoch(time.Now()),
|
|
|
|
Users: []string{"user1", "user2"},
|
|
|
|
}
|
|
|
|
folderCopy := folder.GetACopy()
|
|
|
|
folder.ID = 2
|
|
|
|
folder.Users = []string{"user3"}
|
|
|
|
require.Len(t, folderCopy.Users, 2)
|
|
|
|
require.True(t, utils.IsStringInSlice("user1", folderCopy.Users))
|
|
|
|
require.True(t, utils.IsStringInSlice("user2", folderCopy.Users))
|
|
|
|
require.Equal(t, int64(1), folderCopy.ID)
|
|
|
|
require.Equal(t, folder.Name, folderCopy.Name)
|
|
|
|
require.Equal(t, folder.MappedPath, folderCopy.MappedPath)
|
|
|
|
require.Equal(t, folder.UsedQuotaSize, folderCopy.UsedQuotaSize)
|
|
|
|
require.Equal(t, folder.UsedQuotaFiles, folderCopy.UsedQuotaFiles)
|
|
|
|
require.Equal(t, folder.LastQuotaUpdate, folderCopy.LastQuotaUpdate)
|
2021-03-21 18:15:47 +00:00
|
|
|
|
|
|
|
folder.FsConfig = vfs.Filesystem{
|
|
|
|
CryptConfig: vfs.CryptFsConfig{
|
|
|
|
Passphrase: kms.NewPlainSecret("crypto secret"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
folderCopy = folder.GetACopy()
|
|
|
|
folder.FsConfig.CryptConfig.Passphrase = kms.NewEmptySecret()
|
|
|
|
require.Len(t, folderCopy.Users, 1)
|
|
|
|
require.True(t, utils.IsStringInSlice("user3", folderCopy.Users))
|
|
|
|
require.Equal(t, int64(2), folderCopy.ID)
|
|
|
|
require.Equal(t, folder.Name, folderCopy.Name)
|
|
|
|
require.Equal(t, folder.MappedPath, folderCopy.MappedPath)
|
|
|
|
require.Equal(t, folder.UsedQuotaSize, folderCopy.UsedQuotaSize)
|
|
|
|
require.Equal(t, folder.UsedQuotaFiles, folderCopy.UsedQuotaFiles)
|
|
|
|
require.Equal(t, folder.LastQuotaUpdate, folderCopy.LastQuotaUpdate)
|
|
|
|
require.Equal(t, "crypto secret", folderCopy.FsConfig.CryptConfig.Passphrase.GetPayload())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCachedFs(t *testing.T) {
|
|
|
|
user := dataprovider.User{
|
|
|
|
HomeDir: filepath.Clean(os.TempDir()),
|
|
|
|
}
|
|
|
|
conn := NewBaseConnection("id", ProtocolSFTP, user)
|
|
|
|
// changing the user should not affect the connection
|
|
|
|
user.HomeDir = filepath.Join(os.TempDir(), "temp")
|
|
|
|
err := os.Mkdir(user.HomeDir, os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
fs, err := user.GetFilesystem("")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
p, err := fs.ResolvePath("/")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, user.GetHomeDir(), p)
|
|
|
|
|
|
|
|
_, p, err = conn.GetFsAndResolvedPath("/")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, filepath.Clean(os.TempDir()), p)
|
|
|
|
user.FsConfig.Provider = vfs.S3FilesystemProvider
|
|
|
|
_, err = user.GetFilesystem("")
|
|
|
|
assert.Error(t, err)
|
|
|
|
conn.User.FsConfig.Provider = vfs.S3FilesystemProvider
|
|
|
|
_, p, err = conn.GetFsAndResolvedPath("/")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, filepath.Clean(os.TempDir()), p)
|
|
|
|
err = os.Remove(user.HomeDir)
|
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
}
|
2021-04-25 07:38:33 +00:00
|
|
|
|
2021-05-11 04:54:06 +00:00
|
|
|
func TestParseAllowedIPAndRanges(t *testing.T) {
|
|
|
|
_, err := utils.ParseAllowedIPAndRanges([]string{"1.1.1.1", "not an ip"})
|
|
|
|
assert.Error(t, err)
|
|
|
|
_, err = utils.ParseAllowedIPAndRanges([]string{"1.1.1.5", "192.168.1.0/240"})
|
|
|
|
assert.Error(t, err)
|
|
|
|
allow, err := utils.ParseAllowedIPAndRanges([]string{"192.168.1.2", "172.16.0.0/24"})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, allow[0](net.ParseIP("192.168.1.2")))
|
|
|
|
assert.False(t, allow[0](net.ParseIP("192.168.2.2")))
|
|
|
|
assert.True(t, allow[1](net.ParseIP("172.16.0.1")))
|
|
|
|
assert.False(t, allow[1](net.ParseIP("172.16.1.1")))
|
|
|
|
}
|
|
|
|
|
2021-04-25 07:38:33 +00:00
|
|
|
func BenchmarkBcryptHashing(b *testing.B) {
|
|
|
|
bcryptPassword := "bcryptpassword"
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_, err := bcrypt.GenerateFromPassword([]byte(bcryptPassword), 10)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkCompareBcryptPassword(b *testing.B) {
|
|
|
|
bcryptPassword := "$2a$10$lPDdnDimJZ7d5/GwL6xDuOqoZVRXok6OHHhivCnanWUtcgN0Zafki"
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
err := bcrypt.CompareHashAndPassword([]byte(bcryptPassword), []byte("password"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkArgon2Hashing(b *testing.B) {
|
|
|
|
argonPassword := "argon2password"
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_, err := argon2id.CreateHash(argonPassword, argon2id.DefaultParams)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkCompareArgon2Password(b *testing.B) {
|
|
|
|
argon2Password := "$argon2id$v=19$m=65536,t=1,p=2$aOoAOdAwvzhOgi7wUFjXlw$wn/y37dBWdKHtPXHR03nNaKHWKPXyNuVXOknaU+YZ+s"
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_, err := argon2id.ComparePasswordAndHash("password", argon2Password)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|