File: http_test.go

package info (click to toggle)
websocketd 0.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 612 kB
  • sloc: makefile: 131; sh: 85; ansic: 78; javascript: 65; cs: 58; perl: 51; python: 43; ruby: 41; php: 40; java: 26; haskell: 20
file content (136 lines) | stat: -rw-r--r-- 5,095 bytes parent folder | download
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
package libwebsocketd

import (
	"bufio"
	"crypto/tls"
	"fmt"
	"net/http"
	"strings"
	"testing"
)

var tellHostPortTests = []struct {
	src          string
	ssl          bool
	server, port string
}{
	{"localhost", false, "localhost", "80"},
	{"localhost:8080", false, "localhost", "8080"},
	{"localhost", true, "localhost", "443"},
	{"localhost:8080", true, "localhost", "8080"},
}

func TestTellHostPort(t *testing.T) {
	for _, testcase := range tellHostPortTests {
		s, p, e := tellHostPort(testcase.src, testcase.ssl)
		if testcase.server == "" {
			if e == nil {
				t.Errorf("test case for %#v failed, error was not returned", testcase.src)
			}
		} else if e != nil {
			t.Errorf("test case for %#v failed, error should not happen", testcase.src)
		}
		if testcase.server != s || testcase.port != p {
			t.Errorf("test case for %#v failed, server or port mismatch to expected values (%s:%s)", testcase.src, s, p)
		}
	}
}

var NoOriginsAllowed = []string{}
var NoOriginList []string = nil

const (
	ReqHTTPS = iota
	ReqHTTP
	OriginMustBeSame
	OriginCouldDiffer
	ReturnsPass
	ReturnsError
)

var CheckOriginTests = []struct {
	host    string
	reqtls  int
	origin  string
	same    int
	allowed []string
	getsErr int
	name    string
}{
	{"server.example.com", ReqHTTP, "http://example.com", OriginCouldDiffer, NoOriginList, ReturnsPass, "any origin allowed"},
	{"server.example.com", ReqHTTP, "http://example.com", OriginMustBeSame, NoOriginList, ReturnsError, "same origin mismatch"},
	{"server.example.com", ReqHTTP, "http://server.example.com", OriginMustBeSame, NoOriginList, ReturnsPass, "same origin match"},
	{"server.example.com", ReqHTTP, "https://server.example.com", OriginMustBeSame, NoOriginList, ReturnsError, "same origin schema mismatch 1"},
	{"server.example.com", ReqHTTPS, "http://server.example.com", OriginMustBeSame, NoOriginList, ReturnsError, "same origin schema mismatch 2"},
	{"server.example.com", ReqHTTP, "http://example.com", OriginCouldDiffer, NoOriginsAllowed, ReturnsError, "no origins allowed"},
	{"server.example.com", ReqHTTP, "http://example.com", OriginCouldDiffer, []string{"server.example.com"}, ReturnsError, "no origin allowed matches (junk prefix)"},
	{"server.example.com", ReqHTTP, "http://example.com", OriginCouldDiffer, []string{"example.com.t"}, ReturnsError, "no origin allowed matches (junk suffix)"},
	{"server.example.com", ReqHTTP, "http://example.com", OriginCouldDiffer, []string{"example.com"}, ReturnsPass, "origin allowed clean match"},
	{"server.example.com", ReqHTTP, "http://example.com:81", OriginCouldDiffer, []string{"example.com"}, ReturnsPass, "origin allowed any port match"},
	{"server.example.com", ReqHTTP, "http://example.com", OriginCouldDiffer, []string{"example.com:80"}, ReturnsPass, "origin allowed port match"},
	{"server.example.com", ReqHTTP, "http://example.com", OriginCouldDiffer, []string{"example.com:81"}, ReturnsError, "origin allowed port mismatch"},
	{"server.example.com", ReqHTTP, "http://example.com", OriginCouldDiffer, []string{"example.com:81"}, ReturnsError, "origin allowed port mismatch"},
	{"server.example.com", ReqHTTP, "http://example.com:81", OriginCouldDiffer, []string{"example.com:81"}, ReturnsPass, "origin allowed port 81 match"},
	{"server.example.com", ReqHTTP, "null", OriginCouldDiffer, NoOriginList, ReturnsPass, "any origin allowed, even null"},
	{"server.example.com", ReqHTTP, "", OriginCouldDiffer, NoOriginList, ReturnsPass, "any origin allowed, even empty"},
}

// CONVERT GORILLA
// as method for origin checking changes to handle things without websocket.Config the test
// should be altered too

func TestCheckOrigin(t *testing.T) {
	for _, testcase := range CheckOriginTests {
		br := bufio.NewReader(strings.NewReader(fmt.Sprintf(`GET /chat HTTP/1.1
Host: %s
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Origin: %s
Sec-WebSocket-Version: 13

`, testcase.host, testcase.origin)))

		req, err := http.ReadRequest(br)
		if err != nil {
			t.Fatal("request", err)
		}

		log := new(LogScope)
		log.LogFunc = func(*LogScope, LogLevel, string, string, string, ...interface{}) {}

		config := new(Config)

		if testcase.reqtls == ReqHTTPS { // Fake TLS
			config.Ssl = true
			req.TLS = &tls.ConnectionState{}
		}
		if testcase.same == OriginMustBeSame {
			config.SameOrigin = true
		}
		if testcase.allowed != nil {
			config.AllowOrigins = testcase.allowed
		}

		err = checkOrigin(req, config, log)
		if testcase.getsErr == ReturnsError && err == nil {
			t.Errorf("Test case %#v did not get an error", testcase.name)
		} else if testcase.getsErr == ReturnsPass && err != nil {
			t.Errorf("Test case %#v got error while expected to pass", testcase.name)
		}
	}
}

var mimetest = [][3]string{
	{"Content-Type: text/plain", "Content-Type", "text/plain"},
	{"Content-Type:    ", "Content-Type", ""},
}

func TestSplitMimeHeader(t *testing.T) {
	for _, tst := range mimetest {
		s, v := splitMimeHeader(tst[0])
		if tst[1] != s || tst[2] != v {
			t.Errorf("%v and %v  are not same as expexted %v and %v", s, v, tst[1], tst[2])
		}
	}
}