blob: c65a288204c95b1934b01b97e991d14347e40b51 [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
Alex Deymocddd2d02014-11-10 19:55:35 -08005#include "shill/crypto_des_cbc.h"
6
Alex Vakulenko8a532292014-06-16 17:18:44 -07007#include <string>
8
Ben Chan11c213f2014-09-05 08:21:06 -07009#include <base/files/file_util.h>
Paul Stewart5ad16062013-02-21 18:10:48 -080010#include <base/files/scoped_temp_dir.h>
Darin Petkov823c47e2011-06-27 16:15:35 -070011#include <gtest/gtest.h>
12
Darin Petkov823c47e2011-06-27 16:15:35 -070013#include "shill/glib.h"
14
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080015using base::FilePath;
Darin Petkov823c47e2011-06-27 16:15:35 -070016using std::string;
17using std::vector;
18using testing::Test;
19
20namespace shill {
21
22namespace {
23const char kTestKey[] = "12345678";
24const char kTestIV[] = "abcdefgh";
25const char kEmptyPlain[] = "";
26const char kEmptyCipher[] = "02:4+O1a2KJVRM=";
27const char kEmptyCipherNoSentinel[] = "02:lNRDa8O1tpM=";
28const char kPlainText[] = "Hello world! ~123";
29const char kCipherText[] = "02:MbxzeBqK3HVeS3xfjyhbe47Xx+szYgOp";
30const char kPlainVersion1[] = "This is a test!";
31const char kCipherVersion1[] = "bKlHDISdHMFfmfgBTT5I0w==";
Alex Vakulenko8a532292014-06-16 17:18:44 -070032} // namespace
Darin Petkov823c47e2011-06-27 16:15:35 -070033
34class CryptoDESCBCTest : public Test {
35 public:
36 CryptoDESCBCTest() : crypto_(&glib_) {}
37
38 protected:
39 GLib glib_; // Use actual GLib for the test.
40 CryptoDESCBC crypto_;
41};
42
43TEST_F(CryptoDESCBCTest, GetID) {
44 EXPECT_EQ(CryptoDESCBC::kID, crypto_.GetID());
45}
46
47TEST_F(CryptoDESCBCTest, LoadKeyMatter) {
Paul Stewart5ad16062013-02-21 18:10:48 -080048 base::ScopedTempDir temp_dir;
Darin Petkov823c47e2011-06-27 16:15:35 -070049 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
50 const char kKeyMatterFile[] = "key-matter-file";
51 FilePath key_matter = temp_dir.path().Append(kKeyMatterFile);
52
53 EXPECT_FALSE(crypto_.LoadKeyMatter(key_matter));
54 EXPECT_TRUE(crypto_.key().empty());
55 EXPECT_TRUE(crypto_.iv().empty());
56
57 string matter = string(kTestIV) + kTestKey;
58
Ben Chan6fbf64f2014-05-21 18:07:01 -070059 base::WriteFile(key_matter, matter.data(), matter.size() - 1);
Darin Petkov823c47e2011-06-27 16:15:35 -070060 EXPECT_FALSE(crypto_.LoadKeyMatter(key_matter));
61 EXPECT_TRUE(crypto_.key().empty());
62 EXPECT_TRUE(crypto_.iv().empty());
63
Ben Chan6fbf64f2014-05-21 18:07:01 -070064 base::WriteFile(key_matter, matter.data(), matter.size());
Darin Petkov823c47e2011-06-27 16:15:35 -070065 EXPECT_TRUE(crypto_.LoadKeyMatter(key_matter));
66 EXPECT_EQ(kTestKey, string(crypto_.key().begin(), crypto_.key().end()));
67 EXPECT_EQ(kTestIV, string(crypto_.iv().begin(), crypto_.iv().end()));
68
69 const char kKey2[] = "ABCDEFGH";
70 const char kIV2[] = "87654321";
71 matter = string("X") + kIV2 + kKey2;
72
Ben Chan6fbf64f2014-05-21 18:07:01 -070073 base::WriteFile(key_matter, matter.data(), matter.size());
Darin Petkov823c47e2011-06-27 16:15:35 -070074 EXPECT_TRUE(crypto_.LoadKeyMatter(key_matter));
75 EXPECT_EQ(kKey2, string(crypto_.key().begin(), crypto_.key().end()));
76 EXPECT_EQ(kIV2, string(crypto_.iv().begin(), crypto_.iv().end()));
77
Ben Chan6fbf64f2014-05-21 18:07:01 -070078 base::WriteFile(key_matter, " ", 1);
Darin Petkov823c47e2011-06-27 16:15:35 -070079 EXPECT_FALSE(crypto_.LoadKeyMatter(key_matter));
80 EXPECT_TRUE(crypto_.key().empty());
81 EXPECT_TRUE(crypto_.iv().empty());
82}
83
84TEST_F(CryptoDESCBCTest, Encrypt) {
85 crypto_.key_.assign(kTestKey, kTestKey + strlen(kTestKey));
86 crypto_.iv_.assign(kTestIV, kTestIV + strlen(kTestIV));
87
88 string ciphertext;
Darin Petkov7326cd82012-03-30 11:02:01 +020089 EXPECT_FALSE(crypto_.Encrypt(kPlainText, &ciphertext));
Darin Petkov823c47e2011-06-27 16:15:35 -070090}
91
92TEST_F(CryptoDESCBCTest, Decrypt) {
93 crypto_.key_.assign(kTestKey, kTestKey + strlen(kTestKey));
94 crypto_.iv_.assign(kTestIV, kTestIV + strlen(kTestIV));
95
96 string plaintext;
97 EXPECT_TRUE(crypto_.Decrypt(kEmptyCipher, &plaintext));
98 EXPECT_EQ(kEmptyPlain, plaintext);
99 EXPECT_TRUE(crypto_.Decrypt(kCipherText, &plaintext));
100 EXPECT_EQ(kPlainText, plaintext);
101 EXPECT_TRUE(crypto_.Decrypt(kCipherVersion1, &plaintext));
102 EXPECT_EQ(kPlainVersion1, plaintext);
103
104 EXPECT_FALSE(crypto_.Decrypt("random", &plaintext));
105 EXPECT_FALSE(crypto_.Decrypt("02:random", &plaintext));
106 EXPECT_FALSE(crypto_.Decrypt("~", &plaintext));
107 EXPECT_FALSE(crypto_.Decrypt("02:~", &plaintext));
108 EXPECT_FALSE(crypto_.Decrypt(kEmptyPlain, &plaintext));
109 EXPECT_FALSE(crypto_.Decrypt(kEmptyCipherNoSentinel, &plaintext));
110
111 // echo -n 12345678 | base64
112 EXPECT_FALSE(crypto_.Decrypt("MTIzNDU2Nzg=", &plaintext));
113}
114
115} // namespace shill