Add MinidumpStringListWriter and test.

TEST=minidump_test MinidumpStringWriter.*List
R=rsesek@chromium.org

Review URL: https://codereview.chromium.org/707003002
This commit is contained in:
Mark Mentovai 2014-11-07 09:57:07 -05:00
parent a980409288
commit 6e97189d27
3 changed files with 162 additions and 0 deletions

View File

@ -95,5 +95,43 @@ void MinidumpUTF16StringWriter::SetUTF8(const std::string& string_utf8) {
MinidumpUTF8StringWriter::~MinidumpUTF8StringWriter() {
}
template <typename Traits>
MinidumpStringListWriter<Traits>::MinidumpStringListWriter()
: MinidumpRVAListWriter() {
}
template <typename Traits>
MinidumpStringListWriter<Traits>::~MinidumpStringListWriter() {
}
template <typename Traits>
void MinidumpStringListWriter<Traits>::InitializeFromVector(
const std::vector<std::string>& vector) {
DCHECK_EQ(state(), kStateMutable);
DCHECK(IsEmpty());
for (const std::string& string : vector) {
AddStringUTF8(string);
}
}
template <typename Traits>
void MinidumpStringListWriter<Traits>::AddStringUTF8(
const std::string& string_utf8) {
auto string_writer = make_scoped_ptr(new MinidumpStringWriterType());
string_writer->SetUTF8(string_utf8);
AddChild(string_writer.Pass());
}
template <typename Traits>
bool MinidumpStringListWriter<Traits>::IsUseful() const {
return !IsEmpty();
}
// Explicit template instantiation of the forms of MinidumpStringListWriter<>
// used as type aliases.
template class MinidumpStringListWriter<MinidumpStringListWriterUTF16Traits>;
template class MinidumpStringListWriter<MinidumpStringListWriterUTF8Traits>;
} // namespace internal
} // namespace crashpad

View File

@ -18,10 +18,12 @@
#include <dbghelp.h>
#include <string>
#include <vector>
#include "base/basictypes.h"
#include "base/strings/string16.h"
#include "minidump/minidump_extensions.h"
#include "minidump/minidump_rva_list_writer.h"
#include "minidump/minidump_writable.h"
namespace crashpad {
@ -124,6 +126,68 @@ class MinidumpUTF8StringWriter final
DISALLOW_COPY_AND_ASSIGN(MinidumpUTF8StringWriter);
};
//! \cond
struct MinidumpStringListWriterUTF16Traits {
using MinidumpStringWriterType = MinidumpUTF16StringWriter;
};
struct MinidumpStringListWriterUTF8Traits {
using MinidumpStringWriterType = MinidumpUTF8StringWriter;
};
//! \endcond
//! \brief The writer for a MinidumpRVAList object in a minidump file,
//! containing a list of \a Traits::MinidumpStringWriterType objects.
template <typename Traits>
class MinidumpStringListWriter : public MinidumpRVAListWriter {
public:
using MinidumpStringWriterType = typename Traits::MinidumpStringWriterType;
MinidumpStringListWriter();
~MinidumpStringListWriter() override;
//! \brief Adds a new \a Traits::MinidumpStringWriterType for each element in
//! \a vector to the MinidumpRVAList.
//!
//! \param[in] vector The vector to use as source data. Each string in the
//! vector is treated as a UTF-8 string, and a new string writer will be
//! created for each one and made a child of the MinidumpStringListWriter.
//!
//! \note Valid in #kStateMutable. No mutator methods may be called before
//! this method, and it is not normally necessary to call any mutator
//! methods after this method.
void InitializeFromVector(const std::vector<std::string>& vector);
//! \brief Creates a new \a Traits::MinidumpStringWriterType object and adds
//! it to the MinidumpRVAList.
//!
//! This object creates a new string writer with string value \a string_utf8,
//! takes ownership of it, and becomes its parent in the overall tree of
//! internal::MinidumpWritable objects.
//!
//! \note Valid in #kStateMutable.
void AddStringUTF8(const std::string& string_utf8);
//! \brief Determines whether the object is useful.
//!
//! A useful object is one that carries data that makes a meaningful
//! contribution to a minidump file. An object carrying entries would be
//! considered useful.
//!
//! \return `true` if the object is useful, `false` otherwise.
bool IsUseful() const;
private:
DISALLOW_COPY_AND_ASSIGN(MinidumpStringListWriter);
};
using MinidumpUTF16StringListWriter =
MinidumpStringListWriter<MinidumpStringListWriterUTF16Traits>;
using MinidumpUTF8StringListWriter =
MinidumpStringListWriter<MinidumpStringListWriterUTF8Traits>;
} // namespace internal
} // namespace crashpad

View File

@ -22,8 +22,11 @@
#include "base/basictypes.h"
#include "base/strings/string16.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "gtest/gtest.h"
#include "minidump/test/minidump_rva_list_test_util.h"
#include "minidump/test/minidump_string_writer_test_util.h"
#include "minidump/test/minidump_writable_test_util.h"
#include "util/file/string_file_writer.h"
namespace crashpad {
@ -194,6 +197,63 @@ TEST(MinidumpStringWriter, MinidumpUTF8StringWriter) {
}
}
struct MinidumpUTF16StringListWriterTraits {
using MinidumpStringListWriterType = internal::MinidumpUTF16StringListWriter;
static string16 ExpectationForUTF8(const std::string& utf8) {
return base::UTF8ToUTF16(utf8);
}
static string16 ObservationAtRVA(const std::string& file_contents, RVA rva) {
return MinidumpStringAtRVAAsString(file_contents, rva);
}
};
struct MinidumpUTF8StringListWriterTraits {
using MinidumpStringListWriterType = internal::MinidumpUTF8StringListWriter;
static std::string ExpectationForUTF8(const std::string& utf8) {
return utf8;
}
static std::string ObservationAtRVA(const std::string& file_contents,
RVA rva) {
return MinidumpUTF8StringAtRVAAsString(file_contents, rva);
}
};
template <typename Traits>
void MinidumpStringListTest() {
std::vector<std::string> strings;
strings.push_back(std::string("One"));
strings.push_back(std::string());
strings.push_back(std::string("3"));
strings.push_back(std::string("\360\237\222\251"));
typename Traits::MinidumpStringListWriterType string_list_writer;
EXPECT_FALSE(string_list_writer.IsUseful());
string_list_writer.InitializeFromVector(strings);
EXPECT_TRUE(string_list_writer.IsUseful());
StringFileWriter file_writer;
ASSERT_TRUE(string_list_writer.WriteEverything(&file_writer));
const MinidumpRVAList* list =
MinidumpRVAListAtStart(file_writer.string(), strings.size());
ASSERT_TRUE(list);
for (size_t index = 0; index < strings.size(); ++index) {
EXPECT_EQ(Traits::ExpectationForUTF8(strings[index]),
Traits::ObservationAtRVA(file_writer.string(),
list->children[index]));
}
}
TEST(MinidumpStringWriter, MinidumpUTF16StringList) {
MinidumpStringListTest<MinidumpUTF16StringListWriterTraits>();
}
TEST(MinidumpStringWriter, MinidumpUTF8StringList) {
MinidumpStringListTest<MinidumpUTF8StringListWriterTraits>();
}
} // namespace
} // namespace test
} // namespace crashpad