OSDN Git Service

Update Go library to r60.
[pf3gnuchains/gcc-fork.git] / libgo / go / crypto / openpgp / packet / symmetrically_encrypted_test.go
index 5543b20..1054fc2 100644 (file)
@@ -9,6 +9,7 @@ import (
        "crypto/openpgp/error"
        "crypto/sha1"
        "encoding/hex"
+       "io"
        "io/ioutil"
        "os"
        "testing"
@@ -76,3 +77,48 @@ func testMDCReader(t *testing.T) {
 }
 
 const mdcPlaintextHex = "a302789c3b2d93c4e0eb9aba22283539b3203335af44a134afb800c849cb4c4de10200aff40b45d31432c80cb384299a0655966d6939dfdeed1dddf980"
+
+func TestSerialize(t *testing.T) {
+       buf := bytes.NewBuffer(nil)
+       c := CipherAES128
+       key := make([]byte, c.KeySize())
+
+       w, err := SerializeSymmetricallyEncrypted(buf, c, key)
+       if err != nil {
+               t.Errorf("error from SerializeSymmetricallyEncrypted: %s", err)
+               return
+       }
+
+       contents := []byte("hello world\n")
+
+       w.Write(contents)
+       w.Close()
+
+       p, err := Read(buf)
+       if err != nil {
+               t.Errorf("error from Read: %s", err)
+               return
+       }
+
+       se, ok := p.(*SymmetricallyEncrypted)
+       if !ok {
+               t.Errorf("didn't read a *SymmetricallyEncrypted")
+               return
+       }
+
+       r, err := se.Decrypt(c, key)
+       if err != nil {
+               t.Errorf("error from Decrypt: %s", err)
+               return
+       }
+
+       contentsCopy := bytes.NewBuffer(nil)
+       _, err = io.Copy(contentsCopy, r)
+       if err != nil {
+               t.Errorf("error from io.Copy: %s", err)
+               return
+       }
+       if !bytes.Equal(contentsCopy.Bytes(), contents) {
+               t.Errorf("contents not equal got: %x want: %x", contentsCopy.Bytes(), contents)
+       }
+}