aboutsummaryrefslogtreecommitdiff
path: root/xS/main_test.go
blob: 4d6ccb3edca47c701ffde31807e7d771a806f890 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
// Copyright (c) 2015 - 2018, Přemysl Janouch <p@janouch.name>
//
// 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")
		}
	})
}

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.
}