How to use Err method of prog Package

Best Syzkaller code snippet using prog.Err

loader_test.go

Source:loader_test.go Github

copy

Full Screen

...28// TestFromArgs checks that conf.FromArgs populates conf correctly.29// It does no I/O.30func TestFromArgs(t *testing.T) {31 type result struct {32 Err string33 Rest []string34 ImportPkgs map[string]bool35 CreatePkgs []loader.PkgSpec36 }37 for _, test := range []struct {38 args []string39 tests bool40 want result41 }{42 // Mix of existing and non-existent packages.43 {44 args: []string{"nosuchpkg", "errors"},45 want: result{46 ImportPkgs: map[string]bool{"errors": false, "nosuchpkg": false},47 },48 },49 // Same, with -test flag.50 {51 args: []string{"nosuchpkg", "errors"},52 tests: true,53 want: result{54 ImportPkgs: map[string]bool{"errors": true, "nosuchpkg": true},55 },56 },57 // Surplus arguments.58 {59 args: []string{"fmt", "errors", "--", "surplus"},60 want: result{61 Rest: []string{"surplus"},62 ImportPkgs: map[string]bool{"errors": false, "fmt": false},63 },64 },65 // Ad hoc package specified as *.go files.66 {67 args: []string{"foo.go", "bar.go"},68 want: result{CreatePkgs: []loader.PkgSpec{{69 Filenames: []string{"foo.go", "bar.go"},70 }}},71 },72 // Mixture of *.go and import paths.73 {74 args: []string{"foo.go", "fmt"},75 want: result{76 Err: "named files must be .go files: fmt",77 },78 },79 } {80 var conf loader.Config81 rest, err := conf.FromArgs(test.args, test.tests)82 got := result{83 Rest: rest,84 ImportPkgs: conf.ImportPkgs,85 CreatePkgs: conf.CreatePkgs,86 }87 if err != nil {88 got.Err = err.Error()89 }90 if !reflect.DeepEqual(got, test.want) {91 t.Errorf("FromArgs(%q) = %+v, want %+v", test.args, got, test.want)92 }93 }94}95func TestLoad_NoInitialPackages(t *testing.T) {96 var conf loader.Config97 const wantErr = "no initial packages were loaded"98 prog, err := conf.Load()99 if err == nil {100 t.Errorf("Load succeeded unexpectedly, want %q", wantErr)101 } else if err.Error() != wantErr {102 t.Errorf("Load failed with wrong error %q, want %q", err, wantErr)103 }104 if prog != nil {105 t.Errorf("Load unexpectedly returned a Program")106 }107}108func TestLoad_MissingInitialPackage(t *testing.T) {109 var conf loader.Config110 conf.Import("nosuchpkg")111 conf.Import("errors")112 const wantErr = "couldn't load packages due to errors: nosuchpkg"113 prog, err := conf.Load()114 if err == nil {115 t.Errorf("Load succeeded unexpectedly, want %q", wantErr)116 } else if err.Error() != wantErr {117 t.Errorf("Load failed with wrong error %q, want %q", err, wantErr)118 }119 if prog != nil {120 t.Errorf("Load unexpectedly returned a Program")121 }122}123func TestLoad_MissingInitialPackage_AllowErrors(t *testing.T) {124 if runtime.Compiler == "gccgo" {125 t.Skip("gccgo has no standard library test files")126 }127 var conf loader.Config128 conf.AllowErrors = true129 conf.Import("nosuchpkg")130 conf.ImportWithTests("errors")131 prog, err := conf.Load()132 if err != nil {133 t.Errorf("Load failed unexpectedly: %v", err)134 }135 if prog == nil {136 t.Fatalf("Load returned a nil Program")137 }138 if got, want := created(prog), "errors_test"; got != want {139 t.Errorf("Created = %s, want %s", got, want)140 }141 if got, want := imported(prog), "errors"; got != want {142 t.Errorf("Imported = %s, want %s", got, want)143 }144}145func TestCreateUnnamedPackage(t *testing.T) {146 var conf loader.Config147 conf.CreateFromFilenames("")148 prog, err := conf.Load()149 if err != nil {150 t.Fatalf("Load failed: %v", err)151 }152 if got, want := fmt.Sprint(prog.InitialPackages()), "[(unnamed)]"; got != want {153 t.Errorf("InitialPackages = %s, want %s", got, want)154 }155}156func TestLoad_MissingFileInCreatedPackage(t *testing.T) {157 var conf loader.Config158 conf.CreateFromFilenames("", "missing.go")159 const wantErr = "couldn't load packages due to errors: (unnamed)"160 prog, err := conf.Load()161 if prog != nil {162 t.Errorf("Load unexpectedly returned a Program")163 }164 if err == nil {165 t.Fatalf("Load succeeded unexpectedly, want %q", wantErr)166 }167 if err.Error() != wantErr {168 t.Fatalf("Load failed with wrong error %q, want %q", err, wantErr)169 }170}171func TestLoad_MissingFileInCreatedPackage_AllowErrors(t *testing.T) {172 conf := loader.Config{AllowErrors: true}173 conf.CreateFromFilenames("", "missing.go")174 prog, err := conf.Load()175 if err != nil {176 t.Errorf("Load failed: %v", err)177 }178 if got, want := fmt.Sprint(prog.InitialPackages()), "[(unnamed)]"; got != want {179 t.Fatalf("InitialPackages = %s, want %s", got, want)180 }181}182func TestLoad_ParseError(t *testing.T) {183 var conf loader.Config184 conf.CreateFromFilenames("badpkg", "testdata/badpkgdecl.go")185 const wantErr = "couldn't load packages due to errors: badpkg"186 prog, err := conf.Load()187 if prog != nil {188 t.Errorf("Load unexpectedly returned a Program")189 }190 if err == nil {191 t.Fatalf("Load succeeded unexpectedly, want %q", wantErr)192 }193 if err.Error() != wantErr {194 t.Fatalf("Load failed with wrong error %q, want %q", err, wantErr)195 }196}197func TestLoad_ParseError_AllowErrors(t *testing.T) {198 var conf loader.Config199 conf.AllowErrors = true200 conf.CreateFromFilenames("badpkg", "testdata/badpkgdecl.go")201 prog, err := conf.Load()202 if err != nil {203 t.Errorf("Load failed unexpectedly: %v", err)204 }205 if prog == nil {206 t.Fatalf("Load returned a nil Program")207 }208 if got, want := created(prog), "badpkg"; got != want {209 t.Errorf("Created = %s, want %s", got, want)210 }211 badpkg := prog.Created[0]212 if len(badpkg.Files) != 1 {213 t.Errorf("badpkg has %d files, want 1", len(badpkg.Files))214 }215 wantErr := filepath.Join("testdata", "badpkgdecl.go") + ":1:34: expected 'package', found 'EOF'"216 if !hasError(badpkg.Errors, wantErr) {217 t.Errorf("badpkg.Errors = %v, want %s", badpkg.Errors, wantErr)218 }219}220func TestLoad_FromSource_Success(t *testing.T) {221 var conf loader.Config222 conf.CreateFromFilenames("P", "testdata/a.go", "testdata/b.go")223 prog, err := conf.Load()224 if err != nil {225 t.Errorf("Load failed unexpectedly: %v", err)226 }227 if prog == nil {228 t.Fatalf("Load returned a nil Program")229 }230 if got, want := created(prog), "P"; got != want {231 t.Errorf("Created = %s, want %s", got, want)232 }233}234func TestLoad_FromImports_Success(t *testing.T) {235 if runtime.Compiler == "gccgo" {236 t.Skip("gccgo has no standard library test files")237 }238 var conf loader.Config239 conf.ImportWithTests("fmt")240 conf.ImportWithTests("errors")241 prog, err := conf.Load()242 if err != nil {243 t.Errorf("Load failed unexpectedly: %v", err)244 }245 if prog == nil {246 t.Fatalf("Load returned a nil Program")247 }248 if got, want := created(prog), "errors_test fmt_test"; got != want {249 t.Errorf("Created = %q, want %s", got, want)250 }251 if got, want := imported(prog), "errors fmt"; got != want {252 t.Errorf("Imported = %s, want %s", got, want)253 }254 // Check set of transitive packages.255 // There are >30 and the set may grow over time, so only check a few.256 want := map[string]bool{257 "strings": true,258 "time": true,259 "runtime": true,260 "testing": true,261 "unicode": true,262 }263 for _, path := range all(prog) {264 delete(want, path)265 }266 if len(want) > 0 {267 t.Errorf("AllPackages is missing these keys: %q", keys(want))268 }269}270func TestLoad_MissingIndirectImport(t *testing.T) {271 pkgs := map[string]string{272 "a": `package a; import _ "b"`,273 "b": `package b; import _ "c"`,274 }275 conf := loader.Config{Build: fakeContext(pkgs)}276 conf.Import("a")277 const wantErr = "couldn't load packages due to errors: b"278 prog, err := conf.Load()279 if err == nil {280 t.Errorf("Load succeeded unexpectedly, want %q", wantErr)281 } else if err.Error() != wantErr {282 t.Errorf("Load failed with wrong error %q, want %q", err, wantErr)283 }284 if prog != nil {285 t.Errorf("Load unexpectedly returned a Program")286 }287}288func TestLoad_BadDependency_AllowErrors(t *testing.T) {289 for _, test := range []struct {290 descr string291 pkgs map[string]string292 wantPkgs string293 }{294 {295 descr: "missing dependency",296 pkgs: map[string]string{297 "a": `package a; import _ "b"`,298 "b": `package b; import _ "c"`,299 },300 wantPkgs: "a b",301 },302 {303 descr: "bad package decl in dependency",304 pkgs: map[string]string{305 "a": `package a; import _ "b"`,306 "b": `package b; import _ "c"`,307 "c": `package`,308 },309 wantPkgs: "a b",310 },311 {312 descr: "parse error in dependency",313 pkgs: map[string]string{314 "a": `package a; import _ "b"`,315 "b": `package b; import _ "c"`,316 "c": `package c; var x = `,317 },318 wantPkgs: "a b c",319 },320 } {321 conf := loader.Config{322 AllowErrors: true,323 Build: fakeContext(test.pkgs),324 }325 conf.Import("a")326 prog, err := conf.Load()327 if err != nil {328 t.Errorf("%s: Load failed unexpectedly: %v", test.descr, err)329 }330 if prog == nil {331 t.Fatalf("%s: Load returned a nil Program", test.descr)332 }333 if got, want := imported(prog), "a"; got != want {334 t.Errorf("%s: Imported = %s, want %s", test.descr, got, want)335 }336 if got := all(prog); strings.Join(got, " ") != test.wantPkgs {337 t.Errorf("%s: AllPackages = %s, want %s", test.descr, got, test.wantPkgs)338 }339 }340}341func TestCwd(t *testing.T) {342 ctxt := fakeContext(map[string]string{"one/two/three": `package three`})343 for _, test := range []struct {344 cwd, arg, want string345 }{346 {cwd: "/go/src/one", arg: "./two/three", want: "one/two/three"},347 {cwd: "/go/src/one", arg: "../one/two/three", want: "one/two/three"},348 {cwd: "/go/src/one", arg: "one/two/three", want: "one/two/three"},349 {cwd: "/go/src/one/two/three", arg: ".", want: "one/two/three"},350 {cwd: "/go/src/one", arg: "two/three", want: ""},351 } {352 conf := loader.Config{353 Cwd: test.cwd,354 Build: ctxt,355 }356 conf.Import(test.arg)357 var got string358 prog, err := conf.Load()359 if prog != nil {360 got = imported(prog)361 }362 if got != test.want {363 t.Errorf("Load(%s) from %s: Imported = %s, want %s",364 test.arg, test.cwd, got, test.want)365 if err != nil {366 t.Errorf("Load failed: %v", err)367 }368 }369 }370}371func TestLoad_vendor(t *testing.T) {372 pkgs := map[string]string{373 "a": `package a; import _ "x"`,374 "a/vendor": ``, // mkdir a/vendor375 "a/vendor/x": `package xa`,376 "b": `package b; import _ "x"`,377 "b/vendor": ``, // mkdir b/vendor378 "b/vendor/x": `package xb`,379 "c": `package c; import _ "x"`,380 "x": `package xc`,381 }382 conf := loader.Config{Build: fakeContext(pkgs)}383 conf.Import("a")384 conf.Import("b")385 conf.Import("c")386 prog, err := conf.Load()387 if err != nil {388 t.Fatal(err)389 }390 // Check that a, b, and c see different versions of x.391 for _, r := range "abc" {392 name := string(r)393 got := prog.Package(name).Pkg.Imports()[0]394 want := "x" + name395 if got.Name() != want {396 t.Errorf("package %s import %q = %s, want %s",397 name, "x", got.Name(), want)398 }399 }400}401func TestVendorCwd(t *testing.T) {402 // Test the interaction of cwd and vendor directories.403 ctxt := fakeContext(map[string]string{404 "net": ``, // mkdir net405 "net/http": `package http; import _ "hpack"`,406 "vendor": ``, // mkdir vendor407 "vendor/hpack": `package vendorhpack`,408 "hpack": `package hpack`,409 })410 for i, test := range []struct {411 cwd, arg, want string412 }{413 {cwd: "/go/src/net", arg: "http"}, // not found414 {cwd: "/go/src/net", arg: "./http", want: "net/http vendor/hpack"},415 {cwd: "/go/src/net", arg: "hpack", want: "vendor/hpack"},416 {cwd: "/go/src/vendor", arg: "hpack", want: "vendor/hpack"},417 {cwd: "/go/src/vendor", arg: "./hpack", want: "vendor/hpack"},418 } {419 conf := loader.Config{420 Cwd: test.cwd,421 Build: ctxt,422 }423 conf.Import(test.arg)424 var got string425 prog, err := conf.Load()426 if prog != nil {427 got = strings.Join(all(prog), " ")428 }429 if got != test.want {430 t.Errorf("#%d: Load(%s) from %s: got %s, want %s",431 i, test.arg, test.cwd, got, test.want)432 if err != nil {433 t.Errorf("Load failed: %v", err)434 }435 }436 }437}438func TestVendorCwdIssue16580(t *testing.T) {439 // Regression test for Go issue 16580.440 // Import decls in "created" packages were vendor-resolved441 // w.r.t. cwd, not the parent directory of the package's files.442 ctxt := fakeContext(map[string]string{443 "a": ``, // mkdir a444 "a/vendor": ``, // mkdir a/vendor445 "a/vendor/b": `package b; const X = true`,446 "b": `package b; const X = false`,447 })448 for _, test := range []struct {449 filename, cwd string450 want bool // expected value of b.X; depends on filename, not on cwd451 }{452 {filename: "c.go", cwd: "/go/src", want: false},453 {filename: "c.go", cwd: "/go/src/a", want: false},454 {filename: "c.go", cwd: "/go/src/a/b", want: false},455 {filename: "c.go", cwd: "/go/src/a/vendor/b", want: false},456 {filename: "/go/src/a/c.go", cwd: "/go/src", want: true},457 {filename: "/go/src/a/c.go", cwd: "/go/src/a", want: true},458 {filename: "/go/src/a/c.go", cwd: "/go/src/a/b", want: true},459 {filename: "/go/src/a/c.go", cwd: "/go/src/a/vendor/b", want: true},460 {filename: "/go/src/c/c.go", cwd: "/go/src", want: false},461 {filename: "/go/src/c/c.go", cwd: "/go/src/a", want: false},462 {filename: "/go/src/c/c.go", cwd: "/go/src/a/b", want: false},463 {filename: "/go/src/c/c.go", cwd: "/go/src/a/vendor/b", want: false},464 } {465 conf := loader.Config{466 Cwd: test.cwd,467 Build: ctxt,468 }469 f, err := conf.ParseFile(test.filename, `package dummy; import "b"; const X = b.X`)470 if err != nil {471 t.Fatal(f)472 }473 conf.CreateFromFiles("dummy", f)474 prog, err := conf.Load()475 if err != nil {476 t.Errorf("%+v: Load failed: %v", test, err)477 continue478 }479 x := constant.BoolVal(prog.Created[0].Pkg.Scope().Lookup("X").(*types.Const).Val())480 if x != test.want {481 t.Errorf("%+v: b.X = %t", test, x)482 }483 }484 // TODO(adonovan): also test imports within XTestGoFiles.485}486// TODO(adonovan): more Load tests:487//488// failures:489// - to parse package decl of *_test.go files490// - to parse package decl of external *_test.go files491// - to parse whole of *_test.go files492// - to parse whole of external *_test.go files493// - to open a *.go file during import scanning494// - to import from binary495// features:496// - InitialPackages497// - PackageCreated hook498// - TypeCheckFuncBodies hook499func TestTransitivelyErrorFreeFlag(t *testing.T) {500 // Create an minimal custom build.Context501 // that fakes the following packages:502 //503 // a --> b --> c! c has an error504 // \ d and e are transitively error-free.505 // e --> d506 //507 // Each package [a-e] consists of one file, x.go.508 pkgs := map[string]string{509 "a": `package a; import (_ "b"; _ "e")`,510 "b": `package b; import _ "c"`,511 "c": `package c; func f() { _ = int(false) }`, // type error within function body512 "d": `package d;`,513 "e": `package e; import _ "d"`,514 }515 conf := loader.Config{516 AllowErrors: true,517 Build: fakeContext(pkgs),518 }519 conf.Import("a")520 prog, err := conf.Load()521 if err != nil {522 t.Errorf("Load failed: %s", err)523 }524 if prog == nil {525 t.Fatalf("Load returned nil *Program")526 }527 for pkg, info := range prog.AllPackages {528 var wantErr, wantTEF bool529 switch pkg.Path() {530 case "a", "b":531 case "c":532 wantErr = true533 case "d", "e":534 wantTEF = true535 default:536 t.Errorf("unexpected package: %q", pkg.Path())537 continue538 }539 if (info.Errors != nil) != wantErr {540 if wantErr {541 t.Errorf("Package %q.Error = nil, want error", pkg.Path())542 } else {543 t.Errorf("Package %q has unexpected Errors: %v",544 pkg.Path(), info.Errors)545 }546 }547 if info.TransitivelyErrorFree != wantTEF {548 t.Errorf("Package %q.TransitivelyErrorFree=%t, want %t",549 pkg.Path(), info.TransitivelyErrorFree, wantTEF)550 }551 }552}553// Test that syntax (scan/parse), type, and loader errors are recorded554// (in PackageInfo.Errors) and reported (via Config.TypeChecker.Error).555func TestErrorReporting(t *testing.T) {556 pkgs := map[string]string{557 "a": `package a; import (_ "b"; _ "c"); var x int = false`,558 "b": `package b; 'syntax error!`,559 }560 conf := loader.Config{561 AllowErrors: true,562 Build: fakeContext(pkgs),563 }564 var mu sync.Mutex565 var allErrors []error566 conf.TypeChecker.Error = func(err error) {567 mu.Lock()568 allErrors = append(allErrors, err)569 mu.Unlock()570 }571 conf.Import("a")572 prog, err := conf.Load()573 if err != nil {574 t.Errorf("Load failed: %s", err)575 }576 if prog == nil {577 t.Fatalf("Load returned nil *Program")578 }579 // TODO(adonovan): test keys of ImportMap.580 // Check errors recorded in each PackageInfo.581 for pkg, info := range prog.AllPackages {582 switch pkg.Path() {583 case "a":584 if !hasError(info.Errors, "cannot convert false") {585 t.Errorf("a.Errors = %v, want bool conversion (type) error", info.Errors)586 }587 if !hasError(info.Errors, "could not import c") {588 t.Errorf("a.Errors = %v, want import (loader) error", info.Errors)589 }590 case "b":591 if !hasError(info.Errors, "rune literal not terminated") {592 t.Errorf("b.Errors = %v, want unterminated literal (syntax) error", info.Errors)593 }594 }595 }596 // Check errors reported via error handler.597 if !hasError(allErrors, "cannot convert false") ||598 !hasError(allErrors, "rune literal not terminated") ||599 !hasError(allErrors, "could not import c") {600 t.Errorf("allErrors = %v, want syntax, type and loader errors", allErrors)601 }602}603func TestCycles(t *testing.T) {604 for _, test := range []struct {605 descr string606 ctxt *build.Context607 wantErr string608 }{609 {610 "self-cycle",611 fakeContext(map[string]string{612 "main": `package main; import _ "selfcycle"`,613 "selfcycle": `package selfcycle; import _ "selfcycle"`,614 }),615 `import cycle: selfcycle -> selfcycle`,616 },617 {618 "three-package cycle",619 fakeContext(map[string]string{620 "main": `package main; import _ "a"`,621 "a": `package a; import _ "b"`,622 "b": `package b; import _ "c"`,623 "c": `package c; import _ "a"`,624 }),625 `import cycle: c -> a -> b -> c`,626 },627 {628 "self-cycle in dependency of test file",629 buildutil.FakeContext(map[string]map[string]string{630 "main": {631 "main.go": `package main`,632 "main_test.go": `package main; import _ "a"`,633 },634 "a": {635 "a.go": `package a; import _ "a"`,636 },637 }),638 `import cycle: a -> a`,639 },640 // TODO(adonovan): fix: these fail641 // {642 // "two-package cycle in dependency of test file",643 // buildutil.FakeContext(map[string]map[string]string{644 // "main": {645 // "main.go": `package main`,646 // "main_test.go": `package main; import _ "a"`,647 // },648 // "a": {649 // "a.go": `package a; import _ "main"`,650 // },651 // }),652 // `import cycle: main -> a -> main`,653 // },654 // {655 // "self-cycle in augmented package",656 // buildutil.FakeContext(map[string]map[string]string{657 // "main": {658 // "main.go": `package main`,659 // "main_test.go": `package main; import _ "main"`,660 // },661 // }),662 // `import cycle: main -> main`,663 // },664 } {665 conf := loader.Config{666 AllowErrors: true,667 Build: test.ctxt,668 }669 var mu sync.Mutex670 var allErrors []error671 conf.TypeChecker.Error = func(err error) {672 mu.Lock()673 allErrors = append(allErrors, err)674 mu.Unlock()675 }676 conf.ImportWithTests("main")677 prog, err := conf.Load()678 if err != nil {679 t.Errorf("%s: Load failed: %s", test.descr, err)680 }681 if prog == nil {682 t.Fatalf("%s: Load returned nil *Program", test.descr)683 }684 if !hasError(allErrors, test.wantErr) {685 t.Errorf("%s: Load() errors = %q, want %q",686 test.descr, allErrors, test.wantErr)687 }688 }689 // TODO(adonovan):690 // - Test that in a legal test cycle, none of the symbols691 // defined by augmentation are visible via import.692}693// ---- utilities ----694// Simplifying wrapper around buildutil.FakeContext for single-file packages.695func fakeContext(pkgs map[string]string) *build.Context {696 pkgs2 := make(map[string]map[string]string)697 for path, content := range pkgs {698 pkgs2[path] = map[string]string{"x.go": content}699 }700 return buildutil.FakeContext(pkgs2)701}702func hasError(errors []error, substr string) bool {703 for _, err := range errors {704 if strings.Contains(err.Error(), substr) {705 return true706 }707 }708 return false709}710func keys(m map[string]bool) (keys []string) {711 for key := range m {712 keys = append(keys, key)713 }714 sort.Strings(keys)715 return716}717// Returns all loaded packages.718func all(prog *loader.Program) []string {...

