1 // Copyright (c) 2015-2016 The Khronos Group Inc.
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 "source/spirv_constant.h"
16 #include "test/unit_spirv.h"
17
18 namespace spvtools {
19 namespace {
20
21 class BinaryHeaderGet : public ::testing::Test {
22 public:
BinaryHeaderGet()23 BinaryHeaderGet() { memset(code, 0, sizeof(code)); }
24
SetUp()25 virtual void SetUp() {
26 code[0] = SpvMagicNumber;
27 code[1] = SpvVersion;
28 code[2] = SPV_GENERATOR_CODEPLAY;
29 code[3] = 1; // NOTE: Bound
30 code[4] = 0; // NOTE: Schema; reserved
31 code[5] = 0; // NOTE: Instructions
32
33 binary.code = code;
34 binary.wordCount = 6;
35 }
get_const_binary()36 spv_const_binary_t get_const_binary() {
37 return spv_const_binary_t{binary.code, binary.wordCount};
38 }
TearDown()39 virtual void TearDown() {}
40
41 uint32_t code[6];
42 spv_binary_t binary;
43 };
44
TEST_F(BinaryHeaderGet,Default)45 TEST_F(BinaryHeaderGet, Default) {
46 spv_endianness_t endian;
47 spv_const_binary_t const_bin = get_const_binary();
48 ASSERT_EQ(SPV_SUCCESS, spvBinaryEndianness(&const_bin, &endian));
49
50 spv_header_t header;
51 ASSERT_EQ(SPV_SUCCESS, spvBinaryHeaderGet(&const_bin, endian, &header));
52
53 ASSERT_EQ(static_cast<uint32_t>(SpvMagicNumber), header.magic);
54 // Expect SPIRV-Headers updated to SPIR-V 1.5.
55 ASSERT_EQ(0x00010500u, header.version);
56 ASSERT_EQ(static_cast<uint32_t>(SPV_GENERATOR_CODEPLAY), header.generator);
57 ASSERT_EQ(1u, header.bound);
58 ASSERT_EQ(0u, header.schema);
59 ASSERT_EQ(&code[5], header.instructions);
60 }
61
TEST_F(BinaryHeaderGet,InvalidCode)62 TEST_F(BinaryHeaderGet, InvalidCode) {
63 spv_const_binary_t my_binary = {nullptr, 0};
64 spv_header_t header;
65 ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
66 spvBinaryHeaderGet(&my_binary, SPV_ENDIANNESS_LITTLE, &header));
67 }
68
TEST_F(BinaryHeaderGet,InvalidPointerHeader)69 TEST_F(BinaryHeaderGet, InvalidPointerHeader) {
70 spv_const_binary_t const_bin = get_const_binary();
71 ASSERT_EQ(SPV_ERROR_INVALID_POINTER,
72 spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, nullptr));
73 }
74
TEST_F(BinaryHeaderGet,TruncatedHeader)75 TEST_F(BinaryHeaderGet, TruncatedHeader) {
76 for (uint8_t i = 1; i < SPV_INDEX_INSTRUCTION; i++) {
77 binary.wordCount = i;
78 spv_const_binary_t const_bin = get_const_binary();
79 ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
80 spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, nullptr));
81 }
82 }
83
TEST_F(BinaryHeaderGet,VersionNonZeroHighByte)84 TEST_F(BinaryHeaderGet, VersionNonZeroHighByte) {
85 spv_header_t header;
86 code[1] = 0xFF010300;
87 spv_const_binary_t const_bin = get_const_binary();
88 ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
89 spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, &header));
90 }
91
TEST_F(BinaryHeaderGet,VersionNonZeroLowByte)92 TEST_F(BinaryHeaderGet, VersionNonZeroLowByte) {
93 spv_header_t header;
94 code[1] = 0x000103F0;
95 spv_const_binary_t const_bin = get_const_binary();
96 ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
97 spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, &header));
98 }
99
TEST_F(BinaryHeaderGet,VersionTooLow)100 TEST_F(BinaryHeaderGet, VersionTooLow) {
101 spv_header_t header;
102 code[1] = 0x00000300;
103 spv_const_binary_t const_bin = get_const_binary();
104 ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
105 spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, &header));
106 }
107
TEST_F(BinaryHeaderGet,VersionTooHigh)108 TEST_F(BinaryHeaderGet, VersionTooHigh) {
109 spv_header_t header;
110 code[1] = 0x000F0300;
111 spv_const_binary_t const_bin = get_const_binary();
112 ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
113 spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, &header));
114 }
115
116 } // namespace
117 } // namespace spvtools
118