aboutsummaryrefslogtreecommitdiff
path: root/src/unittest/test_utilities.cpp
diff options
context:
space:
mode:
authorLars Müller <34514239+appgurueu@users.noreply.github.com>2021-05-30 20:23:12 +0200
committerGitHub <noreply@github.com>2021-05-30 20:23:12 +0200
commit89f3991351185b365ccd10525e74d35d7bb2da46 (patch)
treee82a77c9f34bd9e34adfe16c6d1a1fbffd289c5f /src/unittest/test_utilities.cpp
parent1bc753f655db4c7de030f2700361011e1c0278a5 (diff)
downloadminetest-89f3991351185b365ccd10525e74d35d7bb2da46.tar.gz
minetest-89f3991351185b365ccd10525e74d35d7bb2da46.tar.bz2
minetest-89f3991351185b365ccd10525e74d35d7bb2da46.zip
Fix base64 validation and add unittests (#10515)
Implement proper padding character checks
Diffstat (limited to 'src/unittest/test_utilities.cpp')
-rw-r--r--src/unittest/test_utilities.cpp93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/unittest/test_utilities.cpp b/src/unittest/test_utilities.cpp
index 93ba3f844..039110d54 100644
--- a/src/unittest/test_utilities.cpp
+++ b/src/unittest/test_utilities.cpp
@@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/enriched_string.h"
#include "util/numeric.h"
#include "util/string.h"
+#include "util/base64.h"
class TestUtilities : public TestBase {
public:
@@ -56,6 +57,7 @@ public:
void testMyround();
void testStringJoin();
void testEulerConversion();
+ void testBase64();
};
static TestUtilities g_test_instance;
@@ -87,6 +89,7 @@ void TestUtilities::runTests(IGameDef *gamedef)
TEST(testMyround);
TEST(testStringJoin);
TEST(testEulerConversion);
+ TEST(testBase64);
}
////////////////////////////////////////////////////////////////////////////////
@@ -537,3 +540,93 @@ void TestUtilities::testEulerConversion()
setPitchYawRoll(m2, v2);
UASSERT(within(m1, m2, tolL));
}
+
+void TestUtilities::testBase64()
+{
+ // Test character set
+ UASSERT(base64_is_valid("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "0123456789+/") == true);
+ UASSERT(base64_is_valid("/+9876543210"
+ "zyxwvutsrqponmlkjihgfedcba"
+ "ZYXWVUTSRQPONMLKJIHGFEDCBA") == true);
+ UASSERT(base64_is_valid("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "0123456789+.") == false);
+ UASSERT(base64_is_valid("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "0123456789 /") == false);
+
+ // Test empty string
+ UASSERT(base64_is_valid("") == true);
+
+ // Test different lengths, with and without padding,
+ // with correct and incorrect padding
+ UASSERT(base64_is_valid("A") == false);
+ UASSERT(base64_is_valid("AA") == true);
+ UASSERT(base64_is_valid("AAA") == true);
+ UASSERT(base64_is_valid("AAAA") == true);
+ UASSERT(base64_is_valid("AAAAA") == false);
+ UASSERT(base64_is_valid("AAAAAA") == true);
+ UASSERT(base64_is_valid("AAAAAAA") == true);
+ UASSERT(base64_is_valid("AAAAAAAA") == true);
+ UASSERT(base64_is_valid("A===") == false);
+ UASSERT(base64_is_valid("AA==") == true);
+ UASSERT(base64_is_valid("AAA=") == true);
+ UASSERT(base64_is_valid("AAAA") == true);
+ UASSERT(base64_is_valid("AAAA====") == false);
+ UASSERT(base64_is_valid("AAAAA===") == false);
+ UASSERT(base64_is_valid("AAAAAA==") == true);
+ UASSERT(base64_is_valid("AAAAAAA=") == true);
+ UASSERT(base64_is_valid("AAAAAAA==") == false);
+ UASSERT(base64_is_valid("AAAAAAA===") == false);
+ UASSERT(base64_is_valid("AAAAAAA====") == false);
+ UASSERT(base64_is_valid("AAAAAAAA") == true);
+ UASSERT(base64_is_valid("AAAAAAAA=") == false);
+ UASSERT(base64_is_valid("AAAAAAAA==") == false);
+ UASSERT(base64_is_valid("AAAAAAAA===") == false);
+ UASSERT(base64_is_valid("AAAAAAAA====") == false);
+
+ // Test if canonical encoding
+ // Last character limitations, length % 4 == 3
+ UASSERT(base64_is_valid("AAB") == false);
+ UASSERT(base64_is_valid("AAE") == true);
+ UASSERT(base64_is_valid("AAQ") == true);
+ UASSERT(base64_is_valid("AAB=") == false);
+ UASSERT(base64_is_valid("AAE=") == true);
+ UASSERT(base64_is_valid("AAQ=") == true);
+ UASSERT(base64_is_valid("AAAAAAB=") == false);
+ UASSERT(base64_is_valid("AAAAAAE=") == true);
+ UASSERT(base64_is_valid("AAAAAAQ=") == true);
+ // Last character limitations, length % 4 == 2
+ UASSERT(base64_is_valid("AB") == false);
+ UASSERT(base64_is_valid("AE") == false);
+ UASSERT(base64_is_valid("AQ") == true);
+ UASSERT(base64_is_valid("AB==") == false);
+ UASSERT(base64_is_valid("AE==") == false);
+ UASSERT(base64_is_valid("AQ==") == true);
+ UASSERT(base64_is_valid("AAAAAB==") == false);
+ UASSERT(base64_is_valid("AAAAAE==") == false);
+ UASSERT(base64_is_valid("AAAAAQ==") == true);
+
+ // Extraneous character present
+ UASSERT(base64_is_valid(".") == false);
+ UASSERT(base64_is_valid("A.") == false);
+ UASSERT(base64_is_valid("AA.") == false);
+ UASSERT(base64_is_valid("AAA.") == false);
+ UASSERT(base64_is_valid("AAAA.") == false);
+ UASSERT(base64_is_valid("AAAAA.") == false);
+ UASSERT(base64_is_valid("A.A") == false);
+ UASSERT(base64_is_valid("AA.A") == false);
+ UASSERT(base64_is_valid("AAA.A") == false);
+ UASSERT(base64_is_valid("AAAA.A") == false);
+ UASSERT(base64_is_valid("AAAAA.A") == false);
+ UASSERT(base64_is_valid("\xE1""AAA") == false);
+
+ // Padding in wrong position
+ UASSERT(base64_is_valid("A=A") == false);
+ UASSERT(base64_is_valid("AA=A") == false);
+ UASSERT(base64_is_valid("AAA=A") == false);
+ UASSERT(base64_is_valid("AAAA=A") == false);
+ UASSERT(base64_is_valid("AAAAA=A") == false);
+} \ No newline at end of file