github.com/status-im/status-go@v1.1.0/waku/common/topic_test.go (about)

     1  // Copyright 2019 The Waku Library Authors.
     2  //
     3  // The Waku library is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Lesser General Public License as published by
     5  // the Free Software Foundation, either version 3 of the License, or
     6  // (at your option) any later version.
     7  //
     8  // The Waku library is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty off
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    11  // GNU Lesser General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Lesser General Public License
    14  // along with the Waku library. If not, see <http://www.gnu.org/licenses/>.
    15  //
    16  // This software uses the go-ethereum library, which is licensed
    17  // under the GNU Lesser General Public Library, version 3 or any later.
    18  
    19  package common
    20  
    21  import (
    22  	"encoding/json"
    23  	"testing"
    24  )
    25  
    26  var topicStringTests = []struct {
    27  	topic TopicType
    28  	str   string
    29  }{
    30  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, str: "0x00000000"},
    31  	{topic: TopicType{0x00, 0x7f, 0x80, 0xff}, str: "0x007f80ff"},
    32  	{topic: TopicType{0xff, 0x80, 0x7f, 0x00}, str: "0xff807f00"},
    33  	{topic: TopicType{0xf2, 0x6e, 0x77, 0x79}, str: "0xf26e7779"},
    34  }
    35  
    36  func TestTopicString(t *testing.T) {
    37  	for i, tst := range topicStringTests {
    38  		s := tst.topic.String()
    39  		if s != tst.str {
    40  			t.Fatalf("failed test %d: have %s, want %s.", i, s, tst.str)
    41  		}
    42  	}
    43  }
    44  
    45  var bytesToTopicTests = []struct {
    46  	data  []byte
    47  	topic TopicType
    48  }{
    49  	{topic: TopicType{0x8f, 0x9a, 0x2b, 0x7d}, data: []byte{0x8f, 0x9a, 0x2b, 0x7d}},
    50  	{topic: TopicType{0x00, 0x7f, 0x80, 0xff}, data: []byte{0x00, 0x7f, 0x80, 0xff}},
    51  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{0x00, 0x00, 0x00, 0x00}},
    52  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{0x00, 0x00, 0x00}},
    53  	{topic: TopicType{0x01, 0x00, 0x00, 0x00}, data: []byte{0x01}},
    54  	{topic: TopicType{0x00, 0xfe, 0x00, 0x00}, data: []byte{0x00, 0xfe}},
    55  	{topic: TopicType{0xea, 0x1d, 0x43, 0x00}, data: []byte{0xea, 0x1d, 0x43}},
    56  	{topic: TopicType{0x6f, 0x3c, 0xb0, 0xdd}, data: []byte{0x6f, 0x3c, 0xb0, 0xdd, 0x0f, 0x00, 0x90}},
    57  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{}},
    58  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: nil},
    59  }
    60  
    61  var unmarshalTestsGood = []struct {
    62  	topic TopicType
    63  	data  []byte
    64  }{
    65  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x00000000"`)},
    66  	{topic: TopicType{0x00, 0x7f, 0x80, 0xff}, data: []byte(`"0x007f80ff"`)},
    67  	{topic: TopicType{0xff, 0x80, 0x7f, 0x00}, data: []byte(`"0xff807f00"`)},
    68  	{topic: TopicType{0xf2, 0x6e, 0x77, 0x79}, data: []byte(`"0xf26e7779"`)},
    69  }
    70  
    71  var unmarshalTestsBad = []struct {
    72  	topic TopicType
    73  	data  []byte
    74  }{
    75  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x000000"`)},
    76  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x0000000"`)},
    77  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x000000000"`)},
    78  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x0000000000"`)},
    79  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"000000"`)},
    80  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0000000"`)},
    81  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"000000000"`)},
    82  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0000000000"`)},
    83  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"abcdefg0"`)},
    84  }
    85  
    86  var unmarshalTestsUgly = []struct {
    87  	topic TopicType
    88  	data  []byte
    89  }{
    90  	{topic: TopicType{0x01, 0x00, 0x00, 0x00}, data: []byte(`"0x00000001"`)},
    91  }
    92  
    93  func TestBytesToTopic(t *testing.T) {
    94  	for i, tst := range bytesToTopicTests {
    95  		top := BytesToTopic(tst.data)
    96  		if top != tst.topic {
    97  			t.Fatalf("failed test %d: have %v, want %v.", i, t, tst.topic)
    98  		}
    99  	}
   100  }
   101  
   102  func TestUnmarshalTestsGood(t *testing.T) {
   103  	for i, tst := range unmarshalTestsGood {
   104  		var top TopicType
   105  		err := json.Unmarshal(tst.data, &top)
   106  		if err != nil {
   107  			t.Errorf("failed test %d. input: %v. err: %v", i, tst.data, err)
   108  		} else if top != tst.topic {
   109  			t.Errorf("failed test %d: have %v, want %v.", i, t, tst.topic)
   110  		}
   111  	}
   112  }
   113  
   114  func TestUnmarshalTestsBad(t *testing.T) {
   115  	// in this test UnmarshalJSON() is supposed to fail
   116  	for i, tst := range unmarshalTestsBad {
   117  		var top TopicType
   118  		err := json.Unmarshal(tst.data, &top)
   119  		if err == nil {
   120  			t.Fatalf("failed test %d. input: %v.", i, tst.data)
   121  		}
   122  	}
   123  }
   124  
   125  func TestUnmarshalTestsUgly(t *testing.T) {
   126  	// in this test UnmarshalJSON() is NOT supposed to fail, but result should be wrong
   127  	for i, tst := range unmarshalTestsUgly {
   128  		var top TopicType
   129  		err := json.Unmarshal(tst.data, &top)
   130  		if err != nil {
   131  			t.Errorf("failed test %d. input: %v.", i, tst.data)
   132  		} else if top == tst.topic {
   133  			t.Errorf("failed test %d: have %v, want %v.", i, top, tst.topic)
   134  		}
   135  	}
   136  }