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