File: output.cpp

package info (click to toggle)
stenc 2.0.1-0.1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 920 kB
  • sloc: cpp: 15,585; xml: 51; makefile: 25; sh: 1
file content (178 lines) | stat: -rw-r--r-- 7,081 bytes parent folder | download
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
// SPDX-FileCopyrightText: 2022 stenc authors
//
// SPDX-License-Identifier: GPL-2.0-or-later

#define CATCH_CONFIG_MAIN
#include "catch.hpp"

#include <cstdint>
#include <map>
#include <optional>
#include <sstream>
#include <string>
#include <vector>

#include "main.cpp"

using namespace std::literals::string_literals;

TEST_CASE("Test key_from_hex_chars", "[output]")
{
  REQUIRE(key_from_hex_chars(""s) == std::vector<std::uint8_t> {});
  REQUIRE(key_from_hex_chars("hello"s) == std::nullopt);
  REQUIRE(key_from_hex_chars("12z"s) == std::nullopt);
  REQUIRE(key_from_hex_chars("0xabcd"s) == std::nullopt);
  REQUIRE(key_from_hex_chars("ab cd"s) == std::nullopt);
  REQUIRE(key_from_hex_chars("a"s) == std::vector<std::uint8_t> {0x0a});
  REQUIRE(key_from_hex_chars("0123456789abcdef"s) ==
          std::vector<std::uint8_t> {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd,
                                     0xef});
  REQUIRE(key_from_hex_chars("0123456789ABCDEF"s) ==
          std::vector<std::uint8_t> {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd,
                                     0xef});
}

/**
 * Compare the output of stenc given device responses
 *
 * These tests check the representation and interpretation of raw device data
 * and that the program output accurately reports the meaning of the data.
 */
TEST_CASE("Test SCSI inquiry output", "[output]")
{
  const std::uint8_t response[] {
      0x01, 0x80, 0x00, 0x02, 0x5b, 0x00, 0x00, 0x02, 0x41, 0x43, 0x4d, 0x45,
      0x20, 0x20, 0x20, 0x20, 0x55, 0x6c, 0x74, 0x72, 0x69, 0x75, 0x6d, 0x2d,
      0x31, 0x30, 0x30, 0x30, 0x20, 0x20, 0x20, 0x20, 0x31, 0x32, 0x33, 0x34,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  };
  const std::string expected_output {"ACME Ultrium-1000 1234"s};
  std::ostringstream oss;
  print_device_inquiry(oss,
                       reinterpret_cast<const scsi::inquiry_data&>(response));
  REQUIRE(oss.str() == expected_output);
}

TEST_CASE("SCSI get device encryption status output 1", "[output]")
{
  std::map<std::uint8_t, std::string> algorithms {
      {1, "AES-256-GCM-128"s},
  };
  const std::uint8_t page[] {
      0x00, 0x20, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  };
  const std::string expected_output {"\
Reading:                         Not decrypting\n\
Writing:                         Not encrypting\n\
Key instance counter:            0\n"s};
  std::ostringstream oss;
  print_device_status(oss, reinterpret_cast<const scsi::page_des&>(page),
                      algorithms);
  REQUIRE(oss.str() == expected_output);
}

TEST_CASE("SCSI get device encryption status output 2", "[output]")
{
  std::map<std::uint8_t, std::string> algorithms {
      {1, "AES-256-GCM-128"s},
  };
  const std::uint8_t page[] {
      0x00, 0x20, 0x00, 0x24, 0x42, 0x02, 0x02, 0x01, 0x00, 0x00,
      0x00, 0x01, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x48, 0x65,
      0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21,
  };
  const std::string expected_output {"\
Reading:                         Decrypting (AES-256-GCM-128)\n\
                                 Unencrypted blocks not readable\n\
Writing:                         Encrypting (AES-256-GCM-128)\n\
Key instance counter:            1\n\
Drive key desc. (U-KAD):         Hello world!\n"s};
  std::ostringstream oss;
  print_device_status(oss, reinterpret_cast<const scsi::page_des&>(page),
                      algorithms);
  REQUIRE(oss.str() == expected_output);
}

TEST_CASE("Test SCSI get next block encryption status output 1", "[output]")
{
  std::map<std::uint8_t, std::string> algorithms {
      {1, "AES-256-GCM-128"s},
  };
  const std::uint8_t page[] {
      0x00, 0x21, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  };
  const std::string expected_output {"\
Current block status:            Not encrypted\n"s};
  std::ostringstream oss;
  print_block_status(oss, reinterpret_cast<const scsi::page_nbes&>(page),
                     algorithms);
  REQUIRE(oss.str() == expected_output);
}

TEST_CASE("Test SCSI get next block encryption status output 2", "[output]")
{
  std::map<std::uint8_t, std::string> algorithms {
      {1, "AES-256-GCM-128"s},
  };
  const std::uint8_t page[] {
      0x00, 0x21, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x05, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0c, 0x48, 0x65,
      0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21,
  };
  const std::string expected_output {"\
Current block status:            Encrypted and able to decrypt (AES-256-GCM-128)\n"s};
  std::ostringstream oss;
  print_block_status(oss, reinterpret_cast<const scsi::page_nbes&>(page),
                     algorithms);
  REQUIRE(oss.str() == expected_output);
}

TEST_CASE("Test SCSI get next block encryption status output 3", "[output]")
{
  std::map<std::uint8_t, std::string> algorithms {
      {1, "AES-256-GCM-128"s},
  };
  const std::uint8_t page[] {
      0x00, 0x21, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x06, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x0c, 0x48, 0x65,
      0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21,
  };
  const std::string expected_output {"\
Current block status:            Encrypted, key missing or invalid (AES-256-GCM-128)\n\
Current block key desc. (U-KAD): Hello world!\n"s};
  std::ostringstream oss;
  print_block_status(oss, reinterpret_cast<const scsi::page_nbes&>(page),
                     algorithms);
  REQUIRE(oss.str() == expected_output);
}

TEST_CASE("Test SCSI get data encryption capabilities output", "[output]")
{
  const std::uint8_t page[] {
      0x00, 0x10, 0x00, 0x3c, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x14,
      0x8a, 0x8c, 0x00, 0x20, 0x00, 0x3c, 0x00, 0x20, 0xed, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x14, 0x02, 0x00, 0x00, 0x14,
      0x8a, 0x8f, 0x00, 0x20, 0x00, 0x3c, 0x00, 0x20, 0xd9, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x10,
  };
  const std::string expected_output {"\
Supported algorithms:\n\
1    AES-256-GCM-128\n\
     Key descriptors allowed, maximum 32 bytes\n\
     Raw decryption mode not allowed\n\
2    AES-256-CCM-128\n\
     Key descriptors allowed, fixed 32 bytes\n\
     Raw decryption mode allowed, raw read disabled by default\n"s};
  std::ostringstream oss;
  print_algorithms(oss, scsi::read_algorithms(
                            reinterpret_cast<const scsi::page_dec&>(page)));
  REQUIRE(oss.str() == expected_output);
}