From 9db23eceed87fb86b32e749b1874f7ff3ec8cbdc Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sat, 29 Apr 2017 20:13:23 +0700 Subject: [PATCH] reverse the order of the SupportedVersions slice For the client, the Versions option in the quic.Config encodes the first entry is the preferred version. If not set, this should default to the highest supported version. --- h2quic/server.go | 6 +++--- protocol/version.go | 8 ++++---- protocol/version_test.go | 8 ++++---- server_test.go | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/h2quic/server.go b/h2quic/server.go index 1a82a451..a2e0f197 100644 --- a/h2quic/server.go +++ b/h2quic/server.go @@ -274,9 +274,9 @@ func (s *Server) SetQuicHeaders(hdr http.Header) error { } if s.supportedVersionsAsString == "" { - for i := len(protocol.SupportedVersions) - 1; i >= 0; i-- { - s.supportedVersionsAsString += strconv.Itoa(int(protocol.SupportedVersions[i])) - if i != 0 { + for i, v := range protocol.SupportedVersions { + s.supportedVersionsAsString += strconv.Itoa(int(v)) + if i != len(protocol.SupportedVersions)-1 { s.supportedVersionsAsString += "," } } diff --git a/protocol/version.go b/protocol/version.go index 56d6abd6..f1959632 100644 --- a/protocol/version.go +++ b/protocol/version.go @@ -13,9 +13,9 @@ const ( ) // SupportedVersions lists the versions that the server supports -// must be in sorted order +// must be in sorted descending order var SupportedVersions = []VersionNumber{ - Version35, Version36, Version37, + Version37, Version36, Version35, } // VersionNumberToTag maps version numbers ('32') to tags ('Q032') @@ -50,9 +50,9 @@ func HighestSupportedVersion(other []VersionNumber) (bool, VersionNumber) { } } - for i := len(SupportedVersions) - 1; i >= 0; i-- { + for _, v := range SupportedVersions { for _, ver := range otherSupported { - if ver == SupportedVersions[i] { + if ver == v { return true, ver } } diff --git a/protocol/version_test.go b/protocol/version_test.go index ebe65e93..d37784f0 100644 --- a/protocol/version_test.go +++ b/protocol/version_test.go @@ -22,7 +22,7 @@ var _ = Describe("Version", func() { It("has supported versions in sorted order", func() { for i := 0; i < len(SupportedVersions)-1; i++ { - Expect(SupportedVersions[i]).To(BeNumerically("<", SupportedVersions[i+1])) + Expect(SupportedVersions[i]).To(BeNumerically(">", SupportedVersions[i+1])) } }) @@ -39,7 +39,7 @@ var _ = Describe("Version", func() { }) It("finds the supported version", func() { - SupportedVersions = []VersionNumber{1, 2, 3} + SupportedVersions = []VersionNumber{3, 2, 1} other := []VersionNumber{3, 4, 5, 6} found, ver := HighestSupportedVersion(other) Expect(found).To(BeTrue()) @@ -47,7 +47,7 @@ var _ = Describe("Version", func() { }) It("picks the highest supported version", func() { - SupportedVersions = []VersionNumber{1, 2, 3, 6, 7} + SupportedVersions = []VersionNumber{7, 6, 3, 2, 1} other := []VersionNumber{3, 6, 1, 8, 2, 10} found, ver := HighestSupportedVersion(other) Expect(found).To(BeTrue()) @@ -55,7 +55,7 @@ var _ = Describe("Version", func() { }) It("handles empty inputs", func() { - SupportedVersions = []VersionNumber{101, 102} + SupportedVersions = []VersionNumber{102, 101} Expect(HighestSupportedVersion([]VersionNumber{})).To(BeFalse()) SupportedVersions = []VersionNumber{} Expect(HighestSupportedVersion([]VersionNumber{1, 2})).To(BeFalse()) diff --git a/server_test.go b/server_test.go index c2c92893..4c829aaa 100644 --- a/server_test.go +++ b/server_test.go @@ -264,7 +264,7 @@ var _ = Describe("Server", func() { Expect(serv.sessions[connID].(*mockSession).packetCount).To(Equal(1)) b := &bytes.Buffer{} // add an unsupported version - utils.WriteUint32(b, protocol.VersionNumberToTag(protocol.SupportedVersions[0]-2)) + utils.WriteUint32(b, protocol.VersionNumberToTag(protocol.SupportedVersions[0]+1)) data := []byte{0x09, 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c} data = append(append(data, b.Bytes()...), 0x01) err = serv.handlePacket(nil, nil, data)