aliasing_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. package subtle_test
  5. import (
  6. "testing"
  7. "golang.org/x/crypto/internal/subtle"
  8. )
  9. var a, b [100]byte
  10. var aliasingTests = []struct {
  11. x, y []byte
  12. anyOverlap, inexactOverlap bool
  13. }{
  14. {a[:], b[:], false, false},
  15. {a[:], b[:0], false, false},
  16. {a[:], b[:50], false, false},
  17. {a[40:50], a[50:60], false, false},
  18. {a[40:50], a[60:70], false, false},
  19. {a[:51], a[50:], true, true},
  20. {a[:], a[:], true, false},
  21. {a[:50], a[:60], true, false},
  22. {a[:], nil, false, false},
  23. {nil, nil, false, false},
  24. {a[:], a[:0], false, false},
  25. }
  26. func testAliasing(t *testing.T, i int, x, y []byte, anyOverlap, inexactOverlap bool) {
  27. any := subtle.AnyOverlap(x, y)
  28. if any != anyOverlap {
  29. t.Errorf("%d: wrong AnyOverlap result, expected %v, got %v", i, anyOverlap, any)
  30. }
  31. inexact := subtle.InexactOverlap(x, y)
  32. if inexact != inexactOverlap {
  33. t.Errorf("%d: wrong InexactOverlap result, expected %v, got %v", i, inexactOverlap, any)
  34. }
  35. }
  36. func TestAliasing(t *testing.T) {
  37. for i, tt := range aliasingTests {
  38. testAliasing(t, i, tt.x, tt.y, tt.anyOverlap, tt.inexactOverlap)
  39. testAliasing(t, i, tt.y, tt.x, tt.anyOverlap, tt.inexactOverlap)
  40. }
  41. }