TEST_F(TestCacheManager, LocalStoreAndFetch) { CacheManager cm; string name = "/test/name"; EXPECT_FALSE(cm.entryExists(name)); EXPECT_FALSE(cm.fileExists(name)); EXPECT_FALSE(cm.dirExists(name)); const char* data; uint64_t data_len; bool compressed; EXPECT_FALSE(cm.getFileContents(name, &data, &data_len, &compressed)); string uncompressed; EXPECT_FALSE(cm.getDecompressed(name, &uncompressed)); uint64_t uncompressed_size; EXPECT_FALSE(cm.getUncompressedFileSize(name, &uncompressed_size)); // Add a real (highly compressible) file for it to read in. string test_data; for (int i = 0; i < 1024; ++i) { test_data.append("123456789012345678901234567890"); } string data_fn; ASSERT_TRUE(makeTempFile(test_data, &data_fn)); ASSERT_TRUE(cm.addFileContents(name, data_fn)); ASSERT_EQ(unlink(data_fn.c_str()), 0); EXPECT_TRUE(cm.entryExists(name)); EXPECT_TRUE(cm.fileExists(name)); EXPECT_FALSE(cm.dirExists(name)); // The "/test" of "/test/name" should have been added as a directory. EXPECT_TRUE(cm.dirExists("/test")); EXPECT_TRUE(cm.getFileContents(name, &data, &data_len, &compressed)); EXPECT_TRUE(compressed); // It's compressed, so it'll be different. ASSERT_NE(memcmp(test_data.c_str(), data, data_len), 0); ASSERT_TRUE(cm.getUncompressedFileSize(name, &uncompressed_size)); EXPECT_EQ(test_data.length(), uncompressed_size); // So let's decompress it. ASSERT_TRUE(cm.getDecompressed(name, &uncompressed)); ASSERT_EQ(test_data.length(), uncompressed.length()); ASSERT_EQ(test_data, uncompressed); }
TEST_F(TestCacheManager, AddUncompressibleData) { string test_data = "let's hope this won't compress too terribly well."; string data_fn; ASSERT_TRUE(makeTempFile(test_data, &data_fn)); CacheManager cm; string name = "/test/name"; ASSERT_TRUE(cm.addFileContents(name, data_fn)); ASSERT_EQ(unlink(data_fn.c_str()), 0); ASSERT_TRUE(cm.dirExists("/test")); // Read it back from the cache. const char* data; uint64_t data_len; bool compressed; EXPECT_TRUE(cm.getFileContents(name, &data, &data_len, &compressed)); ASSERT_FALSE(compressed); EXPECT_EQ(data_len, test_data.length()); ASSERT_EQ(memcmp(test_data.c_str(), data, data_len), 0); uint64_t file_size; ASSERT_TRUE(cm.getUncompressedFileSize(name, &file_size)); EXPECT_EQ(file_size, test_data.length()); }
TEST_F(TestCacheManager, Empty) { CacheManager cm; string name = "/test/name"; EXPECT_FALSE(cm.entryExists(name)); EXPECT_FALSE(cm.fileExists(name)); EXPECT_FALSE(cm.dirExists(name)); ASSERT_TRUE(cm.addEmptyEntry(name)); EXPECT_TRUE(cm.entryExists(name)); EXPECT_FALSE(cm.fileExists(name)); EXPECT_FALSE(cm.dirExists(name)); const char* data; uint64_t data_len; bool compressed; EXPECT_FALSE(cm.getFileContents(name, &data, &data_len, &compressed)); string uncompressed; EXPECT_FALSE(cm.getDecompressed(name, &uncompressed)); uint64_t file_size; EXPECT_FALSE(cm.getUncompressedFileSize(name, &file_size)); }