Files
filou/contract/public_contract_test.go
2014-11-09 13:18:32 -08:00

77 lines
1.8 KiB
Go

package contract
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"testing"
"github.com/didiercrunch/filou/helper"
"github.com/didiercrunch/filou/keys"
)
func assertJSONMarshalling(c *PublicContract) error {
ret := new(PublicContract)
if data, err := json.Marshal(c); err != nil {
return err
} else if err = json.Unmarshal(data, ret); err != nil {
return err
} else if !reflect.DeepEqual(ret, c) {
fmt.Println(string(data))
return errors.New("bad serisalization")
}
return nil
}
func expectJson(c *PublicContract, jsonData string) error {
ret := new(PublicContract)
if err := json.Unmarshal([]byte(jsonData), ret); err != nil {
return err
} else if !reflect.DeepEqual(ret.RSAPublicKey, c.RSAPublicKey) {
return errors.New("bad serisalization")
}
return nil
}
func TestSimpleJsonification(t *testing.T) {
c := &PublicContract{
AcceptedHashes: []string{"sha246"},
AcceptedBlockCyphers: []string{"aes"},
AcceptedAsynchronousEncryptionScheme: []string{"rsa_oaep"},
AcceptedSignatureScheme: []string{"rsa_pss"},
RSAPublicKey: []*keys.RSAPublicKey{&keys.RSAPublicKey{helper.B(12), 3}},
}
if err := assertJSONMarshalling(c); err != nil {
t.Error(err)
}
}
func TestEmptyRSAPublicKey(t *testing.T) {
c := &PublicContract{
AcceptedHashes: []string{"sha246"},
AcceptedBlockCyphers: []string{"aes"},
AcceptedAsynchronousEncryptionScheme: []string{"rsa_oaep"},
AcceptedSignatureScheme: []string{"rsa_pss"},
RSAPublicKey: []*keys.RSAPublicKey{&keys.RSAPublicKey{helper.B(12), 3}},
}
jsonData := `{
"accepted_hashes": ["sha246"],
"accepted_block_cypher": ["aes"],
"accepted_asynchronous_encryption_scheme": ["rsa_oaep"],
"accepted_signature_scheme": ["rsa_pss"],
"rsa_public_key": [{"n": "c", "e": 3}]
}`
if err := expectJson(c, jsonData); err != nil {
t.Error(err)
}
}