add serial conv. functions and basic tests

This commit is contained in:
Roland Shoemaker 2015-05-06 16:07:19 -07:00
parent b1e3c374c1
commit 771d6d9b6d
3 changed files with 35 additions and 0 deletions

View File

@ -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
}

View File

@ -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)
}

View File

@ -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)