• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2015 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // BinaryStream_unittest.cpp: Unit tests of the binary stream classes.
8 
9 #include <gtest/gtest.h>
10 
11 #include "libANGLE/BinaryStream.h"
12 
13 namespace angle
14 {
15 
16 // Test that errors are properly generated for overflows.
TEST(BinaryInputStream,Overflow)17 TEST(BinaryInputStream, Overflow)
18 {
19     const uint8_t goodValue = 2;
20     const uint8_t badValue  = 255;
21 
22     const size_t dataSize = 1024;
23     const size_t slopSize = 1024;
24 
25     std::vector<uint8_t> data(dataSize + slopSize);
26     std::fill(data.begin(), data.begin() + dataSize, goodValue);
27     std::fill(data.begin() + dataSize, data.end(), badValue);
28 
29     std::vector<uint8_t> outputData(dataSize);
30 
31     auto checkDataIsSafe = [=](uint8_t item) { return item == goodValue; };
32 
33     {
34         // One large read
35         gl::BinaryInputStream stream(data.data(), dataSize);
36         stream.readBytes(outputData.data(), dataSize);
37         ASSERT_FALSE(stream.error());
38         ASSERT_TRUE(std::all_of(outputData.begin(), outputData.end(), checkDataIsSafe));
39         ASSERT_TRUE(stream.endOfStream());
40     }
41 
42     {
43         // Two half-sized reads
44         gl::BinaryInputStream stream(data.data(), dataSize);
45         stream.readBytes(outputData.data(), dataSize / 2);
46         ASSERT_FALSE(stream.error());
47         stream.readBytes(outputData.data() + dataSize / 2, dataSize / 2);
48         ASSERT_FALSE(stream.error());
49         ASSERT_TRUE(std::all_of(outputData.begin(), outputData.end(), checkDataIsSafe));
50         ASSERT_TRUE(stream.endOfStream());
51     }
52 
53     {
54         // One large read that is too big
55         gl::BinaryInputStream stream(data.data(), dataSize);
56         stream.readBytes(outputData.data(), dataSize + 1);
57         ASSERT_TRUE(stream.error());
58     }
59 
60     {
61         // Two reads, one that overflows the offset
62         gl::BinaryInputStream stream(data.data(), dataSize);
63         stream.readBytes(outputData.data(), dataSize - 1);
64         ASSERT_FALSE(stream.error());
65         stream.readBytes(outputData.data(), std::numeric_limits<size_t>::max() - dataSize - 2);
66     }
67 }
68 
69 // Test that readIntVector and writeIntVector match.
TEST(BinaryStream,IntVector)70 TEST(BinaryStream, IntVector)
71 {
72     std::vector<unsigned int> writeData = {1, 2, 3, 4, 5};
73     std::vector<unsigned int> readData;
74 
75     gl::BinaryOutputStream out;
76     out.writeIntVector(writeData);
77 
78     gl::BinaryInputStream in(out.data(), out.length());
79     in.readIntVector<unsigned int>(&readData);
80 
81     ASSERT_EQ(writeData.size(), readData.size());
82 
83     for (size_t i = 0; i < writeData.size(); ++i)
84     {
85         ASSERT_EQ(writeData[i], readData[i]);
86     }
87 }
88 }  // namespace angle
89