Commit 0448ce13 authored by Florian Weimer's avatar Florian Weimer Committed by Adam Langley

encoding/asn1: document support for *big.Int

        Also add basic tests.

R=golang-dev
CC=golang-dev
https://golang.org/cl/5533045
parent 1379d906
...@@ -786,7 +786,8 @@ func setDefaultValue(v reflect.Value, params fieldParameters) (ok bool) { ...@@ -786,7 +786,8 @@ func setDefaultValue(v reflect.Value, params fieldParameters) (ok bool) {
// Because Unmarshal uses the reflect package, the structs // Because Unmarshal uses the reflect package, the structs
// being written to must use upper case field names. // being written to must use upper case field names.
// //
// An ASN.1 INTEGER can be written to an int, int32 or int64. // An ASN.1 INTEGER can be written to an int, int32, int64,
// or *big.Int (from the math/big package).
// If the encoded value does not fit in the Go type, // If the encoded value does not fit in the Go type,
// Unmarshal returns a parse error. // Unmarshal returns a parse error.
// //
......
...@@ -6,6 +6,7 @@ package asn1 ...@@ -6,6 +6,7 @@ package asn1
import ( import (
"bytes" "bytes"
"math/big"
"reflect" "reflect"
"testing" "testing"
"time" "time"
...@@ -351,6 +352,10 @@ type TestElementsAfterString struct { ...@@ -351,6 +352,10 @@ type TestElementsAfterString struct {
A, B int A, B int
} }
type TestBigInt struct {
X *big.Int
}
var unmarshalTestData = []struct { var unmarshalTestData = []struct {
in []byte in []byte
out interface{} out interface{}
...@@ -369,6 +374,7 @@ var unmarshalTestData = []struct { ...@@ -369,6 +374,7 @@ var unmarshalTestData = []struct {
{[]byte{0x01, 0x01, 0x00}, newBool(false)}, {[]byte{0x01, 0x01, 0x00}, newBool(false)},
{[]byte{0x01, 0x01, 0x01}, newBool(true)}, {[]byte{0x01, 0x01, 0x01}, newBool(true)},
{[]byte{0x30, 0x0b, 0x13, 0x03, 0x66, 0x6f, 0x6f, 0x02, 0x01, 0x22, 0x02, 0x01, 0x33}, &TestElementsAfterString{"foo", 0x22, 0x33}}, {[]byte{0x30, 0x0b, 0x13, 0x03, 0x66, 0x6f, 0x6f, 0x02, 0x01, 0x22, 0x02, 0x01, 0x33}, &TestElementsAfterString{"foo", 0x22, 0x33}},
{[]byte{0x30, 0x05, 0x02, 0x03, 0x12, 0x34, 0x56}, &TestBigInt{big.NewInt(0x123456)}},
} }
func TestUnmarshal(t *testing.T) { func TestUnmarshal(t *testing.T) {
......
...@@ -7,6 +7,7 @@ package asn1 ...@@ -7,6 +7,7 @@ package asn1
import ( import (
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"math/big"
"testing" "testing"
"time" "time"
) )
...@@ -20,6 +21,10 @@ type twoIntStruct struct { ...@@ -20,6 +21,10 @@ type twoIntStruct struct {
B int B int
} }
type bigIntStruct struct {
A *big.Int
}
type nestedStruct struct { type nestedStruct struct {
A intStruct A intStruct
} }
...@@ -65,6 +70,7 @@ var marshalTests = []marshalTest{ ...@@ -65,6 +70,7 @@ var marshalTests = []marshalTest{
{-128, "020180"}, {-128, "020180"},
{-129, "0202ff7f"}, {-129, "0202ff7f"},
{intStruct{64}, "3003020140"}, {intStruct{64}, "3003020140"},
{bigIntStruct{big.NewInt(0x123456)}, "30050203123456"},
{twoIntStruct{64, 65}, "3006020140020141"}, {twoIntStruct{64, 65}, "3006020140020141"},
{nestedStruct{intStruct{127}}, "3005300302017f"}, {nestedStruct{intStruct{127}}, "3005300302017f"},
{[]byte{1, 2, 3}, "0403010203"}, {[]byte{1, 2, 3}, "0403010203"},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment