| 413 | } |
| 414 | |
| 415 | func (s) TestBuffer_SliceBoundsCheck(t *testing.T) { |
| 416 | type panicCase struct { |
| 417 | name string |
| 418 | start, end int |
| 419 | } |
| 420 | nonEmptyCases := []panicCase{ |
| 421 | {"end_out_of_bounds", 0, 5}, |
| 422 | {"start_negative", -1, 3}, |
| 423 | {"start_greater_than_end", 3, 0}, |
| 424 | } |
| 425 | tests := []struct { |
| 426 | namedCtor |
| 427 | panicCases []panicCase |
| 428 | }{ |
| 429 | { |
| 430 | namedCtor: namedCtor{ |
| 431 | name: "buffer", |
| 432 | newBuf: newPooledBuffer, |
| 433 | }, |
| 434 | panicCases: nonEmptyCases, |
| 435 | }, |
| 436 | { |
| 437 | namedCtor: namedCtor{ |
| 438 | name: "SliceBuffer", |
| 439 | newBuf: newSliceBuf, |
| 440 | }, |
| 441 | panicCases: nonEmptyCases, |
| 442 | }, |
| 443 | { |
| 444 | namedCtor: namedCtor{ |
| 445 | name: "emptyBuffer", |
| 446 | newBuf: newEmptyBuf, |
| 447 | }, |
| 448 | panicCases: []panicCase{ |
| 449 | {"end_out_of_bounds", 0, 1}, |
| 450 | {"start_negative", -1, 0}, |
| 451 | }, |
| 452 | }, |
| 453 | } |
| 454 | for _, tt := range tests { |
| 455 | t.Run(tt.name, func(t *testing.T) { |
| 456 | for _, pc := range tt.panicCases { |
| 457 | t.Run(pc.name, func(t *testing.T) { |
| 458 | buf := tt.newBuf([]byte{1, 2, 3, 4}) |
| 459 | defer func() { |
| 460 | if recover() == nil { |
| 461 | t.Fatalf("Slice(%d, %d) did not panic", pc.start, pc.end) |
| 462 | } |
| 463 | }() |
| 464 | buf.Slice(pc.start, pc.end) |
| 465 | }) |
| 466 | } |
| 467 | }) |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | func newPooledBuffer(data []byte) mem.Buffer { |
| 472 | return newBuffer(data, mem.NopBufferPool{}) |