Best Got code snippet using got.Fatalf
timestamp_test.go
Source:timestamp_test.go
...18 enc.Write(x[i])19 }20 b, err := enc.Bytes()21 if err != nil {22 t.Fatalf("unexpected error: %v", err)23 }24 if got := b[0] >> 4; got != timeCompressedRLE {25 t.Fatalf("Wrong encoding used: expected rle, got %v", got)26 }27 var dec TimeDecoder28 dec.Init(b)29 for i, v := range x {30 if !dec.Next() {31 t.Fatalf("Next == false, expected true")32 }33 if v != dec.Read() {34 t.Fatalf("Item %d mismatch, got %v, exp %v", i, dec.Read(), v)35 }36 }37}38func Test_TimeEncoder_NoValues(t *testing.T) {39 enc := NewTimeEncoder(0)40 b, err := enc.Bytes()41 if err != nil {42 t.Fatalf("unexpected error: %v", err)43 }44 var dec TimeDecoder45 dec.Init(b)46 if dec.Next() {47 t.Fatalf("unexpected next value: got true, exp false")48 }49}50func Test_TimeEncoder_One(t *testing.T) {51 enc := NewTimeEncoder(1)52 var tm int6453 enc.Write(tm)54 b, err := enc.Bytes()55 if err != nil {56 t.Fatalf("unexpected error: %v", err)57 }58 if got := b[0] >> 4; got != timeCompressedPackedSimple {59 t.Fatalf("Wrong encoding used: expected uncompressed, got %v", got)60 }61 var dec TimeDecoder62 dec.Init(b)63 if !dec.Next() {64 t.Fatalf("unexpected next value: got true, exp false")65 }66 if tm != dec.Read() {67 t.Fatalf("read value mismatch: got %v, exp %v", dec.Read(), tm)68 }69}70func Test_TimeEncoder_Two(t *testing.T) {71 enc := NewTimeEncoder(2)72 t1 := int64(0)73 t2 := int64(1)74 enc.Write(t1)75 enc.Write(t2)76 b, err := enc.Bytes()77 if err != nil {78 t.Fatalf("unexpected error: %v", err)79 }80 if got := b[0] >> 4; got != timeCompressedRLE {81 t.Fatalf("Wrong encoding used: expected rle, got %v", got)82 }83 var dec TimeDecoder84 dec.Init(b)85 if !dec.Next() {86 t.Fatalf("unexpected next value: got true, exp false")87 }88 if t1 != dec.Read() {89 t.Fatalf("read value mismatch: got %v, exp %v", dec.Read(), t1)90 }91 if !dec.Next() {92 t.Fatalf("unexpected next value: got true, exp false")93 }94 if t2 != dec.Read() {95 t.Fatalf("read value mismatch: got %v, exp %v", dec.Read(), t2)96 }97}98func Test_TimeEncoder_Three(t *testing.T) {99 enc := NewTimeEncoder(3)100 t1 := int64(0)101 t2 := int64(1)102 t3 := int64(3)103 enc.Write(t1)104 enc.Write(t2)105 enc.Write(t3)106 b, err := enc.Bytes()107 if err != nil {108 t.Fatalf("unexpected error: %v", err)109 }110 if got := b[0] >> 4; got != timeCompressedPackedSimple {111 t.Fatalf("Wrong encoding used: expected rle, got %v", got)112 }113 var dec TimeDecoder114 dec.Init(b)115 if !dec.Next() {116 t.Fatalf("unexpected next value: got true, exp false")117 }118 if t1 != dec.Read() {119 t.Fatalf("read value mismatch: got %v, exp %v", dec.Read(), t1)120 }121 if !dec.Next() {122 t.Fatalf("unexpected next value: got true, exp false")123 }124 if t2 != dec.Read() {125 t.Fatalf("read value mismatch: got %v, exp %v", dec.Read(), t2)126 }127 if !dec.Next() {128 t.Fatalf("unexpected next value: got true, exp false")129 }130 if t3 != dec.Read() {131 t.Fatalf("read value mismatch: got %v, exp %v", dec.Read(), t3)132 }133}134func Test_TimeEncoder_Large_Range(t *testing.T) {135 enc := NewTimeEncoder(2)136 t1 := int64(1442369134000000000)137 t2 := int64(1442369135000000000)138 enc.Write(t1)139 enc.Write(t2)140 b, err := enc.Bytes()141 if err != nil {142 t.Fatalf("unexpected error: %v", err)143 }144 if got := b[0] >> 4; got != timeCompressedRLE {145 t.Fatalf("Wrong encoding used: expected rle, got %v", got)146 }147 var dec TimeDecoder148 dec.Init(b)149 if !dec.Next() {150 t.Fatalf("unexpected next value: got true, exp false")151 }152 if t1 != dec.Read() {153 t.Fatalf("read value mismatch: got %v, exp %v", dec.Read(), t1)154 }155 if !dec.Next() {156 t.Fatalf("unexpected next value: got true, exp false")157 }158 if t2 != dec.Read() {159 t.Fatalf("read value mismatch: got %v, exp %v", dec.Read(), t2)160 }161}162func Test_TimeEncoder_Uncompressed(t *testing.T) {163 enc := NewTimeEncoder(3)164 t1 := time.Unix(0, 0).UnixNano()165 t2 := time.Unix(1, 0).UnixNano()166 // about 36.5yrs in NS resolution is max range for compressed format167 // This should cause the encoding to fallback to raw points168 t3 := time.Unix(2, (2 << 59)).UnixNano()169 enc.Write(t1)170 enc.Write(t2)171 enc.Write(t3)172 b, err := enc.Bytes()173 if err != nil {174 t.Fatalf("expected error: %v", err)175 }176 if exp := 25; len(b) != exp {177 t.Fatalf("length mismatch: got %v, exp %v", len(b), exp)178 }179 if got := b[0] >> 4; got != timeUncompressed {180 t.Fatalf("Wrong encoding used: expected uncompressed, got %v", got)181 }182 var dec TimeDecoder183 dec.Init(b)184 if !dec.Next() {185 t.Fatalf("unexpected next value: got true, exp false")186 }187 if t1 != dec.Read() {188 t.Fatalf("read value mismatch: got %v, exp %v", dec.Read(), t1)189 }190 if !dec.Next() {191 t.Fatalf("unexpected next value: got true, exp false")192 }193 if t2 != dec.Read() {194 t.Fatalf("read value mismatch: got %v, exp %v", dec.Read(), t2)195 }196 if !dec.Next() {197 t.Fatalf("unexpected next value: got true, exp false")198 }199 if t3 != dec.Read() {200 t.Fatalf("read value mismatch: got %v, exp %v", dec.Read(), t3)201 }202}203func Test_TimeEncoder_RLE(t *testing.T) {204 enc := NewTimeEncoder(512)205 var ts []int64206 for i := 0; i < 500; i++ {207 ts = append(ts, int64(i))208 }209 for _, v := range ts {210 enc.Write(v)211 }212 b, err := enc.Bytes()213 if exp := 12; len(b) != exp {214 t.Fatalf("length mismatch: got %v, exp %v", len(b), exp)215 }216 if got := b[0] >> 4; got != timeCompressedRLE {217 t.Fatalf("Wrong encoding used: expected uncompressed, got %v", got)218 }219 if err != nil {220 t.Fatalf("unexpected error: %v", err)221 }222 var dec TimeDecoder223 dec.Init(b)224 for i, v := range ts {225 if !dec.Next() {226 t.Fatalf("Next == false, expected true")227 }228 if v != dec.Read() {229 t.Fatalf("Item %d mismatch, got %v, exp %v", i, dec.Read(), v)230 }231 }232 if dec.Next() {233 t.Fatalf("unexpected extra values")234 }235}236func Test_TimeEncoder_Reverse(t *testing.T) {237 enc := NewTimeEncoder(3)238 ts := []int64{239 int64(3),240 int64(2),241 int64(0),242 }243 for _, v := range ts {244 enc.Write(v)245 }246 b, err := enc.Bytes()247 if err != nil {248 t.Fatalf("unexpected error: %v", err)249 }250 if got := b[0] >> 4; got != timeUncompressed {251 t.Fatalf("Wrong encoding used: expected uncompressed, got %v", got)252 }253 var dec TimeDecoder254 dec.Init(b)255 i := 0256 for dec.Next() {257 if ts[i] != dec.Read() {258 t.Fatalf("read value %d mismatch: got %v, exp %v", i, dec.Read(), ts[i])259 }260 i++261 }262}263func Test_TimeEncoder_220SecondDelta(t *testing.T) {264 enc := NewTimeEncoder(256)265 var ts []int64266 now := time.Now()267 for i := 0; i < 220; i++ {268 ts = append(ts, now.Add(time.Duration(i*60)*time.Second).UnixNano())269 }270 for _, v := range ts {271 enc.Write(v)272 }273 b, err := enc.Bytes()274 if err != nil {275 t.Fatalf("unexpected error: %v", err)276 }277 // Using RLE, should get 12 bytes278 if exp := 12; len(b) != exp {279 t.Fatalf("unexpected length: got %v, exp %v", len(b), exp)280 }281 if got := b[0] >> 4; got != timeCompressedRLE {282 t.Fatalf("Wrong encoding used: expected uncompressed, got %v", got)283 }284 var dec TimeDecoder285 dec.Init(b)286 i := 0287 for dec.Next() {288 if ts[i] != dec.Read() {289 t.Fatalf("read value %d mismatch: got %v, exp %v", i, dec.Read(), ts[i])290 }291 i++292 }293 if i != len(ts) {294 t.Fatalf("Read too few values: exp %d, got %d", len(ts), i)295 }296 if dec.Next() {297 t.Fatalf("expecte Next() = false, got true")298 }299}300func Test_TimeEncoder_Quick(t *testing.T) {301 quick.Check(func(values []int64) bool {302 // Write values to encoder.303 enc := NewTimeEncoder(1024)304 exp := make([]int64, len(values))305 for i, v := range values {306 exp[i] = int64(v)307 enc.Write(exp[i])308 }309 // Retrieve encoded bytes from encoder.310 buf, err := enc.Bytes()311 if err != nil {312 t.Fatal(err)313 }314 // Read values out of decoder.315 got := make([]int64, 0, len(values))316 var dec TimeDecoder317 dec.Init(buf)318 for dec.Next() {319 if err := dec.Error(); err != nil {320 t.Fatal(err)321 }322 got = append(got, dec.Read())323 }324 // Verify that input and output values match.325 if !reflect.DeepEqual(exp, got) {326 t.Fatalf("mismatch:\n\nexp=%+v\n\ngot=%+v\n\n", exp, got)327 }328 return true329 }, nil)330}331func Test_TimeEncoder_RLESeconds(t *testing.T) {332 enc := NewTimeEncoder(6)333 ts := make([]int64, 6)334 ts[0] = int64(1444448158000000000)335 ts[1] = int64(1444448168000000000)336 ts[2] = int64(1444448178000000000)337 ts[3] = int64(1444448188000000000)338 ts[4] = int64(1444448198000000000)339 ts[5] = int64(1444448208000000000)340 for _, v := range ts {341 enc.Write(v)342 }343 b, err := enc.Bytes()344 if got := b[0] >> 4; got != timeCompressedRLE {345 t.Fatalf("Wrong encoding used: expected rle, got %v", got)346 }347 if err != nil {348 t.Fatalf("unexpected error: %v", err)349 }350 var dec TimeDecoder351 dec.Init(b)352 for i, v := range ts {353 if !dec.Next() {354 t.Fatalf("Next == false, expected true")355 }356 if v != dec.Read() {357 t.Fatalf("Item %d mismatch, got %v, exp %v", i, dec.Read(), v)358 }359 }360 if dec.Next() {361 t.Fatalf("unexpected extra values")362 }363}364func TestTimeEncoder_Count_Uncompressed(t *testing.T) {365 enc := NewTimeEncoder(2)366 t1 := time.Unix(0, 0).UnixNano()367 t2 := time.Unix(1, 0).UnixNano()368 // about 36.5yrs in NS resolution is max range for compressed format369 // This should cause the encoding to fallback to raw points370 t3 := time.Unix(2, (2 << 59)).UnixNano()371 enc.Write(t1)372 enc.Write(t2)373 enc.Write(t3)374 b, err := enc.Bytes()375 if got := b[0] >> 4; got != timeUncompressed {376 t.Fatalf("Wrong encoding used: expected rle, got %v", got)377 }378 if err != nil {379 t.Fatalf("unexpected error: %v", err)380 }381 if got, exp := CountTimestamps(b), 3; got != exp {382 t.Fatalf("count mismatch: got %v, exp %v", got, exp)383 }384}385func TestTimeEncoder_Count_RLE(t *testing.T) {386 enc := NewTimeEncoder(5)387 ts := make([]int64, 6)388 ts[0] = int64(1444448158000000000)389 ts[1] = int64(1444448168000000000)390 ts[2] = int64(1444448178000000000)391 ts[3] = int64(1444448188000000000)392 ts[4] = int64(1444448198000000000)393 ts[5] = int64(1444448208000000000)394 for _, v := range ts {395 enc.Write(v)396 }397 b, err := enc.Bytes()398 if got := b[0] >> 4; got != timeCompressedRLE {399 t.Fatalf("Wrong encoding used: expected rle, got %v", got)400 }401 if err != nil {402 t.Fatalf("unexpected error: %v", err)403 }404 if got, exp := CountTimestamps(b), len(ts); got != exp {405 t.Fatalf("count mismatch: got %v, exp %v", got, exp)406 }407}408func TestTimeEncoder_Count_Simple8(t *testing.T) {409 enc := NewTimeEncoder(3)410 t1 := int64(0)411 t2 := int64(1)412 t3 := int64(3)413 enc.Write(t1)414 enc.Write(t2)415 enc.Write(t3)416 b, err := enc.Bytes()417 if err != nil {418 t.Fatalf("unexpected error: %v", err)419 }420 if got := b[0] >> 4; got != timeCompressedPackedSimple {421 t.Fatalf("Wrong encoding used: expected rle, got %v", got)422 }423 if err != nil {424 t.Fatalf("unexpected error: %v", err)425 }426 if got, exp := CountTimestamps(b), 3; got != exp {427 t.Fatalf("count mismatch: got %v, exp %v", got, exp)428 }429}430func TestTimeDecoder_Corrupt(t *testing.T) {431 cases := []string{432 "", // Empty433 "\x10\x14", // Packed: not enough data434 "\x20\x00", // RLE: not enough data for starting timestamp435 "\x2012345678\x90", // RLE: initial timestamp but invalid uvarint encoding436 "\x2012345678\x7f", // RLE: timestamp, RLE but invalid repeat437 "\x00123", // Raw: data length not multiple of 8438 }439 for _, c := range cases {440 var dec TimeDecoder441 dec.Init([]byte(c))442 if dec.Next() {443 t.Fatalf("exp next == false, got true")444 }445 }446}447func BenchmarkTimeEncoder(b *testing.B) {448 enc := NewTimeEncoder(1024)449 x := make([]int64, 1024)450 for i := 0; i < len(x); i++ {451 x[i] = time.Now().UnixNano()452 enc.Write(x[i])453 }454 b.ResetTimer()455 for i := 0; i < b.N; i++ {456 enc.Bytes()457 enc.Reset()...
vm_aluop_test.go
Source:vm_aluop_test.go
...18 },19 bpf.RetA{},20 })21 if err != nil {22 t.Fatalf("failed to load BPF program: %v", err)23 }24 defer done()25 out, err := vm.Run([]byte{26 0xff, 0xff, 0xff, 0xff,27 0xff, 0xff, 0xff, 0xff,28 8, 2, 3,29 })30 if err != nil {31 t.Fatalf("unexpected error while running program: %v", err)32 }33 if want, got := 3, out; want != got {34 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",35 want, got)36 }37}38func TestVMALUOpSub(t *testing.T) {39 vm, done, err := testVM(t, []bpf.Instruction{40 bpf.LoadAbsolute{41 Off: 8,42 Size: 1,43 },44 bpf.TAX{},45 bpf.ALUOpX{46 Op: bpf.ALUOpSub,47 },48 bpf.RetA{},49 })50 if err != nil {51 t.Fatalf("failed to load BPF program: %v", err)52 }53 defer done()54 out, err := vm.Run([]byte{55 0xff, 0xff, 0xff, 0xff,56 0xff, 0xff, 0xff, 0xff,57 1, 2, 3,58 })59 if err != nil {60 t.Fatalf("unexpected error while running program: %v", err)61 }62 if want, got := 0, out; want != got {63 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",64 want, got)65 }66}67func TestVMALUOpMul(t *testing.T) {68 vm, done, err := testVM(t, []bpf.Instruction{69 bpf.LoadAbsolute{70 Off: 8,71 Size: 1,72 },73 bpf.ALUOpConstant{74 Op: bpf.ALUOpMul,75 Val: 2,76 },77 bpf.RetA{},78 })79 if err != nil {80 t.Fatalf("failed to load BPF program: %v", err)81 }82 defer done()83 out, err := vm.Run([]byte{84 0xff, 0xff, 0xff, 0xff,85 0xff, 0xff, 0xff, 0xff,86 6, 2, 3, 4,87 })88 if err != nil {89 t.Fatalf("unexpected error while running program: %v", err)90 }91 if want, got := 4, out; want != got {92 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",93 want, got)94 }95}96func TestVMALUOpDiv(t *testing.T) {97 vm, done, err := testVM(t, []bpf.Instruction{98 bpf.LoadAbsolute{99 Off: 8,100 Size: 1,101 },102 bpf.ALUOpConstant{103 Op: bpf.ALUOpDiv,104 Val: 2,105 },106 bpf.RetA{},107 })108 if err != nil {109 t.Fatalf("failed to load BPF program: %v", err)110 }111 defer done()112 out, err := vm.Run([]byte{113 0xff, 0xff, 0xff, 0xff,114 0xff, 0xff, 0xff, 0xff,115 20, 2, 3, 4,116 })117 if err != nil {118 t.Fatalf("unexpected error while running program: %v", err)119 }120 if want, got := 2, out; want != got {121 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",122 want, got)123 }124}125func TestVMALUOpDivByZeroALUOpConstant(t *testing.T) {126 _, _, err := testVM(t, []bpf.Instruction{127 bpf.ALUOpConstant{128 Op: bpf.ALUOpDiv,129 Val: 0,130 },131 bpf.RetA{},132 })133 if errStr(err) != "cannot divide by zero using ALUOpConstant" {134 t.Fatalf("unexpected error: %v", err)135 }136}137func TestVMALUOpDivByZeroALUOpX(t *testing.T) {138 vm, done, err := testVM(t, []bpf.Instruction{139 // Load byte 0 into X140 bpf.LoadAbsolute{141 Off: 8,142 Size: 1,143 },144 bpf.TAX{},145 // Load byte 1 into A146 bpf.LoadAbsolute{147 Off: 9,148 Size: 1,149 },150 // Attempt to perform 1/0151 bpf.ALUOpX{152 Op: bpf.ALUOpDiv,153 },154 // Return 4 bytes if program does not terminate155 bpf.LoadConstant{156 Val: 12,157 },158 bpf.RetA{},159 })160 if err != nil {161 t.Fatalf("failed to load BPF program: %v", err)162 }163 defer done()164 out, err := vm.Run([]byte{165 0xff, 0xff, 0xff, 0xff,166 0xff, 0xff, 0xff, 0xff,167 0, 1, 3, 4,168 })169 if err != nil {170 t.Fatalf("unexpected error while running program: %v", err)171 }172 if want, got := 0, out; want != got {173 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",174 want, got)175 }176}177func TestVMALUOpOr(t *testing.T) {178 vm, done, err := testVM(t, []bpf.Instruction{179 bpf.LoadAbsolute{180 Off: 8,181 Size: 2,182 },183 bpf.ALUOpConstant{184 Op: bpf.ALUOpOr,185 Val: 0x01,186 },187 bpf.RetA{},188 })189 if err != nil {190 t.Fatalf("failed to load BPF program: %v", err)191 }192 defer done()193 out, err := vm.Run([]byte{194 0xff, 0xff, 0xff, 0xff,195 0xff, 0xff, 0xff, 0xff,196 0x00, 0x10, 0x03, 0x04,197 0x05, 0x06, 0x07, 0x08,198 0x09, 0xff,199 })200 if err != nil {201 t.Fatalf("unexpected error while running program: %v", err)202 }203 if want, got := 9, out; want != got {204 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",205 want, got)206 }207}208func TestVMALUOpAnd(t *testing.T) {209 vm, done, err := testVM(t, []bpf.Instruction{210 bpf.LoadAbsolute{211 Off: 8,212 Size: 2,213 },214 bpf.ALUOpConstant{215 Op: bpf.ALUOpAnd,216 Val: 0x0019,217 },218 bpf.RetA{},219 })220 if err != nil {221 t.Fatalf("failed to load BPF program: %v", err)222 }223 defer done()224 out, err := vm.Run([]byte{225 0xff, 0xff, 0xff, 0xff,226 0xff, 0xff, 0xff, 0xff,227 0xaa, 0x09,228 })229 if err != nil {230 t.Fatalf("unexpected error while running program: %v", err)231 }232 if want, got := 1, out; want != got {233 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",234 want, got)235 }236}237func TestVMALUOpShiftLeft(t *testing.T) {238 vm, done, err := testVM(t, []bpf.Instruction{239 bpf.LoadAbsolute{240 Off: 8,241 Size: 1,242 },243 bpf.ALUOpConstant{244 Op: bpf.ALUOpShiftLeft,245 Val: 0x01,246 },247 bpf.JumpIf{248 Cond: bpf.JumpEqual,249 Val: 0x02,250 SkipTrue: 1,251 },252 bpf.RetConstant{253 Val: 0,254 },255 bpf.RetConstant{256 Val: 9,257 },258 })259 if err != nil {260 t.Fatalf("failed to load BPF program: %v", err)261 }262 defer done()263 out, err := vm.Run([]byte{264 0xff, 0xff, 0xff, 0xff,265 0xff, 0xff, 0xff, 0xff,266 0x01, 0xaa,267 })268 if err != nil {269 t.Fatalf("unexpected error while running program: %v", err)270 }271 if want, got := 1, out; want != got {272 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",273 want, got)274 }275}276func TestVMALUOpShiftRight(t *testing.T) {277 vm, done, err := testVM(t, []bpf.Instruction{278 bpf.LoadAbsolute{279 Off: 8,280 Size: 1,281 },282 bpf.ALUOpConstant{283 Op: bpf.ALUOpShiftRight,284 Val: 0x01,285 },286 bpf.JumpIf{287 Cond: bpf.JumpEqual,288 Val: 0x04,289 SkipTrue: 1,290 },291 bpf.RetConstant{292 Val: 0,293 },294 bpf.RetConstant{295 Val: 9,296 },297 })298 if err != nil {299 t.Fatalf("failed to load BPF program: %v", err)300 }301 defer done()302 out, err := vm.Run([]byte{303 0xff, 0xff, 0xff, 0xff,304 0xff, 0xff, 0xff, 0xff,305 0x08, 0xff, 0xff,306 })307 if err != nil {308 t.Fatalf("unexpected error while running program: %v", err)309 }310 if want, got := 1, out; want != got {311 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",312 want, got)313 }314}315func TestVMALUOpMod(t *testing.T) {316 vm, done, err := testVM(t, []bpf.Instruction{317 bpf.LoadAbsolute{318 Off: 8,319 Size: 1,320 },321 bpf.ALUOpConstant{322 Op: bpf.ALUOpMod,323 Val: 20,324 },325 bpf.RetA{},326 })327 if err != nil {328 t.Fatalf("failed to load BPF program: %v", err)329 }330 defer done()331 out, err := vm.Run([]byte{332 0xff, 0xff, 0xff, 0xff,333 0xff, 0xff, 0xff, 0xff,334 30, 0, 0,335 })336 if err != nil {337 t.Fatalf("unexpected error while running program: %v", err)338 }339 if want, got := 2, out; want != got {340 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",341 want, got)342 }343}344func TestVMALUOpModByZeroALUOpConstant(t *testing.T) {345 _, _, err := testVM(t, []bpf.Instruction{346 bpf.LoadAbsolute{347 Off: 8,348 Size: 1,349 },350 bpf.ALUOpConstant{351 Op: bpf.ALUOpMod,352 Val: 0,353 },354 bpf.RetA{},355 })356 if errStr(err) != "cannot divide by zero using ALUOpConstant" {357 t.Fatalf("unexpected error: %v", err)358 }359}360func TestVMALUOpModByZeroALUOpX(t *testing.T) {361 vm, done, err := testVM(t, []bpf.Instruction{362 // Load byte 0 into X363 bpf.LoadAbsolute{364 Off: 8,365 Size: 1,366 },367 bpf.TAX{},368 // Load byte 1 into A369 bpf.LoadAbsolute{370 Off: 9,371 Size: 1,372 },373 // Attempt to perform 1%0374 bpf.ALUOpX{375 Op: bpf.ALUOpMod,376 },377 // Return 4 bytes if program does not terminate378 bpf.LoadConstant{379 Val: 12,380 },381 bpf.RetA{},382 })383 if err != nil {384 t.Fatalf("failed to load BPF program: %v", err)385 }386 defer done()387 out, err := vm.Run([]byte{388 0xff, 0xff, 0xff, 0xff,389 0xff, 0xff, 0xff, 0xff,390 0, 1, 3, 4,391 })392 if err != nil {393 t.Fatalf("unexpected error while running program: %v", err)394 }395 if want, got := 0, out; want != got {396 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",397 want, got)398 }399}400func TestVMALUOpXor(t *testing.T) {401 vm, done, err := testVM(t, []bpf.Instruction{402 bpf.LoadAbsolute{403 Off: 8,404 Size: 1,405 },406 bpf.ALUOpConstant{407 Op: bpf.ALUOpXor,408 Val: 0x0a,409 },410 bpf.JumpIf{411 Cond: bpf.JumpEqual,412 Val: 0x01,413 SkipTrue: 1,414 },415 bpf.RetConstant{416 Val: 0,417 },418 bpf.RetConstant{419 Val: 9,420 },421 })422 if err != nil {423 t.Fatalf("failed to load BPF program: %v", err)424 }425 defer done()426 out, err := vm.Run([]byte{427 0xff, 0xff, 0xff, 0xff,428 0xff, 0xff, 0xff, 0xff,429 0x0b, 0x00, 0x00, 0x00,430 })431 if err != nil {432 t.Fatalf("unexpected error while running program: %v", err)433 }434 if want, got := 1, out; want != got {435 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",436 want, got)437 }438}439func TestVMALUOpUnknown(t *testing.T) {440 vm, done, err := testVM(t, []bpf.Instruction{441 bpf.LoadAbsolute{442 Off: 8,443 Size: 1,444 },445 bpf.ALUOpConstant{446 Op: bpf.ALUOpAdd,447 Val: 1,448 },449 // Verify that an unknown operation is a no-op450 bpf.ALUOpConstant{451 Op: 100,452 },453 bpf.JumpIf{454 Cond: bpf.JumpEqual,455 Val: 0x02,456 SkipTrue: 1,457 },458 bpf.RetConstant{459 Val: 0,460 },461 bpf.RetConstant{462 Val: 9,463 },464 })465 if err != nil {466 t.Fatalf("failed to load BPF program: %v", err)467 }468 defer done()469 out, err := vm.Run([]byte{470 0xff, 0xff, 0xff, 0xff,471 0xff, 0xff, 0xff, 0xff,472 1,473 })474 if err != nil {475 t.Fatalf("unexpected error while running program: %v", err)476 }477 if want, got := 1, out; want != got {478 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",479 want, got)480 }481}...
vm_jump_test.go
Source:vm_jump_test.go
...22 Val: 9,23 },24 })25 if err != nil {26 t.Fatalf("failed to load BPF program: %v", err)27 }28 defer done()29 out, err := vm.Run([]byte{30 0xff, 0xff, 0xff, 0xff,31 0xff, 0xff, 0xff, 0xff,32 1,33 })34 if err != nil {35 t.Fatalf("unexpected error while running program: %v", err)36 }37 if want, got := 1, out; want != got {38 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",39 want, got)40 }41}42func TestVMJumpOutOfProgram(t *testing.T) {43 _, _, err := testVM(t, []bpf.Instruction{44 bpf.Jump{45 Skip: 1,46 },47 bpf.RetA{},48 })49 if errStr(err) != "cannot jump 1 instructions; jumping past program bounds" {50 t.Fatalf("unexpected error: %v", err)51 }52}53func TestVMJumpIfTrueOutOfProgram(t *testing.T) {54 _, _, err := testVM(t, []bpf.Instruction{55 bpf.JumpIf{56 Cond: bpf.JumpEqual,57 SkipTrue: 2,58 },59 bpf.RetA{},60 })61 if errStr(err) != "cannot jump 2 instructions in true case; jumping past program bounds" {62 t.Fatalf("unexpected error: %v", err)63 }64}65func TestVMJumpIfFalseOutOfProgram(t *testing.T) {66 _, _, err := testVM(t, []bpf.Instruction{67 bpf.JumpIf{68 Cond: bpf.JumpEqual,69 SkipFalse: 3,70 },71 bpf.RetA{},72 })73 if errStr(err) != "cannot jump 3 instructions in false case; jumping past program bounds" {74 t.Fatalf("unexpected error: %v", err)75 }76}77func TestVMJumpIfEqual(t *testing.T) {78 vm, done, err := testVM(t, []bpf.Instruction{79 bpf.LoadAbsolute{80 Off: 8,81 Size: 1,82 },83 bpf.JumpIf{84 Cond: bpf.JumpEqual,85 Val: 1,86 SkipTrue: 1,87 },88 bpf.RetConstant{89 Val: 0,90 },91 bpf.RetConstant{92 Val: 9,93 },94 })95 if err != nil {96 t.Fatalf("failed to load BPF program: %v", err)97 }98 defer done()99 out, err := vm.Run([]byte{100 0xff, 0xff, 0xff, 0xff,101 0xff, 0xff, 0xff, 0xff,102 1,103 })104 if err != nil {105 t.Fatalf("unexpected error while running program: %v", err)106 }107 if want, got := 1, out; want != got {108 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",109 want, got)110 }111}112func TestVMJumpIfNotEqual(t *testing.T) {113 vm, done, err := testVM(t, []bpf.Instruction{114 bpf.LoadAbsolute{115 Off: 8,116 Size: 1,117 },118 bpf.JumpIf{119 Cond: bpf.JumpNotEqual,120 Val: 1,121 SkipFalse: 1,122 },123 bpf.RetConstant{124 Val: 0,125 },126 bpf.RetConstant{127 Val: 9,128 },129 })130 if err != nil {131 t.Fatalf("failed to load BPF program: %v", err)132 }133 defer done()134 out, err := vm.Run([]byte{135 0xff, 0xff, 0xff, 0xff,136 0xff, 0xff, 0xff, 0xff,137 1,138 })139 if err != nil {140 t.Fatalf("unexpected error while running program: %v", err)141 }142 if want, got := 1, out; want != got {143 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",144 want, got)145 }146}147func TestVMJumpIfGreaterThan(t *testing.T) {148 vm, done, err := testVM(t, []bpf.Instruction{149 bpf.LoadAbsolute{150 Off: 8,151 Size: 4,152 },153 bpf.JumpIf{154 Cond: bpf.JumpGreaterThan,155 Val: 0x00010202,156 SkipTrue: 1,157 },158 bpf.RetConstant{159 Val: 0,160 },161 bpf.RetConstant{162 Val: 12,163 },164 })165 if err != nil {166 t.Fatalf("failed to load BPF program: %v", err)167 }168 defer done()169 out, err := vm.Run([]byte{170 0xff, 0xff, 0xff, 0xff,171 0xff, 0xff, 0xff, 0xff,172 0, 1, 2, 3,173 })174 if err != nil {175 t.Fatalf("unexpected error while running program: %v", err)176 }177 if want, got := 4, out; want != got {178 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",179 want, got)180 }181}182func TestVMJumpIfLessThan(t *testing.T) {183 vm, done, err := testVM(t, []bpf.Instruction{184 bpf.LoadAbsolute{185 Off: 8,186 Size: 4,187 },188 bpf.JumpIf{189 Cond: bpf.JumpLessThan,190 Val: 0xff010203,191 SkipTrue: 1,192 },193 bpf.RetConstant{194 Val: 0,195 },196 bpf.RetConstant{197 Val: 12,198 },199 })200 if err != nil {201 t.Fatalf("failed to load BPF program: %v", err)202 }203 defer done()204 out, err := vm.Run([]byte{205 0xff, 0xff, 0xff, 0xff,206 0xff, 0xff, 0xff, 0xff,207 0, 1, 2, 3,208 })209 if err != nil {210 t.Fatalf("unexpected error while running program: %v", err)211 }212 if want, got := 4, out; want != got {213 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",214 want, got)215 }216}217func TestVMJumpIfGreaterOrEqual(t *testing.T) {218 vm, done, err := testVM(t, []bpf.Instruction{219 bpf.LoadAbsolute{220 Off: 8,221 Size: 4,222 },223 bpf.JumpIf{224 Cond: bpf.JumpGreaterOrEqual,225 Val: 0x00010203,226 SkipTrue: 1,227 },228 bpf.RetConstant{229 Val: 0,230 },231 bpf.RetConstant{232 Val: 12,233 },234 })235 if err != nil {236 t.Fatalf("failed to load BPF program: %v", err)237 }238 defer done()239 out, err := vm.Run([]byte{240 0xff, 0xff, 0xff, 0xff,241 0xff, 0xff, 0xff, 0xff,242 0, 1, 2, 3,243 })244 if err != nil {245 t.Fatalf("unexpected error while running program: %v", err)246 }247 if want, got := 4, out; want != got {248 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",249 want, got)250 }251}252func TestVMJumpIfLessOrEqual(t *testing.T) {253 vm, done, err := testVM(t, []bpf.Instruction{254 bpf.LoadAbsolute{255 Off: 8,256 Size: 4,257 },258 bpf.JumpIf{259 Cond: bpf.JumpLessOrEqual,260 Val: 0xff010203,261 SkipTrue: 1,262 },263 bpf.RetConstant{264 Val: 0,265 },266 bpf.RetConstant{267 Val: 12,268 },269 })270 if err != nil {271 t.Fatalf("failed to load BPF program: %v", err)272 }273 defer done()274 out, err := vm.Run([]byte{275 0xff, 0xff, 0xff, 0xff,276 0xff, 0xff, 0xff, 0xff,277 0, 1, 2, 3,278 })279 if err != nil {280 t.Fatalf("unexpected error while running program: %v", err)281 }282 if want, got := 4, out; want != got {283 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",284 want, got)285 }286}287func TestVMJumpIfBitsSet(t *testing.T) {288 vm, done, err := testVM(t, []bpf.Instruction{289 bpf.LoadAbsolute{290 Off: 8,291 Size: 2,292 },293 bpf.JumpIf{294 Cond: bpf.JumpBitsSet,295 Val: 0x1122,296 SkipTrue: 1,297 },298 bpf.RetConstant{299 Val: 0,300 },301 bpf.RetConstant{302 Val: 10,303 },304 })305 if err != nil {306 t.Fatalf("failed to load BPF program: %v", err)307 }308 defer done()309 out, err := vm.Run([]byte{310 0xff, 0xff, 0xff, 0xff,311 0xff, 0xff, 0xff, 0xff,312 0x01, 0x02,313 })314 if err != nil {315 t.Fatalf("unexpected error while running program: %v", err)316 }317 if want, got := 2, out; want != got {318 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",319 want, got)320 }321}322func TestVMJumpIfBitsNotSet(t *testing.T) {323 vm, done, err := testVM(t, []bpf.Instruction{324 bpf.LoadAbsolute{325 Off: 8,326 Size: 2,327 },328 bpf.JumpIf{329 Cond: bpf.JumpBitsNotSet,330 Val: 0x1221,331 SkipTrue: 1,332 },333 bpf.RetConstant{334 Val: 0,335 },336 bpf.RetConstant{337 Val: 10,338 },339 })340 if err != nil {341 t.Fatalf("failed to load BPF program: %v", err)342 }343 defer done()344 out, err := vm.Run([]byte{345 0xff, 0xff, 0xff, 0xff,346 0xff, 0xff, 0xff, 0xff,347 0x01, 0x02,348 })349 if err != nil {350 t.Fatalf("unexpected error while running program: %v", err)351 }352 if want, got := 2, out; want != got {353 t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",354 want, got)355 }356}...
Fatalf
Using AI Code Generation
1import "fmt"2func main() {3}4Related Posts: GoLang | Fatalf() method in GoLang5GoLang | Fatalln() method in GoLang6GoLang | Panicln() method in GoLang7GoLang | Panicf() method in GoLang8GoLang | Panic() method in GoLang9GoLang | Println() method in GoLang10GoLang | Printf() method in GoLang11GoLang | Print() method in GoLang12GoLang | Sprintf() method in GoLang13GoLang | Sprintln() method in GoLang14GoLang | Sscanln() method in GoLang15GoLang | Sscan() method in GoLang16GoLang | Sscanf() method in GoLang17GoLang | Scanln() method in GoLang18GoLang | Scan() method in GoLang19GoLang | Scanf() method in GoLang20GoLang | Fprintln() method in GoLang21GoLang | Fprintf() method in GoLang22GoLang | Fprint() method in GoLang23GoLang | Fscanln() method in GoLang24GoLang | Fscan() method in GoLang25GoLang | Fscanf() method in GoLang26GoLang | Errorf() method in GoLang27GoLang | Errorln() met
Fatalf
Using AI Code Generation
1import (2func main() {3 fmt.Println("Enter your name")4 fmt.Scanln(&name)5 if name == " " {6 fmt.Println("Please enter your name")7 os.Exit(1)8 }9 fmt.Println("Hello", name)10}11import (12func main() {13 fmt.Println("Enter your name")14 fmt.Scanln(&name)15 if name == " " {16 log.Fatalf("Please enter your name")17 }18 fmt.Println("Hello", name)19}20import (21func main() {22 fmt.Println("Enter your name")23 fmt.Scanln(&
Fatalf
Using AI Code Generation
1import (2func main() {3 log.Fatalf("This is a fatal error")4 fmt.Println("This will not be printed")5}6import (7func main() {8 log.Fatalln("This is a fatal error")9 fmt.Println("This will not be printed")10}11import (12func main() {13 log.Println("This is a log error")14 fmt.Println("This will be printed")15}16import (17func main() {18 log.Print("This is a log error")19 fmt.Println("This will be printed")20}21import (22func main() {23 log.Printf("This is a log error")24 fmt.Println("This will be printed")25}26import (27func main() {28 log.SetFlags(0)29 log.Println("This is a log error")30 fmt.Println("This will be printed")31}32import (
Fatalf
Using AI Code Generation
1import (2func main() {3 fmt.Fprintln(os.Stderr, "An error occurred")4 os.Exit(1)5}6import (7func main() {8 log.SetOutput(os.Stderr)9 log.Fatalln("An error occurred")10}11import (12func main() {13 log.SetOutput(os.Stderr)14 log.Panic("An error occurred")15}16log.Panic(0xc0000a7f78, 0x1, 0x1)17main.main()
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!!