|
4 | 4 | "testing" |
5 | 5 |
|
6 | 6 | "github.com/google/go-cmp/cmp" |
| 7 | + "github.com/google/osv/vulnfeeds/models" |
7 | 8 | "github.com/ossf/osv-schema/bindings/go/osvschema" |
8 | 9 | "google.golang.org/protobuf/testing/protocmp" |
9 | 10 | "google.golang.org/protobuf/types/known/structpb" |
@@ -435,3 +436,200 @@ func TestGroupAffectedRanges(t *testing.T) { |
435 | 436 | }) |
436 | 437 | } |
437 | 438 | } |
| 439 | + |
| 440 | +func TestMergeRangesAndCreateAffected(t *testing.T) { |
| 441 | + tests := []struct { |
| 442 | + name string |
| 443 | + resolvedRanges []*osvschema.Range |
| 444 | + commits []models.AffectedCommit |
| 445 | + successfulRepos []string |
| 446 | + want *osvschema.Affected |
| 447 | + }{ |
| 448 | + { |
| 449 | + name: "Merge existing ranges with commits for the same repo", |
| 450 | + resolvedRanges: []*osvschema.Range{ |
| 451 | + { |
| 452 | + Type: osvschema.Range_GIT, |
| 453 | + Repo: "repo1", |
| 454 | + Events: []*osvschema.Event{ |
| 455 | + {Introduced: "0"}, |
| 456 | + {Fixed: "1.0"}, |
| 457 | + }, |
| 458 | + }, |
| 459 | + }, |
| 460 | + commits: []models.AffectedCommit{ |
| 461 | + { |
| 462 | + Repo: "repo1", |
| 463 | + Introduced: "1.1", |
| 464 | + }, |
| 465 | + { |
| 466 | + Repo: "repo1", |
| 467 | + Fixed: "1.2", |
| 468 | + }, |
| 469 | + }, |
| 470 | + successfulRepos: []string{"repo1"}, |
| 471 | + want: &osvschema.Affected{ |
| 472 | + Ranges: []*osvschema.Range{ |
| 473 | + { |
| 474 | + Type: osvschema.Range_GIT, |
| 475 | + Repo: "repo1", |
| 476 | + Events: []*osvschema.Event{ |
| 477 | + {Introduced: "1.1"}, |
| 478 | + {Introduced: "0"}, |
| 479 | + {Fixed: "1.0"}, |
| 480 | + {Fixed: "1.2"}, |
| 481 | + }, |
| 482 | + }, |
| 483 | + }, |
| 484 | + }, |
| 485 | + }, |
| 486 | + { |
| 487 | + name: "No resolved ranges, only commits", |
| 488 | + resolvedRanges: nil, |
| 489 | + commits: []models.AffectedCommit{ |
| 490 | + { |
| 491 | + Repo: "repo2", |
| 492 | + Introduced: "0", |
| 493 | + Fixed: "1.0", |
| 494 | + }, |
| 495 | + }, |
| 496 | + successfulRepos: []string{"repo2"}, |
| 497 | + want: &osvschema.Affected{ |
| 498 | + Ranges: []*osvschema.Range{ |
| 499 | + { |
| 500 | + Events: []*osvschema.Event{ |
| 501 | + {Introduced: "0"}, |
| 502 | + {Fixed: "1.0"}, |
| 503 | + }, |
| 504 | + }, |
| 505 | + }, |
| 506 | + }, |
| 507 | + }, |
| 508 | + { |
| 509 | + name: "Duplicate events are deduplicated", |
| 510 | + resolvedRanges: []*osvschema.Range{ |
| 511 | + { |
| 512 | + Type: osvschema.Range_GIT, |
| 513 | + Repo: "repo3", |
| 514 | + Events: []*osvschema.Event{ |
| 515 | + {Introduced: "0"}, |
| 516 | + {Fixed: "1.0"}, |
| 517 | + }, |
| 518 | + }, |
| 519 | + }, |
| 520 | + commits: []models.AffectedCommit{ |
| 521 | + // duplicate fixed |
| 522 | + { |
| 523 | + Repo: "repo3", |
| 524 | + Fixed: "1.0", |
| 525 | + }, |
| 526 | + // duplicate introduced |
| 527 | + { |
| 528 | + Repo: "repo3", |
| 529 | + Introduced: "0", |
| 530 | + }, |
| 531 | + // new last affected |
| 532 | + { |
| 533 | + Repo: "repo3", |
| 534 | + LastAffected: "0.5", |
| 535 | + }, |
| 536 | + }, |
| 537 | + successfulRepos: []string{"repo3"}, |
| 538 | + want: &osvschema.Affected{ |
| 539 | + Ranges: []*osvschema.Range{ |
| 540 | + { |
| 541 | + Type: osvschema.Range_GIT, |
| 542 | + Repo: "repo3", |
| 543 | + Events: []*osvschema.Event{ |
| 544 | + {Introduced: "0"}, |
| 545 | + {Fixed: "1.0"}, |
| 546 | + {LastAffected: "0.5"}, |
| 547 | + }, |
| 548 | + }, |
| 549 | + }, |
| 550 | + }, |
| 551 | + }, |
| 552 | + { |
| 553 | + name: "Commits for repos not in successfulRepos are ignored when resolvedRanges exist", |
| 554 | + resolvedRanges: []*osvschema.Range{ |
| 555 | + { |
| 556 | + Type: osvschema.Range_GIT, |
| 557 | + Repo: "repo4", |
| 558 | + Events: []*osvschema.Event{ |
| 559 | + {Introduced: "0"}, |
| 560 | + }, |
| 561 | + }, |
| 562 | + }, |
| 563 | + commits: []models.AffectedCommit{ |
| 564 | + { |
| 565 | + Repo: "repo_ignored", |
| 566 | + Introduced: "1.1", |
| 567 | + }, |
| 568 | + }, |
| 569 | + successfulRepos: []string{"repo4"}, // repo_ignored is absent |
| 570 | + want: &osvschema.Affected{ |
| 571 | + Ranges: []*osvschema.Range{ |
| 572 | + { |
| 573 | + Type: osvschema.Range_GIT, |
| 574 | + Repo: "repo4", |
| 575 | + Events: []*osvschema.Event{ |
| 576 | + {Introduced: "0"}, |
| 577 | + }, |
| 578 | + }, |
| 579 | + }, |
| 580 | + }, |
| 581 | + }, |
| 582 | + { |
| 583 | + name: "Multiple resolved ranges for same repo are merged and commits appended", |
| 584 | + resolvedRanges: []*osvschema.Range{ |
| 585 | + { |
| 586 | + Type: osvschema.Range_GIT, |
| 587 | + Repo: "repo5", |
| 588 | + Events: []*osvschema.Event{ |
| 589 | + {Introduced: "0"}, |
| 590 | + {Fixed: "1.0"}, |
| 591 | + }, |
| 592 | + }, |
| 593 | + { |
| 594 | + Type: osvschema.Range_GIT, |
| 595 | + Repo: "repo5", |
| 596 | + Events: []*osvschema.Event{ |
| 597 | + {Introduced: "2.0"}, |
| 598 | + {Fixed: "3.0"}, |
| 599 | + }, |
| 600 | + }, |
| 601 | + }, |
| 602 | + commits: []models.AffectedCommit{ |
| 603 | + { |
| 604 | + Repo: "repo5", |
| 605 | + Fixed: "4.0", |
| 606 | + }, |
| 607 | + }, |
| 608 | + successfulRepos: []string{"repo5"}, |
| 609 | + want: &osvschema.Affected{ |
| 610 | + Ranges: []*osvschema.Range{ |
| 611 | + { |
| 612 | + Type: osvschema.Range_GIT, |
| 613 | + Repo: "repo5", |
| 614 | + Events: []*osvschema.Event{ |
| 615 | + {Introduced: "0"}, |
| 616 | + {Fixed: "1.0"}, |
| 617 | + {Introduced: "2.0"}, |
| 618 | + {Fixed: "3.0"}, |
| 619 | + {Fixed: "4.0"}, |
| 620 | + }, |
| 621 | + }, |
| 622 | + }, |
| 623 | + }, |
| 624 | + }, |
| 625 | + } |
| 626 | + |
| 627 | + for _, tt := range tests { |
| 628 | + t.Run(tt.name, func(t *testing.T) { |
| 629 | + got := MergeRangesAndCreateAffected(tt.resolvedRanges, tt.commits, tt.successfulRepos, &models.ConversionMetrics{}) |
| 630 | + if diff := cmp.Diff(tt.want, got, protocmp.Transform()); diff != "" { |
| 631 | + t.Errorf("MergeRangesAndCreateAffected() mismatch (-want +got):\n%s", diff) |
| 632 | + } |
| 633 | + }) |
| 634 | + } |
| 635 | +} |
0 commit comments