github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/p9/version_test.go (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package p9 16 17 import ( 18 "testing" 19 ) 20 21 func TestVersionNumberEquivalent(t *testing.T) { 22 for i := uint32(0); i < 1024; i++ { 23 str := versionString(i) 24 version, ok := parseVersion(str) 25 if !ok { 26 t.Errorf("#%d: parseVersion(%q) failed, want success", i, str) 27 continue 28 } 29 if i != version { 30 t.Errorf("#%d: got version %d, want %d", i, i, version) 31 } 32 } 33 } 34 35 func TestVersionStringEquivalent(t *testing.T) { 36 // There is one case where the version is not equivalent on purpose, 37 // that is 9P2000.L.Google.0. It is not equivalent because versionString 38 // must always return the more generic 9P2000.L for legacy servers that 39 // check for it. See net/9p/client.c. 40 str := "9P2000.L.Google.0" 41 version, ok := parseVersion(str) 42 if !ok { 43 t.Errorf("parseVersion(%q) failed, want success", str) 44 } 45 if got := versionString(version); got != "9P2000.L" { 46 t.Errorf("versionString(%d) got %q, want %q", version, got, "9P2000.L") 47 } 48 49 for _, test := range []struct { 50 versionString string 51 }{ 52 { 53 versionString: "9P2000.L", 54 }, 55 { 56 versionString: "9P2000.L.Google.1", 57 }, 58 { 59 versionString: "9P2000.L.Google.347823894", 60 }, 61 } { 62 version, ok := parseVersion(test.versionString) 63 if !ok { 64 t.Errorf("parseVersion(%q) failed, want success", test.versionString) 65 continue 66 } 67 if got := versionString(version); got != test.versionString { 68 t.Errorf("versionString(%d) got %q, want %q", version, got, test.versionString) 69 } 70 } 71 } 72 73 func TestParseVersion(t *testing.T) { 74 for _, test := range []struct { 75 versionString string 76 expectSuccess bool 77 expectedVersion uint32 78 }{ 79 { 80 versionString: "9P", 81 expectSuccess: false, 82 }, 83 { 84 versionString: "9P.L", 85 expectSuccess: false, 86 }, 87 { 88 versionString: "9P200.L", 89 expectSuccess: false, 90 }, 91 { 92 versionString: "9P2000", 93 expectSuccess: false, 94 }, 95 { 96 versionString: "9P2000.L.Google.-1", 97 expectSuccess: false, 98 }, 99 { 100 versionString: "9P2000.L.Google.", 101 expectSuccess: false, 102 }, 103 { 104 versionString: "9P2000.L.Google.3546343826724305832", 105 expectSuccess: false, 106 }, 107 { 108 versionString: "9P2001.L", 109 expectSuccess: false, 110 }, 111 { 112 versionString: "9P2000.L", 113 expectSuccess: true, 114 expectedVersion: 0, 115 }, 116 { 117 versionString: "9P2000.L.Google.0", 118 expectSuccess: true, 119 expectedVersion: 0, 120 }, 121 { 122 versionString: "9P2000.L.Google.1", 123 expectSuccess: true, 124 expectedVersion: 1, 125 }, 126 } { 127 version, ok := parseVersion(test.versionString) 128 if ok != test.expectSuccess { 129 t.Errorf("parseVersion(%q) got (_, %v), want (_, %v)", test.versionString, ok, test.expectSuccess) 130 continue 131 } 132 if !test.expectSuccess { 133 continue 134 } 135 if version != test.expectedVersion { 136 t.Errorf("parseVersion(%q) got (%d, _), want (%d, _)", test.versionString, version, test.expectedVersion) 137 } 138 } 139 } 140 141 func BenchmarkParseVersion(b *testing.B) { 142 for n := 0; n < b.N; n++ { 143 parseVersion("9P2000.L.Google.1") 144 } 145 }