This repository has been archived by the owner on Feb 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
buffer outgoing writes into a single buffer
- Loading branch information
1 parent
29d3c8a
commit fa92b06
Showing
2 changed files
with
102 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package secio | ||
|
||
import ( | ||
"bytes" | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"io" | ||
"testing" | ||
) | ||
|
||
type keyInfo struct { | ||
cipherKey []byte | ||
iv []byte | ||
macKey []byte | ||
} | ||
|
||
func getTestKeyInfo() *keyInfo { | ||
return &keyInfo{ | ||
cipherKey: []byte("this is a test keyaaaaaaaaaaaaaa"), | ||
iv: make([]byte, 16), | ||
macKey: []byte("key for the mac"), | ||
} | ||
} | ||
|
||
func getTestingWriter(w io.Writer, ki *keyInfo) (*etmWriter, error) { | ||
c, err := aes.NewCipher(ki.cipherKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
stream := cipher.NewCFBEncrypter(c, ki.iv) | ||
|
||
mac, err := newMac("SHA256", ki.macKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return NewETMWriter(w, stream, mac).(*etmWriter), nil | ||
} | ||
|
||
func getTestingReader(r io.Reader, ki *keyInfo) (*etmReader, error) { | ||
c, err := aes.NewCipher(ki.cipherKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
stream := cipher.NewCFBDecrypter(c, ki.iv) | ||
|
||
mac, err := newMac("SHA256", ki.macKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return NewETMReader(r, stream, mac).(*etmReader), nil | ||
} | ||
|
||
func TestBasicETMStream(t *testing.T) { | ||
buf := new(bytes.Buffer) | ||
|
||
ki := getTestKeyInfo() | ||
w, err := getTestingWriter(buf, ki) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
before := []byte("hello world") | ||
err = w.WriteMsg(before) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
r, err := getTestingReader(buf, ki) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
msg, err := r.ReadMsg() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if string(before) != string(msg) { | ||
t.Fatal("got wrong message") | ||
} | ||
} |