From 19d95519c74d140b7e5857b7a6f85a064530227c Mon Sep 17 00:00:00 2001 From: Alexey Orlenko Date: Wed, 5 Apr 2017 17:05:55 +0300 Subject: [PATCH] test: add basic cctest for base64.h This commit adds C++ tests for `base64_encode()` and `base64_decode()` functions defined in `base64.h`. The functionality is already being tested indirectly in JavaScript tests for Buffer, but it won't hurt to test the low-level functions too, especially given that they aren't only used in the internal Buffer implementation, Chrome inspector protocol support relies upon them too. PR-URL: https://github.com/nodejs/node/pull/12238 Refs: https://github.com/nodejs/node/pull/12146#issuecomment-291559685 Reviewed-By: James M Snell Reviewed-By: Richard Lau Reviewed-By: Daniel Bevenius --- node.gyp | 1 + test/cctest/test_base64.cc | 127 +++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 test/cctest/test_base64.cc diff --git a/node.gyp b/node.gyp index 9035c585c320e4..8bf26e276b2648 100644 --- a/node.gyp +++ b/node.gyp @@ -603,6 +603,7 @@ ], 'sources': [ + 'test/cctest/test_base64.cc', 'test/cctest/test_util.cc', ], diff --git a/test/cctest/test_base64.cc b/test/cctest/test_base64.cc new file mode 100644 index 00000000000000..fbdb969b4cbe46 --- /dev/null +++ b/test/cctest/test_base64.cc @@ -0,0 +1,127 @@ +#include "base64.h" + +#include +#include + +#include "gtest/gtest.h" + +using node::base64_encode; +using node::base64_decode; + +TEST(Base64Test, Encode) { + auto test = [](const char* string, const char* base64_string) { + const size_t len = strlen(base64_string); + char* const buffer = new char[len + 1]; + buffer[len] = 0; + base64_encode(string, strlen(string), buffer, len); + EXPECT_STREQ(base64_string, buffer); + delete[] buffer; + }; + + test("a", "YQ=="); + test("ab", "YWI="); + test("abc", "YWJj"); + test("abcd", "YWJjZA=="); + test("abcde", "YWJjZGU="); + test("abcdef", "YWJjZGVm"); + + test("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do " + "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut " + "enim ad minim veniam, quis nostrud exercitation ullamco laboris " + "nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in " + "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla " + "pariatur. Excepteur sint occaecat cupidatat non proident, sunt in " + "culpa qui officia deserunt mollit anim id est laborum.", + + "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2Npbmcg" + "ZWxpdCwgc2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFib3JlIGV0" + "IGRvbG9yZSBtYWduYSBhbGlxdWEuIFV0IGVuaW0gYWQgbWluaW0gdmVuaWFtLCBxdWlz" + "IG5vc3RydWQgZXhlcmNpdGF0aW9uIHVsbGFtY28gbGFib3JpcyBuaXNpIHV0IGFsaXF1" + "aXAgZXggZWEgY29tbW9kbyBjb25zZXF1YXQuIER1aXMgYXV0ZSBpcnVyZSBkb2xvciBp" + "biByZXByZWhlbmRlcml0IGluIHZvbHVwdGF0ZSB2ZWxpdCBlc3NlIGNpbGx1bSBkb2xv" + "cmUgZXUgZnVnaWF0IG51bGxhIHBhcmlhdHVyLiBFeGNlcHRldXIgc2ludCBvY2NhZWNh" + "dCBjdXBpZGF0YXQgbm9uIHByb2lkZW50LCBzdW50IGluIGN1bHBhIHF1aSBvZmZpY2lh" + "IGRlc2VydW50IG1vbGxpdCBhbmltIGlkIGVzdCBsYWJvcnVtLg=="); +} + +TEST(Base64Test, Decode) { + auto test = [](const char* base64_string, const char* string) { + const size_t len = strlen(string); + char* const buffer = new char[len + 1]; + buffer[len] = 0; + base64_decode(buffer, len, base64_string, strlen(base64_string)); + EXPECT_STREQ(string, buffer); + delete[] buffer; + }; + + test("YQ", "a"); + test("Y Q", "a"); + test("Y Q ", "a"); + test(" Y Q", "a"); + test("Y Q==", "a"); + test("YQ ==", "a"); + test("YQ == junk", "a"); + test("YWI", "ab"); + test("YWI=", "ab"); + test("YWJj", "abc"); + test("YWJjZA", "abcd"); + test("YWJjZA==", "abcd"); + test("YW Jj ZA ==", "abcd"); + test("YWJjZGU=", "abcde"); + test("YWJjZGVm", "abcdef"); + test("Y WJjZGVm", "abcdef"); + test("YW JjZGVm", "abcdef"); + test("YWJ jZGVm", "abcdef"); + test("YWJj ZGVm", "abcdef"); + test("Y W J j Z G V m", "abcdef"); + test("Y W\n JjZ \nG Vm", "abcdef"); + + const char* text = + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do " + "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut " + "enim ad minim veniam, quis nostrud exercitation ullamco laboris " + "nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in " + "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla " + "pariatur. Excepteur sint occaecat cupidatat non proident, sunt in " + "culpa qui officia deserunt mollit anim id est laborum."; + + test("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2Npbmcg" + "ZWxpdCwgc2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFib3JlIGV0" + "IGRvbG9yZSBtYWduYSBhbGlxdWEuIFV0IGVuaW0gYWQgbWluaW0gdmVuaWFtLCBxdWlz" + "IG5vc3RydWQgZXhlcmNpdGF0aW9uIHVsbGFtY28gbGFib3JpcyBuaXNpIHV0IGFsaXF1" + "aXAgZXggZWEgY29tbW9kbyBjb25zZXF1YXQuIER1aXMgYXV0ZSBpcnVyZSBkb2xvciBp" + "biByZXByZWhlbmRlcml0IGluIHZvbHVwdGF0ZSB2ZWxpdCBlc3NlIGNpbGx1bSBkb2xv" + "cmUgZXUgZnVnaWF0IG51bGxhIHBhcmlhdHVyLiBFeGNlcHRldXIgc2ludCBvY2NhZWNh" + "dCBjdXBpZGF0YXQgbm9uIHByb2lkZW50LCBzdW50IGluIGN1bHBhIHF1aSBvZmZpY2lh" + "IGRlc2VydW50IG1vbGxpdCBhbmltIGlkIGVzdCBsYWJvcnVtLg==", text); + + test("TG9yZW0gaXBzdW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