forked from quic-go/quic-go
move connection ID generation from the utils to the protocol package
This commit is contained in:
19
internal/protocol/connection_id.go
Normal file
19
internal/protocol/connection_id.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
)
|
||||
|
||||
// A ConnectionID in QUIC
|
||||
type ConnectionID uint64
|
||||
|
||||
// GenerateConnectionID generates a connection ID using cryptographic random
|
||||
func GenerateConnectionID() (ConnectionID, error) {
|
||||
b := make([]byte, 8)
|
||||
_, err := rand.Read(b)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return ConnectionID(binary.LittleEndian.Uint64(b)), nil
|
||||
}
|
||||
17
internal/protocol/connection_id_test.go
Normal file
17
internal/protocol/connection_id_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Connection ID generation", func() {
|
||||
It("generates random connection IDs", func() {
|
||||
c1, err := GenerateConnectionID()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(c1).ToNot(BeZero())
|
||||
c2, err := GenerateConnectionID()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(c1).ToNot(Equal(c2))
|
||||
})
|
||||
})
|
||||
@@ -52,9 +52,6 @@ func (t PacketType) String() string {
|
||||
}
|
||||
}
|
||||
|
||||
// A ConnectionID in QUIC
|
||||
type ConnectionID uint64
|
||||
|
||||
// A ByteCount in QUIC
|
||||
type ByteCount uint64
|
||||
|
||||
|
||||
Reference in New Issue
Block a user