aboutsummaryrefslogtreecommitdiffstats
path: root/src/m_pgp_inspect/test/PGPInspectModelTest.cpp
blob: f76d5591155d972992d72846eb9ee6ce8b1b7397 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
 * Copyright (C) 2021-2024 Saturneric <eric@bktus.com>
 *
 * This file is part of GpgFrontend.
 *
 * GpgFrontend is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GpgFrontend is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
 *
 * The initial version of the source code is inherited from
 * the gpg4usb project, which is under GPL-3.0-or-later.
 *
 * All the source code of GpgFrontend was modified and released by
 * Saturneric <eric@bktus.com> starting on May 12, 2021.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 */

#include <gtest/gtest.h>

#include <QCoreApplication>

#include "PGPInspectModel.h"

namespace {

/// A minimal but complete document, in the shape the Rust walk emits.
constexpr const char* kDocument = R"({
  "format": "armored",
  "size": 325,
  "errors": [],
  "blocks": [
    {
      "kind": "armored",
      "offset": 0,
      "armor": {
        "blockType": "PGP SIGNATURE",
        "headers": [{"label": "Version", "value": "GnuPG v2"}],
        "crc24": "ok"
      },
      "packets": [
        {
          "tag": 8,
          "tagName": "Compressed Data",
          "offset": 0,
          "headerVersion": "new",
          "headerLength": 2,
          "bodyLength": 189,
          "lengthType": "fixed",
          "fields": [{"label": "Compression Algorithm", "value": "ZIP"}],
          "children": [
            {
              "tag": 2,
              "tagName": "Signature",
              "offset": 0,
              "headerVersion": "old",
              "headerLength": 3,
              "bodyLength": 100,
              "lengthType": "fixed",
              "version": 4,
              "fields": [{"label": "Hash Algorithm", "value": "Sha256"}],
              "children": [],
              "error": null
            }
          ],
          "error": null
        }
      ],
      "error": null
    }
  ]
})";

}  // namespace

// -- reading the document ----------------------------------------------------

TEST(PGPInspectModelTest, ReadsTheDocumentShape) {
  const auto document = ParsePGPInspectDocument(kDocument);

  ASSERT_TRUE(document.valid);
  EXPECT_EQ(document.format, QString("armored"));
  EXPECT_EQ(document.size, 325);
  ASSERT_EQ(document.blocks.size(), 1);
  EXPECT_TRUE(document.errors.isEmpty());
}

TEST(PGPInspectModelTest, ReadsTheArmorEnvelope) {
  const auto document = ParsePGPInspectDocument(kDocument);
  const auto& block = document.blocks.at(0);

  ASSERT_TRUE(block.has_armor);
  EXPECT_EQ(block.armor.block_type, QString("PGP SIGNATURE"));
  EXPECT_EQ(block.armor.crc24, QString("ok"));
  ASSERT_EQ(block.armor.headers.size(), 1);
  EXPECT_EQ(block.armor.headers.at(0).label, QString("Version"));
}

TEST(PGPInspectModelTest, NestsChildrenUnderTheirContainer) {
  // The recursion is the reason the tree exists; a flat read would lose it.
  const auto document = ParsePGPInspectDocument(kDocument);
  const auto& packets = document.blocks.at(0).packets;

  ASSERT_EQ(packets.size(), 1);
  EXPECT_EQ(packets.at(0).tag, 8);
  ASSERT_EQ(packets.at(0).children.size(), 1);
  EXPECT_EQ(packets.at(0).children.at(0).tag, 2);
  EXPECT_EQ(packets.at(0).children.at(0).version, 4);
}

TEST(PGPInspectModelTest, CountsNestedPacketsToo) {
  EXPECT_EQ(ParsePGPInspectDocument(kDocument).PacketCount(), 2);
}

TEST(PGPInspectModelTest, KeepsTheFramingFiguresVerbatim) {
  const auto document = ParsePGPInspectDocument(kDocument);
  const auto& packet = document.blocks.at(0).packets.at(0);

  EXPECT_EQ(packet.offset, 0);
  EXPECT_EQ(packet.header_length, 2);
  EXPECT_EQ(packet.body_length, 189);
  EXPECT_EQ(packet.header_version, QString("new"));
  EXPECT_EQ(packet.length_type, QString("fixed"));
}

