forked from sakura-editor/sakura
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-cdecode.cpp
142 lines (119 loc) · 4.7 KB
/
test-cdecode.cpp
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
/*
Copyright (C) 2021-2022, Sakura Editor Organization
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "pch.h"
#include <cstdlib>
#include <string>
#include "convert/CDecode.h"
#include "convert/CDecode_Base64Decode.h"
#include "convert/CDecode_UuDecode.h"
struct DecoderTestCase {
const wchar_t* input;
const char* output;
};
TEST(CDecode, Base64)
{
const DecoderTestCase testCases[] = {
{L"", ""},
{L"cw==", "s"},
{L"c2E=", "sa"},
{L"c2Fr", "sak"},
{L"c2FrdQ==", "saku"},
{L"c2FrdXI=", "sakur"},
{L"c2FrdXJh", "sakura"}
};
CNativeW s;
CMemory m;
for (const auto& testCase : testCases) {
s.SetString(testCase.input);
EXPECT_TRUE(CDecode_Base64Decode().DoDecode(s, &m));
EXPECT_STREQ(reinterpret_cast<char*>(m.GetRawPtr()), testCase.output);
}
// 空白は無視する
s.SetString(L"c2Fr \t dQ==");
EXPECT_TRUE(CDecode_Base64Decode().DoDecode(s, &m));
EXPECT_STREQ(reinterpret_cast<char*>(m.GetRawPtr()), "saku");
// 異常な文字があったら変換を中止する
s.SetString(L"c2Fr?dQ==");
EXPECT_FALSE(CDecode_Base64Decode().DoDecode(s, &m));
}
TEST(CDecode, uuencode)
{
const DecoderTestCase testCases[] = {
{L"begin 666 test\r\n \r\nend\r\n", ""},
{L"begin 666 test\r\n!<P \r\n \r\nend\r\n", "s"},
{L"begin 666 test\r\n\"<V$ \r\n \r\nend\r\n", "sa"},
{L"begin 666 test\r\n#<V%K\r\n \r\nend\r\n", "sak"},
{L"begin 666 test\r\n$<V%K=0 \r\n \r\nend\r\n", "saku"},
{L"begin 666 test\r\n%<V%K=7( \r\n \r\nend\r\n", "sakur"},
{L"begin 666 test\r\n&<V%K=7)A\r\n \r\nend\r\n", "sakura"}
};
CNativeW s;
CMemory m;
for (const auto& testCase : testCases) {
s.SetString(testCase.input);
CDecode_UuDecode decoder;
EXPECT_TRUE(decoder.DoDecode(s, &m));
EXPECT_STREQ(reinterpret_cast<char*>(m.GetRawPtr()), testCase.output);
wchar_t fileName[_MAX_PATH];
decoder.CopyFilename(fileName);
EXPECT_STREQ(fileName, L"test");
}
// ヘッダーおよびフッターの先頭と末尾の空白は無視する
s.SetString(L"\tbegin 666 test \r\n!<P \r\n \r\n\tend \r\n");
EXPECT_TRUE(CDecode_UuDecode().DoDecode(s, &m));
EXPECT_STREQ(reinterpret_cast<char*>(m.GetRawPtr()), "s");
// 入力文字列が空の場合
s.SetString(L"");
EXPECT_FALSE(CDecode_UuDecode().DoDecode(s, &m));
// 文字列が begin で始まっていない場合
s.SetString(L"benign 666 test\r\n!<P \r\n \r\nend\r\n");
EXPECT_FALSE(CDecode_UuDecode().DoDecode(s, &m));
s.SetString(L"bigin 666 test\r\n!<P \r\n \r\nend\r\n");
EXPECT_FALSE(CDecode_UuDecode().DoDecode(s, &m));
// パーミッション設定が異常である場合
s.SetString(L"begin 66 test\r\n!<P \r\n \r\nend\r\n");
EXPECT_FALSE(CDecode_UuDecode().DoDecode(s, &m));
s.SetString(L"begin 888 test\r\n!<P \r\n \r\nend\r\n");
EXPECT_FALSE(CDecode_UuDecode().DoDecode(s, &m));
// ファイル名が指定されていない場合
s.SetString(L"begin 666\r\n!<P \r\n \r\nend\r\n");
EXPECT_FALSE(CDecode_UuDecode().DoDecode(s, &m));
// ファイル名の長さが_MAX_PATHを超える場合
std::wstring buffer;
for (int i = 0; i <= _MAX_PATH; ++i)
buffer.push_back(L'a');
s.SetString(L"begin 666 ");
s.AppendString(buffer.c_str());
s.AppendString(L"\r\n!<P \r\n \r\nend\r\n");
EXPECT_FALSE(CDecode_UuDecode().DoDecode(s, &m));
// 改行コードがCRLFではない場合
s.SetString(L"begin 666 test\n!<P \n \nend\n");
EXPECT_FALSE(CDecode_UuDecode().DoDecode(s, &m));
// 妥当なヘッダー行の後にEOSが現れた場合
s.SetString(L"begin 666 test");
EXPECT_FALSE(CDecode_UuDecode().DoDecode(s, &m));
// 文字列が end で終わっていない場合
s.SetString(L"begin 666 test\r\n!<P \r\n \r\nen");
EXPECT_FALSE(CDecode_UuDecode().DoDecode(s, &m));
s.SetString(L"begin 666 test\r\n!<P \r\n \r\nned\r\n");
EXPECT_FALSE(CDecode_UuDecode().DoDecode(s, &m));
// end の後ろに空白以外の文字がある場合
s.SetString(L"begin 666 test\r\n!<P \r\n \r\nendo\r\n");
EXPECT_FALSE(CDecode_UuDecode().DoDecode(s, &m));
}