blob: 81681f9c899def05bf5d52f1bb3179eed867576b [file] [log] [blame]
Darin Petkov7326cd82012-03-30 11:02:01 +02001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkov823c47e2011-06-27 16:15:35 -07002// 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>
Paul Stewart5ad16062013-02-21 18:10:48 -08006#include <base/files/scoped_temp_dir.h>
Darin Petkov823c47e2011-06-27 16:15:35 -07007#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) {
Paul Stewart5ad16062013-02-21 18:10:48 -080044 base::ScopedTempDir temp_dir;
Darin Petkov823c47e2011-06-27 16:15:35 -070045 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;
Darin Petkov7326cd82012-03-30 11:02:01 +020085 EXPECT_FALSE(crypto_.Encrypt(kPlainText, &ciphertext));
Darin Petkov823c47e2011-06-27 16:15:35 -070086}
87
88TEST_F(CryptoDESCBCTest, Decrypt) {
89 crypto_.key_.assign(kTestKey, kTestKey + strlen(kTestKey));
90 crypto_.iv_.assign(kTestIV, kTestIV + strlen(kTestIV));
91
92 string plaintext;
93 EXPECT_TRUE(crypto_.Decrypt(kEmptyCipher, &plaintext));
94 EXPECT_EQ(kEmptyPlain, plaintext);
95 EXPECT_TRUE(crypto_.Decrypt(kCipherText, &plaintext));
96 EXPECT_EQ(kPlainText, plaintext);
97 EXPECT_TRUE(crypto_.Decrypt(kCipherVersion1, &plaintext));
98 EXPECT_EQ(kPlainVersion1, plaintext);
99
100 EXPECT_FALSE(crypto_.Decrypt("random", &plaintext));
101 EXPECT_FALSE(crypto_.Decrypt("02:random", &plaintext));
102 EXPECT_FALSE(crypto_.Decrypt("~", &plaintext));
103 EXPECT_FALSE(crypto_.Decrypt("02:~", &plaintext));
104 EXPECT_FALSE(crypto_.Decrypt(kEmptyPlain, &plaintext));
105 EXPECT_FALSE(crypto_.Decrypt(kEmptyCipherNoSentinel, &plaintext));
106
107 // echo -n 12345678 | base64
108 EXPECT_FALSE(crypto_.Decrypt("MTIzNDU2Nzg=", &plaintext));
109}
110
111} // namespace shill