2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2014-08-21 22:40:44 +00:00
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
2014-08-28 18:35:15 +00:00
|
|
|
#include <libxml/tree.h>
|
2014-08-21 22:40:44 +00:00
|
|
|
|
2014-05-22 02:16:17 +00:00
|
|
|
#include <list>
|
2014-02-14 23:21:05 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/logging.h"
|
|
|
|
#include "packager/base/strings/string_util.h"
|
|
|
|
#include "packager/mpd/base/mpd_builder.h"
|
|
|
|
#include "packager/mpd/base/xml/xml_node.h"
|
|
|
|
#include "packager/mpd/test/xml_compare.h"
|
2014-01-15 03:55:04 +00:00
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace edash_packager {
|
2014-01-15 03:55:04 +00:00
|
|
|
namespace xml {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Template so that it works for ContentProtectionXml and
|
|
|
|
// ContentProtectionXml::Element.
|
|
|
|
template <typename XmlElement>
|
|
|
|
void AddAttribute(const std::string& name,
|
|
|
|
const std::string& value,
|
|
|
|
XmlElement* content_protection_xml) {
|
|
|
|
MediaInfo::ContentProtectionXml::AttributeNameValuePair* attribute =
|
|
|
|
content_protection_xml->add_attributes();
|
|
|
|
attribute->set_name(name);
|
|
|
|
attribute->set_value(value);
|
|
|
|
}
|
|
|
|
|
2014-05-22 02:16:17 +00:00
|
|
|
std::string GetDocAsFlatString(xmlDocPtr doc) {
|
|
|
|
static const int kFlatFormat = 0;
|
|
|
|
int doc_str_size = 0;
|
|
|
|
xmlChar* doc_str = NULL;
|
|
|
|
xmlDocDumpFormatMemoryEnc(doc, &doc_str, &doc_str_size, "UTF-8", kFlatFormat);
|
|
|
|
DCHECK(doc_str);
|
|
|
|
|
|
|
|
std::string output(doc_str, doc_str + doc_str_size);
|
|
|
|
xmlFree(doc_str);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2014-04-12 01:23:20 +00:00
|
|
|
ScopedXmlPtr<xmlDoc>::type MakeDoc(ScopedXmlPtr<xmlNode>::type node) {
|
2014-01-15 03:55:04 +00:00
|
|
|
xml::ScopedXmlPtr<xmlDoc>::type doc(xmlNewDoc(BAD_CAST ""));
|
|
|
|
xmlDocSetRootElement(doc.get(), node.release());
|
2014-04-12 01:23:20 +00:00
|
|
|
return doc.Pass();
|
|
|
|
}
|
2014-05-22 02:16:17 +00:00
|
|
|
|
2014-01-15 03:55:04 +00:00
|
|
|
} // namespace
|
|
|
|
|
2014-05-22 02:16:17 +00:00
|
|
|
class RepresentationTest : public ::testing::Test {
|
|
|
|
public:
|
|
|
|
RepresentationTest() {}
|
|
|
|
virtual ~RepresentationTest() {}
|
|
|
|
|
|
|
|
// Ownership transfers, IOW this function will release the resource for
|
|
|
|
// |node|. Returns |node| in string format.
|
|
|
|
// You should not call this function multiple times.
|
|
|
|
std::string GetStringFormat() {
|
|
|
|
xml::ScopedXmlPtr<xmlDoc>::type doc(xmlNewDoc(BAD_CAST ""));
|
|
|
|
|
|
|
|
// Because you cannot easily get the string format of a xmlNodePtr, it gets
|
|
|
|
// attached to a temporary xml doc.
|
|
|
|
xmlDocSetRootElement(doc.get(), representation_.Release());
|
|
|
|
std::string doc_str = GetDocAsFlatString(doc.get());
|
|
|
|
|
|
|
|
// GetDocAsFlatString() adds
|
|
|
|
// <?xml version="" encoding="UTF-8"?>
|
|
|
|
// to the first line. So this removes the first line.
|
|
|
|
const size_t first_newline_char_pos = doc_str.find('\n');
|
|
|
|
DCHECK_NE(first_newline_char_pos, std::string::npos);
|
|
|
|
return doc_str.substr(first_newline_char_pos + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
RepresentationXmlNode representation_;
|
|
|
|
std::list<SegmentInfo> segment_infos_;
|
|
|
|
};
|
|
|
|
|
2014-04-12 01:23:20 +00:00
|
|
|
// Make sure XmlEqual() is functioning correctly.
|
2014-05-22 02:16:17 +00:00
|
|
|
// TODO(rkuroiwa): Move this to a separate file. This requires it to be TEST_F
|
|
|
|
// due to gtest /test
|
|
|
|
TEST_F(RepresentationTest, MetaTest_XmlEqual) {
|
2014-04-12 01:23:20 +00:00
|
|
|
static const char kXml1[] =
|
|
|
|
"<A>\n"
|
|
|
|
" <B\n"
|
|
|
|
" c=\"1\""
|
|
|
|
" e=\"foobar\""
|
|
|
|
" somelongnameattribute=\"somevalue\">\n"
|
|
|
|
" <Bchild childvalue=\"3\"\n"
|
|
|
|
" f=\"4\"/>\n"
|
|
|
|
" </B>\n"
|
|
|
|
" <C />\n"
|
|
|
|
"</A>";
|
|
|
|
|
|
|
|
|
|
|
|
// This is same as kXml1 but the attributes are reordered. Note that the
|
|
|
|
// children are not reordered.
|
|
|
|
static const char kXml1AttributeReorder[] =
|
|
|
|
"<A>\n"
|
|
|
|
" <B\n"
|
|
|
|
" c=\"1\""
|
|
|
|
" somelongnameattribute=\"somevalue\"\n"
|
|
|
|
" e=\"foobar\">"
|
|
|
|
" <Bchild childvalue=\"3\"\n"
|
|
|
|
" f=\"4\"/>\n"
|
|
|
|
" </B>\n"
|
|
|
|
" <C />\n"
|
|
|
|
"</A>";
|
|
|
|
|
|
|
|
// <C> is before <B>.
|
|
|
|
static const char kXml1ChildrenReordered[] =
|
|
|
|
"<A>\n"
|
|
|
|
" <C />\n"
|
|
|
|
" <B\n"
|
|
|
|
" d=\"2\""
|
|
|
|
" c=\"1\""
|
|
|
|
" somelongnameattribute=\"somevalue\"\n"
|
|
|
|
" e=\"foobar\">"
|
|
|
|
" <Bchild childvalue=\"3\"\n"
|
|
|
|
" f=\"4\"/>\n"
|
|
|
|
" </B>\n"
|
|
|
|
"</A>";
|
|
|
|
|
|
|
|
// <C> is before <B>.
|
|
|
|
static const char kXml1RemovedAttributes[] =
|
|
|
|
"<A>\n"
|
|
|
|
" <B\n"
|
|
|
|
" d=\"2\"\n>"
|
|
|
|
" <Bchild f=\"4\"/>\n"
|
|
|
|
" </B>\n"
|
|
|
|
" <C />\n"
|
|
|
|
"</A>";
|
|
|
|
|
|
|
|
static const char kXml2[] =
|
|
|
|
"<A>\n"
|
|
|
|
" <C />\n"
|
|
|
|
"</A>";
|
|
|
|
|
|
|
|
// In XML <C />, <C></C>, and <C/> mean the same thing.
|
|
|
|
static const char kXml2DifferentSyntax[] =
|
|
|
|
"<A>\n"
|
|
|
|
" <C></C>\n"
|
|
|
|
"</A>";
|
|
|
|
|
|
|
|
static const char kXml2MoreDifferentSyntax[] =
|
|
|
|
"<A>\n"
|
|
|
|
" <C/>\n"
|
|
|
|
"</A>";
|
|
|
|
|
|
|
|
// Identity.
|
|
|
|
ASSERT_TRUE(XmlEqual(kXml1, kXml1));
|
|
|
|
|
|
|
|
// Equivalent.
|
|
|
|
ASSERT_TRUE(XmlEqual(kXml1, kXml1AttributeReorder));
|
|
|
|
ASSERT_TRUE(XmlEqual(kXml2, kXml2DifferentSyntax));
|
|
|
|
ASSERT_TRUE(XmlEqual(kXml2, kXml2MoreDifferentSyntax));
|
|
|
|
|
|
|
|
// Different.
|
|
|
|
ASSERT_FALSE(XmlEqual(kXml1, kXml2));
|
|
|
|
ASSERT_FALSE(XmlEqual(kXml1, kXml1ChildrenReordered));
|
|
|
|
ASSERT_FALSE(XmlEqual(kXml1, kXml1RemovedAttributes));
|
|
|
|
ASSERT_FALSE(XmlEqual(kXml1AttributeReorder, kXml1ChildrenReordered));
|
|
|
|
}
|
|
|
|
|
2014-05-22 02:16:17 +00:00
|
|
|
TEST_F(RepresentationTest, AddContentProtectionXml) {
|
2014-01-15 03:55:04 +00:00
|
|
|
static const char kExpectedRepresentaionString[] =
|
2014-04-12 01:23:20 +00:00
|
|
|
"<Representation>\n"
|
|
|
|
" <ContentProtection\n"
|
|
|
|
" a=\"1\"\n"
|
|
|
|
" b=\"2\"\n"
|
|
|
|
" schemeIdUri=\"http://www.foo.com/drm\"\n"
|
|
|
|
" value=\"somevalue\">\n"
|
|
|
|
" <TestSubElement c=\"3\" d=\"4\"/>\n"
|
|
|
|
" </ContentProtection>\n"
|
|
|
|
"</Representation>";
|
2014-01-15 03:55:04 +00:00
|
|
|
|
|
|
|
MediaInfo media_info;
|
|
|
|
MediaInfo::ContentProtectionXml* content_protection_xml =
|
|
|
|
media_info.add_content_protections();
|
|
|
|
content_protection_xml->set_scheme_id_uri("http://www.foo.com/drm");
|
|
|
|
content_protection_xml->set_value("somevalue");
|
|
|
|
AddAttribute("a", "1", content_protection_xml);
|
|
|
|
AddAttribute("b", "2", content_protection_xml);
|
|
|
|
|
|
|
|
MediaInfo::ContentProtectionXml::Element* subelement =
|
|
|
|
content_protection_xml->add_subelements();
|
|
|
|
subelement->set_name("TestSubElement");
|
|
|
|
AddAttribute("c", "3", subelement);
|
|
|
|
AddAttribute("d", "4", subelement);
|
|
|
|
|
|
|
|
ASSERT_TRUE(
|
2014-05-22 02:16:17 +00:00
|
|
|
representation_.AddContentProtectionElementsFromMediaInfo(media_info));
|
|
|
|
ScopedXmlPtr<xmlDoc>::type doc(MakeDoc(representation_.PassScopedPtr()));
|
2014-04-12 01:23:20 +00:00
|
|
|
ASSERT_TRUE(
|
|
|
|
XmlEqual(kExpectedRepresentaionString, doc.get()));
|
2014-01-15 03:55:04 +00:00
|
|
|
}
|
|
|
|
|
2014-05-22 02:16:17 +00:00
|
|
|
// Some template names cannot be used for init segment name.
|
|
|
|
TEST_F(RepresentationTest, InvalidLiveInitSegmentName) {
|
|
|
|
MediaInfo media_info;
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint32_t kDefaultStartNumber = 1;
|
2014-05-22 02:16:17 +00:00
|
|
|
|
2014-05-27 22:21:42 +00:00
|
|
|
// $Number$ cannot be used for segment name.
|
2014-05-22 02:16:17 +00:00
|
|
|
media_info.set_init_segment_name("$Number$.mp4");
|
2014-05-27 22:21:42 +00:00
|
|
|
ASSERT_FALSE(representation_.AddLiveOnlyInfo(
|
|
|
|
media_info, segment_infos_, kDefaultStartNumber));
|
2014-05-22 02:16:17 +00:00
|
|
|
|
2014-05-27 22:21:42 +00:00
|
|
|
// $Time$ as well.
|
2014-05-22 02:16:17 +00:00
|
|
|
media_info.set_init_segment_name("$Time$.mp4");
|
2014-05-27 22:21:42 +00:00
|
|
|
ASSERT_FALSE(representation_.AddLiveOnlyInfo(
|
|
|
|
media_info, segment_infos_, kDefaultStartNumber));
|
2014-05-22 02:16:17 +00:00
|
|
|
|
|
|
|
// This should be valid.
|
|
|
|
media_info.set_init_segment_name("some_non_template_name.mp4");
|
2014-05-27 22:21:42 +00:00
|
|
|
ASSERT_TRUE(representation_.AddLiveOnlyInfo(
|
|
|
|
media_info, segment_infos_, kDefaultStartNumber));
|
2014-05-22 02:16:17 +00:00
|
|
|
}
|
|
|
|
|
2014-01-15 03:55:04 +00:00
|
|
|
} // namespace xml
|
2014-09-19 20:41:13 +00:00
|
|
|
} // namespace edash_packager
|