From 40b83c27ab3d49a347e4082e5d7e71285556fff2 Mon Sep 17 00:00:00 2001 From: Vlad Date: Fri, 28 Oct 2016 14:53:44 +0200 Subject: [PATCH] whisper: api test added --- whisper/shhapi/api_test.go | 117 ++++++++++++++++++++++++++++++ whisper/whisperv5/whisper.go | 2 +- whisper/whisperv5/whisper_test.go | 5 +- 3 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 whisper/shhapi/api_test.go diff --git a/whisper/shhapi/api_test.go b/whisper/shhapi/api_test.go new file mode 100644 index 0000000000..fa804ef945 --- /dev/null +++ b/whisper/shhapi/api_test.go @@ -0,0 +1,117 @@ +// Copyright 2016 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package shhapi + +import ( + "testing" + + "github.com/ethereum/go-ethereum/rpc" + "github.com/ethereum/go-ethereum/whisper/whisperv5" +) + +func TestBasic(x *testing.T) { + api := NewPublicWhisperAPI() + if api == nil { + x.Errorf("failed to create API.") + return + } + + ver, err := api.Version() + if err != nil { + x.Errorf("failed generateFilter: %s.", err) + return + } + + if ver.Uint64() != whisperv5.ProtocolVersion { + x.Errorf("wrong version: %d.", ver.Uint64()) + return + } + + var id string = "test" + + exist, err := api.HasIdentity(id) + if err != nil { + x.Errorf("failed HasIdentity: %s.", err) + return + } + if exist { + x.Errorf("failed HasIdentity: false positive.") + return + } + + exist, err = api.HasSymKey(id) + if err != nil { + x.Errorf("failed HasSymKey: %s.", err) + return + } + if exist { + x.Errorf("failed HasSymKey: false positive.") + return + } + + err = api.DeleteIdentity(id) + if err != nil { + x.Errorf("failed DeleteIdentity: %s.", err) + return + } + + pub, err := api.NewIdentity() + if err != nil { + x.Errorf("failed NewIdentity: %s.", err) + return + } + if len(pub) == 0 { + x.Errorf("NewIdentity: empty") + return + } + + //spub := string(crypto.FromECDSAPub(pub)) + //fmt.Printf("%s \n", pub) + + exist, err = api.HasIdentity(pub) + if err != nil { + x.Errorf("failed HasIdentity: %s.", err) + return + } + if !exist { + x.Errorf("failed HasIdentity: false negative.") + return + } + + err = api.DeleteIdentity(pub) + if err != nil { + x.Errorf("failed DeleteIdentity 2: %s.", err) + return + } + + exist, err = api.HasIdentity(pub) + if err != nil { + x.Errorf("failed HasIdentity 3: %s.", err) + return + } + if exist { + x.Errorf("failed HasIdentity 3: false positive.") + return + } + + var hexnum rpc.HexNumber + mail := api.GetFilterChanges(hexnum) + if len(mail) != 0 { + x.Errorf("failed GetFilterChanges") + return + } +} diff --git a/whisper/whisperv5/whisper.go b/whisper/whisperv5/whisper.go index 46e6293946..cfc3849489 100644 --- a/whisper/whisperv5/whisper.go +++ b/whisper/whisperv5/whisper.go @@ -150,7 +150,7 @@ func (w *Whisper) NewIdentity() *ecdsa.PrivateKey { } w.keyMu.Lock() defer w.keyMu.Unlock() - w.privateKeys[string(crypto.FromECDSAPub(&key.PublicKey))] = key + w.privateKeys[common.ToHex(crypto.FromECDSAPub(&key.PublicKey))] = key return key } diff --git a/whisper/whisperv5/whisper_test.go b/whisper/whisperv5/whisper_test.go index 92f1a29331..1db26265a5 100644 --- a/whisper/whisperv5/whisper_test.go +++ b/whisper/whisperv5/whisper_test.go @@ -20,6 +20,7 @@ import ( "bytes" "testing" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" ) @@ -143,8 +144,8 @@ func TestWhisperIdentityManagement(x *testing.T) { w := NewWhisper(nil) id1 := w.NewIdentity() id2 := w.NewIdentity() - pub1 := string(crypto.FromECDSAPub(&id1.PublicKey)) - pub2 := string(crypto.FromECDSAPub(&id2.PublicKey)) + pub1 := common.ToHex(crypto.FromECDSAPub(&id1.PublicKey)) + pub2 := common.ToHex(crypto.FromECDSAPub(&id2.PublicKey)) pk1 := w.GetIdentity(pub1) pk2 := w.GetIdentity(pub2) if !w.HasIdentity(pub1) {