aboutsummaryrefslogtreecommitdiff
path: root/lib/Utils/tests/VnEncodingTests.cs
blob: f1ef5f4a723e35c4806b53b555d837455a85d565 (plain)
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
/*
* 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.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()
        {
            
        }

        
    }
}