1/*
2 * Copyright (c) 2014, Yawning Angel <yawning at schwanenlied dot me>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  * Redistributions of source code must retain the above copyright notice,
9 *    this list of conditions and the following disclaimer.
10 *
11 *  * Redistributions in binary form must reproduce the above copyright notice,
12 *    this list of conditions and the following disclaimer in the documentation
13 *    and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28package framing
29
30import (
31	"bytes"
32	"crypto/rand"
33	"testing"
34)
35
36func generateRandomKey() []byte {
37	key := make([]byte, KeyLength)
38
39	_, err := rand.Read(key)
40	if err != nil {
41		panic(err)
42	}
43
44	return key
45}
46
47func newEncoder(t *testing.T) *Encoder {
48	// Generate a key to use.
49	key := generateRandomKey()
50
51	encoder := NewEncoder(key)
52	if encoder == nil {
53		t.Fatalf("NewEncoder returned nil")
54	}
55
56	return encoder
57}
58
59// TestNewEncoder tests the Encoder ctor.
60func TestNewEncoder(t *testing.T) {
61	encoder := newEncoder(t)
62	_ = encoder
63}
64
65// TestEncoder_Encode tests Encoder.Encode.
66func TestEncoder_Encode(t *testing.T) {
67	encoder := newEncoder(t)
68
69	buf := make([]byte, MaximumFramePayloadLength)
70	_, _ = rand.Read(buf) // YOLO
71	for i := 0; i <= MaximumFramePayloadLength; i++ {
72		var frame [MaximumSegmentLength]byte
73		n, err := encoder.Encode(frame[:], buf[0:i])
74		if err != nil {
75			t.Fatalf("Encoder.encode([%d]byte), failed: %s", i, err)
76		}
77		if n != i+FrameOverhead {
78			t.Fatalf("Unexpected encoded framesize: %d, expecting %d", n, i+
79				FrameOverhead)
80		}
81	}
82}
83
84// TestEncoder_Encode_Oversize tests oversized frame rejection.
85func TestEncoder_Encode_Oversize(t *testing.T) {
86	encoder := newEncoder(t)
87
88	var frame [MaximumSegmentLength]byte
89	var buf [MaximumFramePayloadLength + 1]byte
90	_, _ = rand.Read(buf[:]) // YOLO
91	_, err := encoder.Encode(frame[:], buf[:])
92	if _, ok := err.(InvalidPayloadLengthError); !ok {
93		t.Error("Encoder.encode() returned unexpected error:", err)
94	}
95}
96
97// TestNewDecoder tests the Decoder ctor.
98func TestNewDecoder(t *testing.T) {
99	key := generateRandomKey()
100	decoder := NewDecoder(key)
101	if decoder == nil {
102		t.Fatalf("NewDecoder returned nil")
103	}
104}
105
106// TestDecoder_Decode tests Decoder.Decode.
107func TestDecoder_Decode(t *testing.T) {
108	key := generateRandomKey()
109
110	encoder := NewEncoder(key)
111	decoder := NewDecoder(key)
112
113	var buf [MaximumFramePayloadLength]byte
114	_, _ = rand.Read(buf[:]) // YOLO
115	for i := 0; i <= MaximumFramePayloadLength; i++ {
116		var frame [MaximumSegmentLength]byte
117		encLen, err := encoder.Encode(frame[:], buf[0:i])
118		if err != nil {
119			t.Fatalf("Encoder.encode([%d]byte), failed: %s", i, err)
120		}
121		if encLen != i+FrameOverhead {
122			t.Fatalf("Unexpected encoded framesize: %d, expecting %d", encLen,
123				i+FrameOverhead)
124		}
125
126		var decoded [MaximumFramePayloadLength]byte
127
128		decLen, err := decoder.Decode(decoded[:], bytes.NewBuffer(frame[:encLen]))
129		if err != nil {
130			t.Fatalf("Decoder.decode([%d]byte), failed: %s", i, err)
131		}
132		if decLen != i {
133			t.Fatalf("Unexpected decoded framesize: %d, expecting %d",
134				decLen, i)
135		}
136
137		if 0 != bytes.Compare(decoded[:decLen], buf[:i]) {
138			t.Fatalf("Frame %d does not match encoder input", i)
139		}
140	}
141}
142
143// BencharkEncoder_Encode benchmarks Encoder.Encode processing 1 MiB
144// of payload.
145func BenchmarkEncoder_Encode(b *testing.B) {
146	var chopBuf [MaximumFramePayloadLength]byte
147	var frame [MaximumSegmentLength]byte
148	payload := make([]byte, 1024*1024)
149	encoder := NewEncoder(generateRandomKey())
150	b.ResetTimer()
151
152	for i := 0; i < b.N; i++ {
153		transfered := 0
154		buffer := bytes.NewBuffer(payload)
155		for 0 < buffer.Len() {
156			n, err := buffer.Read(chopBuf[:])
157			if err != nil {
158				b.Fatal("buffer.Read() failed:", err)
159			}
160
161			n, _ = encoder.Encode(frame[:], chopBuf[:n])
162			transfered += n - FrameOverhead
163		}
164		if transfered != len(payload) {
165			b.Fatalf("Transfered length mismatch: %d != %d", transfered,
166				len(payload))
167		}
168	}
169}
170