github.com/endophage/docker@v1.4.2-0.20161027011718-242853499895/pkg/stringid/stringid_test.go (about)

     1  package stringid
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  func TestGenerateRandomID(t *testing.T) {
     9  	id := GenerateRandomID()
    10  
    11  	if len(id) != 64 {
    12  		t.Fatalf("Id returned is incorrect: %s", id)
    13  	}
    14  }
    15  
    16  func TestGenerateNonCryptoID(t *testing.T) {
    17  	id := GenerateNonCryptoID()
    18  
    19  	if len(id) != 64 {
    20  		t.Fatalf("Id returned is incorrect: %s", id)
    21  	}
    22  }
    23  
    24  func TestShortenId(t *testing.T) {
    25  	id := "90435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a2"
    26  	truncID := TruncateID(id)
    27  	if truncID != "90435eec5c4e" {
    28  		t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
    29  	}
    30  }
    31  
    32  func TestShortenSha256Id(t *testing.T) {
    33  	id := "sha256:4e38e38c8ce0b8d9041a9c4fefe786631d1416225e13b0bfe8cfa2321aec4bba"
    34  	truncID := TruncateID(id)
    35  	if truncID != "4e38e38c8ce0" {
    36  		t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
    37  	}
    38  }
    39  
    40  func TestShortenIdEmpty(t *testing.T) {
    41  	id := ""
    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 TestShortenIdInvalid(t *testing.T) {
    49  	id := "1234"
    50  	truncID := TruncateID(id)
    51  	if len(truncID) != len(id) {
    52  		t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
    53  	}
    54  }
    55  
    56  func TestIsShortIDNonHex(t *testing.T) {
    57  	id := "some non-hex value"
    58  	if IsShortID(id) {
    59  		t.Fatalf("%s is not a short ID", id)
    60  	}
    61  }
    62  
    63  func TestIsShortIDNotCorrectSize(t *testing.T) {
    64  	id := strings.Repeat("a", shortLen+1)
    65  	if IsShortID(id) {
    66  		t.Fatalf("%s is not a short ID", id)
    67  	}
    68  	id = strings.Repeat("a", shortLen-1)
    69  	if IsShortID(id) {
    70  		t.Fatalf("%s is not a short ID", id)
    71  	}
    72  }