mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 14:46:42 +00:00
whisper: api test added
This commit is contained in:
parent
dea44fe73f
commit
40b83c27ab
3 changed files with 121 additions and 3 deletions
117
whisper/shhapi/api_test.go
Normal file
117
whisper/shhapi/api_test.go
Normal file
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -150,7 +150,7 @@ func (w *Whisper) NewIdentity() *ecdsa.PrivateKey {
|
||||||
}
|
}
|
||||||
w.keyMu.Lock()
|
w.keyMu.Lock()
|
||||||
defer w.keyMu.Unlock()
|
defer w.keyMu.Unlock()
|
||||||
w.privateKeys[string(crypto.FromECDSAPub(&key.PublicKey))] = key
|
w.privateKeys[common.ToHex(crypto.FromECDSAPub(&key.PublicKey))] = key
|
||||||
return key
|
return key
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -143,8 +144,8 @@ func TestWhisperIdentityManagement(x *testing.T) {
|
||||||
w := NewWhisper(nil)
|
w := NewWhisper(nil)
|
||||||
id1 := w.NewIdentity()
|
id1 := w.NewIdentity()
|
||||||
id2 := w.NewIdentity()
|
id2 := w.NewIdentity()
|
||||||
pub1 := string(crypto.FromECDSAPub(&id1.PublicKey))
|
pub1 := common.ToHex(crypto.FromECDSAPub(&id1.PublicKey))
|
||||||
pub2 := string(crypto.FromECDSAPub(&id2.PublicKey))
|
pub2 := common.ToHex(crypto.FromECDSAPub(&id2.PublicKey))
|
||||||
pk1 := w.GetIdentity(pub1)
|
pk1 := w.GetIdentity(pub1)
|
||||||
pk2 := w.GetIdentity(pub2)
|
pk2 := w.GetIdentity(pub2)
|
||||||
if !w.HasIdentity(pub1) {
|
if !w.HasIdentity(pub1) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue