# Changeset View

Changeset View

# Standalone View

Standalone View

# lib/fuzzer/tests/FuzzedDataProviderUnittest.cpp

- This file was added.

1 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||
---|---|---|---|---|---|

2 | // See https://llvm.org/LICENSE.txt for license information. | ||||

3 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||

4 | | ||||

5 | #include "gtest/gtest.h" | ||||

6 | #include <cstdint> | ||||

7 | #include <cstdlib> | ||||

8 | | ||||

9 | #include "utils/FuzzedDataProvider.h" | ||||

10 | | ||||

11 | // The test is intentionally extensive, as behavior of |FuzzedDataProvider| must | ||||

12 | // not be broken, given than many fuzz targets depend on it. Changing the | ||||

13 | // behavior might invalidate existing corpora and make the fuzz targets using | ||||

14 | // |FuzzedDataProvider| to lose code coverage accumulated over time. | ||||

15 | | ||||

16 | /* A random 1KB buffer generated by: | ||||

17 | $ python -c "import os; print ',\n'.join([', '.join(['0x%02X' % ord(i) for i \ | ||||

18 | in list(os.urandom(8))]) for _ in xrange(128)])" | ||||

19 | */ | ||||

20 | const uint8_t Data[] = { | ||||

21 | 0x8A, 0x19, 0x0D, 0x44, 0x37, 0x0D, 0x38, 0x5E, 0x9B, 0xAA, 0xF3, 0xDA, | ||||

22 | 0xAA, 0x88, 0xF2, 0x9B, 0x6C, 0xBA, 0xBE, 0xB1, 0xF2, 0xCF, 0x13, 0xB8, | ||||

23 | 0xAC, 0x1A, 0x7F, 0x1C, 0xC9, 0x90, 0xD0, 0xD9, 0x5C, 0x42, 0xB3, 0xFD, | ||||

24 | 0xE3, 0x05, 0xA4, 0x03, 0x37, 0x49, 0x50, 0x4B, 0xBC, 0x39, 0xA2, 0x09, | ||||

25 | 0x6C, 0x2F, 0xAF, 0xD1, 0xB5, 0x47, 0xBF, 0x92, 0xBD, 0x79, 0xE5, 0xC5, | ||||

26 | 0x6E, 0x51, 0xA4, 0xED, 0xE9, 0xBD, 0x40, 0x4A, 0xFC, 0x25, 0x7A, 0x27, | ||||

27 | 0xC8, 0x92, 0xF7, 0x30, 0xDE, 0x40, 0x66, 0x66, 0xE8, 0x5F, 0x65, 0x39, | ||||

28 | 0x7E, 0x9E, 0x80, 0x2B, 0x01, 0x71, 0x2A, 0xFF, 0xD3, 0x0A, 0xAC, 0x6E, | ||||

29 | 0x49, 0x32, 0x79, 0x10, 0x6A, 0x6F, 0x97, 0x96, 0x70, 0x7E, 0x50, 0x65, | ||||

30 | 0xC9, 0x1D, 0xBD, 0x4E, 0x17, 0x04, 0x1E, 0xBA, 0x26, 0xAC, 0x1F, 0xE3, | ||||

31 | 0x37, 0x1C, 0x15, 0x43, 0x60, 0x41, 0x2A, 0x7C, 0xCA, 0x70, 0xCE, 0xAB, | ||||

32 | 0x20, 0x24, 0xF8, 0xD9, 0x1F, 0x14, 0x7C, 0x5C, 0xDD, 0x6F, 0xB3, 0xD7, | ||||

33 | 0x8B, 0x63, 0x10, 0xB7, 0xDA, 0x99, 0xAF, 0x99, 0x01, 0x21, 0xE6, 0xE1, | ||||

34 | 0x86, 0x27, 0xBE, 0x8D, 0xDF, 0x1E, 0xEA, 0x80, 0x0B, 0x8A, 0x60, 0xC3, | ||||

35 | 0x3A, 0x85, 0x33, 0x53, 0x59, 0xE1, 0xB5, 0xF1, 0x62, 0xA6, 0x7B, 0x24, | ||||

36 | 0x94, 0xE3, 0x8C, 0x10, 0x93, 0xF8, 0x6E, 0xC2, 0x00, 0x91, 0x90, 0x0B, | ||||

37 | 0x5D, 0x52, 0x4F, 0x21, 0xE3, 0x40, 0x3A, 0x6E, 0xB6, 0x32, 0x15, 0xDB, | ||||

38 | 0x5D, 0x01, 0x86, 0x63, 0x83, 0x24, 0xC5, 0xDE, 0xAB, 0x31, 0x84, 0xAA, | ||||

39 | 0xE5, 0x64, 0x02, 0x8D, 0x23, 0x82, 0x86, 0x14, 0x16, 0x18, 0x9F, 0x3D, | ||||

40 | 0x31, 0xBE, 0x3B, 0xF0, 0x6C, 0x26, 0x42, 0x9A, 0x67, 0xFE, 0x28, 0xEC, | ||||

41 | 0x28, 0xDB, 0x01, 0xB4, 0x52, 0x41, 0x81, 0x7C, 0x54, 0xD3, 0xC8, 0x00, | ||||

42 | 0x01, 0x66, 0xB0, 0x2C, 0x3F, 0xBC, 0xAF, 0xAC, 0x87, 0xCD, 0x83, 0xCF, | ||||

43 | 0x23, 0xFC, 0xC8, 0x97, 0x8C, 0x71, 0x32, 0x8B, 0xBF, 0x70, 0xC0, 0x48, | ||||

44 | 0x31, 0x92, 0x18, 0xFE, 0xE5, 0x33, 0x48, 0x82, 0x98, 0x1E, 0x30, 0xCC, | ||||

45 | 0xAD, 0x5D, 0x97, 0xC4, 0xB4, 0x39, 0x7C, 0xCD, 0x39, 0x44, 0xF1, 0xA9, | ||||

46 | 0xD0, 0xF4, 0x27, 0xB7, 0x78, 0x85, 0x9E, 0x72, 0xFC, 0xCC, 0xEE, 0x98, | ||||

47 | 0x25, 0x3B, 0x69, 0x6B, 0x0C, 0x11, 0xEA, 0x22, 0xB6, 0xD0, 0xCD, 0xBF, | ||||

48 | 0x6D, 0xBE, 0x12, 0xDE, 0xFE, 0x78, 0x2E, 0x54, 0xCB, 0xBA, 0xD7, 0x2E, | ||||

49 | 0x54, 0x25, 0x14, 0x84, 0xFE, 0x1A, 0x10, 0xCE, 0xCC, 0x20, 0xE6, 0xE2, | ||||

50 | 0x7F, 0xE0, 0x5F, 0xDB, 0xA7, 0xF3, 0xE2, 0x4C, 0x52, 0x82, 0xFC, 0x0B, | ||||

51 | 0xA0, 0xBD, 0x34, 0x21, 0xF7, 0xEB, 0x1C, 0x5B, 0x67, 0xD0, 0xAF, 0x22, | ||||

52 | 0x15, 0xA1, 0xFF, 0xC2, 0x68, 0x25, 0x5B, 0xB2, 0x13, 0x3F, 0xFF, 0x98, | ||||

53 | 0x53, 0x25, 0xC5, 0x58, 0x39, 0xD0, 0x43, 0x86, 0x6C, 0x5B, 0x57, 0x8E, | ||||

54 | 0x83, 0xBA, 0xB9, 0x09, 0x09, 0x14, 0x0C, 0x9E, 0x99, 0x83, 0x88, 0x53, | ||||

55 | 0x79, 0xFD, 0xF7, 0x49, 0xE9, 0x2C, 0xCE, 0xE6, 0x7B, 0xF5, 0xC2, 0x27, | ||||

56 | 0x5E, 0x56, 0xB5, 0xB4, 0x46, 0x90, 0x91, 0x7F, 0x99, 0x88, 0xA7, 0x23, | ||||

57 | 0xC1, 0x80, 0xB8, 0x2D, 0xCD, 0xF7, 0x6F, 0x9A, 0xEC, 0xBD, 0x16, 0x9F, | ||||

58 | 0x7D, 0x87, 0x1E, 0x15, 0x51, 0xC4, 0x96, 0xE2, 0xBF, 0x61, 0x66, 0xB5, | ||||

59 | 0xFD, 0x01, 0x67, 0xD6, 0xFF, 0xD2, 0x14, 0x20, 0x98, 0x8E, 0xEF, 0xF3, | ||||

60 | 0x22, 0xDB, 0x7E, 0xCE, 0x70, 0x2D, 0x4C, 0x06, 0x5A, 0xA0, 0x4F, 0xC8, | ||||

61 | 0xB0, 0x4D, 0xA6, 0x52, 0xB2, 0xD6, 0x2F, 0xD8, 0x57, 0xE5, 0xEF, 0xF9, | ||||

62 | 0xEE, 0x52, 0x0F, 0xEC, 0xC4, 0x90, 0x33, 0xAD, 0x25, 0xDA, 0xCD, 0x12, | ||||

63 | 0x44, 0x5F, 0x32, 0xF6, 0x6F, 0xEF, 0x85, 0xB8, 0xDC, 0x3C, 0x01, 0x48, | ||||

64 | 0x28, 0x5D, 0x2D, 0x9C, 0x9B, 0xC0, 0x49, 0x36, 0x1E, 0x6A, 0x0A, 0x0C, | ||||

65 | 0xB0, 0x6E, 0x81, 0x89, 0xCB, 0x0A, 0x89, 0xCF, 0x73, 0xC6, 0x63, 0x3D, | ||||

66 | 0x8E, 0x13, 0x57, 0x91, 0x4E, 0xA3, 0x93, 0x8C, 0x61, 0x67, 0xFD, 0x13, | ||||

67 | 0xE0, 0x14, 0x72, 0xB3, 0xE4, 0x23, 0x45, 0x08, 0x4E, 0x4E, 0xF5, 0xA7, | ||||

68 | 0xA8, 0xEE, 0x30, 0xFD, 0x81, 0x80, 0x1F, 0xF3, 0x4F, 0xD7, 0xE7, 0xF2, | ||||

69 | 0x16, 0xC0, 0xD6, 0x15, 0x6A, 0x0F, 0x89, 0x15, 0xA9, 0xCF, 0x35, 0x50, | ||||

70 | 0x6B, 0x49, 0x3E, 0x12, 0x4A, 0x72, 0xE4, 0x59, 0x9D, 0xD7, 0xDB, 0xD2, | ||||

71 | 0xD1, 0x61, 0x7D, 0x52, 0x4A, 0x36, 0xF6, 0xBA, 0x0E, 0xFA, 0x88, 0x6F, | ||||

72 | 0x3C, 0x82, 0x16, 0xF0, 0xD5, 0xED, 0x4D, 0x78, 0xEF, 0x38, 0x17, 0x90, | ||||

73 | 0xEA, 0x28, 0x32, 0xA9, 0x79, 0x40, 0xFF, 0xAA, 0xE6, 0xF5, 0xC7, 0x96, | ||||

74 | 0x56, 0x65, 0x61, 0x83, 0x3D, 0xBD, 0xD7, 0xED, 0xD6, 0xB6, 0xC0, 0xED, | ||||

75 | 0x34, 0xAA, 0x60, 0xA9, 0xE8, 0x82, 0x78, 0xEA, 0x69, 0xF6, 0x47, 0xAF, | ||||

76 | 0x39, 0xAB, 0x11, 0xDB, 0xE9, 0xFB, 0x68, 0x0C, 0xFE, 0xDF, 0x97, 0x9F, | ||||

77 | 0x3A, 0xF4, 0xF3, 0x32, 0x27, 0x30, 0x57, 0x0E, 0xF7, 0xB2, 0xEE, 0xFB, | ||||

78 | 0x1E, 0x98, 0xA8, 0xA3, 0x25, 0x45, 0xE4, 0x6D, 0x2D, 0xAE, 0xFE, 0xDA, | ||||

79 | 0xB3, 0x32, 0x9B, 0x5D, 0xF5, 0x32, 0x74, 0xEA, 0xE5, 0x02, 0x30, 0x53, | ||||

80 | 0x95, 0x13, 0x7A, 0x23, 0x1F, 0x10, 0x30, 0xEA, 0x78, 0xE4, 0x36, 0x1D, | ||||

81 | 0x92, 0x96, 0xB9, 0x91, 0x2D, 0xFA, 0x43, 0xAB, 0xE6, 0xEF, 0x14, 0x14, | ||||

82 | 0xC9, 0xBC, 0x46, 0xC6, 0x05, 0x7C, 0xC6, 0x11, 0x23, 0xCF, 0x3D, 0xC8, | ||||

83 | 0xBE, 0xEC, 0xA3, 0x58, 0x31, 0x55, 0x65, 0x14, 0xA7, 0x94, 0x93, 0xDD, | ||||

84 | 0x2D, 0x76, 0xC9, 0x66, 0x06, 0xBD, 0xF5, 0xE7, 0x30, 0x65, 0x42, 0x52, | ||||

85 | 0xA2, 0x50, 0x9B, 0xE6, 0x40, 0xA2, 0x4B, 0xEC, 0xA6, 0xB7, 0x39, 0xAA, | ||||

86 | 0xD7, 0x61, 0x2C, 0xBF, 0x37, 0x5A, 0xDA, 0xB3, 0x5D, 0x2F, 0x5D, 0x11, | ||||

87 | 0x82, 0x97, 0x32, 0x8A, 0xC1, 0xA1, 0x13, 0x20, 0x17, 0xBD, 0xA2, 0x91, | ||||

88 | 0x94, 0x2A, 0x4E, 0xBE, 0x3E, 0x77, 0x63, 0x67, 0x5C, 0x0A, 0xE1, 0x22, | ||||

89 | 0x0A, 0x4F, 0x63, 0xE2, 0x84, 0xE9, 0x9F, 0x14, 0x86, 0xE2, 0x4B, 0x20, | ||||

90 | 0x9F, 0x50, 0xB3, 0x56, 0xED, 0xDE, 0x39, 0xD8, 0x75, 0x64, 0x45, 0x54, | ||||

91 | 0xE5, 0x34, 0x57, 0x8C, 0x3B, 0xF2, 0x0E, 0x94, 0x1B, 0x10, 0xA2, 0xA2, | ||||

92 | 0x38, 0x76, 0x21, 0x8E, 0x2A, 0x57, 0x64, 0x58, 0x0A, 0x27, 0x6D, 0x4C, | ||||

93 | 0xD0, 0xB5, 0xC1, 0xFC, 0x75, 0xD0, 0x01, 0x86, 0x66, 0xA8, 0xF1, 0x98, | ||||

94 | 0x58, 0xFB, 0xFC, 0x64, 0xD2, 0x31, 0x77, 0xAD, 0x0E, 0x46, 0x87, 0xCC, | ||||

95 | 0x9B, 0x86, 0x90, 0xFF, 0xB6, 0x64, 0x35, 0xA5, 0x5D, 0x9E, 0x44, 0x51, | ||||

96 | 0x87, 0x9E, 0x1E, 0xEE, 0xF3, 0x3B, 0x5C, 0xDD, 0x94, 0x03, 0xAA, 0x18, | ||||

97 | 0x2C, 0xB7, 0xC4, 0x37, 0xD5, 0x53, 0x28, 0x60, 0xEF, 0x77, 0xEF, 0x3B, | ||||

98 | 0x9E, 0xD2, 0xCE, 0xE9, 0x53, 0x2D, 0xF5, 0x19, 0x7E, 0xBB, 0xB5, 0x46, | ||||

99 | 0xE2, 0xF7, 0xD6, 0x4D, 0x6D, 0x5B, 0x81, 0x56, 0x6B, 0x12, 0x55, 0x63, | ||||

100 | 0xC3, 0xAB, 0x08, 0xBB, 0x2E, 0xD5, 0x11, 0xBC, 0x18, 0xCB, 0x8B, 0x12, | ||||

101 | 0x2E, 0x3E, 0x75, 0x32, 0x98, 0x8A, 0xDE, 0x3C, 0xEA, 0x33, 0x46, 0xE7, | ||||

102 | 0x7A, 0xA5, 0x12, 0x09, 0x26, 0x7E, 0x7E, 0x03, 0x4F, 0xFD, 0xC0, 0xFD, | ||||

103 | 0xEA, 0x4F, 0x83, 0x85, 0x39, 0x62, 0xFB, 0xA2, 0x33, 0xD9, 0x2D, 0xB1, | ||||

104 | 0x30, 0x6F, 0x88, 0xAB, 0x61, 0xCB, 0x32, 0xEB, 0x30, 0xF9, 0x51, 0xF6, | ||||

105 | 0x1F, 0x3A, 0x11, 0x4D, 0xFD, 0x54, 0xD6, 0x3D, 0x43, 0x73, 0x39, 0x16, | ||||

106 | 0xCF, 0x3D, 0x29, 0x4A}; | ||||

