go test: make more idiomatic
- gofmt - Give config struct one line per field - Use camel case - Check errors - Log invariants with detail - Use consistent pronouns Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
7887d8024c
commit
85a14af6b0
|
@ -3,61 +3,86 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/titanous/noise"
|
|
||||||
"net"
|
|
||||||
"time"
|
|
||||||
"bytes"
|
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/dchest/blake2s"
|
"github.com/dchest/blake2s"
|
||||||
|
"github.com/titanous/noise"
|
||||||
)
|
)
|
||||||
|
|
||||||
func assert(exp bool) {
|
|
||||||
if !exp {
|
|
||||||
panic("Assertion failed.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
my_private, _ := base64.StdEncoding.DecodeString("WAmgVYXkbT2bCtdcDwolI88/iVi/aV3/PHcUBTQSYmo=")
|
ourPrivate, _ := base64.StdEncoding.DecodeString("WAmgVYXkbT2bCtdcDwolI88/iVi/aV3/PHcUBTQSYmo=")
|
||||||
my_public, _ := base64.StdEncoding.DecodeString("K5sF9yESrSBsOXPd6TcpKNgqoy1Ik3ZFKl4FolzrRyI=")
|
ourPublic, _ := base64.StdEncoding.DecodeString("K5sF9yESrSBsOXPd6TcpKNgqoy1Ik3ZFKl4FolzrRyI=")
|
||||||
preshared, _ := base64.StdEncoding.DecodeString("FpCyhws9cxwWoV4xELtfJvjJN+zQVRPISllRWgeopVE=")
|
preshared, _ := base64.StdEncoding.DecodeString("FpCyhws9cxwWoV4xELtfJvjJN+zQVRPISllRWgeopVE=")
|
||||||
their_public, _ := base64.StdEncoding.DecodeString("qRCwZSKInrMAq5sepfCdaCsRJaoLe5jhtzfiw7CjbwM=")
|
theirPublic, _ := base64.StdEncoding.DecodeString("qRCwZSKInrMAq5sepfCdaCsRJaoLe5jhtzfiw7CjbwM=")
|
||||||
cs := noise.NewCipherSuite(noise.DH25519, noise.CipherChaChaPoly, noise.HashBLAKE2s)
|
cs := noise.NewCipherSuite(noise.DH25519, noise.CipherChaChaPoly, noise.HashBLAKE2s)
|
||||||
hs := noise.NewHandshakeState(noise.Config{CipherSuite: cs, Random: rand.Reader, Pattern: noise.HandshakeIK, Initiator: true, Prologue: []byte("WireGuard v0 zx2c4 Jason@zx2c4.com"), PresharedKey: preshared, StaticKeypair: noise.DHKey{Private: my_private, Public: my_public}, PeerStatic: their_public})
|
hs := noise.NewHandshakeState(noise.Config{
|
||||||
conn, _ := net.Dial("udp", "test.wireguard.io:51820")
|
CipherSuite: cs,
|
||||||
|
Random: rand.Reader,
|
||||||
|
Pattern: noise.HandshakeIK,
|
||||||
|
Initiator: true,
|
||||||
|
Prologue: []byte("WireGuard v0 zx2c4 Jason@zx2c4.com"),
|
||||||
|
PresharedKey: preshared,
|
||||||
|
StaticKeypair: noise.DHKey{Private: ourPrivate, Public: ourPublic},
|
||||||
|
PeerStatic: theirPublic,
|
||||||
|
})
|
||||||
|
conn, err := net.Dial("udp", "test.wireguard.io:51820")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("error dialing udp socket: %s", err)
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
tai64n := make([]byte, 12)
|
tai64n := make([]byte, 12)
|
||||||
binary.BigEndian.PutUint64(tai64n[:], uint64(now.Unix()))
|
binary.BigEndian.PutUint64(tai64n[:], uint64(now.Unix()))
|
||||||
binary.BigEndian.PutUint32(tai64n[8:], uint32(now.UnixNano()))
|
binary.BigEndian.PutUint32(tai64n[8:], uint32(now.UnixNano()))
|
||||||
initiation_packet := make([]byte, 5)
|
initiationPacket := make([]byte, 5)
|
||||||
initiation_packet[0] = 1 /* Type: Initiation */
|
initiationPacket[0] = 1 // Type: Initiation
|
||||||
binary.LittleEndian.PutUint32(initiation_packet[1:], 28) /* Sender index: 28 (arbitrary) */
|
binary.LittleEndian.PutUint32(initiationPacket[1:], 28) // Sender index: 28 (arbitrary)
|
||||||
initiation_packet, _, _ = hs.WriteMessage(initiation_packet, tai64n)
|
initiationPacket, _, _ = hs.WriteMessage(initiationPacket, tai64n)
|
||||||
hasher, _ := blake2s.New(&blake2s.Config{Size: 16, Key: preshared})
|
hasher, _ := blake2s.New(&blake2s.Config{Size: 16, Key: preshared})
|
||||||
hasher.Write(their_public)
|
hasher.Write(theirPublic)
|
||||||
hasher.Write(initiation_packet)
|
hasher.Write(initiationPacket)
|
||||||
initiation_packet = append(initiation_packet, hasher.Sum(nil)[:16]...)
|
initiationPacket = append(initiationPacket, hasher.Sum(nil)[:16]...)
|
||||||
initiation_packet = append(initiation_packet, bytes.Repeat([]byte{ 0 }, 16)...)
|
initiationPacket = append(initiationPacket, make([]byte, 16)...)
|
||||||
conn.Write(initiation_packet)
|
if _, err := conn.Write(initiationPacket); err != nil {
|
||||||
|
log.Fatalf("error writing initiation packet: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
response_packet := make([]byte, 89)
|
responsePacket := make([]byte, 89)
|
||||||
conn.Read(response_packet)
|
n, err := conn.Read(responsePacket)
|
||||||
assert(response_packet[0] == 2 /* Type: Response */)
|
if err != nil {
|
||||||
their_index := binary.LittleEndian.Uint32(response_packet[1:])
|
log.Fatalf("error reading response packet: %s", err)
|
||||||
our_index := binary.LittleEndian.Uint32(response_packet[5:])
|
}
|
||||||
assert(our_index == 28)
|
if n != len(responsePacket) {
|
||||||
payload, send_cs, _, err := hs.ReadMessage(nil, response_packet[9:57])
|
log.Fatalf("response packet too short: want %d, got %d", len(responsePacket), n)
|
||||||
assert(len(payload) == 0 && err == nil)
|
}
|
||||||
|
if responsePacket[0] != 2 { // Type: Response
|
||||||
|
log.Fatalf("response packet type wrong: want %d, got %d", 2, responsePacket[0])
|
||||||
|
}
|
||||||
|
theirIndex := binary.LittleEndian.Uint32(responsePacket[1:])
|
||||||
|
ourIndex := binary.LittleEndian.Uint32(responsePacket[5:])
|
||||||
|
if ourIndex != 28 {
|
||||||
|
log.Fatalf("response packet index wrong: want %d, got %d", 28, ourIndex)
|
||||||
|
}
|
||||||
|
payload, sendCipher, _, err := hs.ReadMessage(nil, responsePacket[9:57])
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("error reading handshake message: %s", err)
|
||||||
|
}
|
||||||
|
if len(payload) > 0 {
|
||||||
|
log.Fatalf("unexpected payload: %x", payload)
|
||||||
|
}
|
||||||
|
|
||||||
keepalive_packet := make([]byte, 13)
|
keepalivePacket := make([]byte, 13)
|
||||||
keepalive_packet[0] = 4 /* Type: Data */
|
keepalivePacket[0] = 4 // Type: Data
|
||||||
binary.LittleEndian.PutUint32(keepalive_packet[1:], their_index)
|
binary.LittleEndian.PutUint32(keepalivePacket[1:], theirIndex)
|
||||||
binary.LittleEndian.PutUint64(keepalive_packet[3:], 0) /* Nonce */
|
binary.LittleEndian.PutUint64(keepalivePacket[3:], 0) // Nonce
|
||||||
keepalive_packet = send_cs.Encrypt(keepalive_packet, nil, nil)
|
keepalivePacket = sendCipher.Encrypt(keepalivePacket, nil, nil)
|
||||||
conn.Write(keepalive_packet)
|
if _, err := conn.Write(keepalivePacket); err != nil {
|
||||||
|
log.Fatalf("error writing keepalive packet: %s", err)
|
||||||
conn.Close()
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue