1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef _MTP_MOCK_DATABASE_H 17 #define _MTP_MOCK_DATABASE_H 18 19 #include <map> 20 21 #include "IMtpDatabase.h" 22 #include "MtpStorage.h" 23 24 namespace android { 25 26 class MtpMockDatabase : public IMtpDatabase { 27 std::map<MtpStorageID, MtpStorage*> mStorage; 28 std::vector<MtpObjectInfo*> mObjects; 29 uint32_t mLastObjectHandle; 30 31 public: 32 MtpMockDatabase(); 33 virtual ~MtpMockDatabase(); 34 35 // MtpFuzzer methods addStorage(MtpStorage * storage)36 void addStorage(MtpStorage* storage) { 37 // we don't own this 38 mStorage[storage->getStorageID()] = storage; 39 } 40 hasStorage(MtpStorageID storage)41 bool hasStorage(MtpStorageID storage) { return mStorage.find(storage) != mStorage.end(); } 42 43 void addObject(MtpObjectInfo* info); 44 MtpObjectHandle allocateObjectHandle(); 45 46 // libmtp interface methods 47 // Called from SendObjectInfo to reserve a database entry for the incoming 48 // file. 49 MtpObjectHandle beginSendObject(const char* path, MtpObjectFormat format, 50 MtpObjectHandle parent, MtpStorageID storage); 51 52 // Called to report success or failure of the SendObject file transfer. 53 void endSendObject(MtpObjectHandle handle, bool succeeded); 54 55 // Called to rescan a file, such as after an edit. 56 void rescanFile(const char* path, MtpObjectHandle handle, MtpObjectFormat format); 57 58 MtpObjectHandleList* getObjectList(MtpStorageID storageID, MtpObjectFormat format, 59 MtpObjectHandle parent); 60 61 int getNumObjects(MtpStorageID storageID, MtpObjectFormat format, MtpObjectHandle parent); 62 63 // callee should delete[] the results from these 64 // results can be NULL 65 MtpObjectFormatList* getSupportedPlaybackFormats(); 66 MtpObjectFormatList* getSupportedCaptureFormats(); 67 MtpObjectPropertyList* getSupportedObjectProperties(MtpObjectFormat format); 68 MtpDevicePropertyList* getSupportedDeviceProperties(); 69 70 MtpResponseCode getObjectPropertyValue(MtpObjectHandle handle, MtpObjectProperty property, 71 MtpDataPacket& packet); 72 73 MtpResponseCode setObjectPropertyValue(MtpObjectHandle handle, MtpObjectProperty property, 74 MtpDataPacket& packet); 75 76 MtpResponseCode getDevicePropertyValue(MtpDeviceProperty property, MtpDataPacket& packet); 77 78 MtpResponseCode setDevicePropertyValue(MtpDeviceProperty property, MtpDataPacket& packet); 79 80 MtpResponseCode resetDeviceProperty(MtpDeviceProperty property); 81 82 MtpResponseCode getObjectPropertyList(MtpObjectHandle handle, uint32_t format, 83 uint32_t property, int groupCode, int depth, 84 MtpDataPacket& packet); 85 86 MtpResponseCode getObjectInfo(MtpObjectHandle handle, MtpObjectInfo& info); 87 88 void* getThumbnail(MtpObjectHandle handle, size_t& outThumbSize); 89 90 MtpResponseCode getObjectFilePath(MtpObjectHandle handle, MtpStringBuffer& outFilePath, 91 int64_t& outFileLength, MtpObjectFormat& outFormat); 92 93 int openFilePath(const char* path, bool transcode); 94 95 MtpResponseCode beginDeleteObject(MtpObjectHandle handle); 96 void endDeleteObject(MtpObjectHandle handle, bool succeeded); 97 98 MtpObjectHandleList* getObjectReferences(MtpObjectHandle handle); 99 100 MtpResponseCode setObjectReferences(MtpObjectHandle handle, MtpObjectHandleList* references); 101 102 MtpProperty* getObjectPropertyDesc(MtpObjectProperty property, MtpObjectFormat format); 103 104 MtpProperty* getDevicePropertyDesc(MtpDeviceProperty property); 105 106 MtpResponseCode beginMoveObject(MtpObjectHandle handle, MtpObjectHandle newParent, 107 MtpStorageID newStorage); 108 109 void endMoveObject(MtpObjectHandle oldParent, MtpObjectHandle newParent, 110 MtpStorageID oldStorage, MtpStorageID newStorage, MtpObjectHandle handle, 111 bool succeeded); 112 113 MtpResponseCode beginCopyObject(MtpObjectHandle handle, MtpObjectHandle newParent, 114 MtpStorageID newStorage); 115 void endCopyObject(MtpObjectHandle handle, bool succeeded); 116 }; 117 118 }; // namespace android 119 120 #endif // _MTP_MOCK_DATABASE_H 121