Debian Codegraph @

assert/rich_assert.go

raw
1 //go:build !no_antithesis_sdk
2
3 package assert
4
5 // A type for writing raw assertions.
6 // guidanceFnType allows the assertion to provide guidance to
7 // the Antithesis platform when testing in Antithesis.
8 // Regular users of the assert package should not use it.
9 type guidanceFnType int
10
11 const (
12 guidanceFnMaximize guidanceFnType = iota // Maximize (left - right) values
13 guidanceFnMinimize // Minimize (left - right) values
14 guidanceFnWantAll // Encourages fuzzing explorations where boolean values are true
15 guidanceFnWantNone // Encourages fuzzing explorations where boolean values are false
16 guidanceFnExplore
17 )
18
19 // guidanceFnExplore
20
21 func get_guidance_type_string(gt guidanceFnType) string {
22 switch gt {
23 case guidanceFnMaximize, guidanceFnMinimize:
24 return "numeric"
25 case guidanceFnWantAll, guidanceFnWantNone:
26 return "boolean"
27 case guidanceFnExplore:
28 return "json"
29 }
30 return ""
31 }
32
33 type numericOperands[T operandConstraint] struct {
34 Left T `json:"left"`
35 Right T `json:"right"`
36 }
37
38 type guidanceInfo struct {
39 Data any `json:"guidance_data,omitempty"`
40 Location *locationInfo `json:"location"`
41 GuidanceType string `json:"guidance_type"`
42 Message string `json:"message"`
43 Id string `json:"id"`
44 Maximize bool `json:"maximize"`
45 Hit bool `json:"hit"`
46 }
47
48 type booleanGuidanceInfo struct {
49 Data any `json:"guidance_data,omitempty"`
50 Location *locationInfo `json:"location"`
51 GuidanceType string `json:"guidance_type"`
52 Message string `json:"message"`
53 Id string `json:"id"`
54 Maximize bool `json:"maximize"`
55 Hit bool `json:"hit"`
56 }
57
58 func uses_maximize(gt guidanceFnType) bool {
59 return gt == guidanceFnMaximize || gt == guidanceFnWantAll
60 }
61
62 func newOperands[T Number](left, right T) any {
63 switch any(left).(type) {
64 case int8, int16, int32:
65 return numericOperands[int32]{int32(left), int32(right)}
66 case int, int64:
67 return numericOperands[int64]{int64(left), int64(right)}
68 case uint8, uint16, uint32, uint, uint64, uintptr:
69 return numericOperands[uint64]{uint64(left), uint64(right)}
70 case float32, float64:
71 return numericOperands[float64]{float64(left), float64(right)}
72 }
73 return nil
74 }
75
76 func build_numeric_guidance[T Number](gt guidanceFnType, message string, left, right T, loc *locationInfo, id string, hit bool) *guidanceInfo {
77
78 operands := newOperands(left, right)
79 if !hit {
80 operands = nil
81 }
82
83 gI := guidanceInfo{
84 GuidanceType: get_guidance_type_string(gt),
85 Message: message,
86 Id: id,
87 Location: loc,
88 Maximize: uses_maximize(gt),
89 Data: operands,
90 Hit: hit,
91 }
92
93 return &gI
94 }
95
96 type namedBoolDictionary map[string]bool
97
98 func build_boolean_guidance(gt guidanceFnType, message string, named_bools []NamedBool,
99 loc *locationInfo,
100 id string, hit bool) *booleanGuidanceInfo {
101
102 var guidance_data any
103
104 // To ensure the sequence and naming for the named_bool values
105 if hit {
106 named_bool_dictionary := namedBoolDictionary{}
107 for _, named_bool := range named_bools {
108 named_bool_dictionary[named_bool.First] = named_bool.Second
109 }
110 guidance_data = named_bool_dictionary
111 }
112
113 bgI := booleanGuidanceInfo{
114 GuidanceType: get_guidance_type_string(gt),
115 Message: message,
116 Id: id,
117 Location: loc,
118 Maximize: uses_maximize(gt),
119 Data: guidance_data,
120 Hit: hit,
121 }
122
123 return &bgI
124 }
125
126 func behavior_to_guidance(behavior string) guidanceFnType {
127 guidance := guidanceFnExplore
128 switch behavior {
129 case "maximize":
130 guidance = guidanceFnMaximize
131 case "minimize":
132 guidance = guidanceFnMinimize
133 case "all":
134 guidance = guidanceFnWantAll
135 case "none":
136 guidance = guidanceFnWantNone
137 }
138 return guidance
139 }
140
141 func numericGuidanceImpl[T Number](left, right T, message, id string, loc *locationInfo, guidanceFn guidanceFnType, hit bool) {
142 tI := getNumericGuidanceEntry(id, gapTypeForOperand(left), uses_maximize(guidanceFn))
143 gI := build_numeric_guidance(guidanceFn, message, left, right, loc, id, hit)
144 send_value_if_needed(tI, gI)
145 }
146
147 func booleanGuidanceImpl(named_bools []NamedBool, message, id string, loc *locationInfo, guidanceFn guidanceFnType, hit bool) {
148 tI := getBooleanGuidanceEntry(id)
149 bgI := build_boolean_guidance(guidanceFn, message, named_bools, loc, id, hit)
150 tI.send_value(bgI)
151 }
152
153 // NumericGuidanceRaw is a low-level method designed to be used by third-party frameworks. Regular users of the assert package should not call it.
154 func NumericGuidanceRaw[T Number](
155 left, right T,
156 message, id string,
157 classname, funcname, filename string,
158 line int,
159 behavior string,
160 hit bool,
161 ) {
162 loc := &locationInfo{classname, funcname, filename, line, columnUnknown}
163 guidanceFn := behavior_to_guidance(behavior)
164 numericGuidanceImpl(left, right, message, id, loc, guidanceFn, hit)
165 }
166
167 // BooleanGuidanceRaw is a low-level method designed to be used by third-party frameworks. Regular users of the assert package should not call it.
168 func BooleanGuidanceRaw(
169 named_bools []NamedBool,
170 message, id string,
171 classname, funcname, filename string,
172 line int,
173 behavior string,
174 hit bool,
175 ) {
176 loc := &locationInfo{classname, funcname, filename, line, columnUnknown}
177 guidanceFn := behavior_to_guidance(behavior)
178 booleanGuidanceImpl(named_bools, message, id, loc, guidanceFn, hit)
179 }
180
181 func add_numeric_details[T Number](details map[string]any, left, right T) map[string]any {
182 // ----------------------------------------------------
183 // Can not use maps.Clone() until go 1.21.0 or above
184 // enhancedDetails := maps.Clone(details)
185 // ----------------------------------------------------
186 enhancedDetails := map[string]any{}
187 for k, v := range details {
188 enhancedDetails[k] = v
189 }
190 enhancedDetails["left"] = left
191 enhancedDetails["right"] = right
192 return enhancedDetails
193 }
194
195 func add_boolean_details(details map[string]any, named_bools []NamedBool) map[string]any {
196 // ----------------------------------------------------
197 // Can not use maps.Clone() until go 1.21.0 or above
198 // enhancedDetails := maps.Clone(details)
199 // ----------------------------------------------------
200 enhancedDetails := map[string]any{}
201 for k, v := range details {
202 enhancedDetails[k] = v
203 }
204 for _, named_bool := range named_bools {
205 enhancedDetails[named_bool.First] = named_bool.Second
206 }
207 return enhancedDetails
208 }
209
210 // Whether a numeric rich-assert evaluation can return before capturing its
211 // location or building its details: the assertion side has already emitted
212 // for this condition, and the guidance side does not improve the tracked
213 // extremum. Both checks are lock-free loads. False while the guidance entry
214 // does not exist, so the first evaluation still takes the full path.
215 func numericRichAssertSettled[T Number](left, right T, message string, condition bool) bool {
216 if !trackerEmitted(message, condition) {
217 return false
218 }
219 entry, ok := numericGuidanceTracker.Load(message)
220 if !ok {
221 return false
222 }
223 return !entry.(*numericGuidanceInfo).improves(candidateGap(newOperands(left, right)))
224 }
225
226 // Equivalent to asserting Always(left > right, message, details). Information about left and right will automatically be added to the details parameter, with keys left and right. If you use this function for assertions that compare numeric quantities, you may help Antithesis find more bugs.
227 func AlwaysGreaterThan[T Number](left, right T, message string, details map[string]any) {
228 if numericRichAssertSettled(left, right, message, left > right) {
229 return
230 }
231 loc := newLocationInfo(offsetAPICaller)
232 id := makeKey(message, loc)
233 condition := left > right
234 all_details := add_numeric_details(details, left, right)
235 assertImpl(condition, message, all_details, loc, wasHit, mustBeHit, universalTest, alwaysDisplay, id)
236
237 numericGuidanceImpl(left, right, message, id, loc, guidanceFnMinimize, wasHit)
238 }
239
240 // Equivalent to asserting Always(left >= right, message, details). Information about left and right will automatically be added to the details parameter, with keys left and right. If you use this function for assertions that compare numeric quantities, you may help Antithesis find more bugs.
241 func AlwaysGreaterThanOrEqualTo[T Number](left, right T, message string, details map[string]any) {
242 if numericRichAssertSettled(left, right, message, left >= right) {
243 return
244 }
245 loc := newLocationInfo(offsetAPICaller)
246 id := makeKey(message, loc)
247 condition := left >= right
248 all_details := add_numeric_details(details, left, right)
249 assertImpl(condition, message, all_details, loc, wasHit, mustBeHit, universalTest, alwaysDisplay, id)
250
251 numericGuidanceImpl(left, right, message, id, loc, guidanceFnMinimize, wasHit)
252 }
253
254 // Equivalent to asserting Sometimes(T left > T right, message, details). Information about left and right will automatically be added to the details parameter, with keys left and right. If you use this function for assertions that compare numeric quantities, you may help Antithesis find more bugs.
255 func SometimesGreaterThan[T Number](left, right T, message string, details map[string]any) {
256 if numericRichAssertSettled(left, right, message, left > right) {
257 return
258 }
259 loc := newLocationInfo(offsetAPICaller)
260 id := makeKey(message, loc)
261 condition := left > right
262 all_details := add_numeric_details(details, left, right)
263 assertImpl(condition, message, all_details, loc, wasHit, mustBeHit, existentialTest, sometimesDisplay, id)
264
265 numericGuidanceImpl(left, right, message, id, loc, guidanceFnMaximize, wasHit)
266 }
267
268 // Equivalent to asserting Sometimes(T left >= T right, message, details). Information about left and right will automatically be added to the details parameter, with keys left and right. If you use this function for assertions that compare numeric quantities, you may help Antithesis find more bugs.
269 func SometimesGreaterThanOrEqualTo[T Number](left, right T, message string, details map[string]any) {
270 if numericRichAssertSettled(left, right, message, left >= right) {
271 return
272 }
273 loc := newLocationInfo(offsetAPICaller)
274 id := makeKey(message, loc)
275 condition := left >= right
276 all_details := add_numeric_details(details, left, right)
277 assertImpl(condition, message, all_details, loc, wasHit, mustBeHit, existentialTest, sometimesDisplay, id)
278
279 numericGuidanceImpl(left, right, message, id, loc, guidanceFnMaximize, wasHit)
280 }
281
282 // Equivalent to asserting Always(left < right, message, details). Information about left and right will automatically be added to the details parameter, with keys left and right. If you use this function for assertions that compare numeric quantities, you may help Antithesis find more bugs.
283 func AlwaysLessThan[T Number](left, right T, message string, details map[string]any) {
284 if numericRichAssertSettled(left, right, message, left < right) {
285 return
286 }
287 loc := newLocationInfo(offsetAPICaller)
288 id := makeKey(message, loc)
289 condition := left < right
290 all_details := add_numeric_details(details, left, right)
291 assertImpl(condition, message, all_details, loc, wasHit, mustBeHit, universalTest, alwaysDisplay, id)
292
293 numericGuidanceImpl(left, right, message, id, loc, guidanceFnMaximize, wasHit)
294 }
295
296 // Equivalent to asserting Always(left <= right, message, details). Information about left and right will automatically be added to the details parameter, with keys left and right. If you use this function for assertions that compare numeric quantities, you may help Antithesis find more bugs.
297 func AlwaysLessThanOrEqualTo[T Number](left, right T, message string, details map[string]any) {
298 if numericRichAssertSettled(left, right, message, left <= right) {
299 return
300 }
301 loc := newLocationInfo(offsetAPICaller)
302 id := makeKey(message, loc)
303 condition := left <= right
304 all_details := add_numeric_details(details, left, right)
305 assertImpl(condition, message, all_details, loc, wasHit, mustBeHit, universalTest, alwaysDisplay, id)
306
307 numericGuidanceImpl(left, right, message, id, loc, guidanceFnMaximize, wasHit)
308 }
309
310 // Equivalent to asserting Sometimes(T left < T right, message, details). Information about left and right will automatically be added to the details parameter, with keys left and right. If you use this function for assertions that compare numeric quantities, you may help Antithesis find more bugs.
311 func SometimesLessThan[T Number](left, right T, message string, details map[string]any) {
312 if numericRichAssertSettled(left, right, message, left < right) {
313 return
314 }
315 loc := newLocationInfo(offsetAPICaller)
316 id := makeKey(message, loc)
317 condition := left < right
318 all_details := add_numeric_details(details, left, right)
319 assertImpl(condition, message, all_details, loc, wasHit, mustBeHit, existentialTest, sometimesDisplay, id)
320
321 numericGuidanceImpl(left, right, message, id, loc, guidanceFnMinimize, wasHit)
322 }
323
324 // Equivalent to asserting Sometimes(T left <= T right, message, details). Information about left and right will automatically be added to the details parameter, with keys left and right. If you use this function for assertions that compare numeric quantities, you may help Antithesis find more bugs.
325 func SometimesLessThanOrEqualTo[T Number](left, right T, message string, details map[string]any) {
326 if numericRichAssertSettled(left, right, message, left <= right) {
327 return
328 }
329 loc := newLocationInfo(offsetAPICaller)
330 id := makeKey(message, loc)
331 condition := left <= right
332 all_details := add_numeric_details(details, left, right)
333 assertImpl(condition, message, all_details, loc, wasHit, mustBeHit, existentialTest, sometimesDisplay, id)
334
335 numericGuidanceImpl(left, right, message, id, loc, guidanceFnMinimize, wasHit)
336 }
337
338 // Asserts that every time this is called, at least one bool in named_bools is true. Equivalent to Always(named_bools[0].second || named_bools[1].second || ..., message, details). If you use this for assertions about the behavior of booleans, you may help Antithesis find more bugs. Information about named_bools will automatically be added to the details parameter, and the keys will be the names of the bools.
339 func AlwaysSome(named_bools []NamedBool, message string, details map[string]any) {
340 loc := newLocationInfo(offsetAPICaller)
341 id := makeKey(message, loc)
342 disjunction := false
343 for _, named_bool := range named_bools {
344 if named_bool.Second {
345 disjunction = true
346 break
347 }
348 }
349 all_details := add_boolean_details(details, named_bools)
350 assertImpl(disjunction, message, all_details, loc, wasHit, mustBeHit, universalTest, alwaysDisplay, id)
351
352 booleanGuidanceImpl(named_bools, message, id, loc, guidanceFnWantNone, wasHit)
353 }
354
355 // Asserts that at least one time this is called, every bool in named_bools is true. Equivalent to Sometimes(named_bools[0].second && named_bools[1].second && ..., message, details). If you use this for assertions about the behavior of booleans, you may help Antithesis find more bugs. Information about named_bools will automatically be added to the details parameter, and the keys will be the names of the bools.
356 func SometimesAll(named_bools []NamedBool, message string, details map[string]any) {
357 loc := newLocationInfo(offsetAPICaller)
358 id := makeKey(message, loc)
359 conjunction := true
360 for _, named_bool := range named_bools {
361 if !named_bool.Second {
362 conjunction = false
363 break
364 }
365 }
366 all_details := add_boolean_details(details, named_bools)
367 assertImpl(conjunction, message, all_details, loc, wasHit, mustBeHit, existentialTest, sometimesDisplay, id)
368
369 booleanGuidanceImpl(named_bools, message, id, loc, guidanceFnWantAll, wasHit)
370 }