1 | /**
|
---|
2 | * OWASP Enterprise Security API (ESAPI)
|
---|
3 | *
|
---|
4 | * This file is part of the Open Web Application Security Project (OWASP)
|
---|
5 | * Enterprise Security API (ESAPI) project. For details, please see
|
---|
6 | * http://www.owasp.org/index.php/ESAPI.
|
---|
7 | *
|
---|
8 | * Copyright (c) 2011 - The OWASP Foundation
|
---|
9 | *
|
---|
10 | * @author Kevin Wall, [email protected]
|
---|
11 | * @author Jeffrey Walton, [email protected]
|
---|
12 | *
|
---|
13 | */
|
---|
14 |
|
---|
15 | /////////////////////////////////////////////////////////////
|
---|
16 | // Used by Windows. For Linux, Boost::Test provides main() //
|
---|
17 | /////////////////////////////////////////////////////////////
|
---|
18 |
|
---|
19 | #include "EsapiCommon.h"
|
---|
20 | using esapi::String;
|
---|
21 | using esapi::StringArray;
|
---|
22 | using esapi::StringStream;
|
---|
23 | using esapi::NarrowString;
|
---|
24 | using esapi::WideString;
|
---|
25 |
|
---|
26 | #include "errors/IllegalArgumentException.h"
|
---|
27 | using esapi::IllegalArgumentException;
|
---|
28 |
|
---|
29 | #include "errors/EncryptionException.h"
|
---|
30 | using esapi::EncryptionException;
|
---|
31 |
|
---|
32 | #include "errors/NoSuchAlgorithmException.h"
|
---|
33 | using esapi::NoSuchAlgorithmException;
|
---|
34 |
|
---|
35 | #include "crypto/SecureRandom.h"
|
---|
36 | using esapi::SecureRandom;
|
---|
37 |
|
---|
38 | #include "crypto/RandomPool.h"
|
---|
39 | using esapi::RandomPool;
|
---|
40 |
|
---|
41 | #include "crypto/KeyGenerator.h"
|
---|
42 | using esapi::KeyGenerator;
|
---|
43 |
|
---|
44 | #include "crypto/PlainText.h"
|
---|
45 | using esapi::PlainText;
|
---|
46 |
|
---|
47 | #include "crypto/CipherText.h"
|
---|
48 | using esapi::CipherText;
|
---|
49 |
|
---|
50 | #include "crypto/SecretKey.h"
|
---|
51 | using esapi::SecretKey;
|
---|
52 |
|
---|
53 | #include "crypto/MessageDigest.h"
|
---|
54 | using esapi::MessageDigest;
|
---|
55 |
|
---|
56 | #include "util/SecureArray.h"
|
---|
57 | using esapi::SecureByteArray;
|
---|
58 | using esapi::SecureIntArray;
|
---|
59 |
|
---|
60 | #include "util/SecureString.h"
|
---|
61 | using esapi::SecureString;
|
---|
62 |
|
---|
63 | #include "DummyConfiguration.h"
|
---|
64 | using esapi::DummyConfiguration;
|
---|
65 |
|
---|
66 | #include "reference/DefaultEncryptor.h"
|
---|
67 | using esapi::DefaultEncryptor;
|
---|
68 |
|
---|
69 | #include "util/TextConvert.h"
|
---|
70 | using esapi::TextConvert;
|
---|
71 |
|
---|
72 | #include "util/AlgorithmName.h"
|
---|
73 | using esapi::AlgorithmName;
|
---|
74 |
|
---|
75 | #include "crypto/Cipher.h"
|
---|
76 | using esapi::Cipher;
|
---|
77 |
|
---|
78 | #include "codecs/HTMLEntityCodec.h"
|
---|
79 | using esapi::HTMLEntityCodec;
|
---|
80 |
|
---|
81 | #include <iostream>
|
---|
82 | using std::cerr;
|
---|
83 | using std::cout;
|
---|
84 | using std::endl;
|
---|
85 |
|
---|
86 | #include <string>
|
---|
87 | using std::string;
|
---|
88 |
|
---|
89 | #include <cstddef>
|
---|
90 | #include <memory>
|
---|
91 | #include <string>
|
---|
92 |
|
---|
93 | static const WideString wide = L"\u9aa8";
|
---|
94 | static const NarrowString narrow("\xe9\xaa\xa8");
|
---|
95 |
|
---|
96 | int main(int, char**)
|
---|
97 | {
|
---|
98 | // Positive test - uses the overload which takes a 'Char' character
|
---|
99 | HTMLEntityCodec codec;
|
---|
100 |
|
---|
101 | struct KnownAnswer
|
---|
102 | {
|
---|
103 | int ch;
|
---|
104 | NarrowString str;
|
---|
105 | };
|
---|
106 |
|
---|
107 | // First and last 4 from entity table
|
---|
108 | const KnownAnswer tests[] = {
|
---|
109 | //{ 34, """ },
|
---|
110 | //{ 38, "&" },
|
---|
111 | //{ 60, "<" },
|
---|
112 | //{ 62, ">" },
|
---|
113 |
|
---|
114 | { 252, "ü" },
|
---|
115 | { 253, "ý" },
|
---|
116 | { 254, "þ" },
|
---|
117 | { 255, "ÿ" }
|
---|
118 | };
|
---|
119 |
|
---|
120 | StringArray immune;
|
---|
121 |
|
---|
122 | for( unsigned int i = 0; i < COUNTOF(tests); i++ )
|
---|
123 | {
|
---|
124 | const NarrowString utf8 = TextConvert::WideToNarrow(WideString(1,tests[i].ch));
|
---|
125 | const NarrowString encoded = codec.encodeCharacter( immune, utf8 );
|
---|
126 | const NarrowString expected = tests[i].str;
|
---|
127 |
|
---|
128 | StringStream oss;
|
---|
129 | oss << "Failed to encode character. Expected ";
|
---|
130 | oss << "'" << expected << "', got ";
|
---|
131 | oss << "'" << encoded << "'";
|
---|
132 |
|
---|
133 | if(!(encoded == expected)) {
|
---|
134 | cout << oss.str() << endl;
|
---|
135 | } else {
|
---|
136 | cout << "** WORKED: " << oss.str() << endl;
|
---|
137 | }
|
---|
138 |
|
---|
139 | }
|
---|
140 |
|
---|
141 | return 0;
|
---|
142 | }
|
---|