1 // Copyright 2019 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "dawn_wire/Wire.h"
16 #include "gtest/gtest.h"
17
18 #include <vector>
19
20 class WireWGPUDevicePropertiesTests : public testing::Test {};
21
22 // Test that the serialization and deserialization of WGPUDeviceProperties can work correctly.
TEST_F(WireWGPUDevicePropertiesTests,SerializeWGPUDeviceProperties)23 TEST_F(WireWGPUDevicePropertiesTests, SerializeWGPUDeviceProperties) {
24 WGPUDeviceProperties sentWGPUDeviceProperties = {};
25 sentWGPUDeviceProperties.textureCompressionBC = true;
26 // Set false to test that the serialization can handle both true and false correctly.
27 sentWGPUDeviceProperties.pipelineStatisticsQuery = false;
28 sentWGPUDeviceProperties.timestampQuery = true;
29
30 size_t sentWGPUDevicePropertiesSize =
31 dawn_wire::SerializedWGPUDevicePropertiesSize(&sentWGPUDeviceProperties);
32 std::vector<char> buffer;
33 buffer.resize(sentWGPUDevicePropertiesSize);
34 dawn_wire::SerializeWGPUDeviceProperties(&sentWGPUDeviceProperties, buffer.data());
35
36 WGPUDeviceProperties receivedWGPUDeviceProperties;
37 ASSERT_TRUE(dawn_wire::DeserializeWGPUDeviceProperties(&receivedWGPUDeviceProperties,
38 buffer.data(), buffer.size()));
39 ASSERT_TRUE(receivedWGPUDeviceProperties.textureCompressionBC);
40 ASSERT_FALSE(receivedWGPUDeviceProperties.pipelineStatisticsQuery);
41 ASSERT_TRUE(receivedWGPUDeviceProperties.timestampQuery);
42 }
43
44 // Test that deserialization if the buffer is just one byte too small fails.
TEST_F(WireWGPUDevicePropertiesTests,DeserializeBufferTooSmall)45 TEST_F(WireWGPUDevicePropertiesTests, DeserializeBufferTooSmall) {
46 WGPUDeviceProperties sentWGPUDeviceProperties = {};
47
48 size_t sentWGPUDevicePropertiesSize =
49 dawn_wire::SerializedWGPUDevicePropertiesSize(&sentWGPUDeviceProperties);
50 std::vector<char> buffer;
51 buffer.resize(sentWGPUDevicePropertiesSize);
52 dawn_wire::SerializeWGPUDeviceProperties(&sentWGPUDeviceProperties, buffer.data());
53
54 WGPUDeviceProperties receivedWGPUDeviceProperties;
55 ASSERT_FALSE(dawn_wire::DeserializeWGPUDeviceProperties(&receivedWGPUDeviceProperties,
56 buffer.data(), buffer.size() - 1));
57 }
58