Skip to content

Commit

Permalink
argon2: define test expectations explicitly
Browse files Browse the repository at this point in the history
This makes it clearer that we are actually testing something valid.
  • Loading branch information
chrisccoulson committed Mar 15, 2024
1 parent d5b6604 commit 497622e
Showing 1 changed file with 63 additions and 73 deletions.
136 changes: 63 additions & 73 deletions argon2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"math"
"os"
"runtime"
"time"

snapd_testutil "github.com/snapcore/snapd/testutil"

Expand All @@ -41,25 +40,25 @@ type argon2Suite struct {

kdf testutil.MockArgon2KDF

halfTotalRamKiB uint32
cpus uint8
maxMemoryCostKiBAuto int
cpusAuto int
}

func (s *argon2Suite) SetUpSuite(c *C) {
var sysInfo unix.Sysinfo_t
c.Check(unix.Sysinfo(&sysInfo), IsNil)

halfTotalRamKiB := uint64(sysInfo.Totalram) * uint64(sysInfo.Unit) / 2048
if halfTotalRamKiB > math.MaxUint32 {
halfTotalRamKiB = math.MaxUint32
if halfTotalRamKiB > math.MaxInt32 {
halfTotalRamKiB = math.MaxInt32
}
s.halfTotalRamKiB = uint32(halfTotalRamKiB)
s.maxMemoryCostKiBAuto = int(halfTotalRamKiB)

cpus := runtime.NumCPU()
if cpus > math.MaxUint8 {
cpus = math.MaxUint8
if cpus > 4 {
cpus = 4
}
s.cpus = uint8(cpus)
s.cpusAuto = cpus
}

func (s *argon2Suite) SetUpTest(c *C) {
Expand All @@ -71,60 +70,6 @@ func (s *argon2Suite) SetUpTest(c *C) {
s.AddCleanup(func() { SetArgon2KDF(origKdf) })
}

func (s *argon2Suite) checkParams(c *C, opts *Argon2Options, ncpus uint8, params *KdfParams) {
expectedMode := Argon2id
if opts.Mode != Argon2Default {
expectedMode = opts.Mode
}
c.Check(params.Type, Equals, string(expectedMode))

if opts.ForceIterations != 0 {
c.Check(params.Time, Equals, int(opts.ForceIterations))

expectedMem := opts.MemoryKiB
if expectedMem == 0 {
expectedMem = 1 * 1024 * 1024
}
c.Check(params.Memory, Equals, int(expectedMem))

expectedThreads := opts.Parallel
if expectedThreads == 0 {
expectedThreads = uint8(ncpus)
}
c.Check(params.CPUs, Equals, int(expectedThreads))
} else {
targetDuration := opts.TargetDuration
if targetDuration == 0 {
targetDuration = 2 * time.Second
}
var kdf testutil.MockArgon2KDF
duration, _ := kdf.Time(Argon2Default, &Argon2CostParams{
Time: uint32(params.Time),
MemoryKiB: uint32(params.Memory),
Threads: uint8(params.CPUs),
})
c.Check(duration, Equals, targetDuration)

maxMem := opts.MemoryKiB
if maxMem == 0 {
maxMem = 1 * 1024 * 1024
}
if maxMem > s.halfTotalRamKiB {
maxMem = s.halfTotalRamKiB
}
c.Check(params.Memory, snapd_testutil.IntLessEqual, int(maxMem))

expectedThreads := opts.Parallel
if expectedThreads == 0 {
expectedThreads = uint8(ncpus)
}
if expectedThreads > 4 {
expectedThreads = 4
}
c.Check(params.CPUs, Equals, int(expectedThreads))
}
}

var _ = Suite(&argon2Suite{})

func (s *argon2Suite) TestKDFParamsDefault(c *C) {
Expand All @@ -133,17 +78,27 @@ func (s *argon2Suite) TestKDFParamsDefault(c *C) {
c.Assert(err, IsNil)
c.Check(s.kdf.BenchmarkMode, Equals, Argon2id)

s.checkParams(c, &opts, s.cpus, params)
c.Check(params, DeepEquals, &KdfParams{
Type: "argon2id",
Time: 4,
Memory: 1024063,
CPUs: s.cpusAuto,
})
}

func (s *argon2Suite) TestKDFParamsExplicitMode(c *C) {
var opts Argon2Options
opts.Mode = Argon2i
params, err := opts.KdfParams(9)
params, err := opts.KdfParams(0)
c.Assert(err, IsNil)
c.Check(s.kdf.BenchmarkMode, Equals, Argon2i)

s.checkParams(c, &opts, s.cpus, params)
c.Check(params, DeepEquals, &KdfParams{
Type: "argon2i",
Time: 4,
Memory: 1024063,
CPUs: s.cpusAuto,
})
}

func (s *argon2Suite) TestKDFParamsMemoryLimit(c *C) {
Expand All @@ -153,7 +108,12 @@ func (s *argon2Suite) TestKDFParamsMemoryLimit(c *C) {
c.Assert(err, IsNil)
c.Check(s.kdf.BenchmarkMode, Equals, Argon2id)

s.checkParams(c, &opts, s.cpus, params)
c.Check(params, DeepEquals, &KdfParams{
Type: "argon2id",
Time: 125,
Memory: 32 * 1024,
CPUs: s.cpusAuto,
})
}

func (s *argon2Suite) TestKDFParamsForceBenchmarkedThreads(c *C) {
Expand All @@ -163,7 +123,12 @@ func (s *argon2Suite) TestKDFParamsForceBenchmarkedThreads(c *C) {
c.Assert(err, IsNil)
c.Check(s.kdf.BenchmarkMode, Equals, Argon2id)

s.checkParams(c, &opts, s.cpus, params)
c.Check(params, DeepEquals, &KdfParams{
Type: "argon2id",
Time: 4,
Memory: 1024063,
CPUs: 1,
})
}

func (s *argon2Suite) TestKDFParamsForceIterations(c *C) {
Expand All @@ -176,7 +141,12 @@ func (s *argon2Suite) TestKDFParamsForceIterations(c *C) {
c.Assert(err, IsNil)
c.Check(s.kdf.BenchmarkMode, Equals, Argon2Default)

s.checkParams(c, &opts, 2, params)
c.Check(params, DeepEquals, &KdfParams{
Type: "argon2id",
Time: 3,
Memory: 1 * 1024 * 1024,
CPUs: 2,
})
}

func (s *argon2Suite) TestKDFParamsForceMemory(c *C) {
Expand All @@ -190,7 +160,12 @@ func (s *argon2Suite) TestKDFParamsForceMemory(c *C) {
c.Assert(err, IsNil)
c.Check(s.kdf.BenchmarkMode, Equals, Argon2Default)

s.checkParams(c, &opts, 2, params)
c.Check(params, DeepEquals, &KdfParams{
Type: "argon2id",
Time: 3,
Memory: 32 * 1024,
CPUs: 2,
})
}

func (s *argon2Suite) TestKDFParamsForceIterationsDifferentCPUNum(c *C) {
Expand All @@ -203,7 +178,12 @@ func (s *argon2Suite) TestKDFParamsForceIterationsDifferentCPUNum(c *C) {
c.Assert(err, IsNil)
c.Check(s.kdf.BenchmarkMode, Equals, Argon2Default)

s.checkParams(c, &opts, 4, params)
c.Check(params, DeepEquals, &KdfParams{
Type: "argon2id",
Time: 3,
Memory: 1 * 1024 * 1024,
CPUs: 4,
})
}

func (s *argon2Suite) TestKDFParamsForceThreads(c *C) {
Expand All @@ -217,7 +197,12 @@ func (s *argon2Suite) TestKDFParamsForceThreads(c *C) {
c.Assert(err, IsNil)
c.Check(s.kdf.BenchmarkMode, Equals, Argon2Default)

s.checkParams(c, &opts, 1, params)
c.Check(params, DeepEquals, &KdfParams{
Type: "argon2id",
Time: 3,
Memory: 1 * 1024 * 1024,
CPUs: 1,
})
}

func (s *argon2Suite) TestKDFParamsForceThreadsGreatherThanCPUNum(c *C) {
Expand All @@ -231,7 +216,12 @@ func (s *argon2Suite) TestKDFParamsForceThreadsGreatherThanCPUNum(c *C) {
c.Assert(err, IsNil)
c.Check(s.kdf.BenchmarkMode, Equals, Argon2Default)

s.checkParams(c, &opts, 8, params)
c.Check(params, DeepEquals, &KdfParams{
Type: "argon2id",
Time: 3,
Memory: 1 * 1024 * 1024,
CPUs: 8,
})
}

func (s *argon2Suite) TestKDFParamsInvalidForceIterations(c *C) {
Expand Down

0 comments on commit 497622e

Please sign in to comment.