Beispiel #1
0
void
shafile(const string &baseDir,
        const string &file)
{
    unsigned char digest[SHA256_DIGEST_LENGTH];
    SHA256_CTX c; 
    string fullFile(prependBaseDir(baseDir, file));
    FastOS_File f;
    std::ostringstream os;
    Alloc buf = Alloc::alloc(65536, MemoryAllocator::HUGEPAGE_SIZE, 0x1000);
    f.EnableDirectIO();
    bool openres = f.OpenReadOnly(fullFile.c_str());
    if (!openres) {
        LOG(error, "Could not open %s for sha256 checksum", fullFile.c_str());
        LOG_ABORT("should not be reached");
    }
    int64_t flen = f.GetSize();
    int64_t remainder = flen;
    SHA256_Init(&c);
    while (remainder > 0) {
        int64_t thistime =
            std::min(remainder, static_cast<int64_t>(buf.size()));
        f.ReadBuf(buf.get(), thistime);
        SHA256_Update(&c, buf.get(), thistime);
        remainder -= thistime;
    }
    f.Close();
    SHA256_Final(digest, &c);
    for (unsigned int i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
        os.width(2);
        os.fill('0');
        os << std::hex << static_cast<unsigned int>(digest[i]);
    }
    LOG(info,
        "SHA256(%s)= %s",
        file.c_str(),
        os.str().c_str());
}
Beispiel #2
0
{
    void * tmpA(a.get());
    void * tmpB(b.get());
    EXPECT_EQUAL(4096ul, a.size());
    EXPECT_EQUAL(8192ul, b.size());
    std::swap(a, b);
    EXPECT_EQUAL(4096ul, b.size());
    EXPECT_EQUAL(8192ul, a.size());
    EXPECT_EQUAL(tmpA, b.get());
    EXPECT_EQUAL(tmpB, a.get());
}

TEST("test basics") {
    {
        Alloc h = Alloc::allocHeap(100);
        EXPECT_EQUAL(100u, h.size());
        EXPECT_TRUE(h.get() != nullptr);
    }
    {
        EXPECT_EXCEPTION(Alloc::allocAlignedHeap(100, 7), IllegalArgumentException, "Alloc::allocAlignedHeap(100, 7) does not support 7 alignment");
        Alloc h = Alloc::allocAlignedHeap(100, 1024);
        EXPECT_EQUAL(100u, h.size());
        EXPECT_TRUE(h.get() != nullptr);
    }
    {
        Alloc h = Alloc::allocMMap(100);
        EXPECT_EQUAL(4096u, h.size());
        EXPECT_TRUE(h.get() != nullptr);
    }
    {
        Alloc a = Alloc::allocHeap(4096), b = Alloc::allocHeap(8192);