Clean up single-line event and new hs_desc_content test for #8 and #9

This commit is contained in:
Chad Retz 2018-07-24 10:41:49 -05:00
parent a3771891b4
commit f77fae492f
2 changed files with 45 additions and 10 deletions

View File

@ -270,17 +270,14 @@ func (c *Conn) relayAsyncEvents(resp *Response) {
var code, data string
var dataArray []string
if len(resp.Data) == 1 {
// If there is a single line of data, first line of it is the code, rest of the first line is data
// Find the index which specfies the char after the event code, either space or newline
index := strings.Index(resp.Data[0], " ")
if index == -1 {
index = strings.Index(resp.Data[0], "\r\n")
if index == -1 {
return
}
// On single line, part up to space, newline, or EOL is the code, rest is data
if index := strings.Index(resp.Data[0], " "); index != -1 {
code, data = resp.Data[0][:index], resp.Data[0][index+1:]
} else if index := strings.Index(resp.Data[0], "\r\n"); index != -1 {
code, data = resp.Data[0][:index], resp.Data[0][index+2:]
} else {
code, data = resp.Data[0], ""
}
code, data = resp.Data[0][:index], resp.Data[0][index+2:]
} else if len(resp.Data) > 0 {
// If there are multiple lines, the entire first line is the code
code, dataArray = resp.Data[0], resp.Data[1:]

View File

@ -0,0 +1,38 @@
package tests
import (
"context"
"strings"
"testing"
"time"
"github.com/cretz/bine/control"
)
func TestHSFetch(t *testing.T) {
ctx := GlobalEnabledNetworkContext(t)
// Add listener
eventCh := make(chan control.Event)
defer close(eventCh)
err := ctx.Control.AddEventListener(eventCh, control.EventCodeHSDescContent)
ctx.Require.NoError(err)
defer ctx.Control.RemoveEventListener(eventCh, control.EventCodeHSDescContent)
// Lookup HS
err = ctx.Control.GetHiddenServiceDescriptorAsync("facebookcorewwwi", "")
ctx.Require.NoError(err)
// Grab events
eventCtx, eventCancel := context.WithTimeout(ctx, 45*time.Second)
defer eventCancel()
errCh := make(chan error, 1)
go func() { errCh <- ctx.Control.HandleEvents(eventCtx) }()
select {
case <-eventCtx.Done():
ctx.Require.NoError(eventCtx.Err())
case err := <-errCh:
ctx.Require.NoError(err)
case event := <-eventCh:
hsEvent := event.(*control.HSDescContentEvent)
ctx.Require.Equal("facebookcorewwwi", hsEvent.Address)
ctx.Require.True(strings.HasPrefix(hsEvent.Descriptor, "rendezvous-service-descriptor "+hsEvent.DescID))
}
}