|
| 1 | +// Copyright 2018 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package subtle_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + |
| 10 | + "golang.org/x/crypto/internal/subtle" |
| 11 | +) |
| 12 | + |
| 13 | +var a, b [100]byte |
| 14 | + |
| 15 | +var aliasingTests = []struct { |
| 16 | + x, y []byte |
| 17 | + anyOverlap, inexactOverlap bool |
| 18 | +}{ |
| 19 | + {a[:], b[:], false, false}, |
| 20 | + {a[:], b[:0], false, false}, |
| 21 | + {a[:], b[:50], false, false}, |
| 22 | + {a[40:50], a[50:60], false, false}, |
| 23 | + {a[40:50], a[60:70], false, false}, |
| 24 | + {a[:51], a[50:], true, true}, |
| 25 | + {a[:], a[:], true, false}, |
| 26 | + {a[:50], a[:60], true, false}, |
| 27 | + {a[:], nil, false, false}, |
| 28 | + {nil, nil, false, false}, |
| 29 | + {a[:], a[:0], false, false}, |
| 30 | +} |
| 31 | + |
| 32 | +func testAliasing(t *testing.T, i int, x, y []byte, anyOverlap, inexactOverlap bool) { |
| 33 | + any := subtle.AnyOverlap(x, y) |
| 34 | + if any != anyOverlap { |
| 35 | + t.Errorf("%d: wrong AnyOverlap result, expected %v, got %v", i, anyOverlap, any) |
| 36 | + } |
| 37 | + inexact := subtle.InexactOverlap(x, y) |
| 38 | + if inexact != inexactOverlap { |
| 39 | + t.Errorf("%d: wrong InexactOverlap result, expected %v, got %v", i, inexactOverlap, any) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestAliasing(t *testing.T) { |
| 44 | + for i, tt := range aliasingTests { |
| 45 | + testAliasing(t, i, tt.x, tt.y, tt.anyOverlap, tt.inexactOverlap) |
| 46 | + testAliasing(t, i, tt.y, tt.x, tt.anyOverlap, tt.inexactOverlap) |
| 47 | + } |
| 48 | +} |
0 commit comments