Commit b111a1c8 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 8b45c47e
...@@ -4,6 +4,7 @@ package neo ...@@ -4,6 +4,7 @@ package neo
import ( import (
"encoding/binary" "encoding/binary"
"reflect"
"sort" "sort"
) )
...@@ -3067,93 +3068,93 @@ func (p *NotifyReady) NEODecode(data []byte) (int, error) { ...@@ -3067,93 +3068,93 @@ func (p *NotifyReady) NEODecode(data []byte) (int, error) {
} }
// registry of packet types // registry of packet types
var pktTypeRegistry = map[PktCode]reflect.Type{ var pktTypeRegistry = map[int]reflect.Type{
{0: reflect.TypeOf(&Address{})}, 0: reflect.TypeOf(Address{}),
{1: reflect.TypeOf(&NodeInfo{})}, 1: reflect.TypeOf(NodeInfo{}),
{2: reflect.TypeOf(&CellInfo{})}, 2: reflect.TypeOf(CellInfo{}),
{3: reflect.TypeOf(&RowInfo{})}, 3: reflect.TypeOf(RowInfo{}),
{4: reflect.TypeOf(&XXXTest{})}, 4: reflect.TypeOf(XXXTest{}),
{5: reflect.TypeOf(&Notify{})}, 5: reflect.TypeOf(Notify{}),
{6: reflect.TypeOf(&Error{})}, 6: reflect.TypeOf(Error{}),
{7: reflect.TypeOf(&Ping{})}, 7: reflect.TypeOf(Ping{}),
{8: reflect.TypeOf(&CloseClient{})}, 8: reflect.TypeOf(CloseClient{}),
{9: reflect.TypeOf(&RequestIdentification{})}, 9: reflect.TypeOf(RequestIdentification{}),
{10: reflect.TypeOf(&AcceptIdentification{})}, 10: reflect.TypeOf(AcceptIdentification{}),
{11: reflect.TypeOf(&PrimaryMaster{})}, 11: reflect.TypeOf(PrimaryMaster{}),
{12: reflect.TypeOf(&AnswerPrimary{})}, 12: reflect.TypeOf(AnswerPrimary{}),
{13: reflect.TypeOf(&AnnouncePrimary{})}, 13: reflect.TypeOf(AnnouncePrimary{}),
{14: reflect.TypeOf(&ReelectPrimary{})}, 14: reflect.TypeOf(ReelectPrimary{}),
{15: reflect.TypeOf(&Recovery{})}, 15: reflect.TypeOf(Recovery{}),
{16: reflect.TypeOf(&AnswerRecovery{})}, 16: reflect.TypeOf(AnswerRecovery{}),
{17: reflect.TypeOf(&LastIDs{})}, 17: reflect.TypeOf(LastIDs{}),
{18: reflect.TypeOf(&AnswerLastIDs{})}, 18: reflect.TypeOf(AnswerLastIDs{}),
{19: reflect.TypeOf(&PartitionTable{})}, 19: reflect.TypeOf(PartitionTable{}),
{20: reflect.TypeOf(&AnswerPartitionTable{})}, 20: reflect.TypeOf(AnswerPartitionTable{}),
{21: reflect.TypeOf(&NotifyPartitionTable{})}, 21: reflect.TypeOf(NotifyPartitionTable{}),
{22: reflect.TypeOf(&PartitionChanges{})}, 22: reflect.TypeOf(PartitionChanges{}),
{23: reflect.TypeOf(&StartOperation{})}, 23: reflect.TypeOf(StartOperation{}),
{24: reflect.TypeOf(&StopOperation{})}, 24: reflect.TypeOf(StopOperation{}),
{25: reflect.TypeOf(&UnfinishedTransactions{})}, 25: reflect.TypeOf(UnfinishedTransactions{}),
{26: reflect.TypeOf(&AnswerUnfinishedTransactions{})}, 26: reflect.TypeOf(AnswerUnfinishedTransactions{}),
{27: reflect.TypeOf(&LockedTransactions{})}, 27: reflect.TypeOf(LockedTransactions{}),
{28: reflect.TypeOf(&AnswerLockedTransactions{})}, 28: reflect.TypeOf(AnswerLockedTransactions{}),
{29: reflect.TypeOf(&FinalTID{})}, 29: reflect.TypeOf(FinalTID{}),
{30: reflect.TypeOf(&AnswerFinalTID{})}, 30: reflect.TypeOf(AnswerFinalTID{}),
{31: reflect.TypeOf(&ValidateTransaction{})}, 31: reflect.TypeOf(ValidateTransaction{}),
{32: reflect.TypeOf(&BeginTransaction{})}, 32: reflect.TypeOf(BeginTransaction{}),
{33: reflect.TypeOf(&AnswerBeginTransaction{})}, 33: reflect.TypeOf(AnswerBeginTransaction{}),
{34: reflect.TypeOf(&FinishTransaction{})}, 34: reflect.TypeOf(FinishTransaction{}),
{35: reflect.TypeOf(&AnswerFinishTransaction{})}, 35: reflect.TypeOf(AnswerFinishTransaction{}),
{36: reflect.TypeOf(&NotifyTransactionFinished{})}, 36: reflect.TypeOf(NotifyTransactionFinished{}),
{37: reflect.TypeOf(&LockInformation{})}, 37: reflect.TypeOf(LockInformation{}),
{38: reflect.TypeOf(&AnswerLockInformation{})}, 38: reflect.TypeOf(AnswerLockInformation{}),
{39: reflect.TypeOf(&InvalidateObjects{})}, 39: reflect.TypeOf(InvalidateObjects{}),
{40: reflect.TypeOf(&UnlockInformation{})}, 40: reflect.TypeOf(UnlockInformation{}),
{41: reflect.TypeOf(&GenerateOIDs{})}, 41: reflect.TypeOf(GenerateOIDs{}),
{42: reflect.TypeOf(&AnswerGenerateOIDs{})}, 42: reflect.TypeOf(AnswerGenerateOIDs{}),
{43: reflect.TypeOf(&StoreObject{})}, 43: reflect.TypeOf(StoreObject{}),
{44: reflect.TypeOf(&AnswerStoreObject{})}, 44: reflect.TypeOf(AnswerStoreObject{}),
{45: reflect.TypeOf(&AbortTransaction{})}, 45: reflect.TypeOf(AbortTransaction{}),
{46: reflect.TypeOf(&StoreTransaction{})}, 46: reflect.TypeOf(StoreTransaction{}),
{47: reflect.TypeOf(&VoteTransaction{})}, 47: reflect.TypeOf(VoteTransaction{}),
{48: reflect.TypeOf(&GetObject{})}, 48: reflect.TypeOf(GetObject{}),
{49: reflect.TypeOf(&AnswerGetObject{})}, 49: reflect.TypeOf(AnswerGetObject{}),
{50: reflect.TypeOf(&TIDList{})}, 50: reflect.TypeOf(TIDList{}),
{51: reflect.TypeOf(&AnswerTIDList{})}, 51: reflect.TypeOf(AnswerTIDList{}),
{52: reflect.TypeOf(&TIDListFrom{})}, 52: reflect.TypeOf(TIDListFrom{}),
{53: reflect.TypeOf(&AnswerTIDListFrom{})}, 53: reflect.TypeOf(AnswerTIDListFrom{}),
{54: reflect.TypeOf(&TransactionInformation{})}, 54: reflect.TypeOf(TransactionInformation{}),
{55: reflect.TypeOf(&AnswerTransactionInformation{})}, 55: reflect.TypeOf(AnswerTransactionInformation{}),
{56: reflect.TypeOf(&ObjectHistory{})}, 56: reflect.TypeOf(ObjectHistory{}),
{57: reflect.TypeOf(&AnswerObjectHistory{})}, 57: reflect.TypeOf(AnswerObjectHistory{}),
{58: reflect.TypeOf(&PartitionList{})}, 58: reflect.TypeOf(PartitionList{}),
{59: reflect.TypeOf(&AnswerPartitionList{})}, 59: reflect.TypeOf(AnswerPartitionList{}),
{60: reflect.TypeOf(&X_NodeList{})}, 60: reflect.TypeOf(X_NodeList{}),
{61: reflect.TypeOf(&AnswerNodeList{})}, 61: reflect.TypeOf(AnswerNodeList{}),
{62: reflect.TypeOf(&SetNodeState{})}, 62: reflect.TypeOf(SetNodeState{}),
{63: reflect.TypeOf(&AddPendingNodes{})}, 63: reflect.TypeOf(AddPendingNodes{}),
{64: reflect.TypeOf(&TweakPartitionTable{})}, 64: reflect.TypeOf(TweakPartitionTable{}),
{65: reflect.TypeOf(&NotifyNodeInformation{})}, 65: reflect.TypeOf(NotifyNodeInformation{}),
{66: reflect.TypeOf(&NodeInformation{})}, 66: reflect.TypeOf(NodeInformation{}),
{67: reflect.TypeOf(&SetClusterState{})}, 67: reflect.TypeOf(SetClusterState{}),
{68: reflect.TypeOf(&ClusterInformation{})}, 68: reflect.TypeOf(ClusterInformation{}),
{69: reflect.TypeOf(&X_ClusterState{})}, 69: reflect.TypeOf(X_ClusterState{}),
{70: reflect.TypeOf(&ObjectUndoSerial{})}, 70: reflect.TypeOf(ObjectUndoSerial{}),
{71: reflect.TypeOf(&AnswerObjectUndoSerial{})}, 71: reflect.TypeOf(AnswerObjectUndoSerial{}),
{72: reflect.TypeOf(&HasLock{})}, 72: reflect.TypeOf(HasLock{}),
{73: reflect.TypeOf(&AnswerHasLock{})}, 73: reflect.TypeOf(AnswerHasLock{}),
{74: reflect.TypeOf(&CheckCurrentSerial{})}, 74: reflect.TypeOf(CheckCurrentSerial{}),
{75: reflect.TypeOf(&AnswerCheckCurrentSerial{})}, 75: reflect.TypeOf(AnswerCheckCurrentSerial{}),
{76: reflect.TypeOf(&Pack{})}, 76: reflect.TypeOf(Pack{}),
{77: reflect.TypeOf(&AnswerPack{})}, 77: reflect.TypeOf(AnswerPack{}),
{78: reflect.TypeOf(&CheckReplicas{})}, 78: reflect.TypeOf(CheckReplicas{}),
{79: reflect.TypeOf(&CheckPartition{})}, 79: reflect.TypeOf(CheckPartition{}),
{80: reflect.TypeOf(&CheckTIDRange{})}, 80: reflect.TypeOf(CheckTIDRange{}),
{81: reflect.TypeOf(&AnswerCheckTIDRange{})}, 81: reflect.TypeOf(AnswerCheckTIDRange{}),
{82: reflect.TypeOf(&CheckSerialRange{})}, 82: reflect.TypeOf(CheckSerialRange{}),
{83: reflect.TypeOf(&AnswerCheckSerialRange{})}, 83: reflect.TypeOf(AnswerCheckSerialRange{}),
{84: reflect.TypeOf(&PartitionCorrupted{})}, 84: reflect.TypeOf(PartitionCorrupted{}),
{85: reflect.TypeOf(&LastTransaction{})}, 85: reflect.TypeOf(LastTransaction{}),
{86: reflect.TypeOf(&AnswerLastTransaction{})}, 86: reflect.TypeOf(AnswerLastTransaction{}),
{87: reflect.TypeOf(&NotifyReady{})}, 87: reflect.TypeOf(NotifyReady{}),
} }
...@@ -68,10 +68,6 @@ type NEOCodec interface { ...@@ -68,10 +68,6 @@ type NEOCodec interface {
NEODecoder NEODecoder
} }
// TODO for _all_ packet types:
// zero-value -> encodedlen -> [encodedlen]byte -> decode (= ok) + check for overflow
// zero-value.encode([<encodedlen]byte) -> panic
// test marshalling for one packet type // test marshalling for one packet type
func testPktMarshal(t *testing.T, pkt NEOCodec, encoded string) { func testPktMarshal(t *testing.T, pkt NEOCodec, encoded string) {
typ := reflect.TypeOf(pkt).Elem() // type of *pkt typ := reflect.TypeOf(pkt).Elem() // type of *pkt
...@@ -258,3 +254,19 @@ func TestPktMarshal(t *testing.T) { ...@@ -258,3 +254,19 @@ func TestPktMarshal(t *testing.T) {
testPktMarshal(t, tt.pkt, tt.encoded) testPktMarshal(t, tt.pkt, tt.encoded)
} }
} }
// XXX for _all_ packet types:
// zero-value -> encodedlen -> [encodedlen]byte -> decode (= ok) + check for overflow
// zero-value.encode([<encodedlen]byte) -> panic
//
// same as testPktMarshal but zero-values of _all_ packet types
// (additional light checks for encode / decode overflow behaviour on all types)
func TestPktMarshalAllOverflowLightly(t *testing.T) {
for _, typ := range pktTypeRegistry {
// zero-value for a type
pkt := reflect.New(typ).Interface().(NEOCodec)
l := pkt.NEOEncodedLen()
encoded := strings.Repeat("\x00", l)
testPktMarshal(t, pkt, encoded)
}
}
...@@ -132,6 +132,7 @@ func main() { ...@@ -132,6 +132,7 @@ func main() {
package neo package neo
import ( import (
"encoding/binary" "encoding/binary"
"reflect"
"sort" "sort"
) )
`) `)
...@@ -176,7 +177,7 @@ import ( ...@@ -176,7 +177,7 @@ import (
// now packet types registry // now packet types registry
buf.emit("\n// registry of packet types") buf.emit("\n// registry of packet types")
buf.emit("var pktTypeRegistry = map[PktCode]reflect.Type {") buf.emit("var pktTypeRegistry = map[int]reflect.Type {") // XXX key -> PktCode ?
// ordered by pktCode // ordered by pktCode
pktCodeV := []int{} pktCodeV := []int{}
...@@ -186,7 +187,7 @@ import ( ...@@ -186,7 +187,7 @@ import (
sort.Ints(pktCodeV) sort.Ints(pktCodeV)
for _, pktCode := range pktCodeV { for _, pktCode := range pktCodeV {
buf.emit("{%v: reflect.TypeOf(&%v{})},", pktCode, pktTypeRegistry[pktCode]) buf.emit("%v: reflect.TypeOf(%v{}),", pktCode, pktTypeRegistry[pktCode])
} }
buf.emit("}") buf.emit("}")
......
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