You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.0 KiB
54 lines
1.0 KiB
package text |
|
|
|
import ( |
|
"testing" |
|
|
|
"crypto.orly/sha256" |
|
"encoders.orly/hex" |
|
"lol.mleku.dev/chk" |
|
"lukechampine.com/frand" |
|
"utils.orly" |
|
) |
|
|
|
func TestUnmarshalHexArray(t *testing.T) { |
|
var ha [][]byte |
|
h := make([]byte, sha256.Size) |
|
frand.Read(h) |
|
var dst []byte |
|
for _ = range 20 { |
|
hh := sha256.Sum256(h) |
|
h = hh[:] |
|
ha = append(ha, h) |
|
} |
|
dst = append(dst, '[') |
|
for i := range ha { |
|
dst = AppendQuote(dst, ha[i], hex.EncAppend) |
|
if i != len(ha)-1 { |
|
dst = append(dst, ',') |
|
} |
|
} |
|
dst = append(dst, ']') |
|
var ha2 [][]byte |
|
var rem []byte |
|
var err error |
|
if ha2, rem, err = UnmarshalHexArray(dst, sha256.Size); chk.E(err) { |
|
t.Fatal(err) |
|
} |
|
if len(ha2) != len(ha) { |
|
t.Fatalf( |
|
"failed to unmarshal, got %d fields, expected %d", len(ha2), |
|
len(ha), |
|
) |
|
} |
|
if len(rem) > 0 { |
|
t.Fatalf("failed to unmarshal, remnant afterwards '%s'", rem) |
|
} |
|
for i := range ha2 { |
|
if !utils.FastEqual(ha[i], ha2[i]) { |
|
t.Fatalf( |
|
"failed to unmarshal at element %d; got %x, expected %x", |
|
i, ha[i], ha2[i], |
|
) |
|
} |
|
} |
|
}
|
|
|