test: Add abstract delegate and better rollback indexes to FakeAvbOps

The abstract delegate allows tests to override and set their own
delegate, effectively customizing the fake behavior with minimal fuss. A
test could even use gmock to implement a delegate.

Rollback indexes will not necessarily be contiguous (0, 1, 2, ...) going
forward so handling them via a index-to-value map is better than a
strict vector.

Also, this CL moves C++ code, including tests, into a namespace.

Bug: 33553097
Test: unit

Change-Id: Ib53637c8b9320d9847b079aad79ce4fbd8ffc701
diff --git a/test/fake_avb_ops.h b/test/fake_avb_ops.h
index 6aa8be4..7680604 100644
--- a/test/fake_avb_ops.h
+++ b/test/fake_avb_ops.h
@@ -26,22 +26,80 @@
 #define FAKE_AVB_OPS_H_
 
 #include <base/files/file_util.h>
+#include <map>
 #include <string>
-#include <vector>
 
 #include <libavb_ab/libavb_ab.h>
 
-struct FakeAvbOpsC;
-typedef struct FakeAvbOpsC FakeAvbOpsC;
+namespace avb {
 
-class FakeAvbOps {
+// A delegate interface for ops callbacks. This allows tests to override default
+// fake implementations.
+class FakeAvbOpsDelegate {
+ public:
+  virtual ~FakeAvbOpsDelegate() {}
+  virtual AvbIOResult read_from_partition(const char* partition,
+                                          int64_t offset,
+                                          size_t num_bytes,
+                                          void* buffer,
+                                          size_t* out_num_read) = 0;
+
+  virtual AvbIOResult write_to_partition(const char* partition,
+                                         int64_t offset,
+                                         size_t num_bytes,
+                                         const void* buffer) = 0;
+
+  virtual AvbIOResult validate_vbmeta_public_key(
+      AvbOps* ops,
+      const uint8_t* public_key_data,
+      size_t public_key_length,
+      const uint8_t* public_key_metadata,
+      size_t public_key_metadata_length,
+      bool* out_key_is_trusted) = 0;
+
+  virtual AvbIOResult read_rollback_index(AvbOps* ops,
+                                          size_t rollback_index_slot,
+                                          uint64_t* out_rollback_index) = 0;
+
+  virtual AvbIOResult write_rollback_index(AvbOps* ops,
+                                           size_t rollback_index_slot,
+                                           uint64_t rollback_index) = 0;
+
+  virtual AvbIOResult read_is_device_unlocked(AvbOps* ops,
+                                              bool* out_is_device_unlocked) = 0;
+
+  virtual AvbIOResult get_unique_guid_for_partition(AvbOps* ops,
+                                                    const char* partition,
+                                                    char* guid_buf,
+                                                    size_t guid_buf_size) = 0;
+};
+
+// Provides fake implementations of AVB ops. All instances of this class must be
+// created on the same thread.
+class FakeAvbOps : public FakeAvbOpsDelegate {
  public:
   FakeAvbOps();
-  ~FakeAvbOps();
+  virtual ~FakeAvbOps();
 
-  AvbOps* avb_ops() { return (AvbOps*)avb_ops_; }
+  static FakeAvbOps* GetInstanceFromAvbOps(AvbOps* ops) {
+    return (*instance_map_)[ops];
+  }
+  static FakeAvbOps* GetInstanceFromAvbABOps(AvbABOps* ops) {
+    return (*instance_map_)[&ops->ops];
+  }
 
-  AvbABOps* avb_ab_ops() { return (AvbABOps*)avb_ops_; }
+  AvbOps* avb_ops() { return &avb_ops_; }
+
+  AvbABOps* avb_ab_ops() { return &avb_ab_ops_; }
+
+  FakeAvbOpsDelegate* delegate() {
+    return delegate_;
+  }
+
+  // Does not take ownership of |delegate|.
+  void set_delegate(FakeAvbOpsDelegate* delegate) {
+    delegate_ = delegate;
+  }
 
   void set_partition_dir(const base::FilePath& partition_dir) {
     partition_dir_ = partition_dir;
@@ -57,11 +115,11 @@
   }
 
   void set_stored_rollback_indexes(
-      const std::vector<uint64_t>& stored_rollback_indexes) {
+      const std::map<size_t, uint64_t>& stored_rollback_indexes) {
     stored_rollback_indexes_ = stored_rollback_indexes;
   }
 
-  std::vector<uint64_t> get_stored_rollback_indexes() {
+  std::map<size_t, uint64_t> get_stored_rollback_indexes() {
     return stored_rollback_indexes_;
   }
 
@@ -69,44 +127,53 @@
     stored_is_device_unlocked_ = stored_is_device_unlocked;
   }
 
+  // FakeAvbOpsDelegate methods.
   AvbIOResult read_from_partition(const char* partition, int64_t offset,
                                   size_t num_bytes, void* buffer,
-                                  size_t* out_num_read);
+                                  size_t* out_num_read) override;
 
   AvbIOResult write_to_partition(const char* partition, int64_t offset,
-                                 size_t num_bytes, const void* buffer);
+                                 size_t num_bytes, const void* buffer) override;
 
   AvbIOResult validate_vbmeta_public_key(AvbOps* ops,
                                          const uint8_t* public_key_data,
                                          size_t public_key_length,
                                          const uint8_t* public_key_metadata,
                                          size_t public_key_metadata_length,
-                                         bool* out_key_is_trusted);
+                                         bool* out_key_is_trusted) override;
 
   AvbIOResult read_rollback_index(AvbOps* ops, size_t rollback_index_location,
-                                  uint64_t* out_rollback_index);
+                                  uint64_t* out_rollback_index) override;
 
   AvbIOResult write_rollback_index(AvbOps* ops, size_t rollback_index_location,
-                                   uint64_t rollback_index);
+                                   uint64_t rollback_index) override;
 
   AvbIOResult read_is_device_unlocked(AvbOps* ops,
-                                      bool* out_is_device_unlocked);
+                                      bool* out_is_device_unlocked) override;
 
   AvbIOResult get_unique_guid_for_partition(AvbOps* ops, const char* partition,
                                             char* guid_buf,
-                                            size_t guid_buf_size);
+                                            size_t guid_buf_size) override;
 
  private:
-  FakeAvbOpsC* avb_ops_;
+  AvbOps avb_ops_;
+  AvbABOps avb_ab_ops_;
+
+  FakeAvbOpsDelegate* delegate_;
 
   base::FilePath partition_dir_;
 
   std::string expected_public_key_;
   std::string expected_public_key_metadata_;
 
-  std::vector<uint64_t> stored_rollback_indexes_;
+  std::map<size_t, uint64_t> stored_rollback_indexes_;
 
   bool stored_is_device_unlocked_;
+
+  // Reverse maps ops pointers back to the associated fake instance.
+  static std::map<AvbOps*, FakeAvbOps*>* instance_map_;
 };
 
+}  // namespace avb
+
 #endif /* FAKE_AVB_OPS_H_ */