Remove BlobCAche from libutils

it has moved to libEGL which was the only client.


Test: compiled & Run
Bug: vndk-stable
Change-Id: Ife18727fa1c48b1be6910058867016348b7f02c1
diff --git a/libutils/Android.bp b/libutils/Android.bp
index 0c777b1..2a392b9 100644
--- a/libutils/Android.bp
+++ b/libutils/Android.bp
@@ -65,7 +65,6 @@
     target: {
         android: {
             srcs: [
-                "BlobCache.cpp",
                 "Looper.cpp",
                 "ProcessCallStack.cpp",
                 "Trace.cpp",
diff --git a/libutils/BlobCache.cpp b/libutils/BlobCache.cpp
deleted file mode 100644
index b0fdd44..0000000
--- a/libutils/BlobCache.cpp
+++ /dev/null
@@ -1,376 +0,0 @@
-/*
- ** Copyright 2011, The Android Open Source Project
- **
- ** Licensed under the Apache License, Version 2.0 (the "License");
- ** you may not use this file except in compliance with the License.
- ** You may obtain a copy of the License at
- **
- **     http://www.apache.org/licenses/LICENSE-2.0
- **
- ** Unless required by applicable law or agreed to in writing, software
- ** distributed under the License is distributed on an "AS IS" BASIS,
- ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- ** See the License for the specific language governing permissions and
- ** limitations under the License.
- */
-
-#define LOG_TAG "BlobCache"
-//#define LOG_NDEBUG 0
-
-#include <utils/BlobCache.h>
-#include <utils/Timers.h>
-
-#include <inttypes.h>
-
-#include <cutils/properties.h>
-
-namespace android {
-
-// BlobCache::Header::mMagicNumber value
-static const uint32_t blobCacheMagic = ('_' << 24) + ('B' << 16) + ('b' << 8) + '$';
-
-// BlobCache::Header::mBlobCacheVersion value
-static const uint32_t blobCacheVersion = 3;
-
-// BlobCache::Header::mDeviceVersion value
-static const uint32_t blobCacheDeviceVersion = 1;
-
-BlobCache::BlobCache(size_t maxKeySize, size_t maxValueSize, size_t maxTotalSize):
-        mMaxKeySize(maxKeySize),
-        mMaxValueSize(maxValueSize),
-        mMaxTotalSize(maxTotalSize),
-        mTotalSize(0) {
-    nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
-#ifdef _WIN32
-    srand(now);
-#else
-    mRandState[0] = (now >> 0) & 0xFFFF;
-    mRandState[1] = (now >> 16) & 0xFFFF;
-    mRandState[2] = (now >> 32) & 0xFFFF;
-#endif
-    ALOGV("initializing random seed using %lld", (unsigned long long)now);
-}
-
-void BlobCache::set(const void* key, size_t keySize, const void* value,
-        size_t valueSize) {
-    if (mMaxKeySize < keySize) {
-        ALOGV("set: not caching because the key is too large: %zu (limit: %zu)",
-                keySize, mMaxKeySize);
-        return;
-    }
-    if (mMaxValueSize < valueSize) {
-        ALOGV("set: not caching because the value is too large: %zu (limit: %zu)",
-                valueSize, mMaxValueSize);
-        return;
-    }
-    if (mMaxTotalSize < keySize + valueSize) {
-        ALOGV("set: not caching because the combined key/value size is too "
-                "large: %zu (limit: %zu)", keySize + valueSize, mMaxTotalSize);
-        return;
-    }
-    if (keySize == 0) {
-        ALOGW("set: not caching because keySize is 0");
-        return;
-    }
-    if (valueSize <= 0) {
-        ALOGW("set: not caching because valueSize is 0");
-        return;
-    }
-
-    sp<Blob> dummyKey(new Blob(key, keySize, false));
-    CacheEntry dummyEntry(dummyKey, NULL);
-
-    while (true) {
-        ssize_t index = mCacheEntries.indexOf(dummyEntry);
-        if (index < 0) {
-            // Create a new cache entry.
-            sp<Blob> keyBlob(new Blob(key, keySize, true));
-            sp<Blob> valueBlob(new Blob(value, valueSize, true));
-            size_t newTotalSize = mTotalSize + keySize + valueSize;
-            if (mMaxTotalSize < newTotalSize) {
-                if (isCleanable()) {
-                    // Clean the cache and try again.
-                    clean();
-                    continue;
-                } else {
-                    ALOGV("set: not caching new key/value pair because the "
-                            "total cache size limit would be exceeded: %zu "
-                            "(limit: %zu)",
-                            keySize + valueSize, mMaxTotalSize);
-                    break;
-                }
-            }
-            mCacheEntries.add(CacheEntry(keyBlob, valueBlob));
-            mTotalSize = newTotalSize;
-            ALOGV("set: created new cache entry with %zu byte key and %zu byte value",
-                    keySize, valueSize);
-        } else {
-            // Update the existing cache entry.
-            sp<Blob> valueBlob(new Blob(value, valueSize, true));
-            sp<Blob> oldValueBlob(mCacheEntries[index].getValue());
-            size_t newTotalSize = mTotalSize + valueSize - oldValueBlob->getSize();
-            if (mMaxTotalSize < newTotalSize) {
-                if (isCleanable()) {
-                    // Clean the cache and try again.
-                    clean();
-                    continue;
-                } else {
-                    ALOGV("set: not caching new value because the total cache "
-                            "size limit would be exceeded: %zu (limit: %zu)",
-                            keySize + valueSize, mMaxTotalSize);
-                    break;
-                }
-            }
-            mCacheEntries.editItemAt(index).setValue(valueBlob);
-            mTotalSize = newTotalSize;
-            ALOGV("set: updated existing cache entry with %zu byte key and %zu byte "
-                    "value", keySize, valueSize);
-        }
-        break;
-    }
-}
-
-size_t BlobCache::get(const void* key, size_t keySize, void* value,
-        size_t valueSize) {
-    if (mMaxKeySize < keySize) {
-        ALOGV("get: not searching because the key is too large: %zu (limit %zu)",
-                keySize, mMaxKeySize);
-        return 0;
-    }
-    sp<Blob> dummyKey(new Blob(key, keySize, false));
-    CacheEntry dummyEntry(dummyKey, NULL);
-    ssize_t index = mCacheEntries.indexOf(dummyEntry);
-    if (index < 0) {
-        ALOGV("get: no cache entry found for key of size %zu", keySize);
-        return 0;
-    }
-
-    // The key was found. Return the value if the caller's buffer is large
-    // enough.
-    sp<Blob> valueBlob(mCacheEntries[index].getValue());
-    size_t valueBlobSize = valueBlob->getSize();
-    if (valueBlobSize <= valueSize) {
-        ALOGV("get: copying %zu bytes to caller's buffer", valueBlobSize);
-        memcpy(value, valueBlob->getData(), valueBlobSize);
-    } else {
-        ALOGV("get: caller's buffer is too small for value: %zu (needs %zu)",
-                valueSize, valueBlobSize);
-    }
-    return valueBlobSize;
-}
-
-static inline size_t align4(size_t size) {
-    return (size + 3) & ~3;
-}
-
-size_t BlobCache::getFlattenedSize() const {
-    size_t size = align4(sizeof(Header) + PROPERTY_VALUE_MAX);
-    for (size_t i = 0; i < mCacheEntries.size(); i++) {
-        const CacheEntry& e(mCacheEntries[i]);
-        sp<Blob> keyBlob = e.getKey();
-        sp<Blob> valueBlob = e.getValue();
-        size += align4(sizeof(EntryHeader) + keyBlob->getSize() +
-                       valueBlob->getSize());
-    }
-    return size;
-}
-
-status_t BlobCache::flatten(void* buffer, size_t size) const {
-    // Write the cache header
-    if (size < sizeof(Header)) {
-        ALOGE("flatten: not enough room for cache header");
-        return BAD_VALUE;
-    }
-    Header* header = reinterpret_cast<Header*>(buffer);
-    header->mMagicNumber = blobCacheMagic;
-    header->mBlobCacheVersion = blobCacheVersion;
-    header->mDeviceVersion = blobCacheDeviceVersion;
-    header->mNumEntries = mCacheEntries.size();
-    char buildId[PROPERTY_VALUE_MAX];
-    header->mBuildIdLength = property_get("ro.build.id", buildId, "");
-    memcpy(header->mBuildId, buildId, header->mBuildIdLength);
-
-    // Write cache entries
-    uint8_t* byteBuffer = reinterpret_cast<uint8_t*>(buffer);
-    off_t byteOffset = align4(sizeof(Header) + header->mBuildIdLength);
-    for (size_t i = 0; i < mCacheEntries.size(); i++) {
-        const CacheEntry& e(mCacheEntries[i]);
-        sp<Blob> keyBlob = e.getKey();
-        sp<Blob> valueBlob = e.getValue();
-        size_t keySize = keyBlob->getSize();
-        size_t valueSize = valueBlob->getSize();
-
-        size_t entrySize = sizeof(EntryHeader) + keySize + valueSize;
-        size_t totalSize = align4(entrySize);
-        if (byteOffset + totalSize > size) {
-            ALOGE("flatten: not enough room for cache entries");
-            return BAD_VALUE;
-        }
-
-        EntryHeader* eheader = reinterpret_cast<EntryHeader*>(
-            &byteBuffer[byteOffset]);
-        eheader->mKeySize = keySize;
-        eheader->mValueSize = valueSize;
-
-        memcpy(eheader->mData, keyBlob->getData(), keySize);
-        memcpy(eheader->mData + keySize, valueBlob->getData(), valueSize);
-
-        if (totalSize > entrySize) {
-            // We have padding bytes. Those will get written to storage, and contribute to the CRC,
-            // so make sure we zero-them to have reproducible results.
-            memset(eheader->mData + keySize + valueSize, 0, totalSize - entrySize);
-        }
-
-        byteOffset += totalSize;
-    }
-
-    return OK;
-}
-
-status_t BlobCache::unflatten(void const* buffer, size_t size) {
-    // All errors should result in the BlobCache being in an empty state.
-    mCacheEntries.clear();
-
-    // Read the cache header
-    if (size < sizeof(Header)) {
-        ALOGE("unflatten: not enough room for cache header");
-        return BAD_VALUE;
-    }
-    const Header* header = reinterpret_cast<const Header*>(buffer);
-    if (header->mMagicNumber != blobCacheMagic) {
-        ALOGE("unflatten: bad magic number: %" PRIu32, header->mMagicNumber);
-        return BAD_VALUE;
-    }
-    char buildId[PROPERTY_VALUE_MAX];
-    int len = property_get("ro.build.id", buildId, "");
-    if (header->mBlobCacheVersion != blobCacheVersion ||
-            header->mDeviceVersion != blobCacheDeviceVersion ||
-            len != header->mBuildIdLength ||
-            strncmp(buildId, header->mBuildId, len)) {
-        // We treat version mismatches as an empty cache.
-        return OK;
-    }
-
-    // Read cache entries
-    const uint8_t* byteBuffer = reinterpret_cast<const uint8_t*>(buffer);
-    off_t byteOffset = align4(sizeof(Header) + header->mBuildIdLength);
-    size_t numEntries = header->mNumEntries;
-    for (size_t i = 0; i < numEntries; i++) {
-        if (byteOffset + sizeof(EntryHeader) > size) {
-            mCacheEntries.clear();
-            ALOGE("unflatten: not enough room for cache entry headers");
-            return BAD_VALUE;
-        }
-
-        const EntryHeader* eheader = reinterpret_cast<const EntryHeader*>(
-                &byteBuffer[byteOffset]);
-        size_t keySize = eheader->mKeySize;
-        size_t valueSize = eheader->mValueSize;
-        size_t entrySize = sizeof(EntryHeader) + keySize + valueSize;
-
-        size_t totalSize = align4(entrySize);
-        if (byteOffset + totalSize > size) {
-            mCacheEntries.clear();
-            ALOGE("unflatten: not enough room for cache entry headers");
-            return BAD_VALUE;
-        }
-
-        const uint8_t* data = eheader->mData;
-        set(data, keySize, data + keySize, valueSize);
-
-        byteOffset += totalSize;
-    }
-
-    return OK;
-}
-
-long int BlobCache::blob_random() {
-#ifdef _WIN32
-    return rand();
-#else
-    return nrand48(mRandState);
-#endif
-}
-
-void BlobCache::clean() {
-    // Remove a random cache entry until the total cache size gets below half
-    // the maximum total cache size.
-    while (mTotalSize > mMaxTotalSize / 2) {
-        size_t i = size_t(blob_random() % (mCacheEntries.size()));
-        const CacheEntry& entry(mCacheEntries[i]);
-        mTotalSize -= entry.getKey()->getSize() + entry.getValue()->getSize();
-        mCacheEntries.removeAt(i);
-    }
-}
-
-bool BlobCache::isCleanable() const {
-    return mTotalSize > mMaxTotalSize / 2;
-}
-
-BlobCache::Blob::Blob(const void* data, size_t size, bool copyData):
-        mData(copyData ? malloc(size) : data),
-        mSize(size),
-        mOwnsData(copyData) {
-    if (data != NULL && copyData) {
-        memcpy(const_cast<void*>(mData), data, size);
-    }
-}
-
-BlobCache::Blob::~Blob() {
-    if (mOwnsData) {
-        free(const_cast<void*>(mData));
-    }
-}
-
-bool BlobCache::Blob::operator<(const Blob& rhs) const {
-    if (mSize == rhs.mSize) {
-        return memcmp(mData, rhs.mData, mSize) < 0;
-    } else {
-        return mSize < rhs.mSize;
-    }
-}
-
-const void* BlobCache::Blob::getData() const {
-    return mData;
-}
-
-size_t BlobCache::Blob::getSize() const {
-    return mSize;
-}
-
-BlobCache::CacheEntry::CacheEntry() {
-}
-
-BlobCache::CacheEntry::CacheEntry(const sp<Blob>& key, const sp<Blob>& value):
-        mKey(key),
-        mValue(value) {
-}
-
-BlobCache::CacheEntry::CacheEntry(const CacheEntry& ce):
-        mKey(ce.mKey),
-        mValue(ce.mValue) {
-}
-
-bool BlobCache::CacheEntry::operator<(const CacheEntry& rhs) const {
-    return *mKey < *rhs.mKey;
-}
-
-const BlobCache::CacheEntry& BlobCache::CacheEntry::operator=(const CacheEntry& rhs) {
-    mKey = rhs.mKey;
-    mValue = rhs.mValue;
-    return *this;
-}
-
-sp<BlobCache::Blob> BlobCache::CacheEntry::getKey() const {
-    return mKey;
-}
-
-sp<BlobCache::Blob> BlobCache::CacheEntry::getValue() const {
-    return mValue;
-}
-
-void BlobCache::CacheEntry::setValue(const sp<Blob>& value) {
-    mValue = value;
-}
-
-} // namespace android
diff --git a/libutils/include/utils/BlobCache.h b/libutils/include/utils/BlobCache.h
deleted file mode 100644
index 27a81e3..0000000
--- a/libutils/include/utils/BlobCache.h
+++ /dev/null
@@ -1,247 +0,0 @@
-/*
- ** Copyright 2011, The Android Open Source Project
- **
- ** Licensed under the Apache License, Version 2.0 (the "License");
- ** you may not use this file except in compliance with the License.
- ** You may obtain a copy of the License at
- **
- **     http://www.apache.org/licenses/LICENSE-2.0
- **
- ** Unless required by applicable law or agreed to in writing, software
- ** distributed under the License is distributed on an "AS IS" BASIS,
- ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- ** See the License for the specific language governing permissions and
- ** limitations under the License.
- */
-
-#ifndef ANDROID_BLOB_CACHE_H
-#define ANDROID_BLOB_CACHE_H
-
-#include <stddef.h>
-
-#include <utils/RefBase.h>
-#include <utils/SortedVector.h>
-
-namespace android {
-
-// A BlobCache is an in-memory cache for binary key/value pairs.  A BlobCache
-// does NOT provide any thread-safety guarantees.
-//
-// The cache contents can be serialized to an in-memory buffer or mmap'd file
-// and then reloaded in a subsequent execution of the program.  This
-// serialization is non-portable and the data should only be used by the device
-// that generated it.
-class BlobCache : public RefBase {
-
-public:
-
-    // Create an empty blob cache. The blob cache will cache key/value pairs
-    // with key and value sizes less than or equal to maxKeySize and
-    // maxValueSize, respectively. The total combined size of ALL cache entries
-    // (key sizes plus value sizes) will not exceed maxTotalSize.
-    BlobCache(size_t maxKeySize, size_t maxValueSize, size_t maxTotalSize);
-
-    // set inserts a new binary value into the cache and associates it with the
-    // given binary key.  If the key or value are too large for the cache then
-    // the cache remains unchanged.  This includes the case where a different
-    // value was previously associated with the given key - the old value will
-    // remain in the cache.  If the given key and value are small enough to be
-    // put in the cache (based on the maxKeySize, maxValueSize, and maxTotalSize
-    // values specified to the BlobCache constructor), then the key/value pair
-    // will be in the cache after set returns.  Note, however, that a subsequent
-    // call to set may evict old key/value pairs from the cache.
-    //
-    // Preconditions:
-    //   key != NULL
-    //   0 < keySize
-    //   value != NULL
-    //   0 < valueSize
-    void set(const void* key, size_t keySize, const void* value,
-            size_t valueSize);
-
-    // get retrieves from the cache the binary value associated with a given
-    // binary key.  If the key is present in the cache then the length of the
-    // binary value associated with that key is returned.  If the value argument
-    // is non-NULL and the size of the cached value is less than valueSize bytes
-    // then the cached value is copied into the buffer pointed to by the value
-    // argument.  If the key is not present in the cache then 0 is returned and
-    // the buffer pointed to by the value argument is not modified.
-    //
-    // Note that when calling get multiple times with the same key, the later
-    // calls may fail, returning 0, even if earlier calls succeeded.  The return
-    // value must be checked for each call.
-    //
-    // Preconditions:
-    //   key != NULL
-    //   0 < keySize
-    //   0 <= valueSize
-    size_t get(const void* key, size_t keySize, void* value, size_t valueSize);
-
-
-    // getFlattenedSize returns the number of bytes needed to store the entire
-    // serialized cache.
-    size_t getFlattenedSize() const;
-
-    // flatten serializes the current contents of the cache into the memory
-    // pointed to by 'buffer'.  The serialized cache contents can later be
-    // loaded into a BlobCache object using the unflatten method.  The contents
-    // of the BlobCache object will not be modified.
-    //
-    // Preconditions:
-    //   size >= this.getFlattenedSize()
-    status_t flatten(void* buffer, size_t size) const;
-
-    // unflatten replaces the contents of the cache with the serialized cache
-    // contents in the memory pointed to by 'buffer'.  The previous contents of
-    // the BlobCache will be evicted from the cache.  If an error occurs while
-    // unflattening the serialized cache contents then the BlobCache will be
-    // left in an empty state.
-    //
-    status_t unflatten(void const* buffer, size_t size);
-
-private:
-    // Copying is disallowed.
-    BlobCache(const BlobCache&);
-    void operator=(const BlobCache&);
-
-    // A random function helper to get around MinGW not having nrand48()
-    long int blob_random();
-
-    // clean evicts a randomly chosen set of entries from the cache such that
-    // the total size of all remaining entries is less than mMaxTotalSize/2.
-    void clean();
-
-    // isCleanable returns true if the cache is full enough for the clean method
-    // to have some effect, and false otherwise.
-    bool isCleanable() const;
-
-    // A Blob is an immutable sized unstructured data blob.
-    class Blob : public RefBase {
-    public:
-        Blob(const void* data, size_t size, bool copyData);
-        ~Blob();
-
-        bool operator<(const Blob& rhs) const;
-
-        const void* getData() const;
-        size_t getSize() const;
-
-    private:
-        // Copying is not allowed.
-        Blob(const Blob&);
-        void operator=(const Blob&);
-
-        // mData points to the buffer containing the blob data.
-        const void* mData;
-
-        // mSize is the size of the blob data in bytes.
-        size_t mSize;
-
-        // mOwnsData indicates whether or not this Blob object should free the
-        // memory pointed to by mData when the Blob gets destructed.
-        bool mOwnsData;
-    };
-
-    // A CacheEntry is a single key/value pair in the cache.
-    class CacheEntry {
-    public:
-        CacheEntry();
-        CacheEntry(const sp<Blob>& key, const sp<Blob>& value);
-        CacheEntry(const CacheEntry& ce);
-
-        bool operator<(const CacheEntry& rhs) const;
-        const CacheEntry& operator=(const CacheEntry&);
-
-        sp<Blob> getKey() const;
-        sp<Blob> getValue() const;
-
-        void setValue(const sp<Blob>& value);
-
-    private:
-
-        // mKey is the key that identifies the cache entry.
-        sp<Blob> mKey;
-
-        // mValue is the cached data associated with the key.
-        sp<Blob> mValue;
-    };
-
-    // A Header is the header for the entire BlobCache serialization format. No
-    // need to make this portable, so we simply write the struct out.
-    struct Header {
-        // mMagicNumber is the magic number that identifies the data as
-        // serialized BlobCache contents.  It must always contain 'Blb$'.
-        uint32_t mMagicNumber;
-
-        // mBlobCacheVersion is the serialization format version.
-        uint32_t mBlobCacheVersion;
-
-        // mDeviceVersion is the device-specific version of the cache.  This can
-        // be used to invalidate the cache.
-        uint32_t mDeviceVersion;
-
-        // mNumEntries is number of cache entries following the header in the
-        // data.
-        size_t mNumEntries;
-
-        // mBuildId is the build id of the device when the cache was created.
-        // When an update to the build happens (via an OTA or other update) this
-        // is used to invalidate the cache.
-        int mBuildIdLength;
-        char mBuildId[];
-    };
-
-    // An EntryHeader is the header for a serialized cache entry.  No need to
-    // make this portable, so we simply write the struct out.  Each EntryHeader
-    // is followed imediately by the key data and then the value data.
-    //
-    // The beginning of each serialized EntryHeader is 4-byte aligned, so the
-    // number of bytes that a serialized cache entry will occupy is:
-    //
-    //   ((sizeof(EntryHeader) + keySize + valueSize) + 3) & ~3
-    //
-    struct EntryHeader {
-        // mKeySize is the size of the entry key in bytes.
-        size_t mKeySize;
-
-        // mValueSize is the size of the entry value in bytes.
-        size_t mValueSize;
-
-        // mData contains both the key and value data for the cache entry.  The
-        // key comes first followed immediately by the value.
-        uint8_t mData[];
-    };
-
-    // mMaxKeySize is the maximum key size that will be cached. Calls to
-    // BlobCache::set with a keySize parameter larger than mMaxKeySize will
-    // simply not add the key/value pair to the cache.
-    const size_t mMaxKeySize;
-
-    // mMaxValueSize is the maximum value size that will be cached. Calls to
-    // BlobCache::set with a valueSize parameter larger than mMaxValueSize will
-    // simply not add the key/value pair to the cache.
-    const size_t mMaxValueSize;
-
-    // mMaxTotalSize is the maximum size that all cache entries can occupy. This
-    // includes space for both keys and values. When a call to BlobCache::set
-    // would otherwise cause this limit to be exceeded, either the key/value
-    // pair passed to BlobCache::set will not be cached or other cache entries
-    // will be evicted from the cache to make room for the new entry.
-    const size_t mMaxTotalSize;
-
-    // mTotalSize is the total combined size of all keys and values currently in
-    // the cache.
-    size_t mTotalSize;
-
-    // mRandState is the pseudo-random number generator state. It is passed to
-    // nrand48 to generate random numbers when needed.
-    unsigned short mRandState[3];
-
-    // mCacheEntries stores all the cache entries that are resident in memory.
-    // Cache entries are added to it by the 'set' method.
-    SortedVector<CacheEntry> mCacheEntries;
-};
-
-}
-
-#endif // ANDROID_BLOB_CACHE_H
diff --git a/libutils/tests/Android.bp b/libutils/tests/Android.bp
index ea606a1..7b62c24 100644
--- a/libutils/tests/Android.bp
+++ b/libutils/tests/Android.bp
@@ -33,7 +33,6 @@
     target: {
         android: {
             srcs: [
-                "BlobCache_test.cpp",
                 "Looper_test.cpp",
                 "RefBase_test.cpp",
                 "SystemClock_test.cpp",
diff --git a/libutils/tests/BlobCache_test.cpp b/libutils/tests/BlobCache_test.cpp
deleted file mode 100644
index 1e2ff98..0000000
--- a/libutils/tests/BlobCache_test.cpp
+++ /dev/null
@@ -1,425 +0,0 @@
-/*
- ** Copyright 2011, The Android Open Source Project
- **
- ** Licensed under the Apache License, Version 2.0 (the "License");
- ** you may not use this file except in compliance with the License.
- ** You may obtain a copy of the License at
- **
- **     http://www.apache.org/licenses/LICENSE-2.0
- **
- ** Unless required by applicable law or agreed to in writing, software
- ** distributed under the License is distributed on an "AS IS" BASIS,
- ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- ** See the License for the specific language governing permissions and
- ** limitations under the License.
- */
-
-#include <fcntl.h>
-#include <stdio.h>
-
-#include <gtest/gtest.h>
-
-#include <utils/BlobCache.h>
-#include <utils/Errors.h>
-
-namespace android {
-
-class BlobCacheTest : public ::testing::Test {
-protected:
-    enum {
-        MAX_KEY_SIZE = 6,
-        MAX_VALUE_SIZE = 8,
-        MAX_TOTAL_SIZE = 13,
-    };
-
-    virtual void SetUp() {
-        mBC = new BlobCache(MAX_KEY_SIZE, MAX_VALUE_SIZE, MAX_TOTAL_SIZE);
-    }
-
-    virtual void TearDown() {
-        mBC.clear();
-    }
-
-    sp<BlobCache> mBC;
-};
-
-TEST_F(BlobCacheTest, CacheSingleValueSucceeds) {
-    unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee };
-    mBC->set("abcd", 4, "efgh", 4);
-    ASSERT_EQ(size_t(4), mBC->get("abcd", 4, buf, 4));
-    ASSERT_EQ('e', buf[0]);
-    ASSERT_EQ('f', buf[1]);
-    ASSERT_EQ('g', buf[2]);
-    ASSERT_EQ('h', buf[3]);
-}
-
-TEST_F(BlobCacheTest, CacheTwoValuesSucceeds) {
-    unsigned char buf[2] = { 0xee, 0xee };
-    mBC->set("ab", 2, "cd", 2);
-    mBC->set("ef", 2, "gh", 2);
-    ASSERT_EQ(size_t(2), mBC->get("ab", 2, buf, 2));
-    ASSERT_EQ('c', buf[0]);
-    ASSERT_EQ('d', buf[1]);
-    ASSERT_EQ(size_t(2), mBC->get("ef", 2, buf, 2));
-    ASSERT_EQ('g', buf[0]);
-    ASSERT_EQ('h', buf[1]);
-}
-
-TEST_F(BlobCacheTest, GetOnlyWritesInsideBounds) {
-    unsigned char buf[6] = { 0xee, 0xee, 0xee, 0xee, 0xee, 0xee };
-    mBC->set("abcd", 4, "efgh", 4);
-    ASSERT_EQ(size_t(4), mBC->get("abcd", 4, buf+1, 4));
-    ASSERT_EQ(0xee, buf[0]);
-    ASSERT_EQ('e', buf[1]);
-    ASSERT_EQ('f', buf[2]);
-    ASSERT_EQ('g', buf[3]);
-    ASSERT_EQ('h', buf[4]);
-    ASSERT_EQ(0xee, buf[5]);
-}
-
-TEST_F(BlobCacheTest, GetOnlyWritesIfBufferIsLargeEnough) {
-    unsigned char buf[3] = { 0xee, 0xee, 0xee };
-    mBC->set("abcd", 4, "efgh", 4);
-    ASSERT_EQ(size_t(4), mBC->get("abcd", 4, buf, 3));
-    ASSERT_EQ(0xee, buf[0]);
-    ASSERT_EQ(0xee, buf[1]);
-    ASSERT_EQ(0xee, buf[2]);
-}
-
-TEST_F(BlobCacheTest, GetDoesntAccessNullBuffer) {
-    mBC->set("abcd", 4, "efgh", 4);
-    ASSERT_EQ(size_t(4), mBC->get("abcd", 4, NULL, 0));
-}
-
-TEST_F(BlobCacheTest, MultipleSetsCacheLatestValue) {
-    unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee };
-    mBC->set("abcd", 4, "efgh", 4);
-    mBC->set("abcd", 4, "ijkl", 4);
-    ASSERT_EQ(size_t(4), mBC->get("abcd", 4, buf, 4));
-    ASSERT_EQ('i', buf[0]);
-    ASSERT_EQ('j', buf[1]);
-    ASSERT_EQ('k', buf[2]);
-    ASSERT_EQ('l', buf[3]);
-}
-
-TEST_F(BlobCacheTest, SecondSetKeepsFirstValueIfTooLarge) {
-    unsigned char buf[MAX_VALUE_SIZE+1] = { 0xee, 0xee, 0xee, 0xee };
-    mBC->set("abcd", 4, "efgh", 4);
-    mBC->set("abcd", 4, buf, MAX_VALUE_SIZE+1);
-    ASSERT_EQ(size_t(4), mBC->get("abcd", 4, buf, 4));
-    ASSERT_EQ('e', buf[0]);
-    ASSERT_EQ('f', buf[1]);
-    ASSERT_EQ('g', buf[2]);
-    ASSERT_EQ('h', buf[3]);
-}
-
-TEST_F(BlobCacheTest, DoesntCacheIfKeyIsTooBig) {
-    char key[MAX_KEY_SIZE+1];
-    unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee };
-    for (int i = 0; i < MAX_KEY_SIZE+1; i++) {
-        key[i] = 'a';
-    }
-    mBC->set(key, MAX_KEY_SIZE+1, "bbbb", 4);
-    ASSERT_EQ(size_t(0), mBC->get(key, MAX_KEY_SIZE+1, buf, 4));
-    ASSERT_EQ(0xee, buf[0]);
-    ASSERT_EQ(0xee, buf[1]);
-    ASSERT_EQ(0xee, buf[2]);
-    ASSERT_EQ(0xee, buf[3]);
-}
-
-TEST_F(BlobCacheTest, DoesntCacheIfValueIsTooBig) {
-    char buf[MAX_VALUE_SIZE+1];
-    for (int i = 0; i < MAX_VALUE_SIZE+1; i++) {
-        buf[i] = 'b';
-    }
-    mBC->set("abcd", 4, buf, MAX_VALUE_SIZE+1);
-    for (int i = 0; i < MAX_VALUE_SIZE+1; i++) {
-        buf[i] = 0xee;
-    }
-    ASSERT_EQ(size_t(0), mBC->get("abcd", 4, buf, MAX_VALUE_SIZE+1));
-    for (int i = 0; i < MAX_VALUE_SIZE+1; i++) {
-        SCOPED_TRACE(i);
-        ASSERT_EQ(0xee, buf[i]);
-    }
-}
-
-TEST_F(BlobCacheTest, DoesntCacheIfKeyValuePairIsTooBig) {
-    // Check a testing assumptions
-    ASSERT_TRUE(MAX_TOTAL_SIZE < MAX_KEY_SIZE + MAX_VALUE_SIZE);
-    ASSERT_TRUE(MAX_KEY_SIZE < MAX_TOTAL_SIZE);
-
-    enum { bufSize = MAX_TOTAL_SIZE - MAX_KEY_SIZE + 1 };
-
-    char key[MAX_KEY_SIZE];
-    char buf[bufSize];
-    for (int i = 0; i < MAX_KEY_SIZE; i++) {
-        key[i] = 'a';
-    }
-    for (int i = 0; i < bufSize; i++) {
-        buf[i] = 'b';
-    }
-
-    mBC->set(key, MAX_KEY_SIZE, buf, MAX_VALUE_SIZE);
-    ASSERT_EQ(size_t(0), mBC->get(key, MAX_KEY_SIZE, NULL, 0));
-}
-
-TEST_F(BlobCacheTest, CacheMaxKeySizeSucceeds) {
-    char key[MAX_KEY_SIZE];
-    unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee };
-    for (int i = 0; i < MAX_KEY_SIZE; i++) {
-        key[i] = 'a';
-    }
-    mBC->set(key, MAX_KEY_SIZE, "wxyz", 4);
-    ASSERT_EQ(size_t(4), mBC->get(key, MAX_KEY_SIZE, buf, 4));
-    ASSERT_EQ('w', buf[0]);
-    ASSERT_EQ('x', buf[1]);
-    ASSERT_EQ('y', buf[2]);
-    ASSERT_EQ('z', buf[3]);
-}
-
-TEST_F(BlobCacheTest, CacheMaxValueSizeSucceeds) {
-    char buf[MAX_VALUE_SIZE];
-    for (int i = 0; i < MAX_VALUE_SIZE; i++) {
-        buf[i] = 'b';
-    }
-    mBC->set("abcd", 4, buf, MAX_VALUE_SIZE);
-    for (int i = 0; i < MAX_VALUE_SIZE; i++) {
-        buf[i] = 0xee;
-    }
-    ASSERT_EQ(size_t(MAX_VALUE_SIZE), mBC->get("abcd", 4, buf,
-            MAX_VALUE_SIZE));
-    for (int i = 0; i < MAX_VALUE_SIZE; i++) {
-        SCOPED_TRACE(i);
-        ASSERT_EQ('b', buf[i]);
-    }
-}
-
-TEST_F(BlobCacheTest, CacheMaxKeyValuePairSizeSucceeds) {
-    // Check a testing assumption
-    ASSERT_TRUE(MAX_KEY_SIZE < MAX_TOTAL_SIZE);
-
-    enum { bufSize = MAX_TOTAL_SIZE - MAX_KEY_SIZE };
-
-    char key[MAX_KEY_SIZE];
-    char buf[bufSize];
-    for (int i = 0; i < MAX_KEY_SIZE; i++) {
-        key[i] = 'a';
-    }
-    for (int i = 0; i < bufSize; i++) {
-        buf[i] = 'b';
-    }
-
-    mBC->set(key, MAX_KEY_SIZE, buf, bufSize);
-    ASSERT_EQ(size_t(bufSize), mBC->get(key, MAX_KEY_SIZE, NULL, 0));
-}
-
-TEST_F(BlobCacheTest, CacheMinKeyAndValueSizeSucceeds) {
-    unsigned char buf[1] = { 0xee };
-    mBC->set("x", 1, "y", 1);
-    ASSERT_EQ(size_t(1), mBC->get("x", 1, buf, 1));
-    ASSERT_EQ('y', buf[0]);
-}
-
-TEST_F(BlobCacheTest, CacheSizeDoesntExceedTotalLimit) {
-    for (int i = 0; i < 256; i++) {
-        uint8_t k = i;
-        mBC->set(&k, 1, "x", 1);
-    }
-    int numCached = 0;
-    for (int i = 0; i < 256; i++) {
-        uint8_t k = i;
-        if (mBC->get(&k, 1, NULL, 0) == 1) {
-            numCached++;
-        }
-    }
-    ASSERT_GE(MAX_TOTAL_SIZE / 2, numCached);
-}
-
-TEST_F(BlobCacheTest, ExceedingTotalLimitHalvesCacheSize) {
-    // Fill up the entire cache with 1 char key/value pairs.
-    const int maxEntries = MAX_TOTAL_SIZE / 2;
-    for (int i = 0; i < maxEntries; i++) {
-        uint8_t k = i;
-        mBC->set(&k, 1, "x", 1);
-    }
-    // Insert one more entry, causing a cache overflow.
-    {
-        uint8_t k = maxEntries;
-        mBC->set(&k, 1, "x", 1);
-    }
-    // Count the number of entries in the cache.
-    int numCached = 0;
-    for (int i = 0; i < maxEntries+1; i++) {
-        uint8_t k = i;
-        if (mBC->get(&k, 1, NULL, 0) == 1) {
-            numCached++;
-        }
-    }
-    ASSERT_EQ(maxEntries/2 + 1, numCached);
-}
-
-class BlobCacheFlattenTest : public BlobCacheTest {
-protected:
-    virtual void SetUp() {
-        BlobCacheTest::SetUp();
-        mBC2 = new BlobCache(MAX_KEY_SIZE, MAX_VALUE_SIZE, MAX_TOTAL_SIZE);
-    }
-
-    virtual void TearDown() {
-        mBC2.clear();
-        BlobCacheTest::TearDown();
-    }
-
-    void roundTrip() {
-        size_t size = mBC->getFlattenedSize();
-        uint8_t* flat = new uint8_t[size];
-        ASSERT_EQ(OK, mBC->flatten(flat, size));
-        ASSERT_EQ(OK, mBC2->unflatten(flat, size));
-        delete[] flat;
-    }
-
-    sp<BlobCache> mBC2;
-};
-
-TEST_F(BlobCacheFlattenTest, FlattenOneValue) {
-    unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee };
-    mBC->set("abcd", 4, "efgh", 4);
-    roundTrip();
-    ASSERT_EQ(size_t(4), mBC2->get("abcd", 4, buf, 4));
-    ASSERT_EQ('e', buf[0]);
-    ASSERT_EQ('f', buf[1]);
-    ASSERT_EQ('g', buf[2]);
-    ASSERT_EQ('h', buf[3]);
-}
-
-TEST_F(BlobCacheFlattenTest, FlattenFullCache) {
-    // Fill up the entire cache with 1 char key/value pairs.
-    const int maxEntries = MAX_TOTAL_SIZE / 2;
-    for (int i = 0; i < maxEntries; i++) {
-        uint8_t k = i;
-        mBC->set(&k, 1, &k, 1);
-    }
-
-    roundTrip();
-
-    // Verify the deserialized cache
-    for (int i = 0; i < maxEntries; i++) {
-        uint8_t k = i;
-        uint8_t v = 0xee;
-        ASSERT_EQ(size_t(1), mBC2->get(&k, 1, &v, 1));
-        ASSERT_EQ(k, v);
-    }
-}
-
-TEST_F(BlobCacheFlattenTest, FlattenDoesntChangeCache) {
-    // Fill up the entire cache with 1 char key/value pairs.
-    const int maxEntries = MAX_TOTAL_SIZE / 2;
-    for (int i = 0; i < maxEntries; i++) {
-        uint8_t k = i;
-        mBC->set(&k, 1, &k, 1);
-    }
-
-    size_t size = mBC->getFlattenedSize();
-    uint8_t* flat = new uint8_t[size];
-    ASSERT_EQ(OK, mBC->flatten(flat, size));
-    delete[] flat;
-
-    // Verify the cache that we just serialized
-    for (int i = 0; i < maxEntries; i++) {
-        uint8_t k = i;
-        uint8_t v = 0xee;
-        ASSERT_EQ(size_t(1), mBC->get(&k, 1, &v, 1));
-        ASSERT_EQ(k, v);
-    }
-}
-
-TEST_F(BlobCacheFlattenTest, FlattenCatchesBufferTooSmall) {
-    // Fill up the entire cache with 1 char key/value pairs.
-    const int maxEntries = MAX_TOTAL_SIZE / 2;
-    for (int i = 0; i < maxEntries; i++) {
-        uint8_t k = i;
-        mBC->set(&k, 1, &k, 1);
-    }
-
-    size_t size = mBC->getFlattenedSize() - 1;
-    uint8_t* flat = new uint8_t[size];
-    // ASSERT_EQ(BAD_VALUE, mBC->flatten(flat, size));
-    // TODO: The above fails. I expect this is so because getFlattenedSize()
-    // overstimates the size by using PROPERTY_VALUE_MAX.
-    delete[] flat;
-}
-
-TEST_F(BlobCacheFlattenTest, UnflattenCatchesBadMagic) {
-    unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee };
-    mBC->set("abcd", 4, "efgh", 4);
-
-    size_t size = mBC->getFlattenedSize();
-    uint8_t* flat = new uint8_t[size];
-    ASSERT_EQ(OK, mBC->flatten(flat, size));
-    flat[1] = ~flat[1];
-
-    // Bad magic should cause an error.
-    ASSERT_EQ(BAD_VALUE, mBC2->unflatten(flat, size));
-    delete[] flat;
-
-    // The error should cause the unflatten to result in an empty cache
-    ASSERT_EQ(size_t(0), mBC2->get("abcd", 4, buf, 4));
-}
-
-TEST_F(BlobCacheFlattenTest, UnflattenCatchesBadBlobCacheVersion) {
-    unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee };
-    mBC->set("abcd", 4, "efgh", 4);
-
-    size_t size = mBC->getFlattenedSize();
-    uint8_t* flat = new uint8_t[size];
-    ASSERT_EQ(OK, mBC->flatten(flat, size));
-    flat[5] = ~flat[5];
-
-    // Version mismatches shouldn't cause errors, but should not use the
-    // serialized entries
-    ASSERT_EQ(OK, mBC2->unflatten(flat, size));
-    delete[] flat;
-
-    // The version mismatch should cause the unflatten to result in an empty
-    // cache
-    ASSERT_EQ(size_t(0), mBC2->get("abcd", 4, buf, 4));
-}
-
-TEST_F(BlobCacheFlattenTest, UnflattenCatchesBadBlobCacheDeviceVersion) {
-    unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee };
-    mBC->set("abcd", 4, "efgh", 4);
-
-    size_t size = mBC->getFlattenedSize();
-    uint8_t* flat = new uint8_t[size];
-    ASSERT_EQ(OK, mBC->flatten(flat, size));
-    flat[10] = ~flat[10];
-
-    // Version mismatches shouldn't cause errors, but should not use the
-    // serialized entries
-    ASSERT_EQ(OK, mBC2->unflatten(flat, size));
-    delete[] flat;
-
-    // The version mismatch should cause the unflatten to result in an empty
-    // cache
-    ASSERT_EQ(size_t(0), mBC2->get("abcd", 4, buf, 4));
-}
-
-TEST_F(BlobCacheFlattenTest, UnflattenCatchesBufferTooSmall) {
-    unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee };
-    mBC->set("abcd", 4, "efgh", 4);
-
-    size_t size = mBC->getFlattenedSize();
-    uint8_t* flat = new uint8_t[size];
-    ASSERT_EQ(OK, mBC->flatten(flat, size));
-
-    // A buffer truncation shouldt cause an error
-    // ASSERT_EQ(BAD_VALUE, mBC2->unflatten(flat, size-1));
-    // TODO: The above appears to fail because getFlattenedSize() is
-    // conservative.
-    delete[] flat;
-
-    // The error should cause the unflatten to result in an empty cache
-    ASSERT_EQ(size_t(0), mBC2->get("abcd", 4, buf, 4));
-}
-
-} // namespace android