Best Syzkaller code snippet using prog.buildTestContext
mutation_test.go
Source:mutation_test.go
...117 for ti, test := range tests {118 test := test119 t.Run(fmt.Sprint(ti), func(t *testing.T) {120 t.Parallel()121 rs, ct, p, goal, err := buildTestContext(test, target)122 if err != nil {123 t.Fatalf("failed to deserialize the program: %v", err)124 }125 want := goal.Serialize()126 for i := 0; i < 1e5; i++ {127 p1 := p.Clone()128 ctx := &mutator{129 p: p1,130 r: newRand(p1.Target, rs),131 ncalls: 2 * len(p.Calls),132 ct: ct,133 corpus: nil,134 }135 ctx.mutateArg()136 data1 := p1.Serialize()137 if bytes.Equal(want, data1) {138 t.Logf("success on iter %v", i)139 return140 }141 }142 t.Fatalf("failed to achieve goal, original:%s\ngoal:%s", test[0], test[1])143 })144 }145}146func TestSizeMutateArg(t *testing.T) {147 target, rs, iters := initRandomTargetTest(t, "test", "64")148 r := newRand(target, rs)149 ct := target.DefaultChoiceTable()150 for i := 0; i < iters; i++ {151 p := target.Generate(rs, 10, ct)152 for it := 0; it < 10; it++ {153 p1 := p.Clone()154 ctx := &mutator{155 p: p1,156 r: r,157 ncalls: 2 * len(p.Calls),158 ct: ct,159 corpus: nil,160 }161 ctx.mutateArg()162 ForeachArg(p.Calls[0], func(arg Arg, ctx *ArgCtx) {163 if _, ok := arg.Type().(*IntType); !ok {164 return165 }166 bits := arg.Type().TypeBitSize()167 limit := uint64(1<<bits - 1)168 val := arg.(*ConstArg).Val169 if val > limit {170 t.Fatalf("Invalid argument value: %d. (arg size: %d; max value: %d)", val, arg.Size(), limit)171 }172 })173 }174 }175}176func TestClone(t *testing.T) {177 target, rs, iters := initTest(t)178 ct := target.DefaultChoiceTable()179 for i := 0; i < iters; i++ {180 p := target.Generate(rs, 10, ct)181 p1 := p.Clone()182 data := p.Serialize()183 data1 := p1.Serialize()184 if !bytes.Equal(data, data1) {185 t.Fatalf("program changed after clone\noriginal:\n%s\n\nnew:\n%s\n", data, data1)186 }187 }188}189func TestMutateRandom(t *testing.T) {190 testEachTargetRandom(t, func(t *testing.T, target *Target, rs rand.Source, iters int) {191 ct := target.DefaultChoiceTable()192 next:193 for i := 0; i < iters; i++ {194 p := target.Generate(rs, 10, ct)195 data0 := p.Serialize()196 p1 := p.Clone()197 // There is a chance that mutation will produce the same program.198 // So we check that at least 1 out of 20 mutations actually change the program.199 for try := 0; try < 20; try++ {200 p1.Mutate(rs, 10, ct, nil)201 data := p.Serialize()202 if !bytes.Equal(data0, data) {203 t.Fatalf("program changed after mutate\noriginal:\n%s\n\nnew:\n%s\n",204 data0, data)205 }206 data1 := p1.Serialize()207 if bytes.Equal(data, data1) {208 continue209 }210 if _, err := target.Deserialize(data1, NonStrict); err != nil {211 t.Fatalf("Deserialize failed after Mutate: %v\n%s", err, data1)212 }213 continue next214 }215 t.Fatalf("mutation does not change program:\n%s", data0)216 }217 })218}219func TestMutateCorpus(t *testing.T) {220 target, rs, iters := initTest(t)221 ct := target.DefaultChoiceTable()222 var corpus []*Prog223 for i := 0; i < 100; i++ {224 p := target.Generate(rs, 10, ct)225 corpus = append(corpus, p)226 }227 for i := 0; i < iters; i++ {228 p1 := target.Generate(rs, 10, ct)229 p1.Mutate(rs, 10, ct, corpus)230 }231}232func TestMutateTable(t *testing.T) {233 tests := [][2]string{234 // Insert a call.235 {`236mutate0()237mutate2()238`, `239mutate0()240mutate1()241mutate2()242`},243 // Remove calls and update args.244 {`245r0 = mutate5(&(0x7f0000000000)="2e2f66696c653000", 0x0)246mutate0()247mutate6(r0, &(0x7f0000000000)="00", 0x1)248mutate1()249`, `250mutate0()251mutate6(0xffffffffffffffff, &(0x7f0000000000)="00", 0x1)252mutate1()253`},254 // Mutate flags.255 {`256r0 = mutate5(&(0x7f0000000000)="2e2f66696c653000", 0x0)257mutate0()258mutate6(r0, &(0x7f0000000000)="00", 0x1)259mutate1()260`, `261r0 = mutate5(&(0x7f0000000000)="2e2f66696c653000", 0xcdcdcdcd)262mutate0()263mutate6(r0, &(0x7f0000000000)="00", 0x1)264mutate1()265`},266 // Mutate data (delete byte and update size).267 {`268mutate4(&(0x7f0000000000)="11223344", 0x4)269`, `270mutate4(&(0x7f0000000000)="113344", 0x3)271`},272 // Mutate data (change byte).273 {`274mutate4(&(0x7f0000000000)="1122", 0x2)275`, `276mutate4(&(0x7f0000000000)="1100", 0x2)277`},278 // Change filename.279 {`280mutate5(&(0x7f0000001000)="2e2f66696c653000", 0x22c0)281mutate5(&(0x7f0000001000)="2e2f66696c653000", 0x22c0)282`, `283mutate5(&(0x7f0000001000)="2e2f66696c653000", 0x22c0)284mutate5(&(0x7f0000001000)="2e2f66696c653100", 0x22c0)285`},286 // Mutate the array.287 {`288mutate_array(0x1, 0x30, &(0x7f0000000000)=[0x1, 0x1, 0x1, 0x1, 0x1])289`, `290mutate_array(0x1, 0x30, &(0x7f0000000000)=[0x1, 0x1, 0x1, 0x1])291`},292 // Extend an array.293 {`294mutate3(&(0x7f0000000000)=[0x1, 0x1], 0x2)295`, `296mutate3(&(0x7f0000000000)=[0x1, 0x1, 0x1], 0x3)297`},298 // Mutate size from it's natural value.299 {`300mutate7(&(0x7f0000000000)='123', 0x3)301`, `302mutate7(&(0x7f0000000000)='123', 0x2)303`},304 // Mutate proc to the special value.305 {`306mutate8(0x2)307`, `308mutate8(0xffffffffffffffff)309`},310 // Increase buffer length.311 {`312mutate_buffer(&(0x7f0000000000)=""/100)313`, `314mutate_buffer(&(0x7f0000000000)=""/200)315`},316 // Decrease buffer length.317 {`318mutate_buffer(&(0x7f0000000000)=""/800)319`, `320mutate_buffer(&(0x7f0000000000)=""/4)321`},322 // Mutate a ranged buffer.323 {`324mutate_rangedbuffer(&(0x7f00000000c0)=""/10)325`, `326mutate_rangedbuffer(&(0x7f00000000c0)=""/7)327`},328 }329 runMutationTests(t, tests, true)330}331func TestNegativeMutations(t *testing.T) {332 tests := [][2]string{333 // Mutate buffer size outside the range limits.334 {`335mutate_rangedbuffer(&(0x7f00000000c0)=""/7)336`, `337mutate_rangedbuffer(&(0x7f00000000c0)=""/4)338`},339 {`340mutate_rangedbuffer(&(0x7f00000000c0)=""/7)341`, `342mutate_rangedbuffer(&(0x7f00000000c0)=""/11)343`},344 }345 runMutationTests(t, tests, false)346}347func BenchmarkMutate(b *testing.B) {348 target, cleanup := initBench(b)349 defer cleanup()350 ct := target.DefaultChoiceTable()351 const progLen = 30352 p := target.Generate(rand.NewSource(0), progLen, ct)353 b.ResetTimer()354 b.RunParallel(func(pb *testing.PB) {355 rs := rand.NewSource(0)356 for pb.Next() {357 p.Clone().Mutate(rs, progLen, ct, nil)358 }359 })360}361func BenchmarkGenerate(b *testing.B) {362 target, cleanup := initBench(b)363 defer cleanup()364 ct := target.DefaultChoiceTable()365 const progLen = 30366 b.ResetTimer()367 b.RunParallel(func(pb *testing.PB) {368 rs := rand.NewSource(0)369 for pb.Next() {370 target.Generate(rs, progLen, ct)371 }372 })373}374func runMutationTests(t *testing.T, tests [][2]string, valid bool) {375 target := initTargetTest(t, "test", "64")376 for ti, test := range tests {377 test := test378 t.Run(fmt.Sprint(ti), func(t *testing.T) {379 t.Parallel()380 rs, ct, p, goal, err := buildTestContext(test, target)381 if err != nil {382 t.Fatalf("failed to deserialize the program: %v", err)383 }384 want := goal.Serialize()385 iters := iterCount()386 if valid {387 iters = 1e6 // it will stop after reaching the goal388 }389 for i := 0; i < iters; i++ {390 p1 := p.Clone()391 p1.Mutate(rs, len(goal.Calls), ct, nil)392 data1 := p1.Serialize()393 if bytes.Equal(want, data1) {394 if !valid {395 t.Fatalf("failed on iter %v", i)396 }397 t.Logf("success on iter %v", i)398 return399 }400 }401 if valid {402 t.Fatalf("failed to achieve goal, original:%s\ngoal:%s", test[0], test[1])403 }404 })405 }406}407func buildTestContext(test [2]string, target *Target) (rs rand.Source, ct *ChoiceTable, p, goal *Prog, err error) {408 p, err = target.Deserialize([]byte(test[0]), Strict)409 if err != nil {410 return411 }412 goal, err = target.Deserialize([]byte(test[1]), Strict)413 if err != nil {414 return415 }416 enabled := make(map[*Syscall]bool)417 for _, c := range p.Calls {418 enabled[c.Meta] = true419 }420 for _, c := range goal.Calls {421 enabled[c.Meta] = true...
buildTestContext
Using AI Code Generation
1import "fmt"2func main() {3 fmt.Println("Hello, playground")4}5import "fmt"6func main() {7 fmt.Println("Hello, playground")8}9import "fmt"10func main() {11 fmt.Println("Hello, playground")12}13import "fmt"14func main() {15 fmt.Println("Hello, playground")16}17import "fmt"18func main() {19 fmt.Println("Hello, playground")20}21import "fmt"22func main() {23 fmt.Println("Hello, playground")24}25import "fmt"26func main() {27 fmt.Println("Hello, playground")28}29import "fmt"30func main() {31 fmt.Println("Hello, playground")32}33import "fmt"34func main() {35 fmt.Println("Hello, playground")36}37import (38func main() {39 files, _ := filepath.Glob("*.go")40 for _, file := range files {41 cmd := exec.Command("go", "run", file)42 stdout, err := cmd.Output()43 if err != nil {44 fmt.Println(err.Error())
buildTestContext
Using AI Code Generation
1import (2func main() {3 fmt.Println(prog.BuildTestContext())4}5import (6func BuildTestContext() string {7 fmt.Println("BuildTestContext")8 _, filename, _, _ := runtime.Caller(0)9 dir := filepath.Dir(filename)10 fmt.Println("dir", dir)11 cmd := exec.Command("go", "build", "-o", "buildtest", "buildtest.go")12 err := cmd.Run()13 if err != nil {14 return fmt.Sprintf("BuildTestContext: build failed: %v", err)15 }16}17import (18func main() {19 fmt.Println("BuildTestContext")20}
buildTestContext
Using AI Code Generation
1import "fmt"2func main() {3 p := prog{}4 p.buildTestContext()5 fmt.Println("Hello World!")6}7type prog struct {8}9func (p *prog) buildTestContext() {10}
buildTestContext
Using AI Code Generation
1import (2func main() {3 p := prog.New()4 fmt.Println("Test context:", p.BuildTestContext())5}6import (7type Prog struct {8}9func New() *Prog {10 return &Prog{}11}12func (p *Prog) BuildTestContext() string {13 return fmt.Sprintf("go version: %s", runtime.Version())14}15import (16func TestBuildTestContext(t *testing.T) {17 p := New()18 want := fmt.Sprintf("go version: %s", runtime.Version())19 got := p.BuildTestContext()20 if got != want {21 t.Fatalf("want %v got %v", want, got)22 }23}24import (25func TestBuildTestContext(t *testing.T) {26 p := New()27 want := fmt.Sprintf("go version: %s", runtime.Version())28 got := p.BuildTestContext()29 if got != want {30 t.Fatalf("want %v got %v", want, got)31 }32}33import (34func TestBuildTestContext(t *testing.T) {35 p := New()36 want := fmt.Sprintf("go version: %s", runtime.Version())37 got := p.BuildTestContext()38 if got != want {39 t.Fatalf("want %v got %v", want, got)40 }41}42import (43func TestBuildTestContext(t *testing.T) {44 p := New()45 want := fmt.Sprintf("go version: %s", runtime.Version())46 got := p.BuildTestContext()47 if got != want {48 t.Fatalf("want %v got %v", want, got)49 }50}51import (
buildTestContext
Using AI Code Generation
1import (2func main() {3 p, err := ctx.Import("hello", "", build.FindOnly)4 if err != nil {5 fmt.Println(err)6 }7 fmt.Println(p)8}9{hello /home/abhishek/gotest/src/hello [main.go] [/home/abhishek/gotest/src/hello] [/home/ab
buildTestContext
Using AI Code Generation
1import (2type testContext struct {3}4func (t *testContext) buildTestContext() {5 t.testQuestions = []string{"q1", "q2", "q3"}6}7type prog struct {8}9func (p *prog) buildTestContext() {10 p.progTestContext.buildTestContext()11}12func main() {13 p := prog{"prog1", 1, "v1", testContext{}}14 p.buildTestContext()15 fmt.Println(p)16}17{prog1 1 v1 {1 MCQ 60 [q1 q2 q3]}}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!