github.com/letsencrypt/boulder@v0.20251208.0/ctpolicy/loglist/loglist_test.go (about) 1 package loglist 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/google/certificate-transparency-go/loglist3" 8 "github.com/jmhodges/clock" 9 10 "github.com/letsencrypt/boulder/test" 11 ) 12 13 func TestNew(t *testing.T) { 14 15 } 16 17 func TestSubset(t *testing.T) { 18 input := List{ 19 Log{Name: "Log A1"}, 20 Log{Name: "Log A2"}, 21 Log{Name: "Log B1"}, 22 Log{Name: "Log B2"}, 23 Log{Name: "Log C1"}, 24 Log{Name: "Log C2"}, 25 } 26 27 actual, err := input.subset(nil) 28 test.AssertNotError(t, err, "nil names should not error") 29 test.AssertEquals(t, len(actual), 0) 30 31 actual, err = input.subset([]string{}) 32 test.AssertNotError(t, err, "empty names should not error") 33 test.AssertEquals(t, len(actual), 0) 34 35 actual, err = input.subset([]string{"Other Log"}) 36 test.AssertError(t, err, "wrong name should result in error") 37 test.AssertEquals(t, len(actual), 0) 38 39 expected := List{ 40 Log{Name: "Log B1"}, 41 Log{Name: "Log A1"}, 42 Log{Name: "Log A2"}, 43 } 44 actual, err = input.subset([]string{"Log B1", "Log A1", "Log A2"}) 45 test.AssertNotError(t, err, "normal usage should not error") 46 test.AssertDeepEquals(t, actual, expected) 47 } 48 49 func TestForPurpose(t *testing.T) { 50 input := List{ 51 Log{Name: "Log A1", Operator: "A", State: loglist3.UsableLogStatus}, 52 Log{Name: "Log A2", Operator: "A", State: loglist3.RejectedLogStatus}, 53 Log{Name: "Log B1", Operator: "B", State: loglist3.UsableLogStatus}, 54 Log{Name: "Log B2", Operator: "B", State: loglist3.RetiredLogStatus}, 55 Log{Name: "Log C1", Operator: "C", State: loglist3.PendingLogStatus}, 56 Log{Name: "Log C2", Operator: "C", State: loglist3.ReadOnlyLogStatus}, 57 } 58 expected := List{ 59 Log{Name: "Log A1", Operator: "A", State: loglist3.UsableLogStatus}, 60 Log{Name: "Log B1", Operator: "B", State: loglist3.UsableLogStatus}, 61 } 62 actual, err := input.forPurpose(Issuance, false) 63 test.AssertNotError(t, err, "should have two acceptable logs") 64 test.AssertDeepEquals(t, actual, expected) 65 66 input = List{ 67 Log{Name: "Log A1", Operator: "A", State: loglist3.UsableLogStatus}, 68 Log{Name: "Log A2", Operator: "A", State: loglist3.RejectedLogStatus}, 69 Log{Name: "Log B1", Operator: "B", State: loglist3.QualifiedLogStatus}, 70 Log{Name: "Log B2", Operator: "B", State: loglist3.RetiredLogStatus}, 71 Log{Name: "Log C1", Operator: "C", State: loglist3.PendingLogStatus}, 72 Log{Name: "Log C2", Operator: "C", State: loglist3.ReadOnlyLogStatus}, 73 } 74 _, err = input.forPurpose(Issuance, false) 75 test.AssertError(t, err, "should only have one acceptable log") 76 77 expected = List{ 78 Log{Name: "Log A1", Operator: "A", State: loglist3.UsableLogStatus}, 79 Log{Name: "Log C2", Operator: "C", State: loglist3.ReadOnlyLogStatus}, 80 } 81 actual, err = input.forPurpose(Validation, false) 82 test.AssertNotError(t, err, "should have two acceptable logs") 83 test.AssertDeepEquals(t, actual, expected) 84 85 expected = List{ 86 Log{Name: "Log A1", Operator: "A", State: loglist3.UsableLogStatus}, 87 Log{Name: "Log B1", Operator: "B", State: loglist3.QualifiedLogStatus}, 88 Log{Name: "Log C1", Operator: "C", State: loglist3.PendingLogStatus}, 89 } 90 actual, err = input.forPurpose(Informational, false) 91 test.AssertNotError(t, err, "should have three acceptable logs") 92 test.AssertDeepEquals(t, actual, expected) 93 94 input = List{ 95 Log{Name: "Log A1", Operator: "A", State: loglist3.UsableLogStatus}, 96 Log{Name: "Log B1", Operator: "B", State: loglist3.UsableLogStatus}, 97 Log{Name: "Log T1", Operator: "T", Type: "test", State: loglist3.UndefinedLogStatus}, 98 Log{Name: "Log M1", Operator: "M", Type: "monitoring_only", State: loglist3.UndefinedLogStatus}, 99 } 100 expected = List{ 101 Log{Name: "Log A1", Operator: "A", State: loglist3.UsableLogStatus}, 102 Log{Name: "Log B1", Operator: "B", State: loglist3.UsableLogStatus}, 103 } 104 actual, err = input.forPurpose(Issuance, false) 105 test.AssertNotError(t, err, "should have two acceptable logs with submitToTestLogs=[false]") 106 test.AssertDeepEquals(t, actual, expected) 107 108 expected = List{ 109 Log{Name: "Log A1", Operator: "A", State: loglist3.UsableLogStatus}, 110 Log{Name: "Log B1", Operator: "B", State: loglist3.UsableLogStatus}, 111 Log{Name: "Log T1", Operator: "T", Type: "test", State: loglist3.UndefinedLogStatus}, 112 Log{Name: "Log M1", Operator: "M", Type: "monitoring_only", State: loglist3.UndefinedLogStatus}, 113 } 114 actual, err = input.forPurpose(Issuance, true) 115 test.AssertNotError(t, err, "should have two acceptable logs with submitToTestLogs=[true]") 116 test.AssertDeepEquals(t, actual, expected) 117 } 118 119 func TestForTime(t *testing.T) { 120 fc := clock.NewFake() 121 fc.Set(time.Now()) 122 123 input := List{ 124 Log{Name: "Fully Bound", StartInclusive: fc.Now().Add(-time.Hour), EndExclusive: fc.Now().Add(time.Hour)}, 125 Log{Name: "Open End", StartInclusive: fc.Now().Add(-time.Hour)}, 126 Log{Name: "Open Start", EndExclusive: fc.Now().Add(time.Hour)}, 127 Log{Name: "Fully Open"}, 128 } 129 130 expected := List{ 131 Log{Name: "Fully Bound", StartInclusive: fc.Now().Add(-time.Hour), EndExclusive: fc.Now().Add(time.Hour)}, 132 Log{Name: "Open End", StartInclusive: fc.Now().Add(-time.Hour)}, 133 Log{Name: "Open Start", EndExclusive: fc.Now().Add(time.Hour)}, 134 Log{Name: "Fully Open"}, 135 } 136 actual := input.ForTime(fc.Now()) 137 test.AssertDeepEquals(t, actual, expected) 138 139 expected = List{ 140 Log{Name: "Fully Bound", StartInclusive: fc.Now().Add(-time.Hour), EndExclusive: fc.Now().Add(time.Hour)}, 141 Log{Name: "Open End", StartInclusive: fc.Now().Add(-time.Hour)}, 142 Log{Name: "Open Start", EndExclusive: fc.Now().Add(time.Hour)}, 143 Log{Name: "Fully Open"}, 144 } 145 actual = input.ForTime(fc.Now().Add(-time.Hour)) 146 test.AssertDeepEquals(t, actual, expected) 147 148 expected = List{ 149 Log{Name: "Open Start", EndExclusive: fc.Now().Add(time.Hour)}, 150 Log{Name: "Fully Open"}, 151 } 152 actual = input.ForTime(fc.Now().Add(-2 * time.Hour)) 153 test.AssertDeepEquals(t, actual, expected) 154 155 expected = List{ 156 Log{Name: "Open End", StartInclusive: fc.Now().Add(-time.Hour)}, 157 Log{Name: "Fully Open"}, 158 } 159 actual = input.ForTime(fc.Now().Add(time.Hour)) 160 test.AssertDeepEquals(t, actual, expected) 161 } 162 163 func TestPermute(t *testing.T) { 164 input := List{ 165 Log{Name: "Log A1"}, 166 Log{Name: "Log A2"}, 167 Log{Name: "Log B1"}, 168 Log{Name: "Log B2"}, 169 Log{Name: "Log C1"}, 170 Log{Name: "Log C2"}, 171 } 172 173 foundIndices := make(map[string]map[int]int) 174 for _, log := range input { 175 foundIndices[log.Name] = make(map[int]int) 176 } 177 178 for range 100 { 179 actual := input.Permute() 180 for index, log := range actual { 181 foundIndices[log.Name][index]++ 182 } 183 } 184 185 for name, counts := range foundIndices { 186 for index, count := range counts { 187 if count == 0 { 188 t.Errorf("Log %s appeared at index %d too few times", name, index) 189 } 190 } 191 } 192 } 193 194 func TestGetByID(t *testing.T) { 195 input := List{ 196 Log{Name: "Log A1", Id: "ID A1"}, 197 Log{Name: "Log B1", Id: "ID B1"}, 198 } 199 200 expected := Log{Name: "Log A1", Id: "ID A1"} 201 actual, err := input.GetByID("ID A1") 202 test.AssertNotError(t, err, "should have found log") 203 test.AssertDeepEquals(t, actual, expected) 204 205 _, err = input.GetByID("Other ID") 206 test.AssertError(t, err, "should not have found log") 207 }