github.com/luckypickle/go-ethereum-vet@v1.14.2/whisper/whisperv5/topic_test.go (about)

     1  // Copyright 2016 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 whisperv5
    18  
    19  import (
    20  	"encoding/json"
    21  	"testing"
    22  )
    23  
    24  var topicStringTests = []struct {
    25  	topic TopicType
    26  	str   string
    27  }{
    28  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, str: "0x00000000"},
    29  	{topic: TopicType{0x00, 0x7f, 0x80, 0xff}, str: "0x007f80ff"},
    30  	{topic: TopicType{0xff, 0x80, 0x7f, 0x00}, str: "0xff807f00"},
    31  	{topic: TopicType{0xf2, 0x6e, 0x77, 0x79}, str: "0xf26e7779"},
    32  }
    33  
    34  func TestTopicString(t *testing.T) {
    35  	for i, tst := range topicStringTests {
    36  		s := tst.topic.String()
    37  		if s != tst.str {
    38  			t.Fatalf("failed test %d: have %s, want %s.", i, s, tst.str)
    39  		}
    40  	}
    41  }
    42  
    43  var bytesToTopicTests = []struct {
    44  	data  []byte
    45  	topic TopicType
    46  }{
    47  	{topic: TopicType{0x8f, 0x9a, 0x2b, 0x7d}, data: []byte{0x8f, 0x9a, 0x2b, 0x7d}},
    48  	{topic: TopicType{0x00, 0x7f, 0x80, 0xff}, data: []byte{0x00, 0x7f, 0x80, 0xff}},
    49  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{0x00, 0x00, 0x00, 0x00}},
    50  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{0x00, 0x00, 0x00}},
    51  	{topic: TopicType{0x01, 0x00, 0x00, 0x00}, data: []byte{0x01}},
    52  	{topic: TopicType{0x00, 0xfe, 0x00, 0x00}, data: []byte{0x00, 0xfe}},
    53  	{topic: TopicType{0xea, 0x1d, 0x43, 0x00}, data: []byte{0xea, 0x1d, 0x43}},
    54  	{topic: TopicType{0x6f, 0x3c, 0xb0, 0xdd}, data: []byte{0x6f, 0x3c, 0xb0, 0xdd, 0x0f, 0x00, 0x90}},
    55  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{}},
    56  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: nil},
    57  }
    58  
    59  var unmarshalTestsGood = []struct {
    60  	topic TopicType
    61  	data  []byte
    62  }{
    63  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x00000000"`)},
    64  	{topic: TopicType{0x00, 0x7f, 0x80, 0xff}, data: []byte(`"0x007f80ff"`)},
    65  	{topic: TopicType{0xff, 0x80, 0x7f, 0x00}, data: []byte(`"0xff807f00"`)},
    66  	{topic: TopicType{0xf2, 0x6e, 0x77, 0x79}, data: []byte(`"0xf26e7779"`)},
    67  }
    68  
    69  var unmarshalTestsBad = []struct {
    70  	topic TopicType
    71  	data  []byte
    72  }{
    73  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x000000"`)},
    74  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x0000000"`)},
    75  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x000000000"`)},
    76  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x0000000000"`)},
    77  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"000000"`)},
    78  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0000000"`)},
    79  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"000000000"`)},
    80  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0000000000"`)},
    81  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"abcdefg0"`)},
    82  }
    83  
    84  var unmarshalTestsUgly = []struct {
    85  	topic TopicType
    86  	data  []byte
    87  }{
    88  	{topic: TopicType{0x01, 0x00, 0x00, 0x00}, data: []byte(`"0x00000001"`)},
    89  }
    90  
    91  func TestBytesToTopic(t *testing.T) {
    92  	for i, tst := range bytesToTopicTests {
    93  		top := BytesToTopic(tst.data)
    94  		if top != tst.topic {
    95  			t.Fatalf("failed test %d: have %v, want %v.", i, t, tst.topic)
    96  		}
    97  	}
    98  }
    99  
   100  func TestUnmarshalTestsGood(t *testing.T) {
   101  	for i, tst := range unmarshalTestsGood {
   102  		var top TopicType
   103  		err := json.Unmarshal(tst.data, &top)
   104  		if err != nil {
   105  			t.Errorf("failed test %d. input: %v. err: %v", i, tst.data, err)
   106  		} else if top != tst.topic {
   107  			t.Errorf("failed test %d: have %v, want %v.", i, t, tst.topic)
   108  		}
   109  	}
   110  }
   111  
   112  func TestUnmarshalTestsBad(t *testing.T) {
   113  	// in this test UnmarshalJSON() is supposed to fail
   114  	for i, tst := range unmarshalTestsBad {
   115  		var top TopicType
   116  		err := json.Unmarshal(tst.data, &top)
   117  		if err == nil {
   118  			t.Fatalf("failed test %d. input: %v.", i, tst.data)
   119  		}
   120  	}
   121  }
   122  
   123  func TestUnmarshalTestsUgly(t *testing.T) {
   124  	// in this test UnmarshalJSON() is NOT supposed to fail, but result should be wrong
   125  	for i, tst := range unmarshalTestsUgly {
   126  		var top TopicType
   127  		err := json.Unmarshal(tst.data, &top)
   128  		if err != nil {
   129  			t.Errorf("failed test %d. input: %v.", i, tst.data)
   130  		} else if top == tst.topic {
   131  			t.Errorf("failed test %d: have %v, want %v.", i, top, tst.topic)
   132  		}
   133  	}
   134  }