From e9bcd0fa53b7213c13b9b223b66a6e7c55056973 Mon Sep 17 00:00:00 2001 From: Přemysl Janouch
Date: Mon, 6 Aug 2018 11:45:06 +0200 Subject: hid: add the first tests This has actually revealed a problem in the SSL 2.0 detection. --- xS/main_test.go | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 xS/main_test.go (limited to 'xS/main_test.go') diff --git a/xS/main_test.go b/xS/main_test.go new file mode 100644 index 0000000..90a55f6 --- /dev/null +++ b/xS/main_test.go @@ -0,0 +1,139 @@ +// +// Copyright (c) 2015 - 2018, Přemysl Janouch
+// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY +// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION +// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +// + +package main + +import ( + "crypto/tls" + "net" + "os" + "reflect" + "syscall" + "testing" +) + +func TestSplitString(t *testing.T) { + var splitStringTests = []struct { + s, delims string + ignoreEmpty bool + result []string + }{ + {",a,,bc", ",", false, []string{"", "a", "", "bc"}}, + {",a,,bc", ",", true, []string{"a", "bc"}}, + {"a,;bc,", ",;", false, []string{"a", "", "bc", ""}}, + {"a,;bc,", ",;", true, []string{"a", "bc"}}, + {"", ",", false, []string{""}}, + {"", ",", true, nil}, + } + + for i, d := range splitStringTests { + got := splitString(d.s, d.delims, d.ignoreEmpty) + if !reflect.DeepEqual(got, d.result) { + t.Errorf("case %d: %v should be %v\n", i, got, d.result) + } + } +} + +func socketpair() (*os.File, *os.File, error) { + pair, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0) + if err != nil { + return nil, nil, err + } + + // See go #24331, this makes 1.11 use the internal poller + // while there wasn't a way to achieve that before. + if err := syscall.SetNonblock(int(pair[0]), true); err != nil { + return nil, nil, err + } + if err := syscall.SetNonblock(int(pair[1]), true); err != nil { + return nil, nil, err + } + + fa := os.NewFile(uintptr(pair[0]), "a") + if fa == nil { + return nil, nil, os.ErrInvalid + } + + fb := os.NewFile(uintptr(pair[1]), "b") + if fb == nil { + fa.Close() + return nil, nil, os.ErrInvalid + } + + return fa, fb, nil +} + +func TestDetectTLS(t *testing.T) { + detectTLSFromFunc := func(t *testing.T, writer func(net.Conn)) bool { + // net.Pipe doesn't use file descriptors, we need a socketpair. + sockA, sockB, err := socketpair() + if err != nil { + t.Fatal(err) + } + defer sockA.Close() + defer sockB.Close() + + fcB, err := net.FileConn(sockB) + if err != nil { + t.Fatal(err) + } + go writer(fcB) + + fcA, err := net.FileConn(sockA) + if err != nil { + t.Fatal(err) + } + sc, err := fcA.(syscall.Conn).SyscallConn() + if err != nil { + t.Fatal(err) + } + return detectTLS(sc) + } + + t.Run("SSL_2.0", func(t *testing.T) { + if !detectTLSFromFunc(t, func(fc net.Conn) { + // The obsolete, useless, unsupported SSL 2.0 record format. + _, _ = fc.Write([]byte{0x80, 0x01, 0x01}) + }) { + t.Error("could not detect SSL") + } + }) + t.Run("crypto_tls", func(t *testing.T) { + if !detectTLSFromFunc(t, func(fc net.Conn) { + conn := tls.Client(fc, &tls.Config{InsecureSkipVerify: true}) + _ = conn.Handshake() + }) { + t.Error("could not detect TLS") + } + }) + t.Run("text", func(t *testing.T) { + if detectTLSFromFunc(t, func(fc net.Conn) { + _, _ = fc.Write([]byte("ПРЕВЕД")) + }) { + t.Error("detected UTF-8 as TLS") + } + }) + t.Run("EOF", func(t *testing.T) { + type connCloseWriter interface { + net.Conn + CloseWrite() error + } + if detectTLSFromFunc(t, func(fc net.Conn) { + _ = fc.(connCloseWriter).CloseWrite() + }) { + t.Error("detected EOF as TLS") + } + }) +} -- cgit v1.2.3-70-g09d2 From 3fee7e8051dc65438e2e633f76c807d743f1a6ea Mon Sep 17 00:00:00 2001 From: Přemysl Janouch
Date: Mon, 6 Aug 2018 12:09:18 +0200 Subject: hid: port IRC tests from liberty, fix tag parsing --- xS/main.go | 2 +- xS/main_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) (limited to 'xS/main_test.go') diff --git a/xS/main.go b/xS/main.go index a7bd2e9..c49b2f6 100644 --- a/xS/main.go +++ b/xS/main.go @@ -420,7 +420,7 @@ func ircFnmatch(pattern string, s string) bool { } var reMsg = regexp.MustCompile( - `^(@[^ ]* +)?(?::([^! ]*)(?:!([^@]*)@([^ ]*))? +)?([^ ]+)(.*)?$`) + `^(?:@([^ ]*) +)?(?::([^! ]*)(?:!([^@]*)@([^ ]*))? +)?([^ ]+)(.*)?$`) var reArgs = regexp.MustCompile(`:.*| [^: ][^ ]*`) type message struct { diff --git a/xS/main_test.go b/xS/main_test.go index 90a55f6..4d6ccb3 100644 --- a/xS/main_test.go +++ b/xS/main_test.go @@ -137,3 +137,32 @@ func TestDetectTLS(t *testing.T) { } }) } + +func TestIRC(t *testing.T) { + msg := ircParseMessage( + `@first=a\:\s\r\n\\;2nd :srv hi there :good m8 :how are you?`) + + if !reflect.DeepEqual(msg.tags, map[string]string{ + "first": "a; \r\n\\", + "2nd": "", + }) { + t.Error("tags parsed incorrectly") + } + + if msg.nick != "srv" || msg.user != "" || msg.host != "" { + t.Error("server name parsed incorrectly") + } + if msg.command != "hi" { + t.Error("command name parsed incorrectly") + } + if !reflect.DeepEqual(msg.params, + []string{"there", "good m8 :how are you?"}) { + t.Error("params parsed incorrectly") + } + + if !ircEqual("[fag]^", "{FAG}~") { + t.Error("string case comparison not according to RFC 2812") + } + + // TODO: More tests. +} -- cgit v1.2.3-70-g09d2 From 6421892ef37206d4585437d2555a911570e8612b Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Sat, 1 Aug 2020 14:01:58 +0200 Subject: Name change --- xS/main.go | 2 +- xS/main_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'xS/main_test.go') diff --git a/xS/main.go b/xS/main.go index 8fcb6c2..28d6fd6 100644 --- a/xS/main.go +++ b/xS/main.go @@ -1,5 +1,5 @@ // -// Copyright (c) 2014 - 2018, Přemysl Janouch
+// Copyright (c) 2014 - 2018, Přemysl Eric Janouch
// // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted. diff --git a/xS/main_test.go b/xS/main_test.go index 4d6ccb3..8241b4e 100644 --- a/xS/main_test.go +++ b/xS/main_test.go @@ -1,5 +1,5 @@ // -// Copyright (c) 2015 - 2018, Přemysl Janouch
+// Copyright (c) 2015 - 2018, Přemysl Eric Janouch
// // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted. -- cgit v1.2.3-70-g09d2