Full Screen

Full Screen

ops_test.go

Source:ops_test.go Github

copy

Full Screen

...11 cases := []struct {12 prog []byte13 pc uint3214 want Instruction15 wantErr error16 }{{17 prog: []byte{byte(OP_ADD)},18 want: Instruction{Op: OP_ADD, Len: 1},19 }, {20 prog: []byte{byte(OP_16)},21 want: Instruction{Op: OP_16, Data: []byte{16}, Len: 1},22 }, {23 prog: []byte{byte(OP_DATA_5), 1, 1, 1, 1, 1},24 want: Instruction{Op: OP_DATA_5, Data: []byte{1, 1, 1, 1, 1}, Len: 6},25 }, {26 prog: []byte{byte(OP_DATA_5), 1, 1, 1, 1, 1, 255},27 want: Instruction{Op: OP_DATA_5, Data: []byte{1, 1, 1, 1, 1}, Len: 6},28 }, {29 prog: []byte{byte(OP_PUSHDATA1), 1, 1},30 want: Instruction{Op: OP_PUSHDATA1, Data: []byte{1}, Len: 3},31 }, {32 prog: []byte{byte(OP_PUSHDATA1), 1, 1, 255},33 want: Instruction{Op: OP_PUSHDATA1, Data: []byte{1}, Len: 3},34 }, {35 prog: []byte{byte(OP_PUSHDATA2), 1, 0, 1},36 want: Instruction{Op: OP_PUSHDATA2, Data: []byte{1}, Len: 4},37 }, {38 prog: []byte{byte(OP_PUSHDATA2), 1, 0, 1, 255},39 want: Instruction{Op: OP_PUSHDATA2, Data: []byte{1}, Len: 4},40 }, {41 prog: []byte{byte(OP_PUSHDATA4), 1, 0, 0, 0, 1},42 want: Instruction{Op: OP_PUSHDATA4, Data: []byte{1}, Len: 6},43 }, {44 prog: []byte{byte(OP_PUSHDATA4), 1, 0, 0, 0, 1, 255},45 want: Instruction{Op: OP_PUSHDATA4, Data: []byte{1}, Len: 6},46 }, {47 prog: []byte{},48 wantErr: ErrShortProgram,49 }, {50 prog: []byte{byte(OP_0)},51 pc: 1,52 wantErr: ErrShortProgram,53 }, {54 prog: []byte{byte(OP_DATA_1)},55 wantErr: ErrShortProgram,56 }, {57 prog: []byte{byte(OP_PUSHDATA1)},58 wantErr: ErrShortProgram,59 }, {60 prog: []byte{byte(OP_PUSHDATA1), 1},61 wantErr: ErrShortProgram,62 }, {63 prog: []byte{byte(OP_PUSHDATA2)},64 wantErr: ErrShortProgram,65 }, {66 prog: []byte{byte(OP_PUSHDATA2), 1, 0},67 wantErr: ErrShortProgram,68 }, {69 prog: []byte{byte(OP_PUSHDATA4)},70 wantErr: ErrShortProgram,71 }, {72 prog: []byte{byte(OP_PUSHDATA4), 1, 0, 0, 0},73 wantErr: ErrShortProgram,74 }, {75 pc: 71,76 prog: []byte{0x6d, 0x6b, 0xaa, 0x20, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x20, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x0, 0x0, 0x4e, 0xff, 0xff, 0xff, 0xff, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30},77 wantErr: checked.ErrOverflow,78 }}79 for _, c := range cases {80 t.Run(fmt.Sprintf("%d: %x", c.pc, c.prog), func(t *testing.T) {81 got, gotErr := ParseOp(c.prog, c.pc)82 if errors.Root(gotErr) != c.wantErr {83 t.Errorf("ParseOp(%x, %d) error = %v want %v", c.prog, c.pc, gotErr, c.wantErr)84 }85 if c.wantErr != nil {86 return87 }88 if !testutil.DeepEqual(got, c.want) {89 t.Errorf("ParseOp(%x, %d) = %+v want %+v", c.prog, c.pc, got, c.want)90 }91 })92 }93}94func TestParseProgram(t *testing.T) {95 cases := []struct {96 prog []byte97 want []Instruction98 wantErr error99 }{100 {101 prog: []byte{byte(OP_2), byte(OP_3), byte(OP_ADD), byte(OP_5), byte(OP_NUMEQUAL)},102 want: []Instruction{103 {Op: OP_2, Data: []byte{0x02}, Len: 1},104 {Op: OP_3, Data: []byte{0x03}, Len: 1},105 {Op: OP_ADD, Len: 1},106 {Op: OP_5, Data: []byte{0x05}, Len: 1},107 {Op: OP_NUMEQUAL, Len: 1},108 },109 },110 {111 prog: []byte{255},112 want: []Instruction{113 {Op: 255, Len: 1},114 },115 },116 }117 for _, c := range cases {118 got, gotErr := ParseProgram(c.prog)119 if errors.Root(gotErr) != c.wantErr {120 t.Errorf("ParseProgram(%x) error = %v want %v", c.prog, gotErr, c.wantErr)121 }122 if c.wantErr != nil {123 continue124 }125 if !testutil.DeepEqual(got, c.want) {126 t.Errorf("ParseProgram(%x) = %+v want %+v", c.prog, got, c.want)127 }128 }129}130func TestIsPushData(t *testing.T) {131 cases := []struct {132 want Instruction133 wantErr error134 }{135 {136 want: Instruction{Op: OP_16, Data: []byte{16}, Len: 1},137 },138 {139 want: Instruction{Op: OP_DATA_32, Data: []byte{16}, Len: 1},140 },141 {142 want: Instruction{Op: OP_FALSE, Data: []byte{}, Len: 1},143 },144 {145 want: Instruction{Op: OP_TRUE, Data: []byte{1}, Len: 1},146 },147 {148 want: Instruction{Op: OP_JUMP, Data: []byte{0x00000000}, Len: 1},149 wantErr: ErrShortProgram,150 },151 {152 want: Instruction{Op: OP_ADD, Data: []byte{0x12, 0x56}, Len: 2},153 wantErr: ErrShortProgram,154 },155 }156 for _, c := range cases {157 if c.want.IsPushdata() {158 t.Logf("check success")159 } else if c.wantErr != nil {160 t.Logf("check err success")161 } else {162 t.Errorf("check false: %v -- %v", reflect.ValueOf(ops[OP_1].fn), reflect.ValueOf(ops[c.want.Op].fn))163 }164 }165}...