107 | | ||||

108 | TEST(FuzzedDataProvider, ConsumeBytes) { | ||||

109 | FuzzedDataProvider DataProv(Data, sizeof(Data)); | ||||

110 | EXPECT_EQ(std::vector<char>(1, 0x8A), DataProv.ConsumeBytes<char>(1)); | ||||

111 | EXPECT_EQ(std::vector<uint8_t>( | ||||

112 | {0x19, 0x0D, 0x44, 0x37, 0x0D, 0x38, 0x5E, 0x9B, 0xAA, 0xF3}), | ||||

113 | DataProv.ConsumeBytes<uint8_t>(10)); | ||||

114 | | ||||

115 | std::vector<unsigned char> UChars = DataProv.ConsumeBytes(24); | ||||

116 | EXPECT_EQ(std::vector<unsigned char>({0xDA, 0xAA, 0x88, 0xF2, 0x9B, 0x6C, | ||||

117 | 0xBA, 0xBE, 0xB1, 0xF2, 0xCF, 0x13, | ||||

118 | 0xB8, 0xAC, 0x1A, 0x7F, 0x1C, 0xC9, | ||||

119 | 0x90, 0xD0, 0xD9, 0x5C, 0x42, 0xB3}), | ||||

120 | UChars); | ||||

121 | | ||||

122 | EXPECT_EQ(std::vector<signed char>(Data + 1 + 10 + 24, Data + sizeof(Data)), | ||||

123 | DataProv.ConsumeBytes<signed char>(31337)); | ||||

124 | } | ||||

