1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2/* 3 * Copyright 2017 Couchbase, Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18#include <daemon/doc_pre_expiry.h> 19#include <gtest/gtest.h> 20#include <memcached/protocol_binary.h> 21#include <memcached/types.h> 22#include <xattr/blob.h> 23#include <gsl/gsl> 24 25TEST(PreExpiry, EmptyDocument) { 26 item_info info{}; 27 EXPECT_TRUE(document_pre_expiry(info).empty()); 28} 29 30TEST(PreExpiry, DocumentWithoutXattr) { 31 uint8_t blob[10] = {0}; 32 item_info info{}; 33 info.value[0].iov_base = static_cast<void*>(blob); 34 info.value[0].iov_len = sizeof(blob); 35 info.nbytes = sizeof(blob); 36 auto rv = document_pre_expiry(info); 37 EXPECT_TRUE(rv.empty()); 38} 39 40TEST(PreExpiry, DocumentWithUserXAttrOnly) { 41 cb::xattr::Blob blob; 42 blob.set("user", "1"); 43 auto body = blob.finalize(); 44 item_info info{}; 45 info.value[0].iov_base = static_cast<void*>(body.data()); 46 info.value[0].iov_len = body.size(); 47 info.nbytes = gsl::narrow<uint32_t>(body.size()); 48 info.datatype = PROTOCOL_BINARY_DATATYPE_XATTR; 49 auto rv = document_pre_expiry(info); 50 EXPECT_TRUE(rv.empty()); 51} 52 53TEST(PreExpiry, DocumentWithSystemXattrOnly) { 54 cb::xattr::Blob blob; 55 blob.set("_system", "1"); 56 auto body = blob.finalize(); 57 item_info info{}; 58 info.value[0].iov_base = static_cast<void*>(body.data()); 59 info.value[0].iov_len = body.size(); 60 info.nbytes = gsl::narrow<uint32_t>(body.size()); 61 info.datatype = PROTOCOL_BINARY_DATATYPE_XATTR; 62 auto rv = document_pre_expiry(info); 63 EXPECT_FALSE(rv.empty()); 64} 65 66TEST(PreExpiry, DocumentWithUserAndSystemXattr) { 67 cb::xattr::Blob blob; 68 blob.set("_system", "1"); 69 blob.set("user", "1"); 70 auto body = blob.finalize(); 71 item_info info{}; 72 info.value[0].iov_base = static_cast<void*>(body.data()); 73 info.value[0].iov_len = body.size(); 74 info.nbytes = gsl::narrow<uint32_t>(body.size()); 75 info.datatype = PROTOCOL_BINARY_DATATYPE_XATTR; 76 auto rv = document_pre_expiry(info); 77 EXPECT_FALSE(rv.empty()); 78 EXPECT_LT(rv.size(), body.size()); 79} 80 81TEST(PreExpiry, DocumentWithJsonBodyAndXattrs) { 82 cb::xattr::Blob blob; 83 blob.set("_system", "1"); 84 blob.set("user", "1"); 85 auto xattr = blob.finalize(); 86 87 std::string body((const char*)xattr.data(), xattr.size()); 88 body.append("{\"foo\":\"bar\"}"); 89 90 item_info info{}; 91 info.value[0].iov_base = const_cast<char*>(body.data()); 92 info.value[0].iov_len = body.size(); 93 info.nbytes = gsl::narrow<uint32_t>(body.size()); 94 info.datatype = 95 PROTOCOL_BINARY_DATATYPE_XATTR | PROTOCOL_BINARY_DATATYPE_JSON; 96 auto rv = document_pre_expiry(info); 97 EXPECT_FALSE(rv.empty()); 98 EXPECT_LT(rv.size(), body.size()); 99} 100