1 /******************************************************************************
2  *
3  *  Copyright 2019 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #include <base/logging.h>
20 #include <binder/ProcessState.h>
21 #include <gtest/gtest.h>
22 #include <fstream>
23 
24 #include "btif/include/btif_keystore.h"
25 
26 using namespace bluetooth;
27 
28 class BtifKeystoreTest : public ::testing::Test {
29  protected:
30   std::unique_ptr<BtifKeystore> btif_keystore_;
SetUp()31   void SetUp() override {
32     android::ProcessState::self()->startThreadPool();
33     btif_keystore_ =
34         std::make_unique<BtifKeystore>(static_cast<keystore::KeystoreClient*>(
35             new keystore::KeystoreClientImpl));
36   };
TearDown()37   void TearDown() override { btif_keystore_ = nullptr; };
38 };
39 
TEST_F(BtifKeystoreTest,test_encrypt_decrypt)40 TEST_F(BtifKeystoreTest, test_encrypt_decrypt) {
41   std::string hash = "test";
42 
43   std::string encrypted_hash = btif_keystore_->Encrypt(hash, 0);
44   std::string decrypted_hash = btif_keystore_->Decrypt(encrypted_hash);
45 
46   EXPECT_FALSE(encrypted_hash.empty());
47   EXPECT_EQ(hash, decrypted_hash);
48 }
49 
TEST_F(BtifKeystoreTest,test_encrypt_empty_hash)50 TEST_F(BtifKeystoreTest, test_encrypt_empty_hash) {
51   std::string hash = "";
52 
53   std::string encrypted_hash = btif_keystore_->Encrypt(hash, 0);
54 
55   EXPECT_TRUE(encrypted_hash.empty());
56 }
57 
TEST_F(BtifKeystoreTest,test_decrypt_empty_hash)58 TEST_F(BtifKeystoreTest, test_decrypt_empty_hash) {
59   std::string hash = "";
60 
61   std::string decrypted_hash = btif_keystore_->Decrypt(hash);
62 
63   EXPECT_TRUE(decrypted_hash.empty());
64 }
65