mirror of
https://github.com/awnumar/memguard.git
synced 2026-02-07 02:09:53 +00:00
84 lines
1.7 KiB
Go
84 lines
1.7 KiB
Go
package core
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
)
|
|
|
|
func TestPurge(t *testing.T) {
|
|
// Create a bunch of things to simulate a working environment.
|
|
enclave, err := NewEnclave([]byte("yellow submarine"))
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
buffer, err := NewBuffer(32)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
// Keep a reference to the old key.
|
|
oldKey := key
|
|
|
|
// Purge the session.
|
|
Purge()
|
|
|
|
// Verify that the buffers list contains only the important buffers.
|
|
buffers.RLock()
|
|
if len(buffers.list) != 3 {
|
|
t.Error("buffers list was not flushed", buffers.list)
|
|
}
|
|
for i := range buffers.list {
|
|
if !buffers.list[i].Alive() {
|
|
t.Error("should not have destroyed excluded buffers")
|
|
}
|
|
}
|
|
if !key.right.Alive() || !key.left.Alive() || !key.rand.Alive() {
|
|
t.Error("buffers left in list aren't the right ones")
|
|
}
|
|
buffers.RUnlock()
|
|
|
|
// Verify that the buffer was destroyed.
|
|
if buffer.Alive() {
|
|
t.Error("buffer was not destroyed")
|
|
}
|
|
|
|
// Verify that the old key was destroyed.
|
|
if oldKey.left.Alive() || oldKey.right.Alive() {
|
|
t.Error("old key was not destroyed")
|
|
}
|
|
|
|
// Verify that the key is not destroyed.
|
|
if !key.left.Alive() || !key.right.Alive() {
|
|
t.Error("current key is destroyed")
|
|
}
|
|
|
|
// Verify that the key changed by decrypting the Enclave.
|
|
if _, err := enclave.Open(); err != ErrDecryptionFailed {
|
|
t.Error("expected decryption failed; got", err)
|
|
}
|
|
|
|
// Create a buffer with invalid canary.
|
|
b, err := NewBuffer(32)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
Scramble(b.inner)
|
|
b.Freeze()
|
|
if !panics(func() {
|
|
Purge()
|
|
}) {
|
|
t.Error("did not panic")
|
|
}
|
|
if !bytes.Equal(b.data, make([]byte, 32)) {
|
|
t.Error("data not wiped")
|
|
}
|
|
buffers.remove(b)
|
|
}
|
|
|
|
func panics(fn func()) (panicked bool) {
|
|
defer func() {
|
|
panicked = (recover() != nil)
|
|
}()
|
|
fn()
|
|
return
|
|
}
|