Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/gtest/mp4_demuxer/TestMP4.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#include "gtest/gtest.h"
7
#include "mp4parse.h"
8
9
#include <stdint.h>
10
#include <stdio.h>
11
#include <string.h>
12
#include <algorithm>
13
#include <vector>
14
15
static intptr_t
16
error_reader(uint8_t* buffer, uintptr_t size, void* userdata)
17
0
{
18
0
  return -1;
19
0
}
20
21
struct read_vector {
22
  explicit read_vector(FILE* file, size_t length);
23
  explicit read_vector(size_t length);
24
25
  size_t location;
26
  std::vector<uint8_t> buffer;
27
};
28
29
read_vector::read_vector(FILE* file, size_t length)
30
 : location(0)
31
0
{
32
0
  buffer.resize(length);
33
0
  size_t read = fread(buffer.data(), sizeof(decltype(buffer)::value_type),
34
0
      buffer.size(), file);
35
0
  buffer.resize(read);
36
0
}
37
38
read_vector::read_vector(size_t length)
39
  : location(0)
40
0
{
41
0
  buffer.resize(length, 0);
42
0
}
43
44
static intptr_t
45
vector_reader(uint8_t* buffer, uintptr_t size, void* userdata)
46
0
{
47
0
  if (!buffer || !userdata) {
48
0
    return -1;
49
0
  }
50
0
51
0
  auto source = reinterpret_cast<read_vector*>(userdata);
52
0
  if (source->location > source->buffer.size()) {
53
0
    return -1;
54
0
  }
55
0
  uintptr_t available = source->buffer.size() - source->location;
56
0
  uintptr_t length = std::min(available, size);
57
0
  memcpy(buffer, source->buffer.data() + source->location, length);
58
0
  source->location += length;
59
0
  return length;
60
0
}
61
62
TEST(rust, MP4MetadataEmpty)
63
0
{
64
0
  Mp4parseStatus rv;
65
0
  Mp4parseIo io;
66
0
67
0
  // Shouldn't be able to read with no context.
68
0
  rv = mp4parse_read(nullptr);
69
0
  EXPECT_EQ(rv, MP4PARSE_STATUS_BAD_ARG);
70
0
71
0
  // Shouldn't be able to wrap an Mp4parseIo with null members.
72
0
  io = { nullptr, nullptr };
73
0
  Mp4parseParser* context = mp4parse_new(&io);
74
0
  EXPECT_EQ(context, nullptr);
75
0
76
0
  io = { nullptr, &io };
77
0
  context = mp4parse_new(&io);
78
0
  EXPECT_EQ(context, nullptr);
79
0
80
0
  // FIXME: this should probably be accepted.
81
0
  io = { error_reader, nullptr };
82
0
  context = mp4parse_new(&io);
83
0
  EXPECT_EQ(context, nullptr);
84
0
85
0
  // Read method errors should propagate.
86
0
  io = { error_reader, &io };
87
0
  context = mp4parse_new(&io);
88
0
  ASSERT_NE(context, nullptr);
89
0
  rv = mp4parse_read(context);
90
0
  EXPECT_EQ(rv, MP4PARSE_STATUS_IO);
91
0
  mp4parse_free(context);
92
0
93
0
  // Short buffers should fail.
94
0
  read_vector buf(0);
95
0
  io = { vector_reader, &buf };
96
0
  context = mp4parse_new(&io);
97
0
  ASSERT_NE(context, nullptr);
98
0
  rv = mp4parse_read(context);
99
0
  EXPECT_EQ(rv, MP4PARSE_STATUS_INVALID);
100
0
  mp4parse_free(context);
101
0
102
0
  buf.buffer.reserve(4097);
103
0
  context = mp4parse_new(&io);
104
0
  ASSERT_NE(context, nullptr);
105
0
  rv = mp4parse_read(context);
106
0
  EXPECT_EQ(rv, MP4PARSE_STATUS_INVALID);
107
0
  mp4parse_free(context);
108
0
109
0
  // Empty buffers should fail.
110
0
  buf.buffer.resize(4097, 0);
111
0
  context = mp4parse_new(&io);
112
0
  rv = mp4parse_read(context);
113
0
  EXPECT_EQ(rv, MP4PARSE_STATUS_UNSUPPORTED);
114
0
  mp4parse_free(context);
115
0
}
116
117
TEST(rust, MP4Metadata)
118
0
{
119
0
  FILE* f = fopen("street.mp4", "rb");
120
0
  ASSERT_TRUE(f != nullptr);
121
0
  // Read just the moov header to work around the parser
122
0
  // treating mid-box eof as an error.
123
0
  //read_vector reader = read_vector(f, 1061);
124
0
  struct stat s;
125
0
  ASSERT_EQ(0, fstat(fileno(f), &s));
126
0
  read_vector reader = read_vector(f, s.st_size);
127
0
  fclose(f);
128
0
129
0
  Mp4parseIo io = { vector_reader, &reader };
130
0
  Mp4parseParser* context = mp4parse_new(&io);
131
0
  ASSERT_NE(nullptr, context);
132
0
133
0
  Mp4parseStatus rv = mp4parse_read(context);
134
0
  EXPECT_EQ(MP4PARSE_STATUS_OK, rv);
135
0
136
0
  uint32_t tracks = 0;
137
0
  rv = mp4parse_get_track_count(context, &tracks);
138
0
  EXPECT_EQ(MP4PARSE_STATUS_OK, rv);
139
0
  EXPECT_EQ(2U, tracks);
140
0
141
0
  mp4parse_free(context);
142
0
}