add serial conv. functions and basic tests
This commit is contained in:
parent
b1e3c374c1
commit
771d6d9b6d
14
core/util.go
14
core/util.go
|
|
@ -18,6 +18,7 @@ import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
blog "github.com/letsencrypt/boulder/log"
|
blog "github.com/letsencrypt/boulder/log"
|
||||||
"hash"
|
"hash"
|
||||||
"io"
|
"io"
|
||||||
|
|
@ -188,3 +189,16 @@ func VerifyCSR(csr *x509.CertificateRequest) error {
|
||||||
|
|
||||||
return errors.New("Unsupported CSR signing algorithm")
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/letsencrypt/boulder/test"
|
"github.com/letsencrypt/boulder/test"
|
||||||
"math"
|
"math"
|
||||||
|
"math/big"
|
||||||
)
|
)
|
||||||
|
|
||||||
// challenges.go
|
// challenges.go
|
||||||
|
|
@ -34,3 +35,16 @@ func TestRandString(t *testing.T) {
|
||||||
// This is covered by NewToken
|
// This is covered by NewToken
|
||||||
return
|
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)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/big"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"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) {
|
func AssertContains(t *testing.T, haystack string, needle string) {
|
||||||
if !strings.Contains(haystack, needle) {
|
if !strings.Contains(haystack, needle) {
|
||||||
t.Errorf("%s String [%s] does not contain [%s]", caller(), haystack, needle)
|
t.Errorf("%s String [%s] does not contain [%s]", caller(), haystack, needle)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue