1/* 2 * Copyright (C) 2014 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31#include "config.h" 32#include "core/xml/parser/SharedBufferReader.h" 33 34#include "platform/SharedBuffer.h" 35 36#include <algorithm> 37#include <cstdlib> 38#include <gtest/gtest.h> 39#include <vector> 40 41namespace blink { 42 43TEST(SharedBufferReaderTest, readDataWithNullSharedBuffer) 44{ 45 SharedBufferReader reader(nullptr); 46 char buffer[32]; 47 48 EXPECT_EQ(0, reader.readData(buffer, sizeof(buffer))); 49} 50 51TEST(SharedBufferReaderTest, readDataWith0BytesRequest) 52{ 53 RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(); 54 SharedBufferReader reader(sharedBuffer); 55 56 EXPECT_EQ(0, reader.readData(0, 0)); 57} 58 59TEST(SharedBufferReaderTest, readDataWithSizeBiggerThanSharedBufferSize) 60{ 61 static const char testData[] = "hello"; 62 RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(testData, sizeof(testData)); 63 64 SharedBufferReader reader(sharedBuffer); 65 66 const int extraBytes = 3; 67 char outputBuffer[sizeof(testData) + extraBytes]; 68 69 const char initializationByte = 'a'; 70 memset(outputBuffer, initializationByte, sizeof(outputBuffer)); 71 EXPECT_EQ(sizeof(testData), 72 static_cast<size_t>(reader.readData(outputBuffer, sizeof(outputBuffer)))); 73 74 EXPECT_TRUE(std::equal(testData, testData + sizeof(testData), outputBuffer)); 75 // Check that the bytes past index sizeof(testData) were not touched. 76 EXPECT_EQ(extraBytes, 77 std::count(outputBuffer, outputBuffer + sizeof(outputBuffer), initializationByte)); 78} 79 80TEST(SharedBufferReaderTest, readDataInMultiples) 81{ 82 const int iterationsCount = 8; 83 const int bytesPerIteration = 64; 84 85 std::vector<char> testData(iterationsCount * bytesPerIteration); 86 std::generate(testData.begin(), testData.end(), &std::rand); 87 88 RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(&testData[0], testData.size()); 89 SharedBufferReader reader(sharedBuffer); 90 91 std::vector<char> destinationVector(testData.size()); 92 93 for (int i = 0; i < iterationsCount; ++i) { 94 const int offset = i * bytesPerIteration; 95 const int bytesRead = reader.readData(&destinationVector[0] + offset, bytesPerIteration); 96 EXPECT_EQ(bytesPerIteration, bytesRead); 97 } 98 99 EXPECT_TRUE(std::equal(testData.begin(), testData.end(), destinationVector.begin())); 100} 101 102TEST(SharedBufferReaderTest, clearSharedBufferBetweenCallsToReadData) 103{ 104 std::vector<char> testData(128); 105 std::generate(testData.begin(), testData.end(), &std::rand); 106 107 RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(&testData[0], testData.size()); 108 SharedBufferReader reader(sharedBuffer); 109 110 std::vector<char> destinationVector(testData.size()); 111 const int bytesToRead = testData.size() / 2; 112 EXPECT_EQ(bytesToRead, reader.readData(&destinationVector[0], bytesToRead)); 113 114 sharedBuffer->clear(); 115 116 EXPECT_EQ(0, reader.readData(&destinationVector[0], bytesToRead)); 117} 118 119} // namespace blink 120