Home | History | Annotate | Download | only in test
      1 // This file is distributed under the University of Illinois Open Source
      2 // License. See LICENSE.TXT for details.
      3 
      4 #include "FuzzerFnAdapter.h"
      5 #include "gtest/gtest-spi.h"
      6 #include "gtest/gtest.h"
      7 
      8 namespace fuzzer {
      9 namespace impl {
     10 
     11 template <typename... Args>
     12 bool Unpack(std::tuple<Args...> *Tuple, std::initializer_list<uint8_t> data) {
     13   std::vector<uint8_t> V(data);
     14   return Unpack(V.data(), V.size(), Tuple);
     15 }
     16 
     17 TEST(Unpack, Bool) {
     18   std::tuple<bool> T;
     19   EXPECT_TRUE(Unpack(&T, {1}));
     20   EXPECT_TRUE(std::get<0>(T));
     21 
     22   EXPECT_TRUE(Unpack(&T, {0}));
     23   EXPECT_FALSE(std::get<0>(T));
     24 
     25   EXPECT_FALSE(Unpack(&T, {}));
     26 }
     27 
     28 TEST(Unpack, BoolBool) {
     29   std::tuple<bool, bool> T;
     30   EXPECT_TRUE(Unpack(&T, {1, 0}));
     31   EXPECT_TRUE(std::get<0>(T));
     32   EXPECT_FALSE(std::get<1>(T));
     33 
     34   EXPECT_TRUE(Unpack(&T, {0, 1}));
     35   EXPECT_FALSE(std::get<0>(T));
     36   EXPECT_TRUE(std::get<1>(T));
     37 
     38   EXPECT_FALSE(Unpack(&T, {}));
     39   EXPECT_FALSE(Unpack(&T, {10}));
     40 }
     41 
     42 TEST(Unpack, BoolInt) {
     43   std::tuple<bool, int> T;
     44   EXPECT_TRUE(Unpack(&T, {1, 16, 2, 0, 0}));
     45   EXPECT_TRUE(std::get<0>(T));
     46   EXPECT_EQ(528, std::get<1>(T));
     47 
     48   EXPECT_FALSE(Unpack(&T, {1, 2}));
     49 }
     50 
     51 TEST(Unpack, Vector) {
     52   std::tuple<std::vector<uint8_t>> T;
     53   const auto &V = std::get<0>(T);
     54 
     55   EXPECT_FALSE(Unpack(&T, {}));
     56 
     57   EXPECT_TRUE(Unpack(&T, {0}));
     58   EXPECT_EQ(0ul, V.size());
     59 
     60   EXPECT_TRUE(Unpack(&T, {0, 1, 2, 3}));
     61   EXPECT_EQ(0ul, V.size());
     62 
     63   EXPECT_TRUE(Unpack(&T, {2}));
     64   EXPECT_EQ(0ul, V.size());
     65 
     66   EXPECT_TRUE(Unpack(&T, {2, 3}));
     67   EXPECT_EQ(1ul, V.size());
     68   EXPECT_EQ(3, V[0]);
     69 
     70   EXPECT_TRUE(Unpack(&T, {2, 9, 8}));
     71   EXPECT_EQ(2ul, V.size());
     72   EXPECT_EQ(9, V[0]);
     73   EXPECT_EQ(8, V[1]);
     74 }
     75 
     76 TEST(Unpack, String) {
     77   std::tuple<std::string> T;
     78   const auto &S = std::get<0>(T);
     79 
     80   EXPECT_TRUE(Unpack(&T, {2, 3}));
     81   EXPECT_EQ(1ul, S.size());
     82   EXPECT_EQ(3, S[0]);
     83 }
     84 
     85 template <typename Fn>
     86 bool UnpackAndApply(Fn F, std::initializer_list<uint8_t> Data) {
     87   std::vector<uint8_t> V(Data);
     88   return UnpackAndApply(F, V.data(), V.size());
     89 }
     90 
     91 static void fnBool(bool b) { EXPECT_TRUE(b); }
     92 
     93 TEST(Apply, Bool) {
     94   EXPECT_FALSE(UnpackAndApply(fnBool, {}));
     95   EXPECT_TRUE(UnpackAndApply(fnBool, {1}));
     96   EXPECT_NONFATAL_FAILURE(UnpackAndApply(fnBool, {0}),
     97                           "Actual: false\nExpected: true");
     98 }
     99 
    100 static void fnInt(int i) { EXPECT_EQ(42, i); }
    101 
    102 TEST(Apply, Int) {
    103   EXPECT_FALSE(UnpackAndApply(fnInt, {}));
    104   EXPECT_TRUE(UnpackAndApply(fnInt, {42, 0, 0, 0}));
    105   EXPECT_NONFATAL_FAILURE(UnpackAndApply(fnInt, {10, 0, 0, 0}),
    106                           "Actual: 10\nExpected: 42");
    107 }
    108 
    109 } // namespace impl
    110 } // namespace fuzzer
    111