Zentaro Kavanagh | 26f0a73 | 2018-10-25 14:36:47 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 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 <string> |
| 6 | |
| 7 | #include <brillo/secure_blob.h> |
| 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | #include "libtpmcrypto/tpm_proto_utils.h" |
| 11 | |
| 12 | using brillo::SecureBlob; |
| 13 | |
| 14 | namespace tpmcrypto { |
| 15 | |
| 16 | TEST(TpmProtoUtilsTest, RoundTrip) { |
| 17 | SecureBlob sealed_key("sealed_key"); |
| 18 | SecureBlob iv("iv"); |
| 19 | SecureBlob tag("tag"); |
| 20 | SecureBlob encrypted_data("encrypted_data"); |
| 21 | |
| 22 | // Serialize the blobs into a proto. |
| 23 | std::string serialized; |
| 24 | ASSERT_TRUE(CreateSerializedTpmCryptoProto(sealed_key, iv, tag, |
| 25 | encrypted_data, &serialized)); |
| 26 | |
| 27 | // Parse the blobs out of the proto. |
| 28 | SecureBlob parsed_sealed_key; |
| 29 | SecureBlob parsed_iv; |
| 30 | SecureBlob parsed_tag; |
| 31 | SecureBlob parsed_encrypted_data; |
| 32 | ASSERT_TRUE(ParseTpmCryptoProto(serialized, &parsed_sealed_key, &parsed_iv, |
| 33 | &parsed_tag, &parsed_encrypted_data)); |
| 34 | |
| 35 | // Verify the blobs that came back are the same as the ones put in. |
| 36 | EXPECT_EQ(sealed_key, parsed_sealed_key); |
| 37 | EXPECT_EQ(iv, parsed_iv); |
| 38 | EXPECT_EQ(tag, parsed_tag); |
| 39 | EXPECT_EQ(encrypted_data, parsed_encrypted_data); |
| 40 | } |
| 41 | |
| 42 | TEST(TpmProtoUtilsTest, EmptyFields) { |
| 43 | SecureBlob sealed_key; |
| 44 | SecureBlob iv; |
| 45 | SecureBlob tag; |
| 46 | SecureBlob encrypted_data; |
| 47 | |
| 48 | // Serialize the blobs into a proto, then parse them back out. |
| 49 | std::string serialized; |
| 50 | ASSERT_TRUE(CreateSerializedTpmCryptoProto(sealed_key, iv, tag, |
| 51 | encrypted_data, &serialized)); |
| 52 | ASSERT_TRUE( |
| 53 | ParseTpmCryptoProto(serialized, &sealed_key, &iv, &tag, &encrypted_data)); |
| 54 | |
| 55 | // Verify the results are empty. |
| 56 | EXPECT_TRUE(sealed_key.empty()); |
| 57 | EXPECT_TRUE(iv.empty()); |
| 58 | EXPECT_TRUE(tag.empty()); |
| 59 | EXPECT_TRUE(encrypted_data.empty()); |
| 60 | } |
| 61 | |
| 62 | } // namespace tpmcrypto |