forked from quic-go/quic-go
move EntropyAccumulator to AckHandler package
This commit is contained in:
18
ackhandler/entropy_accumulator.go
Normal file
18
ackhandler/entropy_accumulator.go
Normal 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)
|
||||
}
|
||||
25
ackhandler/entropy_accumulator_test.go
Normal file
25
ackhandler/entropy_accumulator_test.go
Normal 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())
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user