github.com/pkumar631/talisman@v0.3.2/detector/shannon_entropy_test.go (about)

     1  package detector
     2  
     3  import (
     4  	"testing"
     5  	"github.com/stretchr/testify/assert"
     6  )
     7  
     8  func TestEntropyCandidatesShouldBeFoundForGivenSet(t *testing.T) {
     9  	entropy := Entropy{}
    10  	dc := Base64Detector{}
    11  	dc.initBase64Map()
    12  	candidatesWithinWord := entropy.GetEntropyCandidatesWithinWord("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", 20, dc.base64Map)
    13  	assert.Equal(t, 1, len(candidatesWithinWord))
    14  }
    15  
    16  func TestEntropyCandidatesShouldBeEmptyForShorterWords(t *testing.T) {
    17  	entropy := Entropy{}
    18  	dc := Base64Detector{}
    19  	dc.initBase64Map()
    20  	candidatesWithinWord := entropy.GetEntropyCandidatesWithinWord("abc", 4, dc.base64Map)
    21  	assert.Equal(t, 0, len(candidatesWithinWord))
    22  }
    23  
    24  func TestEntropyValueOfSecretShouldBeHigherThanFour(t *testing.T) {
    25  	entropy := Entropy{}
    26  	shannonEntropy := entropy.GetShannonEntropy("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", BASE64_CHARS)
    27  	assert.True(t, 4 < shannonEntropy)
    28  }
    29  
    30  func TestEntropyValueOfEmptyStringShouldBeZero(t *testing.T) {
    31  	entropy := Entropy{}
    32  	shannonEntropy := entropy.GetShannonEntropy("", BASE64_CHARS)
    33  	assert.Equal(t, float64(0), shannonEntropy)
    34  }