Create a gist now

Instantly share code, notes, and snippets.

@creack /main.go Secret
Last active Aug 29, 2015

What would you like to do?
Challenge 2 files
package main
import (
"flag"
"fmt"
"io"
"log"
"net"
"os"
)
// NewSecureReader instantiates a new SecureReader
func NewSecureReader(r io.Reader, priv, pub *[32]byte) io.Reader {
return nil
}
// NewSecureWriter instantiates a new SecureWriter
func NewSecureWriter(w io.Writer, priv, pub *[32]byte) io.Writer {
return nil
}
// Dial generates a private/public key pair,
// connects to the server, perform the handshake
// and return a reader/writer.
func Dial(addr string) (io.ReadWriteCloser, error) {
return nil, nil
}
// Serve starts a secure echo server on the given listener.
func Serve(l net.Listener) error {
return nil
}
func main() {
port := flag.Int("l", 0, "Listen mode. Specify port")
flag.Parse()
// Server mode
if *port != 0 {
l, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if err != nil {
log.Fatal(err)
}
defer l.Close()
log.Fatal(Serve(l))
}
// Client mode
if len(os.Args) != 3 {
log.Fatalf("Usage: %s <port> <message>", os.Args[0])
}
conn, err := Dial("localhost:" + os.Args[1])
if err != nil {
log.Fatal(err)
}
if _, err := conn.Write([]byte(os.Args[2])); err != nil {
log.Fatal(err)
}
buf := make([]byte, len(os.Args[2]))
n, err := conn.Read(buf)
if err != nil && err != io.EOF {
log.Fatal(err)
}
fmt.Printf("%s\n", buf[:n])
}
package main
import (
"fmt"
"io"
"io/ioutil"
"net"
"testing"
)
func TestReadWriterPing(t *testing.T) {
priv, pub := &[32]byte{'p', 'r', 'i', 'v'}, &[32]byte{'p', 'u', 'b'}
r, w := io.Pipe()
secureR := NewSecureReader(r, priv, pub)
secureW := NewSecureWriter(w, priv, pub)
// Encrypt hello world
go func() {
fmt.Fprintf(secureW, "hello world\n")
w.Close()
}()
// Decrypt message
buf := make([]byte, 1024)
n, err := secureR.Read(buf)
if err != nil && err != io.EOF {
t.Fatal(err)
}
buf = buf[:n]
// Make sure we have hello world back
if res := string(buf); res != "hello world\n" {
t.Fatalf("Unexpected result: %s != %s", res, "hello world")
}
}
func TestSecureWriter(t *testing.T) {
priv, pub := &[32]byte{'p', 'r', 'i', 'v'}, &[32]byte{'p', 'u', 'b'}
r, w := io.Pipe()
secureW := NewSecureWriter(w, priv, pub)
// Make sure we are secure
// Encrypt hello world
go func() {
fmt.Fprintf(secureW, "hello world\n")
w.Close()
}()
// Read from the underlying transport instead of the decoder
buf, err := ioutil.ReadAll(r)
if err != nil {
t.Fatal(err)
}
// Make sure we dont' read the plain text message.
if res := string(buf); res == "hello world\n" {
t.Fatal("Unexpected result. The message is not encrypted.")
}
r, w = io.Pipe()
secureW = NewSecureWriter(w, priv, pub)
// Make sure we are unique
// Encrypt hello world
go func() {
fmt.Fprintf(secureW, "hello world\n")
w.Close()
}()
// Read from the underlying transport instead of the decoder
buf2, err := ioutil.ReadAll(r)
if err != nil {
t.Fatal(err)
}
// Make sure we dont' read the plain text message.
if string(buf) == string(buf2) {
t.Fatal("Unexpected result. The encrypted message is not unique.")
}
}
func TestSecureEchoServer(t *testing.T) {
// Create a random listener
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer l.Close()
// Start the server
go Serve(l)
conn, err := Dial(l.Addr().String())
if err != nil {
t.Fatal(err)
}
defer conn.Close()
expected := "hello world\n"
if _, err := fmt.Fprintf(conn, expected); err != nil {
t.Fatal(err)
}
buf := make([]byte, 2048)
n, err := conn.Read(buf)
if err != nil && err != io.EOF {
t.Fatal(err)
}
if got := string(buf[:n]); got != expected {
t.Fatalf("Unexpected result:\nGot:\t\t%s\nExpected:\t%s\n", got, expected)
}
}
func TestSecureServe(t *testing.T) {
// Create a random listener
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer l.Close()
// Start the server
go Serve(l)
conn, err := net.Dial("tcp", l.Addr().String())
if err != nil {
t.Fatal(err)
}
unexpected := "hello world\n"
if _, err := fmt.Fprintf(conn, unexpected); err != nil {
t.Fatal(err)
}
buf := make([]byte, 2048)
n, err := conn.Read(buf)
if err != nil && err != io.EOF {
t.Fatal(err)
}
if got := string(buf[:n]); got == unexpected {
t.Fatalf("Unexpected result:\nGot raw data instead of serialized key")
}
}
func TestSecureDial(t *testing.T) {
// Create a random listener
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer l.Close()
// Start the server
go func(l net.Listener) {
for {
conn, err := l.Accept()
if err != nil {
return
}
go func(c net.Conn) {
defer c.Close()
key := [32]byte{}
c.Write(key[:])
buf := make([]byte, 2048)
n, err := c.Read(buf)
if err != nil && err != io.EOF {
t.Fatal(err)
}
if got := string(buf[:n]); got == "hello world\n" {
t.Fatal("Unexpected result. Got raw data instead of encrypted")
}
}(conn)
}
}(l)
conn, err := Dial(l.Addr().String())
if err != nil {
t.Fatal(err)
}
defer conn.Close()
expected := "hello world\n"
if _, err := fmt.Fprintf(conn, expected); err != nil {
t.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment