Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-pubsub
Commits
32b8d895
Commit
32b8d895
authored
Mar 19, 2021
by
vyzo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix TestValidate, add TestValidate2
parent
37e77dc5
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
13 deletions
+51
-13
validation_test.go
validation_test.go
+51
-13
No files found.
validation_test.go
View file @
32b8d895
...
...
@@ -100,22 +100,60 @@ func TestValidate(t *testing.T) {
}
for
_
,
tc
:=
range
msgs
{
for
_
,
p
:=
range
psubs
{
err
:=
p
.
Publish
(
topic
,
tc
.
msg
)
err
:=
psubs
[
0
]
.
Publish
(
topic
,
tc
.
msg
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
select
{
case
msg
:=
<-
sub
.
ch
:
if
!
tc
.
validates
{
t
.
Log
(
msg
)
t
.
Error
(
"expected message validation to filter out the message"
)
}
case
<-
time
.
After
(
333
*
time
.
Millisecond
)
:
if
tc
.
validates
{
t
.
Error
(
"expected message validation to accept the message"
)
}
}
}
}
func
TestValidate2
(
t
*
testing
.
T
)
{
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
defer
cancel
()
hosts
:=
getNetHosts
(
t
,
ctx
,
1
)
psubs
:=
getPubsubs
(
ctx
,
hosts
)
topic
:=
"foobar"
err
:=
psubs
[
0
]
.
RegisterTopicValidator
(
topic
,
func
(
ctx
context
.
Context
,
from
peer
.
ID
,
msg
*
Message
)
bool
{
return
!
bytes
.
Contains
(
msg
.
Data
,
[]
byte
(
"illegal"
))
})
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
msgs
:=
[]
struct
{
msg
[]
byte
validates
bool
}{
{
msg
:
[]
byte
(
"this is a legal message"
),
validates
:
true
},
{
msg
:
[]
byte
(
"there also is nothing controversial about this message"
),
validates
:
true
},
{
msg
:
[]
byte
(
"openly illegal content will be censored"
),
validates
:
false
},
{
msg
:
[]
byte
(
"but subversive actors will use leetspeek to spread 1ll3g4l content"
),
validates
:
true
},
}
for
_
,
tc
:=
range
msgs
{
err
:=
psubs
[
0
]
.
Publish
(
topic
,
tc
.
msg
)
if
tc
.
validates
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
select
{
case
msg
:=
<-
sub
.
ch
:
if
!
tc
.
validates
{
t
.
Log
(
msg
)
t
.
Error
(
"expected message validation to filter out the message"
)
}
case
<-
time
.
After
(
333
*
time
.
Millisecond
)
:
if
tc
.
validates
{
t
.
Error
(
"expected message validation to accept the message"
)
}
}
else
{
if
err
==
nil
{
t
.
Fatal
(
"expected validation to fail for this message"
)
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment