github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/pkg/stringid/stringid_test.go (about)

     1  package stringid // import "github.com/demonoid81/moby/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) != 64 {
    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  }