The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_addons.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2016 by Ignacio Riquelme Morelle <[email protected]>
3  Part of the Battle for Wesnoth Project http://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  BOOST_CHECK( addon_name_legal("-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz") );
35 
36  BOOST_CHECK( !addon_name_legal("invalid\nnewline") );
37  BOOST_CHECK( !addon_name_legal("invalid\x0A""explicitLF") );
38  BOOST_CHECK( !addon_name_legal("invalid\x0D\x0A""explicitCRLF") );
39  BOOST_CHECK( !addon_name_legal("invalid\x0D""explicitCR") );
40  BOOST_CHECK( !addon_name_legal("invalid`grave accent`") );
41  BOOST_CHECK( !addon_name_legal("invalid$dollarsign$") );
42 }
43 
45 {
46  BOOST_CHECK( encode_binary("") == "" );
47  BOOST_CHECK( unencode_binary("") == "" );
48 
49  //
50  // Plain string.
51  //
52 
53  const std::string plain = "ABC";
54 
55  BOOST_CHECK( encode_binary(plain) == plain );
56  BOOST_CHECK( unencode_binary(plain) == plain );
57 
58  //
59  // Binary escaping (direct).
60  //
61 
62  const char bin_escape = '\x01';
63  const std::string bin_special = "\x0D\xFE";
64  const std::string raw = "ABC \x01 DEF \x0D\x0A JKL \xFE MNO";
65 
66  std::string encoded;
67 
68  //
69  // The encoding algorithm as of 1.11.15 is as follows:
70  //
71  // * let c be the char to encode
72  // * let e be the escaping char (\x01)
73  // * if (c in \x00\x0D\xFE or c == e) then return e followed by the
74  // character with value c+1.
75  //
76  // There is no test for \x00 here because \x00 really shouldn't occur in
77  // a string -- otherwise things get weird.
78  //
79  for(const char c : raw)
80  {
81  if(c == bin_escape || bin_special.find(c) != std::string::npos) {
82  encoded += bin_escape;
83  encoded += (c + 1);
84  } else {
85  encoded += c;
86  }
87  }
88 
89  BOOST_CHECK( encode_binary(raw) == encoded );
90  BOOST_CHECK( unencode_binary(encoded) == raw );
91  // Identity.
92  BOOST_CHECK( unencode_binary(encode_binary(raw)) == raw );
93  BOOST_CHECK( unencode_binary(encode_binary(encoded)) == encoded );
94 
95  //
96  // Binary escaping (recursive).
97  //
98 
99  const unsigned recursive_steps = 16;
100  std::string recursive_encoded = raw;
101 
102  for(unsigned n = 0; n < recursive_steps; ++n) {
103  recursive_encoded = encode_binary(recursive_encoded);
104  }
105 
106  BOOST_CHECK( recursive_encoded != raw );
107 
108  for(unsigned n = 0; n < recursive_steps; ++n) {
109  recursive_encoded = unencode_binary(recursive_encoded);
110  }
111 
112  BOOST_CHECK( recursive_encoded == raw );
113 }
114 
115 BOOST_AUTO_TEST_SUITE_END()
const GLfloat * c
Definition: glew.h:12741
std::string encode_binary(const std::string &str)
Definition: validation.cpp:120
std::string unencode_binary(const std::string &str)
Definition: validation.cpp:138
BOOST_AUTO_TEST_SUITE(test_map_location)
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:48
GLclampd n
Definition: glew.h:5903
#define c
Definition: glew.h:12743
GLsizei const GLcharARB ** string
Definition: glew.h:4503
bool addon_filename_legal(const std::string &name)
Checks whether an add-on file name is legal or not.
Definition: validation.cpp:59