(De)serialization of Comments
[frida/frida.git] / src / core / tests / CommentTest.cxx
1 #include <gtest/gtest.h>
2
3 #include "gui/qt.hxx"
4 #include "core/InformationManager.hxx"
5 #include "core/BasicBlock.hxx"
6 #include "core/Comment.hxx"
7 #include <iostream>
8
9 extern char * TEST_DATA_DIRECTORY;
10
11 TEST(CommentTest, deserializeValidInstance) {
12 InformationManager manager;
13 QDir directory(QString("%1/%2").arg(TEST_DATA_DIRECTORY,"/core/Comment/valid/"));
14 QStringList filters;
15 filters << "*.xml";
16 directory.setNameFilters(filters);
17
18 for (auto fileinfo : directory.entryInfoList()) {
19 if (false == fileinfo.isFile())
20 continue;
21
22 QFile file(fileinfo.absoluteFilePath());
23 file.open(QFile::ReadOnly | QFile::Text);
24 QXmlStreamReader reader(&file);
25 reader.readNextStartElement();
26
27 Comment* c = Comment::deserialize(reader, &manager);
28 ASSERT_NE((void*)NULL, (void*)c);
29 EXPECT_EQ(fileinfo.baseName().toULongLong(NULL, 16), c->getAddress());
30 }
31 }
32
33 TEST(CommentTest, deserializeInvalidInstance) {
34 InformationManager manager;
35 QDir directory(QString("%1/%2").arg(TEST_DATA_DIRECTORY,"/core/Comment/invalid/"));
36 QStringList filters;
37 filters << "*.xml";
38 directory.setNameFilters(filters);
39
40 for (auto fileinfo : directory.entryInfoList()) {
41 if (false == fileinfo.isFile())
42 continue;
43
44 QFile file(fileinfo.absoluteFilePath());
45 file.open(QFile::ReadOnly | QFile::Text);
46 QXmlStreamReader reader(&file);
47 reader.readNextStartElement();
48
49 Comment* c = Comment::deserialize(reader, &manager);
50 ASSERT_EQ((void*)NULL, (void*)c);
51 }
52 }
53
54 TEST(CommentTest, serializeThenDeserializeIsIdentity) {
55 InformationManager manager;
56 Comment* c = manager.newGlobalComment(0x42234223);
57
58 for (QString text : QStringList({"αβγδ", "<test>", "&auml;"})) {
59 c->setText(text.toStdString());
60
61 QTemporaryFile tmpfile;
62
63 {
64 tmpfile.open();
65 QXmlStreamWriter writer(&tmpfile);
66 c->serialize(writer);
67 }
68 tmpfile.seek(0);
69 {
70 tmpfile.open();
71 QXmlStreamReader reader(&tmpfile);
72 reader.readNextStartElement();
73 Comment* d = Comment::deserialize(reader, &manager);
74 ASSERT_NE((void*)NULL, (void*)d);
75 EXPECT_STREQ(c->getText().c_str(), d->getText().c_str());
76 EXPECT_EQ(0x42234223, d->getAddress());
77 }
78 }
79 }