blob: 0ba820624053f9e638168d799a2a37bb36fe2ea6 [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
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080012using base::FilePath;
Darin Petkov823c47e2011-06-27 16:15:35 -070013using std::string;
14using std::vector;
15using testing::Test;
16
17namespace shill {
18
19namespace {
20const char kTestKey[] = "12345678";
21const char kTestIV[] = "abcdefgh";
22const char kEmptyPlain[] = "";
23const char kEmptyCipher[] = "02:4+O1a2KJVRM=";
24const char kEmptyCipherNoSentinel[] = "02:lNRDa8O1tpM=";
25const char kPlainText[] = "Hello world! ~123";
26const char kCipherText[] = "02:MbxzeBqK3HVeS3xfjyhbe47Xx+szYgOp";
27const char kPlainVersion1[] = "This is a test!";
28const char kCipherVersion1[] = "bKlHDISdHMFfmfgBTT5I0w==";
29} // namespace {}
30
31class CryptoDESCBCTest : public Test {
32 public:
33 CryptoDESCBCTest() : crypto_(&glib_) {}
34
35 protected:
36 GLib glib_; // Use actual GLib for the test.
37 CryptoDESCBC crypto_;
38};
39
40TEST_F(CryptoDESCBCTest, GetID) {
41 EXPECT_EQ(CryptoDESCBC::kID, crypto_.GetID());
42}
43
44TEST_F(CryptoDESCBCTest, LoadKeyMatter) {
Paul Stewart5ad16062013-02-21 18:10:48 -080045 base::ScopedTempDir temp_dir;
Darin Petkov823c47e2011-06-27 16:15:35 -070046 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
47 const char kKeyMatterFile[] = "key-matter-file";
48 FilePath key_matter = temp_dir.path().Append(kKeyMatterFile);
49
50 EXPECT_FALSE(crypto_.LoadKeyMatter(key_matter));
51 EXPECT_TRUE(crypto_.key().empty());
52 EXPECT_TRUE(crypto_.iv().empty());
53
54 string matter = string(kTestIV) + kTestKey;
55
56 file_util::WriteFile(key_matter, matter.data(), matter.size() - 1);
57 EXPECT_FALSE(crypto_.LoadKeyMatter(key_matter));
58 EXPECT_TRUE(crypto_.key().empty());
59 EXPECT_TRUE(crypto_.iv().empty());
60
61 file_util::WriteFile(key_matter, matter.data(), matter.size());
62 EXPECT_TRUE(crypto_.LoadKeyMatter(key_matter));
63 EXPECT_EQ(kTestKey, string(crypto_.key().begin(), crypto_.key().end()));
64 EXPECT_EQ(kTestIV, string(crypto_.iv().begin(), crypto_.iv().end()));
65
66 const char kKey2[] = "ABCDEFGH";
67 const char kIV2[] = "87654321";
68 matter = string("X") + kIV2 + kKey2;
69
70 file_util::WriteFile(key_matter, matter.data(), matter.size());
71 EXPECT_TRUE(crypto_.LoadKeyMatter(key_matter));
72 EXPECT_EQ(kKey2, string(crypto_.key().begin(), crypto_.key().end()));
73 EXPECT_EQ(kIV2, string(crypto_.iv().begin(), crypto_.iv().end()));
74
75 file_util::WriteFile(key_matter, " ", 1);
76 EXPECT_FALSE(crypto_.LoadKeyMatter(key_matter));
77 EXPECT_TRUE(crypto_.key().empty());
78 EXPECT_TRUE(crypto_.iv().empty());
79}
80
81TEST_F(CryptoDESCBCTest, Encrypt) {
82 crypto_.key_.assign(kTestKey, kTestKey + strlen(kTestKey));
83 crypto_.iv_.assign(kTestIV, kTestIV + strlen(kTestIV));
84
85 string ciphertext;
Darin Petkov7326cd82012-03-30 11:02:01 +020086 EXPECT_FALSE(crypto_.Encrypt(kPlainText, &ciphertext));
Darin Petkov823c47e2011-06-27 16:15:35 -070087}
88
89TEST_F(CryptoDESCBCTest, Decrypt) {
90 crypto_.key_.assign(kTestKey, kTestKey + strlen(kTestKey));
91 crypto_.iv_.assign(kTestIV, kTestIV + strlen(kTestIV));
92
93 string plaintext;
94 EXPECT_TRUE(crypto_.Decrypt(kEmptyCipher, &plaintext));
95 EXPECT_EQ(kEmptyPlain, plaintext);
96 EXPECT_TRUE(crypto_.Decrypt(kCipherText, &plaintext));
97 EXPECT_EQ(kPlainText, plaintext);
98 EXPECT_TRUE(crypto_.Decrypt(kCipherVersion1, &plaintext));
99 EXPECT_EQ(kPlainVersion1, plaintext);
100
101 EXPECT_FALSE(crypto_.Decrypt("random", &plaintext));
102 EXPECT_FALSE(crypto_.Decrypt("02:random", &plaintext));
103 EXPECT_FALSE(crypto_.Decrypt("~", &plaintext));
104 EXPECT_FALSE(crypto_.Decrypt("02:~", &plaintext));
105 EXPECT_FALSE(crypto_.Decrypt(kEmptyPlain, &plaintext));
106 EXPECT_FALSE(crypto_.Decrypt(kEmptyCipherNoSentinel, &plaintext));
107
108 // echo -n 12345678 | base64
109 EXPECT_FALSE(crypto_.Decrypt("MTIzNDU2Nzg=", &plaintext));
110}
111
112} // namespace shill