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
143
144
145
146
147
148
149
150
151
152
153
|
/*
* Copyright (c) 2023 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.UtilsTests
* File: VnEncodingTests.cs
*
* VnEncodingTests.cs is part of VNLib.UtilsTests which is part of the larger
* VNLib collection of libraries and utilities.
*
* VNLib.UtilsTests is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 2 of the License,
* or (at your option) any later version.
*
* VNLib.UtilsTests is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VNLib.UtilsTests. If not, see http://www.gnu.org/licenses/.
*/
using System;
using System.Linq;
using System.Text;
using System.Buffers;
using System.Diagnostics;
using System.Buffers.Text;
using System.Security.Cryptography;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace VNLib.Utils.Tests
{
[TestClass()]
public class VnEncodingTests
{
[TestMethod()]
public void Base64ToUrlSafeInPlaceTest()
{
//Get randomd data to encode
byte[] dataToEncode = RandomNumberGenerator.GetBytes(64);
//Calc buffer size
int base64Output = Base64.GetMaxEncodedToUtf8Length(64);
byte[] encodeBuffer = new byte[base64Output];
//Base64 encode
OperationStatus status = Base64.EncodeToUtf8(dataToEncode, encodeBuffer, out _, out int bytesEncoded, true);
Assert.IsTrue(status == OperationStatus.Done);
Span<byte> encodeSpan = encodeBuffer.AsSpan(0, bytesEncoded);
//Make sure some illegal characters are encoded
Assert.IsTrue(encodeSpan.Contains((byte)'+') || encodeSpan.Contains((byte)'/'));
//Convert to url safe
VnEncoding.Base64ToUrlSafeInPlace(encodeSpan);
//Make sure the illegal characters are gone
Assert.IsFalse(encodeSpan.Contains((byte)'+') || encodeSpan.Contains((byte)'/'));
}
[TestMethod()]
public void Base64FromUrlSafeInPlaceTest()
{
//url safe base64 with known encoded characters
const string base64UrlSafe = "lZUABUd8q2BS7p8giysuC7PpEabAFBnMqBPL-9A-qgfR1lbTHQ4tMm8E8nimm2YAd5NGDIQ0vxfU9i5l53tF_WXa_H4vkHfzlv0Df-lLADJV7z8sn-8sfUGdaAiIS8_4OmVGnnY4-TppLMsVR6ov2t07HdOHPPsFFhSpBMXa2pwRveRATcxBA2XxVe09FOWgahhssNS7lU9eC7fRw7icD4ZoJcLSRBbxrjRmeVXKhPIaXR-4mnQ5-vqYzAr9S99CthgbAtVn_WjmDcda6pUB9JW9lp7ylDa9e1r_z39cihTXMOGaUSjVURJaWrNF8CkfW56_x2ODCBmZPov1YyEhww==";
//Convert to utf8 binary
byte[] utf8 = Encoding.UTF8.GetBytes(base64UrlSafe);
//url decode
VnEncoding.Base64FromUrlSafeInPlace(utf8);
//Confirm illegal chars have been converted back to base64
Assert.IsFalse(utf8.Contains((byte)'_') || utf8.Contains((byte)'-'));
//Decode in place to confrim its valid
OperationStatus status = Base64.DecodeFromUtf8InPlace(utf8, out int bytesWritten);
Assert.IsFalse(status == OperationStatus.NeedMoreData);
Assert.IsFalse(status == OperationStatus.DestinationTooSmall);
Assert.IsFalse(status == OperationStatus.InvalidData);
}
[TestMethod()]
public void TryToBase64CharsTest()
{
}
[TestMethod()]
public void PercentEncodeTest()
{
const string urlEnoded = "https%3A%2F%2Fwww.google.com%2Fsearch%3Fq%3Dtest%26oq%3Dtest%26aqs%3Dchrome..69i57j0l7.1001j0j7%26sourceid%3Dchrome%26ie%3DUTF-8";
const string urlDecoded = "https://www.google.com/search?q=test&oq=test&aqs=chrome..69i57j0l7.1001j0j7&sourceid=chrome&ie=UTF-8";
//We need to allow the '.' character to be encoded
ReadOnlySpan<byte> allowedChars = Encoding.UTF8.GetBytes(".");
/*
* Test that the url encoded string is the same as the percent encoded string
*/
ReadOnlySpan<byte> utf8Encoded = Encoding.UTF8.GetBytes(urlDecoded);
string percentEncoded = VnEncoding.PercentEncode(utf8Encoded, allowedChars);
Assert.IsTrue(percentEncoded.Equals(urlEnoded, StringComparison.Ordinal));
/*
* Test decoding the percent encoded string
*/
ReadOnlySpan<byte> percentEncodedUtf8 = Encoding.UTF8.GetBytes(urlEnoded);
byte[] outBuffer = new byte[percentEncodedUtf8.Length];
ERRNO decoded = VnEncoding.PercentDecode(percentEncodedUtf8, outBuffer);
//Make sure result is valid
Debug.Assert(decoded > 0);
string decodedString = Encoding.UTF8.GetString(outBuffer, 0, decoded);
Assert.IsTrue(decodedString.Equals(urlDecoded, StringComparison.Ordinal));
}
[TestMethod()]
public void Base32BasicEncodeDecodeTest()
{
const string base32Encoded = "JBSWY3DPEBLW64TMMQQQ====";
const string base32Decoded = "Hello World!";
byte[] rawBytes = Encoding.UTF8.GetBytes(base32Decoded);
//Recover bytes from base32 encoded string
byte[]? fromString = VnEncoding.FromBase32String(base32Encoded);
Assert.IsNotNull(fromString);
//Test that the decoded bytes are the same as the raw bytes
Assert.IsTrue(rawBytes.SequenceEqual(fromString));
//Test that the encoded string is the same as the base32 encoded string
string toString = VnEncoding.ToBase32String(rawBytes, true);
Assert.IsTrue(toString.Equals(base32Encoded, StringComparison.Ordinal));
}
}
}
|