diff options
author | Nikita Ioffe <ioffe@google.com> | 2020-03-04 12:13:56 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2020-03-04 12:13:56 +0000 |
commit | 8e0f8b4cce17e01331863c461c1c4a34d15928f2 (patch) | |
tree | a0a8016decbe269f29dd99fb7ad1db33ba064154 | |
parent | 19e876f0b53c09a25693e2a5687864ad3e91830d (diff) | |
parent | e6e61f778c409b08b2799c5281b5db74325bc801 (diff) | |
download | extras-8e0f8b4cce17e01331863c461c1c4a34d15928f2.tar.gz |
Merge "libfscrypt: Add == and != operators for EncryptionPolicy/Options" into rvc-dev
-rw-r--r-- | libfscrypt/fscrypt.cpp | 6 | ||||
-rw-r--r-- | libfscrypt/include/fscrypt/fscrypt.h | 18 | ||||
-rw-r--r-- | libfscrypt/tests/fscrypt_test.cpp | 24 |
3 files changed, 42 insertions, 6 deletions
diff --git a/libfscrypt/fscrypt.cpp b/libfscrypt/fscrypt.cpp index b8e6ddcf..622b4cdb 100644 --- a/libfscrypt/fscrypt.cpp +++ b/libfscrypt/fscrypt.cpp @@ -132,12 +132,6 @@ static bool fscrypt_is_encrypted(int fd) { return ioctl(fd, FS_IOC_GET_ENCRYPTION_POLICY, &policy) == 0 || errno == EINVAL; } -bool operator!=(const EncryptionOptions& lhs, const EncryptionOptions& rhs) { - return !((lhs.version == rhs.version) && (lhs.contents_mode == rhs.contents_mode) && - (lhs.filenames_mode == rhs.filenames_mode) && (lhs.flags == rhs.flags) && - (lhs.use_hw_wrapped_key == rhs.use_hw_wrapped_key)); -} - unsigned int GetFirstApiLevel() { return android::base::GetUintProperty<unsigned int>("ro.product.first_api_level", 0); } diff --git a/libfscrypt/include/fscrypt/fscrypt.h b/libfscrypt/include/fscrypt/fscrypt.h index c780c7ce..78b12560 100644 --- a/libfscrypt/include/fscrypt/fscrypt.h +++ b/libfscrypt/include/fscrypt/fscrypt.h @@ -61,6 +61,24 @@ bool ParseOptionsForApiLevel(unsigned int first_api_level, const std::string& op bool EnsurePolicy(const EncryptionPolicy& policy, const std::string& directory); +inline bool operator==(const EncryptionOptions& lhs, const EncryptionOptions& rhs) { + return (lhs.version == rhs.version) && (lhs.contents_mode == rhs.contents_mode) && + (lhs.filenames_mode == rhs.filenames_mode) && (lhs.flags == rhs.flags) && + (lhs.use_hw_wrapped_key == rhs.use_hw_wrapped_key); +} + +inline bool operator!=(const EncryptionOptions& lhs, const EncryptionOptions& rhs) { + return !(lhs == rhs); +} + +inline bool operator==(const EncryptionPolicy& lhs, const EncryptionPolicy& rhs) { + return lhs.key_raw_ref == rhs.key_raw_ref && lhs.options == rhs.options; +} + +inline bool operator!=(const EncryptionPolicy& lhs, const EncryptionPolicy& rhs) { + return !(lhs == rhs); +} + } // namespace fscrypt } // namespace android diff --git a/libfscrypt/tests/fscrypt_test.cpp b/libfscrypt/tests/fscrypt_test.cpp index 7149e7ca..457ac684 100644 --- a/libfscrypt/tests/fscrypt_test.cpp +++ b/libfscrypt/tests/fscrypt_test.cpp @@ -165,3 +165,27 @@ TEST(fscrypt, ParseOptions) { EXPECT_FALSE(ParseOptionsForApiLevel(30, "aes-256-xts:aes-256-cts:blah", &dummy_options)); EXPECT_FALSE(ParseOptionsForApiLevel(30, "aes-256-xts:aes-256-cts:vblah", &dummy_options)); } + +TEST(fscrypt, ComparePolicies) { +#define TEST_INEQUALITY(foo, field, value) { \ + auto bar = foo; \ + bar.field = value; \ + EXPECT_NE(foo, bar); \ +} + EncryptionPolicy foo; + foo.key_raw_ref = "foo"; + EncryptionOptions foo_options; + foo_options.version = 1; + foo_options.contents_mode = 1; + foo_options.filenames_mode = 1; + foo_options.flags = 1; + foo_options.use_hw_wrapped_key = true; + foo.options = foo_options; + EXPECT_EQ(foo, foo); + TEST_INEQUALITY(foo, key_raw_ref, "bar"); + TEST_INEQUALITY(foo, options.version, 2); + TEST_INEQUALITY(foo, options.contents_mode, -1); + TEST_INEQUALITY(foo, options.filenames_mode, 3); + TEST_INEQUALITY(foo, options.flags, 0); + TEST_INEQUALITY(foo, options.use_hw_wrapped_key, false); +} |