From 9f798ea11541b2e2d1b09d72122a1ea0f4bb5a51 Mon Sep 17 00:00:00 2001 From: crusader Date: Tue, 18 Sep 2018 11:51:40 +0900 Subject: [PATCH] ing --- Gopkg.lock | 8 +++- echo/echo.go | 96 +++++++++++++++++++++++++++++++++++++++++++++++ echo/echo_test.go | 37 ++++++++++++++++++ 3 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 echo/echo.go create mode 100644 echo/echo_test.go diff --git a/Gopkg.lock b/Gopkg.lock index 06548f8..36167df 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -13,6 +13,12 @@ revision = "792786c7400a136282c1664665ae0a8db921c6c2" version = "v1.0.0" +[[projects]] + name = "github.com/satori/go.uuid" + packages = ["."] + revision = "f58768cc1a7a7e77a3bd49e98cdd21419399b6a3" + version = "v1.2.0" + [[projects]] name = "github.com/stretchr/testify" packages = ["assert"] @@ -28,6 +34,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "ec738887f78fd05112bc2a2f3b8a899e39749ad1dd11668f687f3ccebe1f3cf6" + inputs-digest = "42705304701176173d344446cd1f46bfef849ef7bc78c789b6a70526b132567a" solver-name = "gps-cdcl" solver-version = 1 diff --git a/echo/echo.go b/echo/echo.go new file mode 100644 index 0000000..a4b3178 --- /dev/null +++ b/echo/echo.go @@ -0,0 +1,96 @@ +package echo + +import ( + "fmt" + + osm "git.loafle.net/overflow/service_matcher-go" + uuid "github.com/satori/go.uuid" +) + +const ( + ECHO_COUNT = 2 +) + +type EchoMatcher struct { + osm.Matchers +} + +func (m *EchoMatcher) Key() string { + return "ECHO" +} + +func (m *EchoMatcher) Type() string { + return "NETWORK" +} + +func (m *EchoMatcher) Vendor(matchCtx *osm.MatchCtx) string { + return "UNKNOWN" +} + +func (m *EchoMatcher) Version(matchCtx *osm.MatchCtx) string { + return "UNKNOWN" +} + +func (m *EchoMatcher) OsType(matchCtx *osm.MatchCtx) string { + return "UNKNOWN" +} + +func (m *EchoMatcher) OsVersion(matchCtx *osm.MatchCtx) string { + return "UNKNOWN" +} + +func (m *EchoMatcher) Name(matchCtx *osm.MatchCtx) string { + return "ECHO" +} + +func (m *EchoMatcher) IsPrePacket() bool { + return false +} + +func (m *EchoMatcher) PacketCount(matchCtx *osm.MatchCtx) int { + return ECHO_COUNT +} + +func (m *EchoMatcher) Packet(matchCtx *osm.MatchCtx, index int) *osm.Packet { + reqStr := uuid.NewV4().String() + rbyte := make([]byte, len(reqStr)) + copy(rbyte[:], reqStr) + + matchCtx.SetAttribute(fmt.Sprintf("%d", index), reqStr) + + return osm.NewPacket(rbyte, len(reqStr)) +} + +func (m *EchoMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { + return ECHO_COUNT-1 > index +} + +func (m *EchoMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { + return false +} + +func (m *EchoMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { + + if packet == nil || !packet.Valid() { + return osm.NoPacketReceivedError() + } + + sendStr, ok := matchCtx.GetAttribute(fmt.Sprintf("%d", index)) + if !ok { + return osm.NotMatchedError() + } + + recvStr := string(packet.Bytes()) + + if sendStr != recvStr { + return osm.NotMatchedError() + } + + return nil +} + +func NewMatcher() osm.Matcher { + m := &EchoMatcher{} + + return m +} diff --git a/echo/echo_test.go b/echo/echo_test.go new file mode 100644 index 0000000..af530f0 --- /dev/null +++ b/echo/echo_test.go @@ -0,0 +1,37 @@ +package echo + +import ( + "net" + "testing" + + osm "git.loafle.net/overflow/service_matcher-go" +) + +func TestEchoMatcher(t *testing.T) { + m := NewMatcher() + + conn, err := net.Dial("tcp", "192.168.1.99:7") + if err != nil { + t.Error(err) + return + } + defer conn.Close() + + matchCtx := osm.NewMatchCtx("192.168.1.99", 7) + + for i := 0; i < m.PacketCount(matchCtx); i++ { + _, err := conn.Write(m.Packet(matchCtx, i).Buffer) + if err != nil { + t.Error(err) + } + bytes := make([]byte, 1024) + n, _ := conn.Read(bytes) + p := osm.NewPacket(bytes, n) + + if err := m.Match(matchCtx, i, p); err != nil { + t.Error(err) + } + } + t.Log(m.Name(matchCtx)) + t.Log(matchCtx) +}