1 // 2 // Copyright (C) 2011 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_provider.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 25 #include "shill/crypto_des_cbc.h" 26 #include "shill/crypto_rot47.h" 27 28 using base::FilePath; 29 using base::ScopedTempDir; 30 using std::string; 31 using testing::Test; 32 33 namespace shill { 34 35 namespace { 36 const char kTestKey[] = "12345678"; 37 const char kTestIV[] = "abcdefgh"; 38 const char kKeyMatterFile[] = "key-matter-file"; 39 const char kEmptyText[] = ""; 40 const char kPlainText[] = "This is a test!"; 41 const char kROT47Text[] = "rot47:%9:D :D 2 E6DEP"; 42 const char kDESCBCText[] = "des-cbc:02:bKlHDISdHMFc0teQd4mAVrXgwlSj6iA+"; 43 } // namespace 44 45 class CryptoProviderTest : public Test { 46 public: 47 CryptoProviderTest() {} 48 49 protected: 50 FilePath InitKeyMatterFile(const FilePath& dir); 51 52 CryptoProvider provider_; 53 }; 54 55 FilePath CryptoProviderTest::InitKeyMatterFile(const FilePath& dir) { 56 FilePath path = dir.Append(kKeyMatterFile); 57 string matter = string(kTestIV) + kTestKey; 58 base::WriteFile(path, matter.data(), matter.size()); 59 return path; 60 } 61 62 TEST_F(CryptoProviderTest, Init) { 63 EXPECT_EQ(CryptoProvider::kKeyMatterFile, provider_.key_matter_file_.value()); 64 65 provider_.set_key_matter_file(FilePath("/some/non/existent/file")); 66 provider_.Init(); 67 ASSERT_EQ(1, provider_.cryptos_.size()); 68 EXPECT_EQ(CryptoROT47::kID, provider_.cryptos_[0]->GetID()); 69 70 ScopedTempDir temp_dir; 71 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 72 provider_.set_key_matter_file(InitKeyMatterFile(temp_dir.path())); 73 provider_.Init(); 74 ASSERT_EQ(2, provider_.cryptos_.size()); 75 EXPECT_EQ(CryptoDESCBC::kID, provider_.cryptos_[0]->GetID()); 76 EXPECT_EQ(CryptoROT47::kID, provider_.cryptos_[1]->GetID()); 77 78 provider_.set_key_matter_file(FilePath("/other/missing/file")); 79 provider_.Init(); 80 ASSERT_EQ(1, provider_.cryptos_.size()); 81 EXPECT_EQ(CryptoROT47::kID, provider_.cryptos_[0]->GetID()); 82 } 83 84 TEST_F(CryptoProviderTest, Encrypt) { 85 EXPECT_EQ(kPlainText, provider_.Encrypt(kPlainText)); 86 EXPECT_EQ(kEmptyText, provider_.Encrypt(kEmptyText)); 87 88 provider_.Init(); 89 EXPECT_EQ(kROT47Text, provider_.Encrypt(kPlainText)); 90 91 ScopedTempDir temp_dir; 92 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 93 provider_.set_key_matter_file(InitKeyMatterFile(temp_dir.path())); 94 provider_.Init(); 95 EXPECT_EQ(kROT47Text, provider_.Encrypt(kPlainText)); 96 } 97 98 TEST_F(CryptoProviderTest, Decrypt) { 99 EXPECT_EQ(kPlainText, provider_.Decrypt(kPlainText)); 100 EXPECT_EQ(kEmptyText, provider_.Decrypt(kEmptyText)); 101 102 ScopedTempDir temp_dir; 103 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 104 provider_.set_key_matter_file(InitKeyMatterFile(temp_dir.path())); 105 provider_.Init(); 106 EXPECT_EQ(kPlainText, provider_.Decrypt(kROT47Text)); 107 EXPECT_EQ(kPlainText, provider_.Decrypt(kDESCBCText)); 108 EXPECT_EQ(kPlainText, provider_.Decrypt(kPlainText)); 109 EXPECT_EQ(kEmptyText, provider_.Decrypt(kEmptyText)); 110 } 111 112 } // namespace shill 113