move EntropyAccumulator to AckHandler package

This commit is contained in:
Marten Seemann
2016-04-21 08:41:54 +07:00
parent 6d9d9a1c29
commit 4b0b8def8d
3 changed files with 7 additions and 6 deletions

View File

@@ -0,0 +1,18 @@
package ackhandler
import "github.com/lucas-clemente/quic-go/protocol"
// EntropyAccumulator accumulates the entropy according to the QUIC docs
type EntropyAccumulator byte
// Add the contribution of the entropy flag of a given packet number
func (e *EntropyAccumulator) Add(packetNumber protocol.PacketNumber, entropyFlag bool) {
if entropyFlag {
(*e) ^= 0x01 << (packetNumber % 8)
}
}
// Get the byte of entropy
func (e *EntropyAccumulator) Get() byte {
return byte(*e)
}

View File

@@ -0,0 +1,25 @@
package ackhandler
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("EntropyAccumulator", func() {
It("initializes as zero", func() {
var e EntropyAccumulator
Expect(e.Get()).To(BeZero())
})
It("adds entropy", func() {
var e EntropyAccumulator
e.Add(9, true)
Expect(e.Get()).To(Equal(byte(0x02)))
})
It("doesn't add entropy for zero entropy flags", func() {
var e EntropyAccumulator
e.Add(9, false)
Expect(e.Get()).To(BeZero())
})
})