gittuf/pkg/gitinterface/config_test.go
Paulo Gomes 9e269f46fb
Introduce gitstore.Storer and make gittuf's core dependency-light
Restructure storage so gittuf verification can run over backends other
than the git binary (e.g. go-git):

- pkg/githash: concrete Git object hash, stdlib-only.
  gitinterface.Hash aliases it.
- pkg/gitstore: the single Storer interface (24 methods) that all
  storage consumers program against, plus the shared
  ErrReferenceNotFound sentinel. *gitinterface.Repository satisfies it
  structurally (compile-time asserted). Also defines ConfigKey, the
  canonical type for the Git config settings gittuf reads.
- pkg/rsl (from internal/rsl): entry model, codec, and readers over
  gitstore.Storer; zero gitinterface/sigstore dependencies. rsl.Hash
  aliases githash.Hash; nil is the unset-Hash sentinel and IsZero
  matches nil and empty as well as both format zeros (no
  object-format-unaware ZeroHash). Entry commits (empty tree on the
  RSL ref) are owned by the package; no storer adapter.
- internal/signerverifier/gitobject: verifies commit/tag signatures
  over (payload, signature) bytes, Rekor URL as an option. The storage
  half is Repository.GetObjectSignature. Removes sigstore, cosign, and
  gitsign from gitinterface's dependency tree.
- internal/propagation: propagation workflow, moved off pkg/rsl's
  public API (its tuf directive types are internal).
- internal/{attestations,cache,policy}: storage via gitstore.Storer;
  tree writing via WriteTree(blobs, subtrees).

Breaking changes to pkg/gitinterface: Repository.VerifySignature and
the verification sentinels are removed (use gitobject.Verify);
ErrReferenceNotFound now aliases gitstore's. Repository.GetGitConfig
(which returned the whole config map) is replaced by
LookupConfig(gitstore.ConfigKey), returning a single setting's value.
Policy resolves the Rekor override from git config once per
verification and extracts signed payloads once per object instead of
per key attempt.

Assisted-by: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Paulo Gomes <paulo@entire.io>
2026-08-03 21:27:49 +01:00

103 lines
2.8 KiB
Go

// Copyright The gittuf Authors
// SPDX-License-Identifier: Apache-2.0
package gitinterface
import (
"testing"
"github.com/gittuf/gittuf/pkg/gitstore"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLookupConfig(t *testing.T) {
tmpDir := t.TempDir()
repo := CreateTestGitRepository(t, tmpDir, false)
// CreateTestGitRepository sets our test config
name, ok, err := repo.LookupConfig(gitstore.ConfigUserName)
assert.Nil(t, err)
assert.True(t, ok)
assert.Equal(t, testName, name)
email, ok, err := repo.LookupConfig(gitstore.ConfigUserEmail)
assert.Nil(t, err)
assert.True(t, ok)
assert.Equal(t, testEmail, email)
_, ok, err = repo.LookupConfig("does.not.exist")
assert.Nil(t, err)
assert.False(t, ok)
}
func TestSetGitConfig(t *testing.T) {
t.Run("basic sets", func(t *testing.T) {
const name = "John Doe"
const email = "john.doe@example.com"
tmpDir := t.TempDir()
repo := CreateTestGitRepository(t, tmpDir, false)
err := repo.SetGitConfig("user.name", name)
require.NoError(t, err)
err = repo.SetGitConfig("user.email", email)
require.NoError(t, err)
gotName, ok, err := repo.LookupConfig(gitstore.ConfigUserName)
require.NoError(t, err)
assert.True(t, ok)
assert.Equal(t, name, gotName)
gotEmail, ok, err := repo.LookupConfig(gitstore.ConfigUserEmail)
require.NoError(t, err)
assert.True(t, ok)
assert.Equal(t, email, gotEmail)
})
t.Run("empty set", func(t *testing.T) {
tmpDir := t.TempDir()
repo := CreateTestGitRepository(t, tmpDir, false)
err := repo.SetGitConfig("user.name", "")
require.NoError(t, err)
err = repo.SetGitConfig("user.email", "")
require.NoError(t, err)
gotName, ok, err := repo.LookupConfig(gitstore.ConfigUserName)
require.NoError(t, err)
assert.True(t, ok)
assert.Equal(t, "", gotName)
gotEmail, ok, err := repo.LookupConfig(gitstore.ConfigUserEmail)
require.NoError(t, err)
assert.True(t, ok)
assert.Equal(t, "", gotEmail)
})
t.Run("gpg.format special case", func(t *testing.T) {
tmpDir := t.TempDir()
repo := CreateTestGitRepository(t, tmpDir, false)
err := repo.SetGitConfig("gpg.format", "gpg")
require.NoError(t, err)
format, ok, err := repo.LookupConfig(gitstore.ConfigGPGFormat)
require.NoError(t, err)
assert.True(t, ok)
assert.Equal(t, "gpg", format)
// A set-but-empty key returns "" with ok true, distinct from unset.
err = repo.SetGitConfig("gpg.format", "")
require.NoError(t, err)
format, ok, err = repo.LookupConfig(gitstore.ConfigGPGFormat)
require.NoError(t, err)
assert.True(t, ok)
assert.Equal(t, "", format)
})
t.Run("invalid key", func(t *testing.T) {
tmpDir := t.TempDir()
repo := CreateTestGitRepository(t, tmpDir, false)
err := repo.SetGitConfig("", "value")
assert.ErrorContains(t, err, "unable to set")
})
}