From 497622e3f3a5a18f993f2531be3db7f1af046ab8 Mon Sep 17 00:00:00 2001 From: Chris Coulson Date: Fri, 15 Mar 2024 23:30:18 +0000 Subject: [PATCH] argon2: define test expectations explicitly This makes it clearer that we are actually testing something valid. --- argon2_test.go | 136 +++++++++++++++++++++++-------------------------- 1 file changed, 63 insertions(+), 73 deletions(-) diff --git a/argon2_test.go b/argon2_test.go index 509703e2..2265ecc4 100644 --- a/argon2_test.go +++ b/argon2_test.go @@ -23,7 +23,6 @@ import ( "math" "os" "runtime" - "time" snapd_testutil "github.com/snapcore/snapd/testutil" @@ -41,8 +40,8 @@ type argon2Suite struct { kdf testutil.MockArgon2KDF - halfTotalRamKiB uint32 - cpus uint8 + maxMemoryCostKiBAuto int + cpusAuto int } func (s *argon2Suite) SetUpSuite(c *C) { @@ -50,16 +49,16 @@ func (s *argon2Suite) SetUpSuite(c *C) { 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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) {