2018-02-05 16:05:59 -05:00
|
|
|
package stringid // import "github.com/docker/docker/pkg/stringid"
|
2015-02-25 13:27:14 -05:00
|
|
|
|
2015-04-10 13:09:08 -04:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
2015-03-24 07:25:26 -04:00
|
|
|
|
|
|
|
func TestGenerateRandomID(t *testing.T) {
|
|
|
|
id := GenerateRandomID()
|
|
|
|
|
|
|
|
if len(id) != 64 {
|
|
|
|
t.Fatalf("Id returned is incorrect: %s", id)
|
|
|
|
}
|
|
|
|
}
|
2015-02-25 13:27:14 -05:00
|
|
|
|
2016-07-19 18:20:12 -04:00
|
|
|
func TestGenerateNonCryptoID(t *testing.T) {
|
|
|
|
id := GenerateNonCryptoID()
|
|
|
|
|
|
|
|
if len(id) != 64 {
|
|
|
|
t.Fatalf("Id returned is incorrect: %s", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 13:27:14 -05:00
|
|
|
func TestShortenId(t *testing.T) {
|
2016-07-19 18:20:12 -04:00
|
|
|
id := "90435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a2"
|
|
|
|
truncID := TruncateID(id)
|
|
|
|
if truncID != "90435eec5c4e" {
|
|
|
|
t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShortenSha256Id(t *testing.T) {
|
|
|
|
id := "sha256:4e38e38c8ce0b8d9041a9c4fefe786631d1416225e13b0bfe8cfa2321aec4bba"
|
2015-02-25 13:27:14 -05:00
|
|
|
truncID := TruncateID(id)
|
2016-07-19 18:20:12 -04:00
|
|
|
if truncID != "4e38e38c8ce0" {
|
2015-02-25 13:27:14 -05:00
|
|
|
t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShortenIdEmpty(t *testing.T) {
|
|
|
|
id := ""
|
|
|
|
truncID := TruncateID(id)
|
|
|
|
if len(truncID) > len(id) {
|
|
|
|
t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShortenIdInvalid(t *testing.T) {
|
|
|
|
id := "1234"
|
|
|
|
truncID := TruncateID(id)
|
|
|
|
if len(truncID) != len(id) {
|
|
|
|
t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
|
|
|
|
}
|
|
|
|
}
|
2015-04-10 13:09:08 -04:00
|
|
|
|
|
|
|
func TestIsShortIDNonHex(t *testing.T) {
|
|
|
|
id := "some non-hex value"
|
|
|
|
if IsShortID(id) {
|
|
|
|
t.Fatalf("%s is not a short ID", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsShortIDNotCorrectSize(t *testing.T) {
|
|
|
|
id := strings.Repeat("a", shortLen+1)
|
|
|
|
if IsShortID(id) {
|
|
|
|
t.Fatalf("%s is not a short ID", id)
|
|
|
|
}
|
|
|
|
id = strings.Repeat("a", shortLen-1)
|
|
|
|
if IsShortID(id) {
|
|
|
|
t.Fatalf("%s is not a short ID", id)
|
|
|
|
}
|
|
|
|
}
|