add Substract method for EntropyAccumulator

This commit is contained in:
Marten Seemann
2016-04-25 19:04:15 +07:00
parent a3ecf2ee69
commit c6b69e632e
2 changed files with 29 additions and 8 deletions

View File

@@ -12,6 +12,11 @@ func (e *EntropyAccumulator) Add(packetNumber protocol.PacketNumber, entropyFlag
}
}
// Add the contribution of the entropy flag of a given packet number
func (e *EntropyAccumulator) Substract(packetNumber protocol.PacketNumber, entropyFlag bool) {
e.Add(packetNumber, entropyFlag)
}
// Get the byte of entropy
func (e *EntropyAccumulator) Get() byte {
return byte(*e)

View File

@@ -11,15 +11,31 @@ var _ = Describe("EntropyAccumulator", func() {
Expect(e.Get()).To(BeZero())
})
It("adds entropy", func() {
var e EntropyAccumulator
e.Add(9, true)
Expect(e.Get()).To(Equal(byte(0x02)))
Context("Add", func() {
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())
})
})
It("doesn't add entropy for zero entropy flags", func() {
var e EntropyAccumulator
e.Add(9, false)
Expect(e.Get()).To(BeZero())
Context("Substract", func() {
It("calculates the correct entropy", func() {
var e1 EntropyAccumulator
e1.Add(3, true)
var e2 EntropyAccumulator
e2.Add(1, true)
e2.Add(3, true)
e2.Substract(1, true)
Expect(e1).To(Equal(e2))
})
})
})