File: negotiate_test.go

package info (click to toggle)
vagrant 2.3.7%2Bgit20230731.5fc64cde%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 17,616 kB
  • sloc: ruby: 111,820; sh: 462; makefile: 123; ansic: 34; lisp: 1
file content (90 lines) | stat: -rw-r--r-- 2,105 bytes parent folder | download | duplicates (3)
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
package protocolversion

import (
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/hashicorp/vagrant/internal/server/proto/vagrant_server"
)

func TestNegotiate(t *testing.T) {
	cases := []struct {
		Name           string
		Client, Server *vagrant_server.VersionInfo_ProtocolVersion
		Err            string
		Vsn            uint32
	}{
		{
			"latest",
			&vagrant_server.VersionInfo_ProtocolVersion{Minimum: 1, Current: 10},
			&vagrant_server.VersionInfo_ProtocolVersion{Minimum: 1, Current: 10},
			"",
			10,
		},

		{
			"client older but can negotiate",
			&vagrant_server.VersionInfo_ProtocolVersion{Minimum: 1, Current: 6},
			&vagrant_server.VersionInfo_ProtocolVersion{Minimum: 4, Current: 10},
			"",
			6,
		},

		{
			"client older but can negotiate minimum",
			&vagrant_server.VersionInfo_ProtocolVersion{Minimum: 1, Current: 4},
			&vagrant_server.VersionInfo_ProtocolVersion{Minimum: 4, Current: 10},
			"",
			4,
		},

		{
			"server older but can negotiate",
			&vagrant_server.VersionInfo_ProtocolVersion{Minimum: 4, Current: 10},
			&vagrant_server.VersionInfo_ProtocolVersion{Minimum: 1, Current: 5},
			"",
			5,
		},

		{
			"server older but can negotiate minimum",
			&vagrant_server.VersionInfo_ProtocolVersion{Minimum: 4, Current: 10},
			&vagrant_server.VersionInfo_ProtocolVersion{Minimum: 1, Current: 4},
			"",
			4,
		},

		{
			"client too old",
			&vagrant_server.VersionInfo_ProtocolVersion{Minimum: 1, Current: 4},
			&vagrant_server.VersionInfo_ProtocolVersion{Minimum: 5, Current: 6},
			ErrClientOutdated.Error(),
			0,
		},

		{
			"server too old",
			&vagrant_server.VersionInfo_ProtocolVersion{Minimum: 5, Current: 6},
			&vagrant_server.VersionInfo_ProtocolVersion{Minimum: 1, Current: 4},
			ErrServerOutdated.Error(),
			0,
		},
	}

	for _, tt := range cases {
		t.Run(tt.Name, func(t *testing.T) {
			require := require.New(t)

			vsn, err := Negotiate(tt.Client, tt.Server)
			if tt.Err != "" {
				require.Error(err)
				require.Contains(err.Error(), tt.Err)
				return
			}

			require.NoError(err)
			require.Equal(vsn, tt.Vsn)
		})
	}
}