125 | | ||||

126 | TEST(FuzzedDataProvider, ConsumeBytesAsString) { | ||||

127 | FuzzedDataProvider DataProv(Data, sizeof(Data)); | ||||

128 | EXPECT_EQ(std::string("\x8A\x19\x0D\x44\x37\x0D\x38\x5E\x9B\xAA\xF3\xDA"), | ||||

129 | DataProv.ConsumeBytesAsString(12)); | ||||

130 | EXPECT_EQ(std::string(Data + 12, Data + sizeof(Data)), | ||||

131 | DataProv.ConsumeBytesAsString(31337)); | ||||

132 | } | ||||

133 | | ||||

134 | TEST(FuzzedDataProvider, ConsumeIntegralInRange) { | ||||

135 | FuzzedDataProvider DataProv(Data, sizeof(Data)); | ||||

136 | EXPECT_EQ(int32_t(21), DataProv.ConsumeIntegralInRange<int32_t>(10, 30)); | ||||

137 | EXPECT_EQ(int32_t(1337), | ||||

138 | DataProv.ConsumeIntegralInRange<int32_t>(1337, 1337)); | ||||

139 | EXPECT_EQ(int8_t(-59), DataProv.ConsumeIntegralInRange<int8_t>(-100, 100)); | ||||

140 | EXPECT_EQ(uint16_t(15823), | ||||

141 | DataProv.ConsumeIntegralInRange<uint16_t>(0, 65535)); | ||||

142 | EXPECT_EQ((signed char)(-101), | ||||

143 | DataProv.ConsumeIntegralInRange<signed char>(-123, 123)); | ||||

144 | EXPECT_EQ(int64_t(-53253077544), DataProv.ConsumeIntegralInRange<int64_t>( | ||||

145 | -99999999999, 99999999999)); | ||||

146 | | ||||

147 | // Exhaust the buffer. | ||||

148 | auto String = DataProv.ConsumeBytesAsString(31337); | ||||

149 | EXPECT_EQ(size_t(1014), String.length()); | ||||

150 | EXPECT_EQ(uint64_t(123456789), | ||||

151 | DataProv.ConsumeIntegralInRange<uint64_t>(123456789, 987654321)); | ||||

152 | } | ||||

