From 73b839892359c6df1930ebf7247bdd5f87336030 Mon Sep 17 00:00:00 2001 From: Chris Johnston Date: Sun, 3 Mar 2019 22:57:17 -0800 Subject: [PATCH] add tests for padding method, and for token that needs padding --- test/Discord.Net.Tests/Tests.TokenUtils.cs | 38 ++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/Discord.Net.Tests/Tests.TokenUtils.cs b/test/Discord.Net.Tests/Tests.TokenUtils.cs index 9a1102ec5..d9ed60ae8 100644 --- a/test/Discord.Net.Tests/Tests.TokenUtils.cs +++ b/test/Discord.Net.Tests/Tests.TokenUtils.cs @@ -77,6 +77,8 @@ namespace Discord // 59 char token [InlineData("MTk4NjIyNDgzNDcxOTI1MjQ4.Cl2FMQ.ZnCjm1XVW7vRze4b7Cq4se7kKWs")] [InlineData("MTk4NjIyNDgzNDcxOTI1MjQ4.Cl2FMQ.ZnCjm1XVW7vRze4b7Cq4se7kKWss")] + // simulated token with a very old user id + [InlineData("ODIzNjQ4MDEzNTAxMDcxMzY=.Cl2FMQ.ZnCjm1XVW7vRze4b7Cq4se7kKW")] public void TestBotTokenDoesNotThrowExceptions(string token) { // This example token is pulled from the Discord Docs @@ -151,6 +153,10 @@ namespace Discord // cannot pass a ulong? as a param in InlineData, so have to have a separate param // indicating if a value is null [InlineData("NDI4NDc3OTQ0MDA5MTk1NTIw", false, 428477944009195520)] + // user id that has base 64 '=' padding + [InlineData("ODIzNjQ4MDEzNTAxMDcxMzY=", false, 82364801350107136)] + // user id that does not have '=' padding, and needs it + [InlineData("ODIzNjQ4MDEzNTAxMDcxMzY", false, 82364801350107136)] // should return null w/o throwing other exceptions [InlineData("", true, 0)] [InlineData(" ", true, 0)] @@ -164,5 +170,37 @@ namespace Discord else Assert.Equal(expectedUserId, result); } + + [Theory] + [InlineData("QQ", "QQ==")] // "A" encoded + [InlineData("QUE", "QUE=")] // "AA" + [InlineData("QUFB", "QUFB")] // "AAA" + [InlineData("QUFBQQ", "QUFBQQ==")] // "AAAA" + [InlineData("QUFBQUFB", "QUFBQUFB")] // "AAAAAA" + // strings that already contain padding will be returned, even if invalid + [InlineData("QUFBQQ==", "QUFBQQ==")] + [InlineData("QUFBQQ=", "QUFBQQ=")] + [InlineData("=", "=")] + public void TestPadBase64String(string input, string expected) + { + Assert.Equal(expected, TokenUtils.PadBase64String(input)); + } + + [Theory] + // no null, empty, or whitespace + [InlineData("", typeof(ArgumentNullException))] + [InlineData(" ", typeof(ArgumentNullException))] + [InlineData("\t", typeof(ArgumentNullException))] + [InlineData(null, typeof(ArgumentNullException))] + // cannot require 3 padding chars + [InlineData("A", typeof(FormatException))] + [InlineData("QUFBQ", typeof(FormatException))] + public void TestPadBase64StringException(string input, Type type) + { + Assert.Throws(type, () => + { + TokenUtils.PadBase64String(input); + }); + } } }