github.com/valorbit/go-ethereum@v1.9.11-rc4/tests/fuzzers/whisperv6/whisper-fuzzer.go (about)

     1  // Copyright 2019 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package whisperv6
    18  
    19  import (
    20  	"bytes"
    21  
    22  	"github.com/valorbit/go-ethereum/crypto"
    23  	"github.com/valorbit/go-ethereum/rlp"
    24  	"github.com/valorbit/go-ethereum/whisper/whisperv6"
    25  )
    26  
    27  type MessageParams struct {
    28  	Topic    whisperv6.TopicType
    29  	WorkTime uint32
    30  	TTL      uint32
    31  	KeySym   []byte
    32  	Payload  []byte
    33  }
    34  
    35  //export fuzzer_entry
    36  func Fuzz(input []byte) int {
    37  
    38  	var paramsDecoded MessageParams
    39  	err := rlp.DecodeBytes(input, &paramsDecoded)
    40  	if err != nil {
    41  		return 0
    42  	}
    43  	var params whisperv6.MessageParams
    44  	params.KeySym = make([]byte, 32)
    45  	if len(paramsDecoded.KeySym) <= 32 {
    46  		copy(params.KeySym, paramsDecoded.KeySym)
    47  	}
    48  	if input[0] == 255 {
    49  		params.PoW = 0.01
    50  		params.WorkTime = 1
    51  	} else {
    52  		params.PoW = 0
    53  		params.WorkTime = 0
    54  	}
    55  	params.TTL = paramsDecoded.TTL
    56  	params.Payload = paramsDecoded.Payload
    57  	text := make([]byte, 0, 512)
    58  	text = append(text, params.Payload...)
    59  	params.Topic = paramsDecoded.Topic
    60  	params.Src, err = crypto.GenerateKey()
    61  	if err != nil {
    62  		return 0
    63  	}
    64  	msg, err := whisperv6.NewSentMessage(&params)
    65  	if err != nil {
    66  		panic(err)
    67  		//return
    68  	}
    69  	env, err := msg.Wrap(&params)
    70  	if err != nil {
    71  		panic(err)
    72  	}
    73  	decrypted, err := env.OpenSymmetric(params.KeySym)
    74  	if err != nil {
    75  		panic(err)
    76  	}
    77  	if !decrypted.ValidateAndParse() {
    78  		panic("ValidateAndParse failed")
    79  	}
    80  	if !bytes.Equal(text, decrypted.Payload) {
    81  		panic("text != decrypted.Payload")
    82  	}
    83  	if len(decrypted.Signature) != 65 {
    84  		panic("Unexpected signature length")
    85  	}
    86  	if !whisperv6.IsPubKeyEqual(decrypted.Src, &params.Src.PublicKey) {
    87  		panic("Unexpected public key")
    88  	}
    89  	return 0
    90  }