153 | | ||||

154 | TEST(FuzzedDataProvider, ConsumeRandomLengthString) { | ||||

155 | FuzzedDataProvider DataProv(Data, sizeof(Data)); | ||||

156 | EXPECT_EQ( | ||||

157 | std::string( | ||||

158 | "\x8A\x19\x0D\x44\x37\x0D\x38\x5E\x9B\xAA\xF3\xDA\xAA\x88\xF2\x9B\x6C" | ||||

159 | "\xBA\xBE\xB1\xF2\xCF\x13\xB8\xAC\x1A\x7F\x1C\xC9\x90\xD0\xD9"), | ||||

160 | DataProv.ConsumeRandomLengthString(1337)); | ||||

161 | EXPECT_EQ(std::string( | ||||

162 | "\xB3\xFD\xE3\x05\xA4\x03\x37\x49\x50\x4B\xBC\x39\xA2\x09\x6C" | ||||

163 | "\x2F\xAF\xD1\xB5\x47\xBF\x92\xBD\x79\xE5\xC5\x6E\x51\xA4\xED" | ||||

164 | "\xE9\xBD\x40\x4A\xFC\x25\x7A\x27\xC8\x92\xF7\x30\xDE\x40\x66" | ||||

165 | "\x66\xE8\x5F\x65\x39\x7E\x9E\x80\x2B\x01\x71\x2A\xFF\xD3\x0A" | ||||

166 | "\xAC\x6E\x49\x32\x79\x10\x6A\x6F\x97\x96\x70\x7E\x50\x65\xC9" | ||||

167 | "\x1D\xBD\x4E\x17\x04\x1E\xBA\x26\xAC\x1F\xE3\x37\x1C\x15\x43" | ||||

168 | "\x60\x41\x2A\x7C\xCA\x70\xCE\xAB\x20\x24\xF8\xD9\x1F\x14\x7C"), | ||||

169 | DataProv.ConsumeRandomLengthString(31337)); | ||||

170 | EXPECT_EQ(std::string(Data + 141, Data + 141 + 5), | ||||

171 | DataProv.ConsumeRandomLengthString(5)); | ||||

172 | EXPECT_EQ(std::string(Data + 141 + 5, Data + 141 + 5 + 2), | ||||

173 | DataProv.ConsumeRandomLengthString(2)); | ||||

174 | | ||||

175 | // Exhaust the buffer. | ||||

176 | auto String = DataProv.ConsumeBytesAsString(31337); | ||||

177 | EXPECT_EQ(size_t(876), String.length()); | ||||

178 | EXPECT_EQ(std::string(), DataProv.ConsumeRandomLengthString(1)); | ||||

179 | } | ||||

