The Battle for Wesnoth  1.19.7+dev
test_addons.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2024
3  by Iris Morelle <shadowm2006@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-test"
17 
18 #include <boost/test/unit_test.hpp>
19 
20 #include "addon/validation.hpp"
21 
22 BOOST_AUTO_TEST_SUITE( addons )
23 
24 BOOST_AUTO_TEST_CASE( validation )
25 {
26  BOOST_CHECK( !addon_filename_legal("") );
27  BOOST_CHECK( !addon_filename_legal(".") );
28  BOOST_CHECK( !addon_filename_legal("..") );
29  BOOST_CHECK( !addon_filename_legal("invalid/slash") );
30  BOOST_CHECK( !addon_filename_legal("invalid\\backslash") );
31  BOOST_CHECK( !addon_filename_legal("invalid:colon") );
32  BOOST_CHECK( !addon_filename_legal("invalid~tilde") );
33  BOOST_CHECK( !addon_filename_legal("invalid/../parent") );
34 
35  std::vector<std::string> ddns = { "NUL", "CON", "AUX", "PRN", "CONIN$", "CONOUT$" };
36  for(unsigned i = 1; i < 10; ++i) {
37  ddns.emplace_back(std::string{"LPT"} + std::to_string(i));
38  ddns.emplace_back(std::string{"COM"} + std::to_string(i));
39  }
40 
41  for(const auto& name : ddns) {
42  BOOST_CHECK( addon_filename_legal("foo.bar." + name) );
43  BOOST_CHECK( addon_filename_legal("foo." + name + ".bar") );
44  BOOST_CHECK( !addon_filename_legal(name + ".foo.bar") );
45  BOOST_CHECK( !addon_filename_legal(name + ':') );
46  BOOST_CHECK( !addon_filename_legal(name) );
47  }
48 
49  BOOST_CHECK( addon_name_legal("-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz") );
50 
51  BOOST_CHECK( !addon_name_legal("invalid\nnewline") );
52  BOOST_CHECK( !addon_name_legal("invalid\x0A""explicitLF") );
53  BOOST_CHECK( !addon_name_legal("invalid\x0D\x0A""explicitCRLF") );
54  BOOST_CHECK( !addon_name_legal("invalid\x0D""explicitCR") );
55  BOOST_CHECK( !addon_name_legal("invalid`grave accent`") );
56  BOOST_CHECK( !addon_name_legal("invalid$dollarsign$") );
57 }
58 
60 {
61  BOOST_CHECK( encode_binary("").empty() );
62  BOOST_CHECK( unencode_binary("").empty() );
63 
64  //
65  // Plain string.
66  //
67 
68  const std::string plain = "ABC";
69 
70  BOOST_CHECK( encode_binary(plain) == plain );
71  BOOST_CHECK( unencode_binary(plain) == plain );
72 
73  //
74  // Binary escaping (direct).
75  //
76 
77  const char bin_escape = '\x01';
78  const std::string bin_special = "\x0D\xFE";
79  const std::string raw = "ABC \x01 DEF \x0D\x0A JKL \xFE MNO";
80 
81  std::string encoded;
82 
83  //
84  // The encoding algorithm as of 1.11.15 is as follows:
85  //
86  // * let c be the char to encode
87  // * let e be the escaping char (\x01)
88  // * if (c in \x00\x0D\xFE or c == e) then return e followed by the
89  // character with value c+1.
90  //
91  // There is no test for \x00 here because \x00 really shouldn't occur in
92  // a string -- otherwise things get weird.
93  //
94  for(const char c : raw)
95  {
96  if(c == bin_escape || bin_special.find(c) != std::string::npos) {
97  encoded += bin_escape;
98  encoded += (c + 1);
99  } else {
100  encoded += c;
101  }
102  }
103 
104  BOOST_CHECK( encode_binary(raw) == encoded );
105  BOOST_CHECK( unencode_binary(encoded) == raw );
106  // Identity.
107  BOOST_CHECK( unencode_binary(encode_binary(raw)) == raw );
108  BOOST_CHECK( unencode_binary(encode_binary(encoded)) == encoded );
109 
110  //
111  // Binary escaping (recursive).
112  //
113 
114  const unsigned recursive_steps = 16;
115  std::string recursive_encoded = raw;
116 
117  for(unsigned n = 0; n < recursive_steps; ++n) {
118  recursive_encoded = encode_binary(recursive_encoded);
119  }
120 
121  BOOST_CHECK( recursive_encoded != raw );
122 
123  for(unsigned n = 0; n < recursive_steps; ++n) {
124  recursive_encoded = unencode_binary(recursive_encoded);
125  }
126 
127  BOOST_CHECK( recursive_encoded == raw );
128 }
129 
130 BOOST_AUTO_TEST_SUITE_END()
std::size_t i
Definition: function.cpp:1029
BOOST_AUTO_TEST_CASE(validation)
Definition: test_addons.cpp:24
BOOST_AUTO_TEST_SUITE(filesystem)
mock_char c
static map_location::direction n
bool addon_name_legal(const std::string &name)
Checks whether an add-on id/name is legal or not.
Definition: validation.cpp:56
std::string unencode_binary(const std::string &str)
Definition: validation.cpp:240
bool addon_filename_legal(const std::string &name)
Checks whether an add-on file name is legal or not.
Definition: validation.cpp:66
std::string encode_binary(const std::string &str)
Definition: validation.cpp:222