1// 2// Copyright (C) 2012 The Android Open Source Project 3// 4// Licensed under the Apache License, Version 2.0 (the "License"); 5// you may not use this file except in compliance with the License. 6// You may obtain a copy of the License at 7// 8// http://www.apache.org/licenses/LICENSE-2.0 9// 10// Unless required by applicable law or agreed to in writing, software 11// distributed under the License is distributed on an "AS IS" BASIS, 12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13// See the License for the specific language governing permissions and 14// limitations under the License. 15// 16 17#include "shill/crypto_des_cbc.h" 18 19#include <string> 20 21#include <base/files/file_util.h> 22#include <base/files/scoped_temp_dir.h> 23#include <gtest/gtest.h> 24 25using base::FilePath; 26using std::string; 27using std::vector; 28using testing::Test; 29 30namespace shill { 31 32namespace { 33const char kTestKey[] = "12345678"; 34const char kTestIV[] = "abcdefgh"; 35const char kEmptyPlain[] = ""; 36const char kEmptyCipher[] = "02:4+O1a2KJVRM="; 37const char kEmptyCipherNoSentinel[] = "02:lNRDa8O1tpM="; 38const char kPlainText[] = "Hello world! ~123"; 39const char kCipherText[] = "02:MbxzeBqK3HVeS3xfjyhbe47Xx+szYgOp"; 40const char kPlainVersion1[] = "This is a test!"; 41const char kCipherVersion1[] = "bKlHDISdHMFfmfgBTT5I0w=="; 42} // namespace 43 44class CryptoDESCBCTest : public Test { 45 public: 46 CryptoDESCBCTest() {} 47 48 protected: 49 CryptoDESCBC crypto_; 50}; 51 52TEST_F(CryptoDESCBCTest, GetID) { 53 EXPECT_EQ(CryptoDESCBC::kID, crypto_.GetID()); 54} 55 56TEST_F(CryptoDESCBCTest, LoadKeyMatter) { 57 base::ScopedTempDir temp_dir; 58 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 59 const char kKeyMatterFile[] = "key-matter-file"; 60 FilePath key_matter = temp_dir.path().Append(kKeyMatterFile); 61 62 EXPECT_FALSE(crypto_.LoadKeyMatter(key_matter)); 63 EXPECT_TRUE(crypto_.key().empty()); 64 EXPECT_TRUE(crypto_.iv().empty()); 65 66 string matter = string(kTestIV) + kTestKey; 67 68 base::WriteFile(key_matter, matter.data(), matter.size() - 1); 69 EXPECT_FALSE(crypto_.LoadKeyMatter(key_matter)); 70 EXPECT_TRUE(crypto_.key().empty()); 71 EXPECT_TRUE(crypto_.iv().empty()); 72 73 base::WriteFile(key_matter, matter.data(), matter.size()); 74 EXPECT_TRUE(crypto_.LoadKeyMatter(key_matter)); 75 EXPECT_EQ(kTestKey, string(crypto_.key().begin(), crypto_.key().end())); 76 EXPECT_EQ(kTestIV, string(crypto_.iv().begin(), crypto_.iv().end())); 77 78 const char kKey2[] = "ABCDEFGH"; 79 const char kIV2[] = "87654321"; 80 matter = string("X") + kIV2 + kKey2; 81 82 base::WriteFile(key_matter, matter.data(), matter.size()); 83 EXPECT_TRUE(crypto_.LoadKeyMatter(key_matter)); 84 EXPECT_EQ(kKey2, string(crypto_.key().begin(), crypto_.key().end())); 85 EXPECT_EQ(kIV2, string(crypto_.iv().begin(), crypto_.iv().end())); 86 87 base::WriteFile(key_matter, " ", 1); 88 EXPECT_FALSE(crypto_.LoadKeyMatter(key_matter)); 89 EXPECT_TRUE(crypto_.key().empty()); 90 EXPECT_TRUE(crypto_.iv().empty()); 91} 92 93TEST_F(CryptoDESCBCTest, Encrypt) { 94 crypto_.key_.assign(kTestKey, kTestKey + strlen(kTestKey)); 95 crypto_.iv_.assign(kTestIV, kTestIV + strlen(kTestIV)); 96 97 string ciphertext; 98 EXPECT_FALSE(crypto_.Encrypt(kPlainText, &ciphertext)); 99} 100 101TEST_F(CryptoDESCBCTest, Decrypt) { 102 crypto_.key_.assign(kTestKey, kTestKey + strlen(kTestKey)); 103 crypto_.iv_.assign(kTestIV, kTestIV + strlen(kTestIV)); 104 105 string plaintext; 106 EXPECT_TRUE(crypto_.Decrypt(kEmptyCipher, &plaintext)); 107 EXPECT_EQ(kEmptyPlain, plaintext); 108 EXPECT_TRUE(crypto_.Decrypt(kCipherText, &plaintext)); 109 EXPECT_EQ(kPlainText, plaintext); 110 EXPECT_TRUE(crypto_.Decrypt(kCipherVersion1, &plaintext)); 111 EXPECT_EQ(kPlainVersion1, plaintext); 112 113 EXPECT_FALSE(crypto_.Decrypt("random", &plaintext)); 114 EXPECT_FALSE(crypto_.Decrypt("02:random", &plaintext)); 115 EXPECT_FALSE(crypto_.Decrypt("~", &plaintext)); 116 EXPECT_FALSE(crypto_.Decrypt("02:~", &plaintext)); 117 EXPECT_FALSE(crypto_.Decrypt(kEmptyPlain, &plaintext)); 118 EXPECT_FALSE(crypto_.Decrypt(kEmptyCipherNoSentinel, &plaintext)); 119 120 // echo -n 12345678 | base64 121 EXPECT_FALSE(crypto_.Decrypt("MTIzNDU2Nzg=", &plaintext)); 122} 123 124} // namespace shill 125