180 | | ||||

181 | TEST(FuzzedDataProvider, ConsumeRemainingBytes) { | ||||

182 | { | ||||

183 | FuzzedDataProvider DataProv(Data, sizeof(Data)); | ||||

184 | EXPECT_EQ(std::vector<uint8_t>(Data, Data + sizeof(Data)), | ||||

185 | DataProv.ConsumeRemainingBytes()); | ||||

186 | EXPECT_EQ(std::vector<uint8_t>(), DataProv.ConsumeRemainingBytes()); | ||||

187 | } | ||||

188 | | ||||

189 | { | ||||

190 | FuzzedDataProvider DataProv(Data, sizeof(Data)); | ||||

191 | EXPECT_EQ(std::vector<uint8_t>(Data, Data + 123), | ||||

192 | DataProv.ConsumeBytes(123)); | ||||

193 | EXPECT_EQ(std::vector<char>(Data + 123, Data + sizeof(Data)), | ||||

194 | DataProv.ConsumeRemainingBytes<char>()); | ||||

195 | } | ||||

196 | } | ||||

197 | | ||||

198 | TEST(FuzzedDataProvider, ConsumeRemainingBytesAsString) { | ||||

199 | { | ||||

200 | FuzzedDataProvider DataProv(Data, sizeof(Data)); | ||||

201 | EXPECT_EQ(std::string(Data, Data + sizeof(Data)), | ||||

202 | DataProv.ConsumeRemainingBytesAsString()); | ||||

203 | EXPECT_EQ(std::string(""), DataProv.ConsumeRemainingBytesAsString()); | ||||

204 | } | ||||

205 | | ||||

206 | { | ||||

207 | FuzzedDataProvider DataProv(Data, sizeof(Data)); | ||||

208 | EXPECT_EQ(std::vector<uint8_t>(Data, Data + 123), | ||||

209 | DataProv.ConsumeBytes(123)); | ||||

210 | EXPECT_EQ(std::string(Data + 123, Data + sizeof(Data)), | ||||

211 | DataProv.ConsumeRemainingBytesAsString()); | ||||

212 | } | ||||

213 | } | ||||

