github.com/ylsgit/go-ethereum@v1.6.5/whisper/whisperv2/filter_test.go (about)

     1  // Copyright 2015 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 whisperv2
    18  
    19  import (
    20  	"bytes"
    21  
    22  	"testing"
    23  )
    24  
    25  var filterTopicsCreationTests = []struct {
    26  	topics [][]string
    27  	filter [][][4]byte
    28  }{
    29  	{ // Simple topic filter
    30  		topics: [][]string{
    31  			{"abc", "def", "ghi"},
    32  			{"def"},
    33  			{"ghi", "abc"},
    34  		},
    35  		filter: [][][4]byte{
    36  			{{0x4e, 0x03, 0x65, 0x7a}, {0x34, 0x60, 0x7c, 0x9b}, {0x21, 0x41, 0x7d, 0xf9}},
    37  			{{0x34, 0x60, 0x7c, 0x9b}},
    38  			{{0x21, 0x41, 0x7d, 0xf9}, {0x4e, 0x03, 0x65, 0x7a}},
    39  		},
    40  	},
    41  	{ // Wild-carded topic filter
    42  		topics: [][]string{
    43  			{"abc", "def", "ghi"},
    44  			{},
    45  			{""},
    46  			{"def"},
    47  		},
    48  		filter: [][][4]byte{
    49  			{{0x4e, 0x03, 0x65, 0x7a}, {0x34, 0x60, 0x7c, 0x9b}, {0x21, 0x41, 0x7d, 0xf9}},
    50  			{},
    51  			{},
    52  			{{0x34, 0x60, 0x7c, 0x9b}},
    53  		},
    54  	},
    55  }
    56  
    57  var filterTopicsCreationFlatTests = []struct {
    58  	topics []string
    59  	filter [][][4]byte
    60  }{
    61  	{ // Simple topic list
    62  		topics: []string{"abc", "def", "ghi"},
    63  		filter: [][][4]byte{
    64  			{{0x4e, 0x03, 0x65, 0x7a}},
    65  			{{0x34, 0x60, 0x7c, 0x9b}},
    66  			{{0x21, 0x41, 0x7d, 0xf9}},
    67  		},
    68  	},
    69  	{ // Wild-carded topic list
    70  		topics: []string{"abc", "", "ghi"},
    71  		filter: [][][4]byte{
    72  			{{0x4e, 0x03, 0x65, 0x7a}},
    73  			{},
    74  			{{0x21, 0x41, 0x7d, 0xf9}},
    75  		},
    76  	},
    77  }
    78  
    79  func TestFilterTopicsCreation(t *testing.T) {
    80  	// Check full filter creation
    81  	for i, tt := range filterTopicsCreationTests {
    82  		// Check the textual creation
    83  		filter := NewFilterTopicsFromStrings(tt.topics...)
    84  		if len(filter) != len(tt.topics) {
    85  			t.Errorf("test %d: condition count mismatch: have %v, want %v", i, len(filter), len(tt.topics))
    86  			continue
    87  		}
    88  		for j, condition := range filter {
    89  			if len(condition) != len(tt.filter[j]) {
    90  				t.Errorf("test %d, condition %d: size mismatch: have %v, want %v", i, j, len(condition), len(tt.filter[j]))
    91  				continue
    92  			}
    93  			for k := 0; k < len(condition); k++ {
    94  				if !bytes.Equal(condition[k][:], tt.filter[j][k][:]) {
    95  					t.Errorf("test %d, condition %d, segment %d: filter mismatch: have 0x%x, want 0x%x", i, j, k, condition[k], tt.filter[j][k])
    96  				}
    97  			}
    98  		}
    99  		// Check the binary creation
   100  		binary := make([][][]byte, len(tt.topics))
   101  		for j, condition := range tt.topics {
   102  			binary[j] = make([][]byte, len(condition))
   103  			for k, segment := range condition {
   104  				binary[j][k] = []byte(segment)
   105  			}
   106  		}
   107  		filter = NewFilterTopics(binary...)
   108  		if len(filter) != len(tt.topics) {
   109  			t.Errorf("test %d: condition count mismatch: have %v, want %v", i, len(filter), len(tt.topics))
   110  			continue
   111  		}
   112  		for j, condition := range filter {
   113  			if len(condition) != len(tt.filter[j]) {
   114  				t.Errorf("test %d, condition %d: size mismatch: have %v, want %v", i, j, len(condition), len(tt.filter[j]))
   115  				continue
   116  			}
   117  			for k := 0; k < len(condition); k++ {
   118  				if !bytes.Equal(condition[k][:], tt.filter[j][k][:]) {
   119  					t.Errorf("test %d, condition %d, segment %d: filter mismatch: have 0x%x, want 0x%x", i, j, k, condition[k], tt.filter[j][k])
   120  				}
   121  			}
   122  		}
   123  	}
   124  	// Check flat filter creation
   125  	for i, tt := range filterTopicsCreationFlatTests {
   126  		// Check the textual creation
   127  		filter := NewFilterTopicsFromStringsFlat(tt.topics...)
   128  		if len(filter) != len(tt.topics) {
   129  			t.Errorf("test %d: condition count mismatch: have %v, want %v", i, len(filter), len(tt.topics))
   130  			continue
   131  		}
   132  		for j, condition := range filter {
   133  			if len(condition) != len(tt.filter[j]) {
   134  				t.Errorf("test %d, condition %d: size mismatch: have %v, want %v", i, j, len(condition), len(tt.filter[j]))
   135  				continue
   136  			}
   137  			for k := 0; k < len(condition); k++ {
   138  				if !bytes.Equal(condition[k][:], tt.filter[j][k][:]) {
   139  					t.Errorf("test %d, condition %d, segment %d: filter mismatch: have 0x%x, want 0x%x", i, j, k, condition[k], tt.filter[j][k])
   140  				}
   141  			}
   142  		}
   143  		// Check the binary creation
   144  		binary := make([][]byte, len(tt.topics))
   145  		for j, topic := range tt.topics {
   146  			binary[j] = []byte(topic)
   147  		}
   148  		filter = NewFilterTopicsFlat(binary...)
   149  		if len(filter) != len(tt.topics) {
   150  			t.Errorf("test %d: condition count mismatch: have %v, want %v", i, len(filter), len(tt.topics))
   151  			continue
   152  		}
   153  		for j, condition := range filter {
   154  			if len(condition) != len(tt.filter[j]) {
   155  				t.Errorf("test %d, condition %d: size mismatch: have %v, want %v", i, j, len(condition), len(tt.filter[j]))
   156  				continue
   157  			}
   158  			for k := 0; k < len(condition); k++ {
   159  				if !bytes.Equal(condition[k][:], tt.filter[j][k][:]) {
   160  					t.Errorf("test %d, condition %d, segment %d: filter mismatch: have 0x%x, want 0x%x", i, j, k, condition[k], tt.filter[j][k])
   161  				}
   162  			}
   163  		}
   164  	}
   165  }
   166  
   167  var filterCompareTests = []struct {
   168  	matcher filterer
   169  	message filterer
   170  	match   bool
   171  }{
   172  	{ // Wild-card filter matching anything
   173  		matcher: filterer{to: "", from: "", matcher: newTopicMatcher()},
   174  		message: filterer{to: "to", from: "from", matcher: newTopicMatcher(NewFilterTopicsFromStringsFlat("topic")...)},
   175  		match:   true,
   176  	},
   177  	{ // Filter matching the to field
   178  		matcher: filterer{to: "to", from: "", matcher: newTopicMatcher()},
   179  		message: filterer{to: "to", from: "from", matcher: newTopicMatcher(NewFilterTopicsFromStringsFlat("topic")...)},
   180  		match:   true,
   181  	},
   182  	{ // Filter rejecting the to field
   183  		matcher: filterer{to: "to", from: "", matcher: newTopicMatcher()},
   184  		message: filterer{to: "", from: "from", matcher: newTopicMatcher(NewFilterTopicsFromStringsFlat("topic")...)},
   185  		match:   false,
   186  	},
   187  	{ // Filter matching the from field
   188  		matcher: filterer{to: "", from: "from", matcher: newTopicMatcher()},
   189  		message: filterer{to: "to", from: "from", matcher: newTopicMatcher(NewFilterTopicsFromStringsFlat("topic")...)},
   190  		match:   true,
   191  	},
   192  	{ // Filter rejecting the from field
   193  		matcher: filterer{to: "", from: "from", matcher: newTopicMatcher()},
   194  		message: filterer{to: "to", from: "", matcher: newTopicMatcher(NewFilterTopicsFromStringsFlat("topic")...)},
   195  		match:   false,
   196  	},
   197  	{ // Filter matching the topic field
   198  		matcher: filterer{to: "", from: "from", matcher: newTopicMatcher(NewFilterTopicsFromStringsFlat("topic")...)},
   199  		message: filterer{to: "to", from: "from", matcher: newTopicMatcher(NewFilterTopicsFromStringsFlat("topic")...)},
   200  		match:   true,
   201  	},
   202  	{ // Filter rejecting the topic field
   203  		matcher: filterer{to: "", from: "", matcher: newTopicMatcher(NewFilterTopicsFromStringsFlat("topic")...)},
   204  		message: filterer{to: "to", from: "from", matcher: newTopicMatcher()},
   205  		match:   false,
   206  	},
   207  }
   208  
   209  func TestFilterCompare(t *testing.T) {
   210  	for i, tt := range filterCompareTests {
   211  		if match := tt.matcher.Compare(tt.message); match != tt.match {
   212  			t.Errorf("test %d: match mismatch: have %v, want %v", i, match, tt.match)
   213  		}
   214  	}
   215  }