github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/whisper/whisperv6/topic_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:46</date>
    10  //</624450125778915328>
    11  
    12  
    13  package whisperv6
    14  
    15  import (
    16  	"encoding/json"
    17  	"testing"
    18  )
    19  
    20  var topicStringTests = []struct {
    21  	topic TopicType
    22  	str   string
    23  }{
    24  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, str: "0x00000000"},
    25  	{topic: TopicType{0x00, 0x7f, 0x80, 0xff}, str: "0x007f80ff"},
    26  	{topic: TopicType{0xff, 0x80, 0x7f, 0x00}, str: "0xff807f00"},
    27  	{topic: TopicType{0xf2, 0x6e, 0x77, 0x79}, str: "0xf26e7779"},
    28  }
    29  
    30  func TestTopicString(t *testing.T) {
    31  	for i, tst := range topicStringTests {
    32  		s := tst.topic.String()
    33  		if s != tst.str {
    34  			t.Fatalf("failed test %d: have %s, want %s.", i, s, tst.str)
    35  		}
    36  	}
    37  }
    38  
    39  var bytesToTopicTests = []struct {
    40  	data  []byte
    41  	topic TopicType
    42  }{
    43  	{topic: TopicType{0x8f, 0x9a, 0x2b, 0x7d}, data: []byte{0x8f, 0x9a, 0x2b, 0x7d}},
    44  	{topic: TopicType{0x00, 0x7f, 0x80, 0xff}, data: []byte{0x00, 0x7f, 0x80, 0xff}},
    45  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{0x00, 0x00, 0x00, 0x00}},
    46  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{0x00, 0x00, 0x00}},
    47  	{topic: TopicType{0x01, 0x00, 0x00, 0x00}, data: []byte{0x01}},
    48  	{topic: TopicType{0x00, 0xfe, 0x00, 0x00}, data: []byte{0x00, 0xfe}},
    49  	{topic: TopicType{0xea, 0x1d, 0x43, 0x00}, data: []byte{0xea, 0x1d, 0x43}},
    50  	{topic: TopicType{0x6f, 0x3c, 0xb0, 0xdd}, data: []byte{0x6f, 0x3c, 0xb0, 0xdd, 0x0f, 0x00, 0x90}},
    51  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{}},
    52  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: nil},
    53  }
    54  
    55  var unmarshalTestsGood = []struct {
    56  	topic TopicType
    57  	data  []byte
    58  }{
    59  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x00000000"`)},
    60  	{topic: TopicType{0x00, 0x7f, 0x80, 0xff}, data: []byte(`"0x007f80ff"`)},
    61  	{topic: TopicType{0xff, 0x80, 0x7f, 0x00}, data: []byte(`"0xff807f00"`)},
    62  	{topic: TopicType{0xf2, 0x6e, 0x77, 0x79}, data: []byte(`"0xf26e7779"`)},
    63  }
    64  
    65  var unmarshalTestsBad = []struct {
    66  	topic TopicType
    67  	data  []byte
    68  }{
    69  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x000000"`)},
    70  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x0000000"`)},
    71  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x000000000"`)},
    72  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x0000000000"`)},
    73  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"000000"`)},
    74  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0000000"`)},
    75  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"000000000"`)},
    76  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0000000000"`)},
    77  	{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"abcdefg0"`)},
    78  }
    79  
    80  var unmarshalTestsUgly = []struct {
    81  	topic TopicType
    82  	data  []byte
    83  }{
    84  	{topic: TopicType{0x01, 0x00, 0x00, 0x00}, data: []byte(`"0x00000001"`)},
    85  }
    86  
    87  func TestBytesToTopic(t *testing.T) {
    88  	for i, tst := range bytesToTopicTests {
    89  		top := BytesToTopic(tst.data)
    90  		if top != tst.topic {
    91  			t.Fatalf("failed test %d: have %v, want %v.", i, t, tst.topic)
    92  		}
    93  	}
    94  }
    95  
    96  func TestUnmarshalTestsGood(t *testing.T) {
    97  	for i, tst := range unmarshalTestsGood {
    98  		var top TopicType
    99  		err := json.Unmarshal(tst.data, &top)
   100  		if err != nil {
   101  			t.Errorf("failed test %d. input: %v. err: %v", i, tst.data, err)
   102  		} else if top != tst.topic {
   103  			t.Errorf("failed test %d: have %v, want %v.", i, t, tst.topic)
   104  		}
   105  	}
   106  }
   107  
   108  func TestUnmarshalTestsBad(t *testing.T) {
   109  //在这个测试中,unmashaljson()应该失败
   110  	for i, tst := range unmarshalTestsBad {
   111  		var top TopicType
   112  		err := json.Unmarshal(tst.data, &top)
   113  		if err == nil {
   114  			t.Fatalf("failed test %d. input: %v.", i, tst.data)
   115  		}
   116  	}
   117  }
   118  
   119  func TestUnmarshalTestsUgly(t *testing.T) {
   120  //在这个测试中,unmashaljson()不应该失败,但结果应该是错误的。
   121  	for i, tst := range unmarshalTestsUgly {
   122  		var top TopicType
   123  		err := json.Unmarshal(tst.data, &top)
   124  		if err != nil {
   125  			t.Errorf("failed test %d. input: %v.", i, tst.data)
   126  		} else if top == tst.topic {
   127  			t.Errorf("failed test %d: have %v, want %v.", i, top, tst.topic)
   128  		}
   129  	}
   130  }
   131