Darin Petkov | 7326cd8 | 2012-03-30 11:02:01 +0200 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 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> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 6 | #include <base/scoped_temp_dir.h> |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | #include "shill/crypto_des_cbc.h" |
| 10 | #include "shill/glib.h" |
| 11 | |
| 12 | using std::string; |
| 13 | using std::vector; |
| 14 | using testing::Test; |
| 15 | |
| 16 | namespace shill { |
| 17 | |
| 18 | namespace { |
| 19 | const char kTestKey[] = "12345678"; |
| 20 | const char kTestIV[] = "abcdefgh"; |
| 21 | const char kEmptyPlain[] = ""; |
| 22 | const char kEmptyCipher[] = "02:4+O1a2KJVRM="; |
| 23 | const char kEmptyCipherNoSentinel[] = "02:lNRDa8O1tpM="; |
| 24 | const char kPlainText[] = "Hello world! ~123"; |
| 25 | const char kCipherText[] = "02:MbxzeBqK3HVeS3xfjyhbe47Xx+szYgOp"; |
| 26 | const char kPlainVersion1[] = "This is a test!"; |
| 27 | const char kCipherVersion1[] = "bKlHDISdHMFfmfgBTT5I0w=="; |
| 28 | } // namespace {} |
| 29 | |
| 30 | class 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 | |
| 39 | TEST_F(CryptoDESCBCTest, GetID) { |
| 40 | EXPECT_EQ(CryptoDESCBC::kID, crypto_.GetID()); |
| 41 | } |
| 42 | |
| 43 | TEST_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 | |
| 80 | TEST_F(CryptoDESCBCTest, Encrypt) { |
| 81 | crypto_.key_.assign(kTestKey, kTestKey + strlen(kTestKey)); |
| 82 | crypto_.iv_.assign(kTestIV, kTestIV + strlen(kTestIV)); |
| 83 | |
| 84 | string ciphertext; |
Darin Petkov | 7326cd8 | 2012-03-30 11:02:01 +0200 | [diff] [blame] | 85 | EXPECT_FALSE(crypto_.Encrypt(kPlainText, &ciphertext)); |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | TEST_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 |