text: // Copyright Google Inc. Apache 2.0.
  
  #include <stdio.h>  // for remove
  #include <string.h>  // for memset
  #include <unistd.h>  // for usleep
  
  #include <gmock/gmock.h>
  #include <google/protobuf/text_format.h>
  #include <gtest/gtest.h>
  
  #include "gmock_utils.h"
  #include "mpeg2ts.pb.h"
  #include "mpeg2ts_parser.h"
  
  class TestableMpeg2TsParser : public Mpeg2TsParser {
   public:
    TestableMpeg2TsParser() : Mpeg2TsParser(true) {}
  };
  
  class Mpeg2TsParserTest : public ::testing::Test {
   protected:
    void SetUp() override {
    }
  
    void TearDown() override {
    }
  
    TestableMpeg2TsParser mpeg2ts_parser_;
  };
  
  // TODO(chema): add some tests. Really.
  
  TEST_F(Mpeg2TsParserTest, SimpleTest) {
    EXPECT_EQ(0, 0);
  }
  
  static const char kMpeg2TsPacketWithPacketPayload[] = R"EOF(
  packet: 0
  byte: 0
  parsed {
    header {
      transport_error_indicator: false
      payload_unit_start_indicator: true
      transport_priority: true
      pid: 290
      transport_scrambling_control: 2
      adaptation_field_exists: false
      payload_exists: true
      continuity_counter: 0
    }
    psi_packet {
      pointer_field: "E\"\317\313\331\001.\361n._I\253\376\227\202\310\344\262F\312\307_\342\032\202"
      other_psi_section {
        table_id: 108
        remaining: "\246\017\317\3329\022\353Q\370\262TJ\316K\024Mn}\027\207\271\202\262X\376\021\214\344\311\035E\334\204\262-n\025&\036\362;M\007wB\335B\252\035\270\245T$i,\035\005K\257\025G\275w\0012D\243\035\201\t9\001\224\306\372\013\371\036\014\002\016-*\266\240\021I9Mv\200\327\371\335.\322\016\t\210\355\311\267\231\244\326\244>\347\026\363W\251MK\3371\362|\333\263Kv\260\207\307\t\277RY\262\264\357\217\2609\272\317EC\320\353\003\211tw`f\033\205\2173\215\327\357\353c"
      }
    }
  }
  )EOF";
  
  static const char kMpeg2TsPacketSDT[] = R"EOF(
  packet: 0
  byte: 0
  parsed {
    header {
      transport_error_indicator: false
      payload_unit_start_indicator: true
      transport_priority: false
      pid: 17
      transport_scrambling_control: 0
      adaptation_field_exists: false
      payload_exists: true
      continuity_counter: 1
    }
    psi_packet {
      pointer_field: ""
      service_description_section {
        table_id: 66
        section_length: 178
        transport_stream_id: 1
        version_number: 7
        current_next_indicator: true
        section_number: 0
        last_section_number: 0
        original_network_id: 1
        service_description {
          service_id: 300
          eit_schedule_flag: false
          eit_present_following_flag: false
          running_status: 4
          free_ca_mode: false
          descriptors_loop_length: 20
          mpegts_descriptor {
            tag: 72
            length: 18
            data: "\001\006Turner\tNBA Slate"
          }
        }
        service_description {
          service_id: 251
          eit_schedule_flag: false
          eit_present_following_flag: false
          running_status: 4
          free_ca_mode: false
          descriptors_loop_length: 17
          mpegts_descriptor {
            tag: 72
            length: 15
            data: "\001\006Turner\006HLN HD"
          }
        }
        service_description {
          service_id: 249
          eit_schedule_flag: false
          eit_present_following_flag: false
          running_status: 4
          free_ca_mode: false
          descriptors_loop_length: 19
          mpegts_descriptor {
            tag: 72
            length: 17
            data: "\001\006Turner\010TRUTV HD"
          }
        }
        service_description {
          service_id: 247
          eit_schedule_flag: false
          eit_present_following_flag: false
          running_status: 4
          free_ca_mode: false
          descriptors_loop_length: 17
          mpegts_descriptor {
            tag: 72
            length: 15
            data: "\001\006Turner\006TCM HD"
          }
        }
        service_description {
          service_id: 246
          eit_schedule_flag: false
          eit_present_following_flag: false
          running_status: 4
          free_ca_mode: false
          descriptors_loop_length: 22
          mpegts_descriptor {
            tag: 72
            length: 20
            data: "\001\006Turner\013TEN S.E. HD"
          }
        }
        service_description {
          service_id: 245
          eit_schedule_flag: false
          eit_present_following_flag: false
          running_status: 4
          free_ca_mode: false
          descriptors_loop_length: 19
          mpegts_descriptor {
            tag: 72
            length: 17
            data: "\001\006Turner\010TBS E HD"
          }
        }
        service_description {
          service_id: 244
          eit_schedule_flag: false
          eit_present_following_flag: false
          running_status: 4
          free_ca_mode: false
          descriptors_loop_length: 17
          mpegts_descriptor {
            tag: 72
            length: 15
            data: "\001\006Turner\006CNN HD"
          }
        }
        crc_32: 11111111
      }
    }
    data_bytes: "\000\000"
  }
  )EOF";
  
  TEST_F(Mpeg2TsParserTest, ProtobufPackets) {
    struct protobuf_start_test {
      int line;
      const char *pb;
    } test_arr[] = {
      {__LINE__, kMpeg2TsPacketWithPacketPayload},
      {__LINE__, kMpeg2TsPacketSDT},
    };
  
    uint8_t buf[MPEG_TS_PACKET_SIZE];
  
    for (const auto &test_item : test_arr) {
      // parse pb packet (text -> pb)
      Mpeg2Ts mpeg2ts;
      EXPECT_TRUE(google::protobuf::TextFormat::ParseFromString(
          test_item.pb, &mpeg2ts))
          << "line " << test_item.line;
      // dump it to binary (pb -> bin)
      EXPECT_EQ(MPEG_TS_PACKET_SIZE,
          mpeg2ts_parser_.DumpPacket(mpeg2ts, buf, MPEG_TS_PACKET_SIZE))
          << "line " << test_item.line;
      // parse it back to pb (bin -> pb)
      Mpeg2Ts mpeg2ts_from_binary;
      mpeg2ts_parser_.ParsePacket(0, 0, buf, MPEG_TS_PACKET_SIZE,
          &mpeg2ts_from_binary);
      EXPECT_THAT(mpeg2ts, EqualsProto(mpeg2ts_from_binary))
          << "line " << test_item.line;
    }
  }
  
  const uint8_t mpts_pat_header[] = {
    0x47, 0x40, 0x00, 0x11, 0x00, 0x00, 0xb0, 0x29, 0x02, 0xa4, 0xf3, 0x00,
    0x00, 0x00, 0x00, 0xef, 0xfe, 0x00, 0x02, 0xe0, 0x29, 0x00, 0x03, 0xe0,
    0x69, 0x00, 0x97, 0xe0, 0x40, 0x00, 0x04, 0xe0, 0xa9, 0x00, 0x05, 0xe0,
    0xc9, 0x00, 0x06, 0xe0, 0xe9, 0x00, 0x07, 0xe1, 0x29, 0xb9, 0x72, 0x43,
    0xb8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  };
  
  unsigned char s1_00057d98_ts[] = {
    0x47, 0x60, 0x91, 0xd2, 0x4e, 0x49, 0xa7, 0x21, 0xe6, 0x54, 0xb3, 0xcb,
    0x7a, 0xb3, 0xc1, 0x04, 0x5c, 0xc7, 0x5a, 0x0d, 0x2e, 0xb5, 0x77, 0x93,
    0xa8, 0x63, 0x72, 0x1a, 0xe9, 0xd9, 0xc9, 0xd1, 0x23, 0x71, 0xc5, 0x2b,
    0x42, 0xfc, 0x36, 0x5d, 0xe1, 0xae, 0x85, 0xfc, 0xb9, 0x65, 0x37, 0xff,
    0x32, 0x49, 0x79, 0xaa, 0xb8, 0x43, 0xef, 0xe0, 0xe5, 0x1a, 0xc0, 0x6e,
    0xb2, 0x5d, 0x9a, 0x88, 0x57, 0x35, 0xc3, 0x79, 0x78, 0x60, 0x71, 0x72,
    0x36, 0xee, 0x0d, 0x48, 0x83, 0x20, 0xc7, 0x4e, 0x29, 0xc4, 0x48, 0xff,
    0xa0, 0x0e, 0x2d, 0xd2, 0xe6, 0x19, 0xe5, 0xcf, 0xcd, 0x56, 0xc1, 0x2b,
    0xf5, 0xdf, 0xaa, 0x80, 0xc4, 0x1c, 0x8a, 0xcd, 0x99, 0x27, 0x51, 0x3f,
    0x3c, 0xc8, 0x4c, 0x4f, 0xa5, 0xd8, 0xd5, 0xdd, 0xd2, 0xfb, 0x39, 0xcc,
    0x5b, 0x6f, 0xab, 0xe9, 0x2c, 0x9f, 0xb2, 0x39, 0x23, 0x47, 0xac, 0xfd,
    0x5a, 0x17, 0x30, 0x98, 0xb2, 0x8e, 0x01, 0x4c, 0x83, 0xc5, 0x57, 0x1f,
    0x60, 0x0a, 0xf6, 0x33, 0x08, 0xfc, 0x59, 0x2c, 0xfa, 0x5c, 0x69, 0x21,
    0x40, 0xbc, 0xb4, 0x9a, 0x7f, 0x4c, 0x76, 0xd7, 0x07, 0x81, 0x6c, 0x33,
    0xd4, 0xd7, 0x30, 0x2f, 0xe0, 0xfe, 0x26, 0xdd, 0xb9, 0x9d, 0x8f, 0xc6,
    0xff, 0xd3, 0x6a, 0xea, 0xd7, 0x52, 0x6e, 0x3e
  };
  
  unsigned char s1_000b5060_ts[] = {
    0x47, 0x60, 0x91, 0xd7, 0x2a, 0xb6, 0x97, 0x78, 0xd4, 0x1f, 0x7a, 0xf3,
    0x40, 0x0b, 0x0a, 0x7f, 0x73, 0x05, 0xd3, 0x24, 0x4e, 0xde, 0xbc, 0x2a,
    0x5f, 0x7d, 0xdd, 0xa0, 0x62, 0x9d, 0x59, 0x70, 0x14, 0xeb, 0x87, 0x4c,
    0x29, 0x00, 0x08, 0x70, 0x14, 0x67, 0x07, 0x40, 0x25, 0xaf, 0x9b, 0xff,
    0x8b, 0xeb, 0xdc, 0xb8, 0xad, 0x7f, 0xdd, 0xff, 0x52, 0x99, 0x4c, 0x8e,
    0xe5, 0xc3, 0xdc, 0x89, 0xee, 0xe0, 0x4d, 0xa9, 0x7e, 0xd0, 0xbc, 0x82,
    0x7a, 0x0e, 0x80, 0x0f, 0x63, 0x88, 0xa8, 0x7b, 0x2d, 0x60, 0x7f, 0x94,
    0xa2, 0x20, 0x8e, 0x62, 0x45, 0xae, 0x2a, 0x21, 0x82, 0xce, 0x0e, 0xb1,
    0xc7, 0xf5, 0x0d, 0x16, 0xc6, 0x3c, 0x33, 0x9b, 0xdd, 0x56, 0xb5, 0x29,
    0xd3, 0x5d, 0x76, 0x53, 0x40, 0xd5, 0xd1, 0xb5, 0xe4, 0xf0, 0x79, 0x70,
    0x54, 0xd6, 0x71, 0x22, 0xdc, 0x7a, 0x8b, 0x41, 0x80, 0xaa, 0xb2, 0x4d,
    0x59, 0xdf, 0xf1, 0x39, 0xee, 0xc7, 0x66, 0x7d, 0xe3, 0x13, 0xa5, 0x3e,
    0xcd, 0x4d, 0xf9, 0x97, 0xb7, 0x47, 0xc0, 0xde, 0xfb, 0x45, 0x85, 0xd3,
    0x7d, 0x78, 0xfc, 0x67, 0x9c, 0xb4, 0xde, 0x63, 0xa6, 0xbf, 0xef, 0x72,
    0x19, 0x41, 0xb4, 0xbc, 0xff, 0xa6, 0xfc, 0xdc, 0x16, 0xc5, 0x15, 0x8b,
    0x11, 0x04, 0x76, 0x47, 0xf0, 0x03, 0xa6, 0x29
  };
  
  unsigned char s1_2080802_ts[] = {
    0x47, 0x61, 0x11, 0x9b, 0x6b, 0x15, 0xb4, 0xcd, 0x4e, 0xc2, 0xe8, 0xdc,
    0x41, 0x4d, 0x44, 0x1f, 0xb6, 0xb2, 0x4d, 0x70, 0x9e, 0x2a, 0x16, 0x74,
    0x0d, 0x3b, 0x00, 0x65, 0x7f, 0xf7, 0xdb, 0xb5, 0xf9, 0x8b, 0x2f, 0x0c,
    0x20, 0x3e, 0x28, 0xcb, 0x0c, 0x0a, 0x29, 0x90, 0x19, 0x9d, 0x38, 0x0a,
    0x44, 0xe8, 0x01, 0xac, 0xd9, 0x50, 0x82, 0xa5, 0x2e, 0x77, 0xc3, 0x16,
    0xbd, 0xc5, 0x05, 0xf9, 0x6d, 0xf7, 0xc5, 0x9c, 0x55, 0x63, 0xb8, 0x3d,
    0xed, 0xd1, 0xd4, 0xa4, 0x9e, 0x74, 0x4e, 0xec, 0x41, 0x99, 0x1d, 0xfb,
    0x08, 0x4a, 0x8e, 0x38, 0x26, 0x5c, 0xd6, 0x86, 0x9b, 0x48, 0x1c, 0xa9,
    0xeb, 0x2f, 0x22, 0x1f, 0x1d, 0x05, 0x21, 0xf2, 0xbb, 0x5f, 0x88, 0x11,
    0x1f, 0x4b, 0x8e, 0x9b, 0x00, 0x00, 0x0c, 0xb5, 0x69, 0x58, 0xfc, 0x6a,
    0xb0, 0x82, 0x12, 0x2b, 0xf5, 0xb5, 0xfc, 0xde, 0xa8, 0x0e, 0x15, 0x93,
    0x83, 0x62, 0xe2, 0xaf, 0x57, 0x4f, 0x8f, 0x20, 0x4d, 0xfc, 0xe2, 0x63,
    0x5f, 0xc3, 0x18, 0xa7, 0x9b, 0x11, 0x7b, 0x4d, 0x03, 0xcb, 0x2b, 0x98,
    0x96, 0x72, 0xb8, 0x82, 0xac, 0xfd, 0x74, 0x1c, 0x84, 0x1c, 0xb2, 0x9e,
    0x6b, 0x32, 0x5d, 0xeb, 0xcc, 0x42, 0x06, 0x67, 0xf2, 0x5d, 0xef, 0xb7,
    0x18, 0xbb, 0x70, 0x52, 0x28, 0xfe, 0x0b, 0x19
  };
  
  unsigned char s1_2389999_ts[] = {
    0x47, 0x61, 0x12, 0xdc, 0x22, 0xbe, 0x9d, 0x3d, 0x1f, 0x6a, 0x86, 0x2f,
    0xf8, 0x80, 0xdc, 0x9c, 0x46, 0x75, 0x0d, 0x4b, 0x24, 0x43, 0x52, 0x3b,
    0x00, 0x8b, 0x77, 0x08, 0xc1, 0x83, 0xd2, 0xae, 0x7a, 0x22, 0xe8, 0xf7,
    0x84, 0xda, 0xe2, 0x00, 0x10, 0x51, 0x79, 0x85, 0x5b, 0xc9, 0x10, 0x30,
    0x6c, 0x6f, 0x51, 0x43, 0xfc, 0x18, 0x7c, 0x12, 0xa1, 0x0f, 0x54, 0xed,
    0x32, 0xb0, 0xe0, 0x4a, 0x66, 0x41, 0x67, 0x7e, 0x1f, 0x21, 0xa6, 0x8d,
    0x10, 0xe2, 0xdd, 0x3b, 0x10, 0x27, 0x32, 0x4b, 0x7d, 0x9a, 0xef, 0xfe,
    0x6a, 0x03, 0xda, 0x46, 0xa0, 0x18, 0x3c, 0x46, 0xad, 0xaf, 0xc0, 0x2b,
    0x65, 0x2d, 0xf2, 0x5e, 0xcd, 0xa4, 0xee, 0x4e, 0xa6, 0x78, 0xf2, 0x7e,
    0x81, 0xe2, 0x44, 0x52, 0x61, 0x64, 0xdd, 0xae, 0x3c, 0x98, 0xe4, 0x93,
    0x97, 0xc5, 0x91, 0x37, 0xc0, 0xb3, 0x1b, 0x96, 0x6f, 0xcd, 0xde, 0x6c,
    0x1e, 0x7e, 0x66, 0x0a, 0xb6, 0x3b, 0x1a, 0x0d, 0xa8, 0xd1, 0x5f, 0x26,
    0xe1, 0xd5, 0xc1, 0x34, 0xb6, 0x46, 0x01, 0xf6, 0xfb, 0xf0, 0x98, 0xd1,
    0xda, 0x9a, 0x73, 0x3f, 0xc1, 0x45, 0xfa, 0xfd, 0xe9, 0x53, 0xe3, 0x29,
    0x01, 0x76, 0xb1, 0xd4, 0xe4, 0x69, 0x07, 0xf0, 0x65, 0x1b, 0x77, 0x50,
    0xa7, 0x7e, 0x26, 0x93, 0x54, 0x93, 0x2d, 0x9b
  };
  
  TEST_F(Mpeg2TsParserTest, BinaryPackets) {
    struct binary_start_test {
      int line;
      const uint8_t *buf;
    } test_arr[] = {
      {__LINE__, mpts_pat_header},
      {__LINE__, s1_00057d98_ts},
      {__LINE__, s1_000b5060_ts},
      {__LINE__, s1_2080802_ts},
      {__LINE__, s1_2389999_ts},
    };
  
    uint8_t buf[MPEG_TS_PACKET_SIZE];
  
    for (const auto &test_item : test_arr) {
      // parse binary packet to pb
      Mpeg2Ts mpeg2ts;
      mpeg2ts_parser_.ParsePacket(0, 0, test_item.buf, MPEG_TS_PACKET_SIZE,
          &mpeg2ts);
  
      // dump the pb back to binary
      EXPECT_EQ(MPEG_TS_PACKET_SIZE,
          mpeg2ts_parser_.DumpPacket(mpeg2ts, buf, MPEG_TS_PACKET_SIZE))
          << "line " << test_item.line;
      EXPECT_EQ(0, memcmp(buf, test_item.buf, MPEG_TS_PACKET_SIZE))
          << "line " << test_item.line;
    }
  }
  
  
  int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
  }
  