214 | | ||||

215 | TEST(FuzzedDataProvider, ConsumeIntegral) { | ||||

216 | FuzzedDataProvider DataProv(Data, sizeof(Data)); | ||||

217 | EXPECT_EQ(int32_t(-903266865), DataProv.ConsumeIntegral<int32_t>()); | ||||

218 | EXPECT_EQ(uint32_t(372863811), DataProv.ConsumeIntegral<uint32_t>()); | ||||

219 | EXPECT_EQ(uint8_t(61), DataProv.ConsumeIntegral<uint8_t>()); | ||||

220 | EXPECT_EQ(int16_t(22100), DataProv.ConsumeIntegral<int16_t>()); | ||||

221 | EXPECT_EQ(uint64_t(18252263806144500217u), | ||||

222 | DataProv.ConsumeIntegral<uint64_t>()); | ||||

223 | | ||||

224 | // Exhaust the buffer. | ||||

225 | auto String = DataProv.ConsumeBytesAsString(31337); | ||||

226 | EXPECT_EQ(size_t(1005), String.length()); | ||||

227 | EXPECT_EQ(std::numeric_limits<int64_t>::min(), | ||||

228 | DataProv.ConsumeIntegral<int64_t>()); | ||||

229 | } | ||||

230 | | ||||

231 | TEST(FuzzedDataProvider, ConsumeBool) { | ||||

232 | FuzzedDataProvider DataProv(Data, sizeof(Data)); | ||||

233 | EXPECT_EQ(false, DataProv.ConsumeBool()); | ||||

234 | EXPECT_EQ(true, DataProv.ConsumeBool()); | ||||

235 | EXPECT_EQ(true, DataProv.ConsumeBool()); | ||||

236 | EXPECT_EQ(true, DataProv.ConsumeBool()); | ||||

237 | EXPECT_EQ(false, DataProv.ConsumeBool()); | ||||

238 | EXPECT_EQ(true, DataProv.ConsumeBool()); | ||||

239 | EXPECT_EQ(true, DataProv.ConsumeBool()); | ||||

240 | EXPECT_EQ(true, DataProv.ConsumeBool()); | ||||

241 | EXPECT_EQ(true, DataProv.ConsumeBool()); | ||||

242 | EXPECT_EQ(false, DataProv.ConsumeBool()); | ||||

243 | | ||||

244 | // Exhaust the buffer. | ||||

245 | auto String = DataProv.ConsumeBytesAsString(31337); | ||||

246 | EXPECT_EQ(size_t(1014), String.length()); | ||||

247 | EXPECT_EQ(false, DataProv.ConsumeBool()); | ||||

248 | } | ||||