TEST(PGPInspectModelTest, AbsentVersionReadsAsMinusOne) {
  // Most container packets carry no version octet. That is a fact about the
  // type, so it must not look like a defect in the document.
  const auto document = ParsePGPInspectDocument(kDocument);
  const auto& packet = document.blocks.at(0).packets.at(0);
  EXPECT_EQ(packet.version, -1);
  EXPECT_FALSE(packet.Malformed());
}

// -- the shapes that are not the happy one ------------------------------------

TEST(PGPInspectModelTest, MalformedJsonIsReportedNotThrown) {
  const auto document = ParsePGPInspectDocument("{ this is not json");

  EXPECT_FALSE(document.valid);
  EXPECT_FALSE(document.parse_error.isEmpty());
  EXPECT_TRUE(document.blocks.isEmpty());
}

TEST(PGPInspectModelTest, EmptyInputIsReportedNotThrown) {
  const auto document = ParsePGPInspectDocument({});

  EXPECT_FALSE(document.valid);
  EXPECT_FALSE(document.parse_error.isEmpty());
}

TEST(PGPInspectModelTest, ANonObjectDocumentIsRefused) {
  EXPECT_FALSE(ParsePGPInspectDocument("[1, 2, 3]").valid);
}

TEST(PGPInspectModelTest, AnEmptyDocumentIsValidAndEmpty) {
  // "nothing recognisable here" is a finding the inspector reports, not a
  // failure to read its answer.
  const auto document = ParsePGPInspectDocument(
      R"({"format":"binary","size":4,"blocks":[],"errors":["not OpenPGP data"]})");

  ASSERT_TRUE(document.valid);
  EXPECT_TRUE(document.blocks.isEmpty());
  ASSERT_EQ(document.errors.size(), 1);
  EXPECT_EQ(document.errors.at(0), QString("not OpenPGP data"));
}

TEST(PGPInspectModelTest, APacketErrorSurvivesIntoTheTree) {
  const auto document = ParsePGPInspectDocument(
      R"({"blocks":[{"packets":[{"tag":2,"error":"the packet body is truncated"}]}]})");

  ASSERT_EQ(document.blocks.size(), 1);
  const auto& packet = document.blocks.at(0).packets.at(0);
  EXPECT_TRUE(packet.Malformed());
  EXPECT_EQ(packet.error, QString("the packet body is truncated"));
}

TEST(PGPInspectModelTest, MissingFieldsTakeTheirDefaults) {
  // A document from an older or newer engine must degrade field by field
  // rather than costing the reader the whole tree.
  const auto document =
      ParsePGPInspectDocument(R"({"blocks":[{"packets":[{}]}]})");

  ASSERT_TRUE(document.valid);
  const auto& packet = document.blocks.at(0).packets.at(0);
  EXPECT_EQ(packet.tag, -1);
  EXPECT_TRUE(packet.tag_name.isEmpty());
  EXPECT_EQ(packet.body_length, 0);
}

TEST(PGPInspectModelTest, ADetailRowWithNeitherHalfIsDropped) {
  const auto document = ParsePGPInspectDocument(
      R"({"blocks":[{"packets":[{"fields":[{},{"label":"A","value":"B"}]}]}]})");

  const auto& fields = document.blocks.at(0).packets.at(0).fields;
  ASSERT_EQ(fields.size(), 1);
  EXPECT_EQ(fields.at(0).label, QString("A"));
}

TEST(PGPInspectModelTest, ReadsTheCleartextHalf) {
  const auto document = ParsePGPInspectDocument(
      R"({"format":"cleartext","blocks":[{"kind":"cleartext","cleartext":{"textSize":42,"headers":[{"label":"Hash","value":"SHA256"}]},"packets":[]}]})");

  const auto& block = document.blocks.at(0);
  ASSERT_TRUE(block.has_cleartext);
  EXPECT_EQ(block.cleartext_text_size, 42);
  ASSERT_EQ(block.cleartext_headers.size(), 1);
  EXPECT_EQ(block.cleartext_headers.at(0).value, QString("SHA256"));
}

