github.com/FISCO-BCOS/crypto@v0.0.0-20200202032121-bd8ab0b5d4f1/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  	"testing"
     9  
    10  	"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  	{a[:10], a[:10:20], true, false},
    31  	{a[:10], a[5:10:20], true, true},
    32  }
    33  
    34  func testAliasing(t *testing.T, i int, x, y []byte, anyOverlap, inexactOverlap bool) {
    35  	any := subtle.AnyOverlap(x, y)
    36  	if any != anyOverlap {
    37  		t.Errorf("%d: wrong AnyOverlap result, expected %v, got %v", i, anyOverlap, any)
    38  	}
    39  	inexact := subtle.InexactOverlap(x, y)
    40  	if inexact != inexactOverlap {
    41  		t.Errorf("%d: wrong InexactOverlap result, expected %v, got %v", i, inexactOverlap, any)
    42  	}
    43  }
    44  
    45  func TestAliasing(t *testing.T) {
    46  	for i, tt := range aliasingTests {
    47  		testAliasing(t, i, tt.x, tt.y, tt.anyOverlap, tt.inexactOverlap)
    48  		testAliasing(t, i, tt.y, tt.x, tt.anyOverlap, tt.inexactOverlap)
    49  	}
    50  }