249 | | ||||

250 | TEST(FuzzedDataProvider, PickValueInArray) { | ||||

251 | FuzzedDataProvider DataProv(Data, sizeof(Data)); | ||||

252 | const int Array[] = {1, 2, 3, 4, 5}; | ||||

253 | EXPECT_EQ(5, DataProv.PickValueInArray(Array)); | ||||

254 | EXPECT_EQ(2, DataProv.PickValueInArray(Array)); | ||||

255 | EXPECT_EQ(2, DataProv.PickValueInArray(Array)); | ||||

256 | EXPECT_EQ(3, DataProv.PickValueInArray(Array)); | ||||

257 | EXPECT_EQ(3, DataProv.PickValueInArray(Array)); | ||||

258 | EXPECT_EQ(3, DataProv.PickValueInArray(Array)); | ||||

259 | EXPECT_EQ(1, DataProv.PickValueInArray(Array)); | ||||

260 | EXPECT_EQ(3, DataProv.PickValueInArray(Array)); | ||||

261 | EXPECT_EQ(2, DataProv.PickValueInArray(Array)); | ||||

262 | | ||||

263 | EXPECT_EQ(uint8_t(0x9D), DataProv.PickValueInArray(Data)); | ||||

264 | EXPECT_EQ(uint8_t(0xBA), DataProv.PickValueInArray(Data)); | ||||

265 | EXPECT_EQ(uint8_t(0x69), DataProv.PickValueInArray(Data)); | ||||

266 | EXPECT_EQ(uint8_t(0xD6), DataProv.PickValueInArray(Data)); | ||||

267 | | ||||

268 | // Exhaust the buffer. | ||||

269 | auto String = DataProv.ConsumeBytesAsString(31337); | ||||

270 | EXPECT_EQ(size_t(1007), String.length()); | ||||

271 | EXPECT_EQ(uint8_t(0x8A), DataProv.PickValueInArray(Data)); | ||||

272 | } | ||||

