This repository has been archived by the owner on Dec 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FAB-11081 check marshaled json for eth RPC fields
- explicitly list the fields and check for existence of exactly that list Change-Id: I2ef4ed0c9ea97b669aff2c7440e13e5a3701326c Signed-off-by: Morgan Bauer <mbauer@us.ibm.com>
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package fabproxy_test | ||
|
||
import ( | ||
"encoding/json" | ||
"reflect" | ||
|
||
"github.com/hyperledger/fabric-chaincode-evm/fabproxy" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("ethereum json rpc struct fields", func() { | ||
By("that match case exactly, in camelCase starting with a lowercase letter") | ||
assertTypeMarshalsJSONFields := func(fieldNames []string, itype interface{}) { | ||
b, err := json.Marshal(itype) | ||
Expect(err).ToNot(HaveOccurred()) | ||
m := make(map[string]interface{}) | ||
err = json.Unmarshal(b, &m) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(m).To(HaveLen(len(fieldNames)), "did you add a field to this type? %q", reflect.TypeOf(itype)) | ||
for _, fieldName := range fieldNames { | ||
_, ok := m[fieldName] | ||
Expect(ok).To(BeTrue(), "couldn't find an expected field %q in this type", fieldName) | ||
} | ||
} | ||
|
||
It("for TxReceipt with the proper cases", func() { | ||
fieldNames := []string{"transactionHash", "transactionIndex", | ||
"blockHash", "blockNumber", "contractAddress", "gasUsed", | ||
"cumulativeGasUsed", "to", "status", "logs"} | ||
assertTypeMarshalsJSONFields(fieldNames, fabproxy.TxReceipt{}) | ||
}) | ||
It("for Log subobjects in TxReceipt with the proper cases", func() { | ||
fieldNames := []string{"address", "topics", "data", "blockNumber", | ||
"transactionHash", "transactionIndex", "blockHash", "logIndex"} | ||
assertTypeMarshalsJSONFields(fieldNames, fabproxy.Log{}) | ||
}) | ||
It("for Transaction with the proper cases", func() { | ||
fieldNames := []string{"blockHash", "blockNumber", "to", "input", "transactionIndex", "hash"} | ||
assertTypeMarshalsJSONFields(fieldNames, fabproxy.Transaction{}) | ||
}) | ||
It("for Block with the proper cases", func() { | ||
fieldNames := []string{"number", "hash", "parentHash", "transactions"} | ||
assertTypeMarshalsJSONFields(fieldNames, fabproxy.Block{}) | ||
}) | ||
}) |