github.com/google/osv-scalibr@v0.4.1/veles/velestest/fakesecrets.go (about) 1 // Copyright 2025 Google LLC 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 velestest 16 17 import ( 18 "testing" 19 20 "github.com/google/osv-scalibr/veles" 21 ) 22 23 // FakeStringSecret is a fake veles.Secret that contains a string Value. 24 // 25 // Since types are important in Veles, there is also a FakeIntSecret to be able 26 // to distinguish two different secret types. 27 type FakeStringSecret struct { 28 Value string 29 } 30 31 // NewFakeStringSecret creates a new FakeStringSecret from a given string. 32 func NewFakeStringSecret(s string) FakeStringSecret { 33 return FakeStringSecret{Value: s} 34 } 35 36 // FakeIntSecret is a fake veles.Secret that contains integer Data. 37 // 38 // Since types are important in Veles, there is also a FakeIntSecret to be able 39 // to distinguish two different secret types. 40 type FakeIntSecret struct { 41 Data int 42 } 43 44 // NewFakeIntSecret creates a new FakeIntSecret from a given int. 45 func NewFakeIntSecret(i int) FakeIntSecret { 46 return FakeIntSecret{Data: i} 47 } 48 49 // FakeSecretsT is a testing helper to conveniently create a slice of 50 // veles.Secret. 51 // 52 // Based on the underlying type of each element of datas, either a 53 // FakeStringSecret or a FakeIntSecret is created. If the type is neither int 54 // nor string, t.Fatalf is invoked because this constitutes a violated 55 // assumption about the environment. 56 func FakeSecretsT(t *testing.T) func(datas ...any) []veles.Secret { 57 t.Helper() 58 return func(datas ...any) []veles.Secret { 59 var secrets []veles.Secret 60 for _, d := range datas { 61 switch s := d.(type) { 62 case string: 63 secrets = append(secrets, NewFakeStringSecret(s)) 64 case int: 65 secrets = append(secrets, NewFakeIntSecret(s)) 66 default: 67 t.Fatalf("expected string or int, got %T", d) 68 } 69 } 70 return secrets 71 } 72 } 73 74 // LessFakeSecretT is used to sort a slice containing instances of the above 75 // fake veles.Secret types. 76 // 77 // This way, tests can e.g. encode the notion that the order in which the 78 // DetectionEngine detects secrets should be seen as non-deterministic and not 79 // relied upon (see hyrumslaw.com). 80 // 81 // The returned function expects the provided veles.Secret instances to have 82 // underlying type FakeStringSecret or FakeIntSecret, otherwise the t.Fatalf is 83 // invoked. 84 func LessFakeSecretT(t *testing.T) func(a, b veles.Secret) bool { 85 t.Helper() 86 return func(a veles.Secret, b veles.Secret) bool { 87 strA, isStrA := a.(FakeStringSecret) 88 intA, isIntA := a.(FakeIntSecret) 89 strB, isStrB := b.(FakeStringSecret) 90 intB, isIntB := b.(FakeIntSecret) 91 if !isStrA && !isIntA { 92 t.Fatalf("want FakeStringSecret or FakeIntSecret, got %T", a) 93 } 94 if !isStrB && !isIntB { 95 t.Fatalf("want FakeStringSecret or FakeIntSecret, got %T", b) 96 } 97 if isStrA { 98 if isStrB { 99 return strA.Value < strB.Value 100 } 101 return true 102 } 103 if isIntB { 104 return intA.Data < intB.Data 105 } 106 return false 107 } 108 }