github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/swarmkit/identity/randomid_test.go (about)

     1  package identity
     2  
     3  import (
     4  	"math/big"
     5  	"math/rand"
     6  	"testing"
     7  )
     8  
     9  func TestGenerateGUID(t *testing.T) {
    10  	idReader = rand.New(rand.NewSource(0))
    11  
    12  	for i := 0; i < 1000; i++ {
    13  		guid := NewID()
    14  
    15  		var i big.Int
    16  		_, ok := i.SetString(guid, randomIDBase)
    17  		if !ok {
    18  			t.Fatal("id should be base 36", i, guid)
    19  		}
    20  
    21  		// To ensure that all identifiers are fixed length, we make sure they
    22  		// get padded out to 25 characters, which is the maximum for the base36
    23  		// representation of 128-bit identifiers.
    24  		//
    25  		// For academics,  f5lxx1zz5pnorynqglhzmsp33  == 2^128 - 1. This value
    26  		// was calculated from floor(log(2^128-1, 36)) + 1.
    27  		//
    28  		// See http://mathworld.wolfram.com/NumberLength.html for more information.
    29  		if len(guid) != maxRandomIDLength {
    30  			t.Fatalf("len(%s) != %v", guid, maxRandomIDLength)
    31  		}
    32  	}
    33  }