Welcome to mirror list, hosted at ThFree Co, Russian Federation.

channel_settings_test.go « api « internal « workhorse - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4aa2c835579f21d32e157aeeb02c7b54f39a7f60 (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
package api

import (
	"net/http"
	"testing"
)

func channel(url string, subprotocols ...string) *ChannelSettings {
	return &ChannelSettings{
		Url:            url,
		Subprotocols:   subprotocols,
		MaxSessionTime: 0,
	}
}

func ca(channel *ChannelSettings) *ChannelSettings {
	channel = channel.Clone()
	channel.CAPem = "Valid CA data"

	return channel
}

func timeout(channel *ChannelSettings) *ChannelSettings {
	channel = channel.Clone()
	channel.MaxSessionTime = 600

	return channel
}

func header(channel *ChannelSettings, values ...string) *ChannelSettings {
	if len(values) == 0 {
		values = []string{"Dummy Value"}
	}

	channel = channel.Clone()
	channel.Header = http.Header{
		"Header": values,
	}

	return channel
}

func TestClone(t *testing.T) {
	a := ca(header(channel("ws:", "", "")))
	b := a.Clone()

	if a == b {
		t.Fatalf("Address of cloned channel didn't change")
	}

	if &a.Subprotocols == &b.Subprotocols {
		t.Fatalf("Address of cloned subprotocols didn't change")
	}

	if &a.Header == &b.Header {
		t.Fatalf("Address of cloned header didn't change")
	}
}

func TestValidate(t *testing.T) {
	for i, tc := range []struct {
		channel *ChannelSettings
		valid   bool
		msg     string
	}{
		{nil, false, "nil channel"},
		{channel("", ""), false, "empty URL"},
		{channel("ws:"), false, "empty subprotocols"},
		{channel("ws:", "foo"), true, "any subprotocol"},
		{channel("ws:", "foo", "bar"), true, "multiple subprotocols"},
		{channel("ws:", ""), true, "websocket URL"},
		{channel("wss:", ""), true, "secure websocket URL"},
		{channel("http:", ""), false, "HTTP URL"},
		{channel("https:", ""), false, " HTTPS URL"},
		{ca(channel("ws:", "")), true, "any CA pem"},
		{header(channel("ws:", "")), true, "any headers"},
		{ca(header(channel("ws:", ""))), true, "PEM and headers"},
	} {
		if err := tc.channel.Validate(); (err != nil) == tc.valid {
			t.Fatalf("test case %d: "+tc.msg+": valid=%v: %s: %+v", i, tc.valid, err, tc.channel)
		}
	}
}

func TestDialer(t *testing.T) {
	channel := channel("ws:", "foo")
	dialer := channel.Dialer()

	if len(dialer.Subprotocols) != len(channel.Subprotocols) {
		t.Fatalf("Subprotocols don't match: %+v vs. %+v", channel.Subprotocols, dialer.Subprotocols)
	}

	for i, subprotocol := range channel.Subprotocols {
		if dialer.Subprotocols[i] != subprotocol {
			t.Fatalf("Subprotocols don't match: %+v vs. %+v", channel.Subprotocols, dialer.Subprotocols)
		}
	}

	if dialer.TLSClientConfig != nil {
		t.Fatalf("Unexpected TLSClientConfig: %+v", dialer)
	}

	channel = ca(channel)
	dialer = channel.Dialer()

	if dialer.TLSClientConfig == nil || dialer.TLSClientConfig.RootCAs == nil {
		t.Fatalf("Custom CA certificates not recognised!")
	}
}

func TestIsEqual(t *testing.T) {
	chann := channel("ws:", "foo")

	chann_header2 := header(chann, "extra")
	chann_header3 := header(chann)
	chann_header3.Header.Add("Extra", "extra")

	chann_ca2 := ca(chann)
	chann_ca2.CAPem = "other value"

	for i, tc := range []struct {
		channelA *ChannelSettings
		channelB *ChannelSettings
		expected bool
	}{
		{nil, nil, true},
		{chann, nil, false},
		{nil, chann, false},
		{chann, chann, true},
		{chann.Clone(), chann.Clone(), true},
		{chann, channel("foo:"), false},
		{chann, channel(chann.Url), false},
		{header(chann), header(chann), true},
		{chann_header2, chann_header2, true},
		{chann_header3, chann_header3, true},
		{header(chann), chann_header2, false},
		{header(chann), chann_header3, false},
		{header(chann), chann, false},
		{chann, header(chann), false},
		{ca(chann), ca(chann), true},
		{ca(chann), chann, false},
		{chann, ca(chann), false},
		{ca(header(chann)), ca(header(chann)), true},
		{chann_ca2, ca(chann), false},
		{chann, timeout(chann), false},
	} {
		if actual := tc.channelA.IsEqual(tc.channelB); tc.expected != actual {
			t.Fatalf(
				"test case %d: Comparison:\n-%+v\n+%+v\nexpected=%v: actual=%v",
				i, tc.channelA, tc.channelB, tc.expected, actual,
			)
		}
	}
}