summaryrefslogtreecommitdiff
path: root/libfscrypt
diff options
context:
space:
mode:
authorNikita Ioffe <ioffe@google.com>2020-02-27 17:19:33 +0000
committerNikita Ioffe <ioffe@google.com>2020-03-04 12:19:07 +0000
commitc577ac4e9303b36b0314ce865dcaba605c3ca238 (patch)
tree12ac843a628a68a686b520b44ec288a9048cf6d8 /libfscrypt
parentc4b5c59477f202e15a52c78203062cc21614f349 (diff)
downloadextras-c577ac4e9303b36b0314ce865dcaba605c3ca238.tar.gz
libfscrypt: Add == and != operators for EncryptionPolicy/Options
Test: atest libfscrypt_unit_test Bug: 143970043 Change-Id: Ibb8ee68513d4f04c1a64773768cc5ded9f7425ca Merged-In: Ibb8ee68513d4f04c1a64773768cc5ded9f7425ca (cherry picked from commit e6e61f778c409b08b2799c5281b5db74325bc801)
Diffstat (limited to 'libfscrypt')
-rw-r--r--libfscrypt/fscrypt.cpp6
-rw-r--r--libfscrypt/include/fscrypt/fscrypt.h18
-rw-r--r--libfscrypt/tests/fscrypt_test.cpp24
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);
+}