github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/vcs/linux_patches_test.go (about) 1 // Copyright 2023 syzkaller project authors. All rights reserved. 2 // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. 3 4 package vcs 5 6 import ( 7 "os" 8 "path/filepath" 9 "testing" 10 11 "github.com/google/syzkaller/pkg/osutil" 12 ) 13 14 func TestFixBackport(t *testing.T) { 15 baseDir := t.TempDir() 16 repo := MakeTestRepo(t, baseDir) 17 18 repo.Git("checkout", "-b", "main") 19 repo.Git("commit", "--no-edit", "--allow-empty", "-m", "starting commit") 20 21 // Let the fix stay in a separate branch. 22 repo.Git("checkout", "-b", "branch-with-a-fix") 23 filePath := filepath.Join(baseDir, "object.txt") 24 if err := os.WriteFile(filePath, []byte("content"), 0644); err != nil { 25 t.Fatal(err) 26 } 27 repo.Git("add", "object.txt") 28 repo.Git("commit", "--no-edit", "-m", "fix title") 29 fixCommit, _ := repo.repo.HeadCommit() 30 31 // Return to the original branch. 32 repo.Git("checkout", "main") 33 34 // Check the test is sane. 35 if osutil.IsExist(filePath) { 36 t.Fatalf("we have switched the branch, object.txt should not be present") 37 } 38 39 // Verify that the fix gets backported. 40 err := applyFixBackports(repo.repo, []BackportCommit{ 41 { 42 FixHash: fixCommit.Hash, 43 FixTitle: `fix title`, 44 }, 45 }) 46 if err != nil { 47 t.Fatal(err) 48 } 49 if !osutil.IsExist(filePath) { 50 t.Fatalf("the commit was not backported, but should have") 51 } 52 } 53 54 func TestConditionalFixBackport(t *testing.T) { 55 baseDir := t.TempDir() 56 repo := MakeTestRepo(t, baseDir) 57 58 repo.Git("checkout", "-b", "main") 59 repo.Git("commit", "--no-edit", "--allow-empty", "-m", "starting commit") 60 61 // Let the fix stay in a separate branch. 62 repo.Git("checkout", "-b", "branch-with-fix") 63 filePath := filepath.Join(baseDir, "object.txt") 64 if err := os.WriteFile(filePath, []byte("content"), 0644); err != nil { 65 t.Fatal(err) 66 } 67 repo.Git("add", "object.txt") 68 repo.Git("commit", "--no-edit", "-m", "fix title") 69 fixCommit, _ := repo.repo.HeadCommit() 70 71 // Create a branch without a bug. 72 repo.Git("checkout", "main") 73 repo.Git("checkout", "-b", "branch-no-bug") 74 repo.Git("commit", "--no-edit", "--allow-empty", "-m", "some commit") 75 76 // Create a branch with a bug. 77 repo.Git("checkout", "-b", "branch-with-bug") 78 repo.Git("commit", "--no-edit", "--allow-empty", "-m", "bad commit") 79 badCommit, _ := repo.repo.HeadCommit() 80 repo.Git("commit", "--no-edit", "--allow-empty", "-m", "some other commit") 81 82 // Ensure we do not cherry-pick the fix when there's no bug. 83 repo.Git("checkout", "branch-no-bug") 84 rules := []BackportCommit{ 85 { 86 GuiltyHash: badCommit.Hash, 87 FixHash: fixCommit.Hash, 88 FixTitle: `fix title`, 89 }, 90 } 91 err := applyFixBackports(repo.repo, rules) 92 if err != nil { 93 t.Fatal(err) 94 } 95 if osutil.IsExist(filePath) { 96 t.Fatalf("the commit was backported, but shouldn't have been") 97 } 98 99 // .. but we do cherry-pick otherwise. 100 repo.Git("checkout", "branch-with-bug") 101 err = applyFixBackports(repo.repo, rules) 102 if err != nil { 103 t.Fatal(err) 104 } 105 if !osutil.IsExist(filePath) { 106 t.Fatalf("the commit was not backported, but should have been") 107 } 108 }