github.com/moby/docker@v26.1.3+incompatible/pkg/stringid/stringid_test.go (about)

     1  package stringid // import "github.com/docker/docker/pkg/stringid"
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  func TestGenerateRandomID(t *testing.T) {
     9  	id := GenerateRandomID()
    10  
    11  	if len(id) != fullLen {
    12  		t.Fatalf("Id returned is incorrect: %s", id)
    13  	}
    14  }
    15  
    16  func TestShortenId(t *testing.T) {
    17  	id := "90435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a2"
    18  	truncID := TruncateID(id)
    19  	if truncID != "90435eec5c4e" {
    20  		t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
    21  	}
    22  }
    23  
    24  func TestShortenSha256Id(t *testing.T) {
    25  	id := "sha256:4e38e38c8ce0b8d9041a9c4fefe786631d1416225e13b0bfe8cfa2321aec4bba"
    26  	truncID := TruncateID(id)
    27  	if truncID != "4e38e38c8ce0" {
    28  		t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
    29  	}
    30  }
    31  
    32  func TestShortenIdEmpty(t *testing.T) {
    33  	id := ""
    34  	truncID := TruncateID(id)
    35  	if len(truncID) > len(id) {
    36  		t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
    37  	}
    38  }
    39  
    40  func TestShortenIdInvalid(t *testing.T) {
    41  	id := "1234"
    42  	truncID := TruncateID(id)
    43  	if len(truncID) != len(id) {
    44  		t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
    45  	}
    46  }
    47  
    48  func TestIsShortIDNonHex(t *testing.T) {
    49  	id := "some non-hex value"
    50  	if IsShortID(id) {
    51  		t.Fatalf("%s is not a short ID", id)
    52  	}
    53  }
    54  
    55  func TestIsShortIDNotCorrectSize(t *testing.T) {
    56  	id := strings.Repeat("a", shortLen+1)
    57  	if IsShortID(id) {
    58  		t.Fatalf("%s is not a short ID", id)
    59  	}
    60  	id = strings.Repeat("a", shortLen-1)
    61  	if IsShortID(id) {
    62  		t.Fatalf("%s is not a short ID", id)
    63  	}
    64  }
    65  
    66  var testIDs = []string{
    67  	"4e38e38c8ce0",
    68  	strings.Repeat("a", shortLen+1),
    69  	strings.Repeat("a", 16000),
    70  	"90435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a2",
    71  }
    72  
    73  func BenchmarkIsShortID(b *testing.B) {
    74  	b.ReportAllocs()
    75  	for i := 0; i < b.N; i++ {
    76  		for _, id := range testIDs {
    77  			_ = IsShortID(id)
    78  		}
    79  	}
    80  }
    81  
    82  func BenchmarkValidateID(b *testing.B) {
    83  	b.ReportAllocs()
    84  	for i := 0; i < b.N; i++ {
    85  		for _, id := range testIDs {
    86  			_ = ValidateID(id)
    87  		}
    88  	}
    89  }