From 771d6d9b6d8eb16d1fdb626b32f4829bb2afb800 Mon Sep 17 00:00:00 2001 From: Roland Shoemaker Date: Wed, 6 May 2015 16:07:19 -0700 Subject: [PATCH] add serial conv. functions and basic tests --- core/util.go | 14 ++++++++++++++ core/util_test.go | 14 ++++++++++++++ test/test-tools.go | 7 +++++++ 3 files changed, 35 insertions(+) diff --git a/core/util.go b/core/util.go index 51ec48a0f..2f7eb6825 100644 --- a/core/util.go +++ b/core/util.go @@ -18,6 +18,7 @@ import ( "encoding/base64" "encoding/json" "errors" + "fmt" blog "github.com/letsencrypt/boulder/log" "hash" "io" @@ -188,3 +189,16 @@ func VerifyCSR(csr *x509.CertificateRequest) error { return errors.New("Unsupported CSR signing algorithm") } + +func SerialToString(serial *big.Int) string { + return fmt.Sprintf("%032x", serial) +} + +func StringToSerial(serial string) (*big.Int, error) { + var serialNum big.Int + if len(serial) != 32 { + return &serialNum, errors.New("Serial number should be 32 characters long") + } + _, err := fmt.Sscanf(serial, "%032x", &serialNum) + return &serialNum, err +} diff --git a/core/util_test.go b/core/util_test.go index 91d37bbca..95470687c 100644 --- a/core/util_test.go +++ b/core/util_test.go @@ -10,6 +10,7 @@ import ( "fmt" "github.com/letsencrypt/boulder/test" "math" + "math/big" ) // challenges.go @@ -34,3 +35,16 @@ func TestRandString(t *testing.T) { // This is covered by NewToken return } + +func TestSerialUtils(t *testing.T) { + serial := SerialToString(big.NewInt(100000000000000000)) + test.AssertEquals(t, serial, "0000000000000000016345785d8a0000") + + serialNum, err := StringToSerial("0000000000000000016345785d8a0000") + test.AssertNotError(t, err, "Couldn't convert serial number to *big.Int") + test.AssertBigIntEquals(t, serialNum, big.NewInt(100000000000000000)) + + badSerial, err := StringToSerial("doop!!!!000") + test.AssertEquals(t, fmt.Sprintf("%v", err), "Serial number should be 32 characters long") + fmt.Println(badSerial) +} diff --git a/test/test-tools.go b/test/test-tools.go index ccba62c8c..45d78791f 100644 --- a/test/test-tools.go +++ b/test/test-tools.go @@ -9,6 +9,7 @@ import ( "bytes" "encoding/base64" "fmt" + "math/big" "runtime" "strings" "testing" @@ -74,6 +75,12 @@ func AssertIntEquals(t *testing.T, one int, two int) { } } +func AssertBigIntEquals(t *testing.T, one *big.Int, two *big.Int) { + if one.Cmp(two) != 0 { + t.Errorf("%s Int [%d] != [%d]", caller(), one, two) + } +} + func AssertContains(t *testing.T, haystack string, needle string) { if !strings.Contains(haystack, needle) { t.Errorf("%s String [%s] does not contain [%s]", caller(), haystack, needle)