|
| 1 | +// SPDX-FileCopyrightText: Copyright 2025 The SLSA Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package repo |
| 5 | + |
| 6 | +import ( |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + memfs "github.com/go-git/go-billy/v6/memfs" |
| 12 | + git "github.com/go-git/go-git/v6" |
| 13 | + "github.com/go-git/go-git/v6/storage/memory" |
| 14 | + |
| 15 | + "github.com/slsa-framework/source-tool/pkg/repo/options" |
| 16 | + "github.com/slsa-framework/source-tool/pkg/sourcetool/models" |
| 17 | +) |
| 18 | + |
| 19 | +// TestCloneAddFiles_CreatesValidCommit is an integration test that verifies |
| 20 | +// the entire workflow of adding files and committing works correctly with timestamps. |
| 21 | +func TestCloneAddFiles_CreatesValidCommit(t *testing.T) { |
| 22 | + // Create an in-memory repository |
| 23 | + storer := memory.NewStorage() |
| 24 | + fs := memfs.New() |
| 25 | + |
| 26 | + repo, err := git.Init(storer, git.WithWorkTree(fs)) |
| 27 | + if err != nil { |
| 28 | + t.Fatalf("Failed to initialize test repository: %v", err) |
| 29 | + } |
| 30 | + |
| 31 | + clone := &Clone{ |
| 32 | + Repository: models.Repository{ |
| 33 | + Hostname: "github.com", |
| 34 | + Path: "test/repo", |
| 35 | + }, |
| 36 | + repo: repo, |
| 37 | + fs: fs, |
| 38 | + } |
| 39 | + |
| 40 | + // Add files using the AddFiles method |
| 41 | + files := []*PullRequestFileEntry{ |
| 42 | + { |
| 43 | + Path: "workflow.yaml", |
| 44 | + Reader: strings.NewReader("name: Test Workflow\non: push\n"), |
| 45 | + }, |
| 46 | + } |
| 47 | + |
| 48 | + err = clone.AddFiles(clone, files) |
| 49 | + if err != nil { |
| 50 | + t.Fatalf("AddFiles failed: %v", err) |
| 51 | + } |
| 52 | + |
| 53 | + // Create a commit |
| 54 | + useGit := false |
| 55 | + commitOpts := &options.CommitOptions{ |
| 56 | + Name: "Workflow Bot", |
| 57 | + |
| 58 | + Message: "Add SLSA Source Provenance Workflow", |
| 59 | + UseGit: &useGit, |
| 60 | + } |
| 61 | + |
| 62 | + beforeCommit := time.Now() |
| 63 | + err = clone.Commit(commitOpts) |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("Commit failed: %v", err) |
| 66 | + } |
| 67 | + afterCommit := time.Now() |
| 68 | + |
| 69 | + // Verify the commit |
| 70 | + ref, err := repo.Head() |
| 71 | + if err != nil { |
| 72 | + t.Fatalf("Failed to get HEAD reference: %v", err) |
| 73 | + } |
| 74 | + |
| 75 | + commit, err := repo.CommitObject(ref.Hash()) |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("Failed to get commit object: %v", err) |
| 78 | + } |
| 79 | + |
| 80 | + // Check the commit is not from epoch |
| 81 | + epochTime := time.Unix(0, 0) |
| 82 | + year1970 := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC) |
| 83 | + |
| 84 | + if commit.Author.When.Year() == 1970 { |
| 85 | + t.Errorf("Commit has 1970 timestamp: %v (this is the bug we're fixing)", commit.Author.When) |
| 86 | + } |
| 87 | + |
| 88 | + if commit.Author.When.Equal(epochTime) || commit.Author.When.Equal(year1970) { |
| 89 | + t.Errorf("Commit timestamp is epoch (Jan 1, 1970): %v", commit.Author.When) |
| 90 | + } |
| 91 | + |
| 92 | + // Verify timestamp is current |
| 93 | + if commit.Author.When.Before(beforeCommit.Add(-1*time.Minute)) || |
| 94 | + commit.Author.When.After(afterCommit.Add(1*time.Minute)) { |
| 95 | + t.Errorf("Commit timestamp %v is not close to current time [%v, %v]", |
| 96 | + commit.Author.When, beforeCommit, afterCommit) |
| 97 | + } |
| 98 | + |
| 99 | + // Verify the file was added |
| 100 | + tree, err := commit.Tree() |
| 101 | + if err != nil { |
| 102 | + t.Fatalf("Failed to get commit tree: %v", err) |
| 103 | + } |
| 104 | + |
| 105 | + _, err = tree.File("workflow.yaml") |
| 106 | + if err != nil { |
| 107 | + t.Errorf("workflow.yaml not found in commit tree: %v", err) |
| 108 | + } |
| 109 | +} |
0 commit comments