blob: c88fa18eb044954bbc65a4b279647c6bd4b773e9 [file] [log] [blame]
Darin Petkov823c47e2011-06-27 16:15:35 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <base/file_util.h>
6#include <base/memory/scoped_temp_dir.h>
7#include <gtest/gtest.h>
8
9#include "shill/crypto_des_cbc.h"
10#include "shill/glib.h"
11
12using std::string;
13using std::vector;
14using testing::Test;
15
16namespace shill {
17
18namespace {
19const char kTestKey[] = "12345678";
20const char kTestIV[] = "abcdefgh";
21const char kEmptyPlain[] = "";
22const char kEmptyCipher[] = "02:4+O1a2KJVRM=";
23const char kEmptyCipherNoSentinel[] = "02:lNRDa8O1tpM=";
24const char kPlainText[] = "Hello world! ~123";
25const char kCipherText[] = "02:MbxzeBqK3HVeS3xfjyhbe47Xx+szYgOp";
26const char kPlainVersion1[] = "This is a test!";
27const char kCipherVersion1[] = "bKlHDISdHMFfmfgBTT5I0w==";
28} // namespace {}
29
30class CryptoDESCBCTest : public Test {
31 public:
32 CryptoDESCBCTest() : crypto_(&glib_) {}
33
34 protected:
35 GLib glib_; // Use actual GLib for the test.
36 CryptoDESCBC crypto_;
37};
38
39TEST_F(CryptoDESCBCTest, GetID) {
40 EXPECT_EQ(CryptoDESCBC::kID, crypto_.GetID());
41}
42
43TEST_F(CryptoDESCBCTest, LoadKeyMatter) {
44 ScopedTempDir temp_dir;
45 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
46 const char kKeyMatterFile[] = "key-matter-file";
47 FilePath key_matter = temp_dir.path().Append(kKeyMatterFile);
48
49 EXPECT_FALSE(crypto_.LoadKeyMatter(key_matter));
50 EXPECT_TRUE(crypto_.key().empty());
51 EXPECT_TRUE(crypto_.iv().empty());
52
53 string matter = string(kTestIV) + kTestKey;
54
55 file_util::WriteFile(key_matter, matter.data(), matter.size() - 1);
56 EXPECT_FALSE(crypto_.LoadKeyMatter(key_matter));
57 EXPECT_TRUE(crypto_.key().empty());
58 EXPECT_TRUE(crypto_.iv().empty());
59
60 file_util::WriteFile(key_matter, matter.data(), matter.size());
61 EXPECT_TRUE(crypto_.LoadKeyMatter(key_matter));
62 EXPECT_EQ(kTestKey, string(crypto_.key().begin(), crypto_.key().end()));
63 EXPECT_EQ(kTestIV, string(crypto_.iv().begin(), crypto_.iv().end()));
64
65 const char kKey2[] = "ABCDEFGH";
66 const char kIV2[] = "87654321";
67 matter = string("X") + kIV2 + kKey2;
68
69 file_util::WriteFile(key_matter, matter.data(), matter.size());
70 EXPECT_TRUE(crypto_.LoadKeyMatter(key_matter));
71 EXPECT_EQ(kKey2, string(crypto_.key().begin(), crypto_.key().end()));
72 EXPECT_EQ(kIV2, string(crypto_.iv().begin(), crypto_.iv().end()));
73
74 file_util::WriteFile(key_matter, " ", 1);
75 EXPECT_FALSE(crypto_.LoadKeyMatter(key_matter));
76 EXPECT_TRUE(crypto_.key().empty());
77 EXPECT_TRUE(crypto_.iv().empty());
78}
79
80TEST_F(CryptoDESCBCTest, Encrypt) {
81 crypto_.key_.assign(kTestKey, kTestKey + strlen(kTestKey));
82 crypto_.iv_.assign(kTestIV, kTestIV + strlen(kTestIV));
83
84 string ciphertext;
85 EXPECT_TRUE(crypto_.Encrypt(kEmptyPlain, &ciphertext));
86 EXPECT_EQ(kEmptyCipher, ciphertext);
87 EXPECT_TRUE(crypto_.Encrypt(kPlainText, &ciphertext));
88 EXPECT_EQ(kCipherText, ciphertext);
89}
90
91TEST_F(CryptoDESCBCTest, Decrypt) {
92 crypto_.key_.assign(kTestKey, kTestKey + strlen(kTestKey));
93 crypto_.iv_.assign(kTestIV, kTestIV + strlen(kTestIV));
94
95 string plaintext;
96 EXPECT_TRUE(crypto_.Decrypt(kEmptyCipher, &plaintext));
97 EXPECT_EQ(kEmptyPlain, plaintext);
98 EXPECT_TRUE(crypto_.Decrypt(kCipherText, &plaintext));
99 EXPECT_EQ(kPlainText, plaintext);
100 EXPECT_TRUE(crypto_.Decrypt(kCipherVersion1, &plaintext));
101 EXPECT_EQ(kPlainVersion1, plaintext);
102
103 EXPECT_FALSE(crypto_.Decrypt("random", &plaintext));
104 EXPECT_FALSE(crypto_.Decrypt("02:random", &plaintext));
105 EXPECT_FALSE(crypto_.Decrypt("~", &plaintext));
106 EXPECT_FALSE(crypto_.Decrypt("02:~", &plaintext));
107 EXPECT_FALSE(crypto_.Decrypt(kEmptyPlain, &plaintext));
108 EXPECT_FALSE(crypto_.Decrypt(kEmptyCipherNoSentinel, &plaintext));
109
110 // echo -n 12345678 | base64
111 EXPECT_FALSE(crypto_.Decrypt("MTIzNDU2Nzg=", &plaintext));
112}
113
114} // namespace shill