273 | | ||||

274 | TEST(FuzzedDataProvider, ConsumeEnum) { | ||||

275 | FuzzedDataProvider DataProv(Data, sizeof(Data)); | ||||

276 | enum class Enum { | ||||

277 | Zero, | ||||

278 | One, | ||||

279 | Two, | ||||

280 | Three, | ||||

281 | Four, | ||||

282 | Five, | ||||

283 | Six, | ||||

284 | Seven, | ||||

285 | kMaxValue = Seven | ||||

286 | }; | ||||

287 | EXPECT_EQ(Enum::Two, DataProv.ConsumeEnum<Enum>()); | ||||

288 | EXPECT_EQ(Enum::One, DataProv.ConsumeEnum<Enum>()); | ||||

289 | EXPECT_EQ(Enum::Five, DataProv.ConsumeEnum<Enum>()); | ||||

290 | EXPECT_EQ(Enum::Seven, DataProv.ConsumeEnum<Enum>()); | ||||

291 | EXPECT_EQ(Enum::Six, DataProv.ConsumeEnum<Enum>()); | ||||

292 | EXPECT_EQ(Enum::One, DataProv.ConsumeEnum<Enum>()); | ||||

293 | EXPECT_EQ(Enum::Three, DataProv.ConsumeEnum<Enum>()); | ||||

294 | EXPECT_EQ(Enum::Three, DataProv.ConsumeEnum<Enum>()); | ||||

295 | EXPECT_EQ(Enum::Five, DataProv.ConsumeEnum<Enum>()); | ||||

296 | EXPECT_EQ(Enum::Six, DataProv.ConsumeEnum<Enum>()); | ||||

297 | | ||||

298 | // Exhaust the buffer. | ||||

299 | auto String = DataProv.ConsumeBytesAsString(31337); | ||||

300 | EXPECT_EQ(size_t(1014), String.length()); | ||||

301 | EXPECT_EQ(Enum::Zero, DataProv.ConsumeEnum<Enum>()); | ||||

302 | } | ||||

303 | | ||||

304 | TEST(FuzzedDataProvider, remaining_bytes) { | ||||

305 | FuzzedDataProvider DataProv(Data, sizeof(Data)); | ||||

306 | EXPECT_EQ(size_t(1024), DataProv.remaining_bytes()); | ||||

307 | EXPECT_EQ(false, DataProv.ConsumeBool()); | ||||

308 | EXPECT_EQ(size_t(1024 - 1), DataProv.remaining_bytes()); | ||||

309 | EXPECT_EQ(std::vector<uint8_t>(Data, Data + 8), DataProv.ConsumeBytes(8)); | ||||

310 | EXPECT_EQ(size_t(1024 - 1 - 8), DataProv.remaining_bytes()); | ||||

311 | | ||||

312 | // Exhaust the buffer. | ||||

313 | EXPECT_EQ(std::vector<uint8_t>(Data + 8, Data + sizeof(Data) - 1), | ||||

314 | DataProv.ConsumeRemainingBytes()); | ||||

315 | EXPECT_EQ(size_t(0), DataProv.remaining_bytes()); | ||||

316 | } | ||||

317 | | ||||

318 | int main(int argc, char **argv) { | ||||

319 | testing::InitGoogleTest(&argc, argv); | ||||

320 | return RUN_ALL_TESTS(); | ||||

321 | } |