e1d52c122e9cff9fc6ce46d0f8857ce33384d9b7
[platform/core/system/edge-orchestration.git] /
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package packet
6
7 import (
8         "bytes"
9         "encoding/hex"
10         "io"
11         "io/ioutil"
12         "testing"
13 )
14
15 func TestSymmetricKeyEncrypted(t *testing.T) {
16         buf := readerFromHex(symmetricallyEncryptedHex)
17         packet, err := Read(buf)
18         if err != nil {
19                 t.Errorf("failed to read SymmetricKeyEncrypted: %s", err)
20                 return
21         }
22         ske, ok := packet.(*SymmetricKeyEncrypted)
23         if !ok {
24                 t.Error("didn't find SymmetricKeyEncrypted packet")
25                 return
26         }
27         key, cipherFunc, err := ske.Decrypt([]byte("password"))
28         if err != nil {
29                 t.Error(err)
30                 return
31         }
32
33         packet, err = Read(buf)
34         if err != nil {
35                 t.Errorf("failed to read SymmetricallyEncrypted: %s", err)
36                 return
37         }
38         se, ok := packet.(*SymmetricallyEncrypted)
39         if !ok {
40                 t.Error("didn't find SymmetricallyEncrypted packet")
41                 return
42         }
43         r, err := se.Decrypt(cipherFunc, key)
44         if err != nil {
45                 t.Error(err)
46                 return
47         }
48
49         contents, err := ioutil.ReadAll(r)
50         if err != nil && err != io.EOF {
51                 t.Error(err)
52                 return
53         }
54
55         expectedContents, _ := hex.DecodeString(symmetricallyEncryptedContentsHex)
56         if !bytes.Equal(expectedContents, contents) {
57                 t.Errorf("bad contents got:%x want:%x", contents, expectedContents)
58         }
59 }
60
61 const symmetricallyEncryptedHex = "8c0d04030302371a0b38d884f02060c91cf97c9973b8e58e028e9501708ccfe618fb92afef7fa2d80ddadd93cf"
62 const symmetricallyEncryptedContentsHex = "cb1062004d14c4df636f6e74656e74732e0a"
63
64 func TestSerializeSymmetricKeyEncryptedCiphers(t *testing.T) {
65         tests := [...]struct {
66                 cipherFunc CipherFunction
67                 name       string
68         }{
69                 {Cipher3DES, "Cipher3DES"},
70                 {CipherCAST5, "CipherCAST5"},
71                 {CipherAES128, "CipherAES128"},
72                 {CipherAES192, "CipherAES192"},
73                 {CipherAES256, "CipherAES256"},
74         }
75
76         for _, test := range tests {
77                 var buf bytes.Buffer
78                 passphrase := []byte("testing")
79                 config := &Config{
80                         DefaultCipher: test.cipherFunc,
81                 }
82
83                 key, err := SerializeSymmetricKeyEncrypted(&buf, passphrase, config)
84                 if err != nil {
85                         t.Errorf("cipher(%s) failed to serialize: %s", test.name, err)
86                         continue
87                 }
88
89                 p, err := Read(&buf)
90                 if err != nil {
91                         t.Errorf("cipher(%s) failed to reparse: %s", test.name, err)
92                         continue
93                 }
94
95                 ske, ok := p.(*SymmetricKeyEncrypted)
96                 if !ok {
97                         t.Errorf("cipher(%s) parsed a different packet type: %#v", test.name, p)
98                         continue
99                 }
100
101                 if ske.CipherFunc != config.DefaultCipher {
102                         t.Errorf("cipher(%s) SKE cipher function is %d (expected %d)", test.name, ske.CipherFunc, config.DefaultCipher)
103                 }
104                 parsedKey, parsedCipherFunc, err := ske.Decrypt(passphrase)
105                 if err != nil {
106                         t.Errorf("cipher(%s) failed to decrypt reparsed SKE: %s", test.name, err)
107                         continue
108                 }
109                 if !bytes.Equal(key, parsedKey) {
110                         t.Errorf("cipher(%s) keys don't match after Decrypt: %x (original) vs %x (parsed)", test.name, key, parsedKey)
111                 }
112                 if parsedCipherFunc != test.cipherFunc {
113                         t.Errorf("cipher(%s) cipher function doesn't match after Decrypt: %d (original) vs %d (parsed)",
114                                 test.name, test.cipherFunc, parsedCipherFunc)
115                 }
116         }
117 }