github.com/insolar/x-crypto@v0.0.0-20191031140942-75fab8a325f6/internal/subtle/aliasing_test.go (about)

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