From d2fbec350893e23f4bb6cb97f31a0bf2f29c5282 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Fri, 27 Jan 2023 22:03:36 +0100 Subject: [PATCH] Add unit tests for tlsVerifyConfig's yaml.Unmarshaler MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a security-critical piece of code, make sure we detect if it breaks. Signed-off-by: Miloslav Trmač --- cmd/skopeo/sync_test.go | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 cmd/skopeo/sync_test.go diff --git a/cmd/skopeo/sync_test.go b/cmd/skopeo/sync_test.go new file mode 100644 index 00000000..0db43b2b --- /dev/null +++ b/cmd/skopeo/sync_test.go @@ -0,0 +1,46 @@ +package main + +import ( + "testing" + + "github.com/containers/image/v5/types" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v2" +) + +var _ yaml.Unmarshaler = (*tlsVerifyConfig)(nil) + +func TestTLSVerifyConfig(t *testing.T) { + type container struct { // An example of a larger config file + TLSVerify tlsVerifyConfig `yaml:"tls-verify"` + } + + for _, c := range []struct { + input string + expected tlsVerifyConfig + }{ + { + input: `tls-verify: true`, + expected: tlsVerifyConfig{skip: types.OptionalBoolFalse}, + }, + { + input: `tls-verify: false`, + expected: tlsVerifyConfig{skip: types.OptionalBoolTrue}, + }, + { + input: ``, // No value + expected: tlsVerifyConfig{skip: types.OptionalBoolUndefined}, + }, + } { + config := container{} + err := yaml.Unmarshal([]byte(c.input), &config) + require.NoError(t, err, c.input) + assert.Equal(t, c.expected, config.TLSVerify, c.input) + } + + // Invalid input + config := container{} + err := yaml.Unmarshal([]byte(`tls-verify: "not a valid bool"`), &config) + assert.Error(t, err) +}