Full Screen

Full Screen

Err

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := os.Open("test.txt")4 if err != nil {5 log.Fatal(err)6 }7 fmt.Println(f.Name(), "opened successfully")8}

Full Screen

Full Screen

Err

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := os.Open("2.go")4 if err != nil {5 log.Fatal(err)6 }7 fmt.Println(f.Name(), "opened successfully")8}

Full Screen

Full Screen

Err

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 err := prog.Err()4 fmt.Println(err)5}6import "errors"7func Err() error {8 return errors.New("error")9}10import (11func main() {12 err := prog.Err()13 fmt.Println(err)14}15import "errors"16func Err() error {17 return errors.New("error")18}19import (20func main() {21 err := prog.Err()22 fmt.Println(err)23}24import "errors"25func Err() error {26 return errors.New("error")27}28import (29func main() {30 err := prog.Err()31 fmt.Println(err)32}33import "errors"34func Err() error {35 return errors.New("error")36}37import (38func main() {39 err := prog.Err()40 fmt.Println(err)41}42import "errors"43func Err() error {44 return errors.New("error")45}46import (47func main() {48 err := prog.Err()49 fmt.Println(err)50}51import "errors"52func Err() error {53 return errors.New("error")54}55import (56func main() {57 err := prog.Err()58 fmt.Println(err)59}60import "errors"

Full Screen

Full Screen

Err

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 p := prog{1, "Go"}4 fmt.Println(p.Err())5}6import (7func main() {8 p := prog{1, "Go"}9 fmt.Println(p.Err())10}

Full Screen

Full Screen

Err

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := os.Open("address_book")4 if err != nil {5 log.Fatalln("Error opening file:", err)6 }7 defer f.Close()8 addressBook := &protos.AddressBook{}9 if err := proto.UnmarshalText("address_book", addressBook); err != nil {10 log.Fatalln("Failed to parse address book:", err)11 }12 addressBook.People = append(addressBook.People, &protos.Person{

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Syzkaller automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful