func TestAuthoritySignature(t *testing.T) { s := testHelper.CreateAndPopulateTestState() idindex := s.CreateBlankFactomIdentity(primitives.NewZeroHash()) s.Identities[idindex].ManagementChainID = primitives.NewZeroHash() index := s.AddAuthorityFromChainID(primitives.NewZeroHash()) s.Authorities[index].SigningKey = *(s.GetServerPublicKey()) s.Authorities[index].Status = 1 ack := new(messages.Ack) ack.DBHeight = s.LLeaderHeight ack.VMIndex = 1 ack.Minute = byte(5) ack.Timestamp = s.GetTimestamp() ack.MessageHash = primitives.NewZeroHash() ack.LeaderChainID = s.IdentityChainID ack.SerialHash = primitives.NewZeroHash() err := ack.Sign(s) if err != nil { t.Error("Authority Test Failed when signing message") } msg, err := ack.MarshalForSignature() if err != nil { t.Error("Authority Test Failed when marshalling for sig") } sig := ack.GetSignature() server, err := s.Authorities[0].VerifySignature(msg, sig.GetSignature()) if !server || err != nil { t.Error("Authority Test Failed when checking sigs") } }
func TestV2HandleEntryCreditBalance(t *testing.T) { state := testHelper.CreateAndPopulateTestState() eckey := testHelper.NewECAddressPublicKeyString(0) req := new(AddressRequest) req.Address = eckey resp, err := HandleV2EntryCreditBalance(state, req) if err != nil { t.Errorf("%v", err) } var expectedAmount int64 = 2000 if resp.(*EntryCreditBalanceResponse).Balance != expectedAmount { t.Errorf("Invalid balance returned - %v vs %v", resp.(*EntryCreditBalanceResponse).Balance, expectedAmount) } eckey = testHelper.NewECAddressString(0) req = new(AddressRequest) req.Address = eckey resp, err = HandleV2EntryCreditBalance(state, req) if err != nil { t.Errorf("%v", err) } if resp.(*EntryCreditBalanceResponse).Balance != expectedAmount { t.Errorf("Invalid balance returned - %v vs %v", resp.(*EntryCreditBalanceResponse).Balance, expectedAmount) } }
func TestHandleV2GetReceipt(t *testing.T) { state := testHelper.CreateAndPopulateTestState() //Start(state) hashkey := new(HashRequest) hashkey.Hash = "be5fb8c3ba92c0436269fab394ff7277c67e9b2de4431b723ce5d89799c0b93a" resp, jErr := HandleV2Receipt(state, hashkey) if jErr != nil { t.Errorf("%v", jErr) return } dbo := state.GetAndLockDB() defer state.UnlockDB() marshalled, err := json.Marshal(resp.(*ReceiptResponse).Receipt) if err != nil { t.Error(err) } t.Logf("Resp - %s", marshalled) err = receipts.VerifyFullReceipt(dbo, string(marshalled)) if err != nil { t.Logf("receipt - %s", marshalled) t.Error(err) } }
func TestIdentity(t *testing.T) { s := testHelper.CreateAndPopulateTestState() index := s.CreateBlankFactomIdentity(primitives.NewZeroHash()) if len(s.Identities) == 0 || index != 0 { t.Errorf("Failed making blank identity") } }
func TestDBStateMsgValidate(t *testing.T) { state := testHelper.CreateAndPopulateTestState() msg := new(DBStateMsg) if msg.Validate(state) >= 0 { t.Errorf("Empty DBState validated") } msg = newDBStateMsg() msg.DirectoryBlock.GetHeader().SetNetworkID(0x00) if msg.Validate(state) >= 0 { t.Errorf("Wrong network ID validated") } msg = newDBStateMsg() msg.DirectoryBlock.GetHeader().SetDBHeight(state.GetHighestCompletedBlock() + 1) constants.CheckPoints[state.GetHighestCompletedBlock()+1] = "123" if msg.Validate(state) >= 0 { t.Errorf("Wrong checkpoint validated") } delete(constants.CheckPoints, state.GetHighestCompletedBlock()+1) msg = newDBStateMsg() msg.DirectoryBlock.GetHeader().SetDBHeight(state.GetHighestCompletedBlock() + 1) if msg.Validate(state) <= 0 { t.Errorf("Proper block not validated!") } }
func TestGetDirectoryBlockByHeight(t *testing.T) { state := testHelper.CreateAndPopulateTestState() blocks := testHelper.CreateFullTestBlockSet() for i, block := range blocks { dBlock := state.GetDirectoryBlockByHeight(uint32(i)) if dBlock.GetKeyMR().IsSameAs(block.DBlock.GetKeyMR()) == false { t.Errorf("DBlocks are not the same at height %v", i+1) continue } if dBlock.GetFullHash().IsSameAs(block.DBlock.GetFullHash()) == false { t.Errorf("DBlocks are not the same at height %v", i+1) continue } } }
func TestServerFaultUpdateState(t *testing.T) { sigs := 10 sf := new(ServerFault) sf.Timestamp = primitives.NewTimestampNow() sf.ServerID = testHelper.NewRepeatingHash(1) sf.AuditServerID = testHelper.NewRepeatingHash(2) sf.VMIndex = 0x33 sf.DBHeight = 0x44556677 sf.Height = 0x88990011 core, err := sf.MarshalCore() if err != nil { t.Errorf("%v", err) } for i := 0; i < sigs; i++ { priv := testHelper.NewPrimitivesPrivateKey(uint64(i)) sig := priv.Sign(core) sf.SignatureList.List = append(sf.SignatureList.List, sig) } sf.SignatureList.Length = uint32(len(sf.SignatureList.List)) s := testHelper.CreateAndPopulateTestState() idindex := s.CreateBlankFactomIdentity(primitives.NewZeroHash()) s.Identities[idindex].ManagementChainID = primitives.NewZeroHash() for i := 0; i < sigs; i++ { //Federated Server index := s.AddAuthorityFromChainID(testHelper.NewRepeatingHash(byte(i))) s.Authorities[index].SigningKey = *testHelper.NewPrimitivesPrivateKey(uint64(i)).Pub s.Authorities[index].Status = 1 s.AddFedServer(s.GetLeaderHeight(), testHelper.NewRepeatingHash(byte(i))) //Audit Server index = s.AddAuthorityFromChainID(testHelper.NewRepeatingHash(byte(i + sigs))) s.Authorities[index].SigningKey = *testHelper.NewPrimitivesPrivateKey(uint64(i + sigs)).Pub s.Authorities[index].Status = 0 s.AddFedServer(s.GetLeaderHeight(), testHelper.NewRepeatingHash(byte(i+sigs))) } err = sf.UpdateState(s) if err != nil { t.Errorf("%v", err) } }
func TestV2HandleFactoidBalance(t *testing.T) { state := testHelper.CreateAndPopulateTestState() eckey := testHelper.NewFactoidRCDAddressString(0) req := new(AddressRequest) req.Address = eckey resp, err := HandleV2FactoidBalance(state, req) if err != nil { t.Errorf("%v", err) } var expectedAmount int64 = 999889000 if resp.(*FactoidBalanceResponse).Balance != expectedAmount { t.Errorf("Invalid balance returned - %v vs %v", resp.(*FactoidBalanceResponse).Balance, expectedAmount) } }
func TestDirBlockHead(t *testing.T) { state := testHelper.CreateAndPopulateTestState() height := state.GetHighestCompletedBlock() if height != 9 { t.Errorf("Invalid DBLock Height - got %v, expected 10", height+1) } d := state.GetDirectoryBlockByHeight(height) //fmt.Println(d) //fmt.Println("------------") //fmt.Println(d.String()) //data, _ := d.MarshalBinary() //fmt.Printf("%x\n", data) //fmt.Printf("nwtwork number %d\n", state.NetworkNumber) //fmt.Printf("network id %x\n", d.GetHeader().GetNetworkID()) //fmt.Printf("network id %x\n", d.GetHeader.GetBodyMR) if d.GetKeyMR().String() != "12d6c012e3598ca1c10dbf60ac12af9fa8904b8fd98968e86f4c66c14884c225" { t.Errorf("Invalid DBLock KeyMR - got %v, expected 12d6c012e3598ca1c10dbf60ac12af9fa8904b8fd98968e86f4c66c14884c225", d.GetKeyMR().String()) } }
func TestHandleV2GetTranasction(t *testing.T) { state := testHelper.CreateAndPopulateTestState() blocks := testHelper.CreateFullTestBlockSet() for _, block := range blocks { for _, tx := range block.FBlock.GetTransactions() { hashkey := new(HashRequest) hashkey.Hash = tx.GetFullHash().String() resp, jErr := HandleV2GetTranasction(state, hashkey) if jErr != nil { t.Errorf("%v", jErr) return } r := resp.(*TransactionResponse) if r.ECTranasction != nil { t.Errorf("ECTranasction != nil") } if r.Entry != nil { t.Errorf("Entry != nil") } if r.FactoidTransaction.GetFullHash().String() != hashkey.Hash { t.Errorf("Got wrong hash for FactoidTransaction") } if r.IncludedInTransactionBlock != block.FBlock.DatabasePrimaryIndex().String() { t.Errorf("Invalid IncludedInTransactionBlock") } if r.IncludedInDirectoryBlock != block.DBlock.DatabasePrimaryIndex().String() { t.Errorf("Invalid IncludedInDirectoryBlock") } if r.IncludedInDirectoryBlockHeight != int64(block.DBlock.GetDatabaseHeight()) { t.Errorf("Invalid IncludedInDirectoryBlockHeight") } } for _, h := range block.ECBlock.GetEntryHashes() { hashkey := new(HashRequest) hashkey.Hash = h.String() resp, jErr := HandleV2GetTranasction(state, hashkey) if jErr != nil { t.Errorf("%v", jErr) return } r := resp.(*TransactionResponse) if r.FactoidTransaction != nil { t.Errorf("FactoidTransaction != nil") } if r.Entry != nil { t.Errorf("Entry != nil") } if r.ECTranasction.Hash().String() != hashkey.Hash { t.Errorf("Got wrong hash for ECTranasction") } if r.IncludedInTransactionBlock != block.ECBlock.DatabasePrimaryIndex().String() { t.Errorf("Invalid IncludedInTransactionBlock") } if r.IncludedInDirectoryBlock != block.DBlock.DatabasePrimaryIndex().String() { t.Errorf("Invalid IncludedInDirectoryBlock") } if r.IncludedInDirectoryBlockHeight != int64(block.DBlock.GetDatabaseHeight()) { t.Errorf("Invalid IncludedInDirectoryBlockHeight") } } for _, tx := range block.EBlock.GetEntryHashes() { hashkey := new(HashRequest) hashkey.Hash = tx.String() resp, jErr := HandleV2GetTranasction(state, hashkey) if jErr != nil { t.Errorf("%v", jErr) return } r := resp.(*TransactionResponse) if r.ECTranasction != nil { t.Errorf("ECTranasction != nil") } if r.FactoidTransaction != nil { t.Errorf("FactoidTransaction != nil") } if r.Entry.GetHash().String() != hashkey.Hash { t.Errorf("Got wrong hash for Entry") } if r.IncludedInTransactionBlock != block.EBlock.DatabasePrimaryIndex().String() { t.Errorf("Invalid IncludedInTransactionBlock") } if r.IncludedInDirectoryBlock != block.DBlock.DatabasePrimaryIndex().String() { t.Errorf("Invalid IncludedInDirectoryBlock") } if r.IncludedInDirectoryBlockHeight != int64(block.DBlock.GetDatabaseHeight()) { t.Errorf("Invalid IncludedInDirectoryBlockHeight") } } for _, tx := range block.AnchorEBlock.GetEntryHashes() { hashkey := new(HashRequest) hashkey.Hash = tx.String() resp, jErr := HandleV2GetTranasction(state, hashkey) if jErr != nil { t.Errorf("%v", jErr) return } r := resp.(*TransactionResponse) if r.ECTranasction != nil { t.Errorf("ECTranasction != nil") } if r.FactoidTransaction != nil { t.Errorf("FactoidTransaction != nil") } if r.Entry.GetHash().String() != hashkey.Hash { t.Errorf("Got wrong hash for Entry") } if r.IncludedInTransactionBlock != block.AnchorEBlock.DatabasePrimaryIndex().String() { t.Errorf("Invalid IncludedInTransactionBlock") } if r.IncludedInDirectoryBlock != block.DBlock.DatabasePrimaryIndex().String() { t.Errorf("Invalid IncludedInDirectoryBlock") } if r.IncludedInDirectoryBlockHeight != int64(block.DBlock.GetDatabaseHeight()) { t.Errorf("Invalid IncludedInDirectoryBlockHeight") } } } }
func TestHandleV2GetRaw(t *testing.T) { type RawData struct { Hash1 string Hash2 string Raw string } toTest := []RawData{} var err error blockSet := testHelper.CreateTestBlockSet(nil) aBlock := blockSet.ABlock raw := RawData{} raw.Hash1 = aBlock.DatabasePrimaryIndex().String() raw.Hash2 = aBlock.DatabaseSecondaryIndex().String() hex, err := aBlock.MarshalBinary() if err != nil { panic(err) } raw.Raw = primitives.EncodeBinary(hex) toTest = append(toTest, raw) //1 eBlock := blockSet.EBlock raw = RawData{} raw.Hash1 = eBlock.DatabasePrimaryIndex().String() raw.Hash2 = eBlock.DatabaseSecondaryIndex().String() hex, err = eBlock.MarshalBinary() if err != nil { panic(err) } raw.Raw = primitives.EncodeBinary(hex) toTest = append(toTest, raw) //2 ecBlock := blockSet.ECBlock raw = RawData{} raw.Hash1 = ecBlock.(interfaces.DatabaseBatchable).DatabasePrimaryIndex().String() raw.Hash2 = ecBlock.(interfaces.DatabaseBatchable).DatabaseSecondaryIndex().String() hex, err = ecBlock.MarshalBinary() if err != nil { panic(err) } raw.Raw = primitives.EncodeBinary(hex) toTest = append(toTest, raw) //3 fBlock := blockSet.FBlock raw = RawData{} raw.Hash1 = fBlock.(interfaces.DatabaseBatchable).DatabasePrimaryIndex().String() raw.Hash2 = fBlock.(interfaces.DatabaseBatchable).DatabaseSecondaryIndex().String() hex, err = fBlock.MarshalBinary() if err != nil { panic(err) } raw.Raw = primitives.EncodeBinary(hex) toTest = append(toTest, raw) //4 dBlock := blockSet.DBlock raw = RawData{} raw.Hash1 = dBlock.DatabasePrimaryIndex().String() raw.Hash2 = dBlock.DatabaseSecondaryIndex().String() hex, err = dBlock.MarshalBinary() if err != nil { panic(err) } raw.Raw = primitives.EncodeBinary(hex) toTest = append(toTest, raw) //5 //initializing server state := testHelper.CreateAndPopulateTestState() Start(state) for i, v := range toTest { data := new(HashRequest) data.Hash = v.Hash1 req := primitives.NewJSON2Request("raw-data", 1, data) resp, err := v2Request(req) if err != nil { t.Errorf("%v", err) } if strings.Contains(resp.String(), v.Raw) == false { t.Errorf("Looking for %v", v.Hash1) t.Errorf("GetRaw %v/%v from Hash1 failed - %v", i, len(toTest), resp.String()) } data.Hash = v.Hash2 req = primitives.NewJSON2Request("raw-data", 1, data) resp, err = v2Request(req) if err != nil { t.Errorf("%v", err) } if strings.Contains(resp.String(), v.Raw) == false { t.Errorf("Looking for %v", v.Hash1) t.Errorf("GetRaw %v/%v from Hash2 failed - %v", i, len(toTest), resp.String()) } } }
func TestHandleV2FactoidACK(t *testing.T) { state := testHelper.CreateAndPopulateTestState() blocks := testHelper.CreateFullTestBlockSet() for _, block := range blocks { for _, tx := range block.FBlock.GetTransactions() { req := AckRequest{} txID := tx.GetSigHash().String() req.TxID = txID r, jError := HandleV2FactoidACK(state, req) if jError != nil { t.Errorf("%v", jError) continue } resp, ok := r.(*FactoidTxStatus) if ok == false { t.Error("Invalid response type returned") continue } if resp.TxID != txID { t.Error("Invalid TxID returned") } if resp.Status != AckStatusDBlockConfirmed { t.Error("Invalid status returned") } req = AckRequest{} h, err := tx.MarshalBinary() if err != nil { t.Errorf("%v", err) continue } req.FullTransaction = hex.EncodeToString(h) r, jError = HandleV2FactoidACK(state, req) if jError != nil { t.Errorf("%v", jError) continue } resp, ok = r.(*FactoidTxStatus) if ok == false { t.Error("Invalid response type returned") continue } if resp.TxID != txID { t.Error("Invalid TxID returned") } if resp.Status != AckStatusDBlockConfirmed { t.Error("Invalid status returned") } } } for i := 0; i < 10; i++ { h := testHelper.NewRepeatingHash(byte(i)) req := AckRequest{} req.TxID = h.String() r, jError := HandleV2FactoidACK(state, req) if jError != nil { t.Errorf("%v", jError) continue } resp, ok := r.(*FactoidTxStatus) if ok == false { t.Error("Invalid response type returned") continue } if resp.TxID != h.String() { t.Error("Invalid TxID returned") } if resp.Status != AckStatusUnknown { t.Error("Invalid status returned") } req = AckRequest{} req.FullTransaction = h.String() _, jError = HandleV2FactoidACK(state, req) if jError == nil { t.Error("Invalid transaciton not caught") continue } } }
func TestHandleV2EntryACK(t *testing.T) { state := testHelper.CreateAndPopulateTestState() blocks := testHelper.CreateFullTestBlockSet() for _, block := range blocks { for _, tx := range block.Entries { req := AckRequest{} txID := tx.GetHash().String() req.TxID = txID r, jError := HandleV2EntryACK(state, req) if jError != nil { t.Errorf("%v", jError) continue } resp, ok := r.(*EntryStatus) if ok == false { t.Error("Invalid response type returned") continue } if resp.EntryHash != txID { t.Errorf("Invalid EntryHash returned - %v vs %v", resp.EntryHash, txID) } if resp.CommitTxID == "" { t.Errorf("Invalid CommitTxID returned - %v", resp.CommitTxID) } if resp.CommitData.Status != AckStatusDBlockConfirmed { t.Errorf("Invalid status returned - %v vs %v", resp.CommitData.Status, AckStatusDBlockConfirmed) } if resp.EntryData.Status != AckStatusDBlockConfirmed { t.Errorf("Invalid status returned - %v vs %v", resp.EntryData.Status, AckStatusDBlockConfirmed) } req = AckRequest{} h, err := tx.MarshalBinary() if err != nil { t.Errorf("%v", err) continue } req.FullTransaction = hex.EncodeToString(h) r, jError = HandleV2EntryACK(state, req) if jError != nil { t.Errorf("%v", jError) continue } resp, ok = r.(*EntryStatus) if ok == false { t.Error("Invalid response type returned") continue } if resp.EntryHash != txID { t.Errorf("Invalid EntryHash returned - %v vs %v", resp.EntryHash, txID) } if resp.CommitTxID == "" { t.Errorf("Invalid CommitTxID returned - %v", resp.CommitTxID) } if resp.CommitData.Status != AckStatusDBlockConfirmed { t.Errorf("Invalid status returned - %v vs %v", resp.CommitData.Status, AckStatusDBlockConfirmed) } if resp.EntryData.Status != AckStatusDBlockConfirmed { t.Errorf("Invalid status returned - %v vs %v", resp.EntryData.Status, AckStatusDBlockConfirmed) } } for _, tx := range block.ECBlock.GetEntries() { if tx.ECID() != entryCreditBlock.ECIDChainCommit && tx.ECID() != entryCreditBlock.ECIDEntryCommit { continue } req := AckRequest{} txID := tx.GetSigHash().String() entryHash := tx.GetEntryHash().String() req.TxID = txID r, jError := HandleV2EntryACK(state, req) if jError != nil { t.Errorf("%v", jError) continue } resp, ok := r.(*EntryStatus) if ok == false { t.Error("Invalid response type returned") continue } t.Logf("resp - %v", resp) if resp.CommitTxID != txID { t.Errorf("Invalid CommitTxID returned - %v vs %v", resp.CommitTxID, txID) } if resp.EntryHash != entryHash { t.Errorf("Invalid EntryHash returned - %v vs %v", resp.EntryHash, entryHash) } if resp.CommitData.Status != AckStatusDBlockConfirmed { t.Errorf("Invalid status returned - %v vs %v", resp.CommitData.Status, AckStatusDBlockConfirmed) } if resp.EntryData.Status != AckStatusDBlockConfirmed { t.Errorf("Invalid status returned - %v vs %v", resp.EntryData.Status, AckStatusDBlockConfirmed) } req = AckRequest{} h, err := tx.MarshalBinary() if err != nil { t.Errorf("%v", err) continue } req.FullTransaction = hex.EncodeToString(h) r, jError = HandleV2EntryACK(state, req) if jError != nil { t.Errorf("%v", jError) continue } resp, ok = r.(*EntryStatus) if ok == false { t.Error("Invalid response type returned") continue } t.Logf("resp - %v", resp) if resp.CommitTxID != txID { t.Errorf("Invalid CommitTxID returned - %v vs %v", resp.CommitTxID, txID) } if resp.EntryHash != entryHash { t.Errorf("Invalid EntryHash returned - %v vs %v", resp.EntryHash, entryHash) } if resp.CommitData.Status != AckStatusDBlockConfirmed { t.Errorf("Invalid status returned - %v vs %v", resp.CommitData.Status, AckStatusDBlockConfirmed) } if resp.EntryData.Status != AckStatusDBlockConfirmed { t.Errorf("Invalid status returned - %v vs %v", resp.EntryData.Status, AckStatusDBlockConfirmed) } } } for i := 0; i < 10; i++ { h := testHelper.NewRepeatingHash(byte(i)) req := AckRequest{} req.TxID = h.String() r, jError := HandleV2EntryACK(state, req) if jError != nil { t.Errorf("%v", jError) continue } resp, ok := r.(*EntryStatus) if ok == false { t.Error("Invalid response type returned") continue } if resp.CommitTxID != "" { t.Error("Invalid CommitTxID returned") } if resp.EntryHash != "" { t.Error("Invalid EntryHash returned") } if resp.CommitData.Status != AckStatusUnknown { t.Error("Invalid status returned") } if resp.EntryData.Status != AckStatusUnknown { t.Error("Invalid status returned") } req = AckRequest{} req.FullTransaction = h.String() _, jError = HandleV2EntryACK(state, req) if jError == nil { t.Error("Invalid transaciton not caught") continue } } }
func TestCheckDatabaseFromState(t *testing.T) { state := testHelper.CreateAndPopulateTestState() CheckDatabase(state.DB.DB) }