github.com/google/trillian-examples@v0.0.0-20240520080811-0d40d35cef0e/binary_transparency/firmware/internal/ftmap/aggregate_test.go (about) 1 // Copyright 2021 Google LLC. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package ftmap 16 17 import ( 18 "fmt" 19 "testing" 20 21 "github.com/apache/beam/sdks/v2/go/pkg/beam" 22 "github.com/apache/beam/sdks/v2/go/pkg/beam/register" 23 "github.com/apache/beam/sdks/v2/go/pkg/beam/testing/passert" 24 "github.com/apache/beam/sdks/v2/go/pkg/beam/testing/ptest" 25 "github.com/google/trillian-examples/binary_transparency/firmware/api" 26 ) 27 28 func init() { 29 register.Function1x1(testAggregationToStringFn) 30 } 31 32 func TestMain(m *testing.M) { 33 ptest.Main(m) 34 } 35 36 func TestAggregate(t *testing.T) { 37 fwEntries := []*firmwareLogEntry{ 38 {Index: 0, Firmware: createFW("dummy", 400)}, 39 {Index: 1, Firmware: createFW("dummy", 401)}, 40 } 41 logHead := int64(len(fwEntries) - 1) 42 createAnnotationMalware := func(fwIndex int, good bool) *annotationMalwareLogEntry { 43 logHead++ 44 return &annotationMalwareLogEntry{ 45 Index: logHead, 46 Annotation: api.MalwareStatement{ 47 FirmwareID: api.FirmwareID{ 48 LogIndex: uint64(fwIndex), 49 FirmwareImageSHA512: fwEntries[fwIndex].Firmware.FirmwareImageSHA512, 50 }, 51 Good: good, 52 }, 53 } 54 } 55 tests := []struct { 56 name string 57 treeID int64 58 annotationMalwares []*annotationMalwareLogEntry 59 60 wantGood []string 61 }{ 62 { 63 name: "No annotations", 64 treeID: 12345, 65 66 wantGood: []string{"0: true", "1: true"}, 67 }, 68 { 69 name: "One bad annotation", 70 treeID: 12345, 71 72 annotationMalwares: []*annotationMalwareLogEntry{ 73 createAnnotationMalware(1, false), 74 }, 75 76 wantGood: []string{"0: true", "1: false"}, 77 }, 78 { 79 name: "Many annotations all good", 80 treeID: 12345, 81 82 annotationMalwares: []*annotationMalwareLogEntry{ 83 createAnnotationMalware(0, true), 84 createAnnotationMalware(0, true), 85 }, 86 87 wantGood: []string{"0: true", "1: true"}, 88 }, 89 { 90 name: "Many annotations one bad", 91 treeID: 12345, 92 93 annotationMalwares: []*annotationMalwareLogEntry{ 94 createAnnotationMalware(0, true), 95 createAnnotationMalware(0, false), 96 createAnnotationMalware(0, true), 97 }, 98 99 wantGood: []string{"0: false", "1: true"}, 100 }, 101 } 102 103 for _, test := range tests { 104 test := test 105 t.Run(test.name, func(t *testing.T) { 106 p, s := beam.NewPipelineWithRoot() 107 108 fws := beam.CreateList(s, fwEntries) 109 annotationMalwares := beam.CreateList(s, test.annotationMalwares) 110 111 entries, aggs := Aggregate(s, test.treeID, fws, annotationMalwares) 112 113 passert.Count(s, entries, "entries", len(fwEntries)) 114 passert.Count(s, aggs, "aggs", len(fwEntries)) 115 116 passert.Equals(s, beam.ParDo(s, testAggregationToStringFn, aggs), beam.CreateList(s, test.wantGood)) 117 118 err := ptest.Run(p) 119 if err != nil { 120 t.Errorf("unexpected error: %v", err) 121 } 122 }) 123 } 124 } 125 126 func testAggregationToStringFn(a *api.AggregatedFirmware) string { 127 return fmt.Sprintf("%d: %t", a.Index, a.Good) 128 }