summaryrefslogtreecommitdiffhomepage
path: root/pkg/p9/version_test.go
blob: 634ac3ca5c84107ae2846da86fbbd4889d6ce7a3 (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
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package p9

import (
	"testing"
)

func TestVersionNumberEquivalent(t *testing.T) {
	for i := uint32(0); i < 1024; i++ {
		str := versionString(i)
		version, ok := parseVersion(str)
		if !ok {
			t.Errorf("#%d: parseVersion(%q) failed, want success", i, str)
			continue
		}
		if i != version {
			t.Errorf("#%d: got version %d, want %d", i, i, version)
		}
	}
}

func TestVersionStringEquivalent(t *testing.T) {
	// There is one case where the version is not equivalent on purpose,
	// that is 9P2000.L.Google.0.  It is not equivalent because versionString
	// must always return the more generic 9P2000.L for legacy servers that
	// check for it.  See net/9p/client.c.
	str := "9P2000.L.Google.0"
	version, ok := parseVersion(str)
	if !ok {
		t.Errorf("parseVersion(%q) failed, want success", str)
	}
	if got := versionString(version); got != "9P2000.L" {
		t.Errorf("versionString(%d) got %q, want %q", version, got, "9P2000.L")
	}

	for _, test := range []struct {
		versionString string
	}{
		{
			versionString: "9P2000.L",
		},
		{
			versionString: "9P2000.L.Google.1",
		},
		{
			versionString: "9P2000.L.Google.347823894",
		},
	} {
		version, ok := parseVersion(test.versionString)
		if !ok {
			t.Errorf("parseVersion(%q) failed, want success", test.versionString)
			continue
		}
		if got := versionString(version); got != test.versionString {
			t.Errorf("versionString(%d) got %q, want %q", version, got, test.versionString)
		}
	}
}

func TestParseVersion(t *testing.T) {
	for _, test := range []struct {
		versionString   string
		expectSuccess   bool
		expectedVersion uint32
	}{
		{
			versionString: "9P",
			expectSuccess: false,
		},
		{
			versionString: "9P.L",
			expectSuccess: false,
		},
		{
			versionString: "9P200.L",
			expectSuccess: false,
		},
		{
			versionString: "9P2000",
			expectSuccess: false,
		},
		{
			versionString: "9P2000.L.Google.-1",
			expectSuccess: false,
		},
		{
			versionString: "9P2000.L.Google.",
			expectSuccess: false,
		},
		{
			versionString: "9P2000.L.Google.3546343826724305832",
			expectSuccess: false,
		},
		{
			versionString: "9P2001.L",
			expectSuccess: false,
		},
		{
			versionString:   "9P2000.L",
			expectSuccess:   true,
			expectedVersion: 0,
		},
		{
			versionString:   "9P2000.L.Google.0",
			expectSuccess:   true,
			expectedVersion: 0,
		},
		{
			versionString:   "9P2000.L.Google.1",
			expectSuccess:   true,
			expectedVersion: 1,
		},
	} {
		version, ok := parseVersion(test.versionString)
		if ok != test.expectSuccess {
			t.Errorf("parseVersion(%q) got (_, %v), want (_, %v)", test.versionString, ok, test.expectSuccess)
			continue
		}
		if !test.expectSuccess {
			continue
		}
		if version != test.expectedVersion {
			t.Errorf("parseVersion(%q) got (%d, _), want (%d, _)", test.versionString, version, test.expectedVersion)
		}
	}
}

func BenchmarkParseVersion(b *testing.B) {
	for n := 0; n < b.N; n++ {
		parseVersion("9P2000.L.Google.1")
	}
}