// -- whether there is anything to show ----------------------------------------

TEST(PGPInspectModelTest, ADocumentWithPacketsHasStructure) {
  EXPECT_TRUE(PGPInspectHasStructure(ParsePGPInspectDocument(kDocument)));
}

TEST(PGPInspectModelTest, PlainTextHasNoStructure) {
  // The menu entry is greyed out on exactly this: a valid document saying the
  // input is not OpenPGP. Opening a window to be told nothing is worse than
  // an entry that cannot be clicked.
  EXPECT_FALSE(PGPInspectHasStructure(ParsePGPInspectDocument(
      R"({"format":"binary","size":9,"blocks":[],"errors":["not OpenPGP data"]})")));
}

TEST(PGPInspectModelTest, AnUnreadableDocumentHasNoStructure) {
  EXPECT_FALSE(PGPInspectHasStructure(ParsePGPInspectDocument("not json")));
  EXPECT_FALSE(PGPInspectHasStructure({}));
}

TEST(PGPInspectModelTest, ABlockWithNoPacketsHasNoStructure) {
  // An armor envelope whose body decoded to nothing: the header parsed, so
  // the document is valid, but there is still no packet tree to render.
  EXPECT_FALSE(PGPInspectHasStructure(ParsePGPInspectDocument(
      R"({"format":"armored","blocks":[{"kind":"armored","packets":[]}]})")));
}

TEST(PGPInspectModelTest, ANestedPacketAloneIsStructureEnough) {
  EXPECT_TRUE(PGPInspectHasStructure(ParsePGPInspectDocument(
      R"({"blocks":[{"packets":[{"tag":8,"children":[{"tag":2}]}]}]})")));
}

// -- the summaries the tree shows ---------------------------------------------

TEST(PGPInspectModelTest, APacketSummaryNamesTagVersionOffsetAndSize) {
  // Held by value: a reference into the temporary document would dangle as
  // soon as this statement ends.
  const auto document = ParsePGPInspectDocument(kDocument);
  const auto& packet = document.blocks.at(0).packets.at(0).children.at(0);
  const auto summary = PGPInspectPacketSummary(packet, 1);

  EXPECT_TRUE(summary.contains("#1"));
  EXPECT_TRUE(summary.contains("Signature"));
  EXPECT_TRUE(summary.contains("tag 2"));
  EXPECT_TRUE(summary.contains("v4"));
  EXPECT_TRUE(summary.contains("@0x0000"));
  // Header plus body: the bytes this packet occupies on the wire.
  EXPECT_TRUE(summary.contains("103"));
}

TEST(PGPInspectModelTest, ASummaryFlagsANonFixedLength) {
  PGPInspectPacket packet;
  packet.tag = 11;
  packet.tag_name = "Literal Data";
  packet.length_type = "partial";

  EXPECT_TRUE(PGPInspectPacketSummary(packet, 2).contains("partial"));
}

TEST(PGPInspectModelTest, ASummaryOfANamelessPacketStillReads) {
  PGPInspectPacket packet;
  packet.tag = 60;

  const auto summary = PGPInspectPacketSummary(packet, 1);
  EXPECT_TRUE(summary.contains("Unknown"));
  EXPECT_TRUE(summary.contains("tag 60"));
}

TEST(PGPInspectModelTest, ABlockSummaryDistinguishesTheThreeKinds) {
  const auto armored = ParsePGPInspectDocument(kDocument).blocks.at(0);
  EXPECT_TRUE(PGPInspectBlockSummary(armored, 1).contains("PGP SIGNATURE"));

  PGPInspectBlock cleartext;
  cleartext.kind = "cleartext";
  EXPECT_TRUE(PGPInspectBlockSummary(cleartext, 1).contains("cleartext"));

  PGPInspectBlock binary;
  binary.kind = "binary";
  EXPECT_TRUE(PGPInspectBlockSummary(binary, 1).contains("binary"));
}