11cb0ef41Sopenharmony_ci#include "base64-inl.h" 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci#include <cstddef> 41cb0ef41Sopenharmony_ci#include <cstring> 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ci#include "gtest/gtest.h" 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciusing node::base64_decode; 91cb0ef41Sopenharmony_ciusing node::base64_encode; 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ciTEST(Base64Test, Encode) { 121cb0ef41Sopenharmony_ci auto test = [](const char* string, const char* base64_string) { 131cb0ef41Sopenharmony_ci const size_t len = strlen(base64_string); 141cb0ef41Sopenharmony_ci char* const buffer = new char[len + 1]; 151cb0ef41Sopenharmony_ci buffer[len] = 0; 161cb0ef41Sopenharmony_ci base64_encode(string, strlen(string), buffer, len); 171cb0ef41Sopenharmony_ci EXPECT_STREQ(base64_string, buffer); 181cb0ef41Sopenharmony_ci delete[] buffer; 191cb0ef41Sopenharmony_ci }; 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci test("a", "YQ=="); 221cb0ef41Sopenharmony_ci test("ab", "YWI="); 231cb0ef41Sopenharmony_ci test("abc", "YWJj"); 241cb0ef41Sopenharmony_ci test("abcd", "YWJjZA=="); 251cb0ef41Sopenharmony_ci test("abcde", "YWJjZGU="); 261cb0ef41Sopenharmony_ci test("abcdef", "YWJjZGVm"); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci test("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do " 291cb0ef41Sopenharmony_ci "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut " 301cb0ef41Sopenharmony_ci "enim ad minim veniam, quis nostrud exercitation ullamco laboris " 311cb0ef41Sopenharmony_ci "nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in " 321cb0ef41Sopenharmony_ci "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla " 331cb0ef41Sopenharmony_ci "pariatur. Excepteur sint occaecat cupidatat non proident, sunt in " 341cb0ef41Sopenharmony_ci "culpa qui officia deserunt mollit anim id est laborum.", 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2Npbmcg" 371cb0ef41Sopenharmony_ci "ZWxpdCwgc2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFib3JlIGV0" 381cb0ef41Sopenharmony_ci "IGRvbG9yZSBtYWduYSBhbGlxdWEuIFV0IGVuaW0gYWQgbWluaW0gdmVuaWFtLCBxdWlz" 391cb0ef41Sopenharmony_ci "IG5vc3RydWQgZXhlcmNpdGF0aW9uIHVsbGFtY28gbGFib3JpcyBuaXNpIHV0IGFsaXF1" 401cb0ef41Sopenharmony_ci "aXAgZXggZWEgY29tbW9kbyBjb25zZXF1YXQuIER1aXMgYXV0ZSBpcnVyZSBkb2xvciBp" 411cb0ef41Sopenharmony_ci "biByZXByZWhlbmRlcml0IGluIHZvbHVwdGF0ZSB2ZWxpdCBlc3NlIGNpbGx1bSBkb2xv" 421cb0ef41Sopenharmony_ci "cmUgZXUgZnVnaWF0IG51bGxhIHBhcmlhdHVyLiBFeGNlcHRldXIgc2ludCBvY2NhZWNh" 431cb0ef41Sopenharmony_ci "dCBjdXBpZGF0YXQgbm9uIHByb2lkZW50LCBzdW50IGluIGN1bHBhIHF1aSBvZmZpY2lh" 441cb0ef41Sopenharmony_ci "IGRlc2VydW50IG1vbGxpdCBhbmltIGlkIGVzdCBsYWJvcnVtLg=="); 451cb0ef41Sopenharmony_ci} 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ciTEST(Base64Test, EncodeURL) { 481cb0ef41Sopenharmony_ci auto test = [](const char* string, const char* base64_string) { 491cb0ef41Sopenharmony_ci const size_t len = strlen(base64_string); 501cb0ef41Sopenharmony_ci char* const buffer = new char[len + 1]; 511cb0ef41Sopenharmony_ci buffer[len] = 0; 521cb0ef41Sopenharmony_ci base64_encode(string, strlen(string), buffer, len, node::Base64Mode::URL); 531cb0ef41Sopenharmony_ci EXPECT_STREQ(base64_string, buffer); 541cb0ef41Sopenharmony_ci delete[] buffer; 551cb0ef41Sopenharmony_ci }; 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci test("\x68\xd9\x16\x25\x5c\x1e\x40\x92\x2d\xfb", "aNkWJVweQJIt-w"); 581cb0ef41Sopenharmony_ci test("\xac\xc7\x93\xaa\x83\x6f\xc3\xe3\x3f\x75", "rMeTqoNvw-M_dQ"); 591cb0ef41Sopenharmony_ci} 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ciTEST(Base64Test, Decode) { 621cb0ef41Sopenharmony_ci auto test = [](const char* base64_string, const char* string) { 631cb0ef41Sopenharmony_ci const size_t len = strlen(string); 641cb0ef41Sopenharmony_ci char* const buffer = new char[len + 1]; 651cb0ef41Sopenharmony_ci buffer[len] = 0; 661cb0ef41Sopenharmony_ci base64_decode(buffer, len, base64_string, strlen(base64_string)); 671cb0ef41Sopenharmony_ci EXPECT_STREQ(string, buffer); 681cb0ef41Sopenharmony_ci delete[] buffer; 691cb0ef41Sopenharmony_ci }; 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci test("YQ", "a"); 721cb0ef41Sopenharmony_ci test("Y Q", "a"); 731cb0ef41Sopenharmony_ci test("Y Q ", "a"); 741cb0ef41Sopenharmony_ci test(" Y Q", "a"); 751cb0ef41Sopenharmony_ci test("Y Q==", "a"); 761cb0ef41Sopenharmony_ci test("YQ ==", "a"); 771cb0ef41Sopenharmony_ci test("YQ == junk", "a"); 781cb0ef41Sopenharmony_ci test("YWI", "ab"); 791cb0ef41Sopenharmony_ci test("YWI=", "ab"); 801cb0ef41Sopenharmony_ci test("YWJj", "abc"); 811cb0ef41Sopenharmony_ci test("YWJjZA", "abcd"); 821cb0ef41Sopenharmony_ci test("YWJjZA==", "abcd"); 831cb0ef41Sopenharmony_ci test("YW Jj ZA ==", "abcd"); 841cb0ef41Sopenharmony_ci test("YWJjZGU=", "abcde"); 851cb0ef41Sopenharmony_ci test("YWJjZGVm", "abcdef"); 861cb0ef41Sopenharmony_ci test("Y WJjZGVm", "abcdef"); 871cb0ef41Sopenharmony_ci test("YW JjZGVm", "abcdef"); 881cb0ef41Sopenharmony_ci test("YWJ jZGVm", "abcdef"); 891cb0ef41Sopenharmony_ci test("YWJj ZGVm", "abcdef"); 901cb0ef41Sopenharmony_ci test("Y W J j Z G V m", "abcdef"); 911cb0ef41Sopenharmony_ci test("Y W\n JjZ \nG Vm", "abcdef"); 921cb0ef41Sopenharmony_ci test("rMeTqoNvw-M_dQ", "\xac\xc7\x93\xaa\x83\x6f\xc3\xe3\x3f\x75"); 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci const char* text = 951cb0ef41Sopenharmony_ci "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do " 961cb0ef41Sopenharmony_ci "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut " 971cb0ef41Sopenharmony_ci "enim ad minim veniam, quis nostrud exercitation ullamco laboris " 981cb0ef41Sopenharmony_ci "nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in " 991cb0ef41Sopenharmony_ci "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla " 1001cb0ef41Sopenharmony_ci "pariatur. Excepteur sint occaecat cupidatat non proident, sunt in " 1011cb0ef41Sopenharmony_ci "culpa qui officia deserunt mollit anim id est laborum."; 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ci test("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2Npbmcg" 1041cb0ef41Sopenharmony_ci "ZWxpdCwgc2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFib3JlIGV0" 1051cb0ef41Sopenharmony_ci "IGRvbG9yZSBtYWduYSBhbGlxdWEuIFV0IGVuaW0gYWQgbWluaW0gdmVuaWFtLCBxdWlz" 1061cb0ef41Sopenharmony_ci "IG5vc3RydWQgZXhlcmNpdGF0aW9uIHVsbGFtY28gbGFib3JpcyBuaXNpIHV0IGFsaXF1" 1071cb0ef41Sopenharmony_ci "aXAgZXggZWEgY29tbW9kbyBjb25zZXF1YXQuIER1aXMgYXV0ZSBpcnVyZSBkb2xvciBp" 1081cb0ef41Sopenharmony_ci "biByZXByZWhlbmRlcml0IGluIHZvbHVwdGF0ZSB2ZWxpdCBlc3NlIGNpbGx1bSBkb2xv" 1091cb0ef41Sopenharmony_ci "cmUgZXUgZnVnaWF0IG51bGxhIHBhcmlhdHVyLiBFeGNlcHRldXIgc2ludCBvY2NhZWNh" 1101cb0ef41Sopenharmony_ci "dCBjdXBpZGF0YXQgbm9uIHByb2lkZW50LCBzdW50IGluIGN1bHBhIHF1aSBvZmZpY2lh" 1111cb0ef41Sopenharmony_ci "IGRlc2VydW50IG1vbGxpdCBhbmltIGlkIGVzdCBsYWJvcnVtLg==", text); 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci test("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2Npbmcg" 1141cb0ef41Sopenharmony_ci "ZWxpdCwgc2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFib3JlIGV0" 1151cb0ef41Sopenharmony_ci "IGRvbG9yZSBtYWduYSBhbGlxdWEuIFV0IGVuaW0gYWQgbWluaW0gdmVuaWFtLCBxdWlz" 1161cb0ef41Sopenharmony_ci "IG5vc3RydWQgZXhlcmNpdGF0aW9uIHVsbGFtY28gbGFib3JpcyBuaXNpIHV0IGFsaXF1" 1171cb0ef41Sopenharmony_ci "aXAgZXggZWEgY29tbW9kbyBjb25zZXF1YXQuIER1aXMgYXV0ZSBpcnVyZSBkb2xvciBp" 1181cb0ef41Sopenharmony_ci "biByZXByZWhlbmRlcml0IGluIHZvbHVwdGF0ZSB2ZWxpdCBlc3NlIGNpbGx1bSBkb2xv" 1191cb0ef41Sopenharmony_ci "cmUgZXUgZnVnaWF0IG51bGxhIHBhcmlhdHVyLiBFeGNlcHRldXIgc2ludCBvY2NhZWNh" 1201cb0ef41Sopenharmony_ci "dCBjdXBpZGF0YXQgbm9uIHByb2lkZW50LCBzdW50IGluIGN1bHBhIHF1aSBvZmZpY2lh" 1211cb0ef41Sopenharmony_ci "IGRlc2VydW50IG1vbGxpdCBhbmltIGlkIGVzdCBsYWJvcnVtLg", text); 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ci test("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2Npbmcg\n" 1241cb0ef41Sopenharmony_ci "ZWxpdCwgc2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFib3JlIGV0\n" 1251cb0ef41Sopenharmony_ci "IGRvbG9yZSBtYWduYSBhbGlxdWEuIFV0IGVuaW0gYWQgbWluaW0gdmVuaWFtLCBxdWlz\n" 1261cb0ef41Sopenharmony_ci "IG5vc3RydWQgZXhlcmNpdGF0aW9uIHVsbGFtY28gbGFib3JpcyBuaXNpIHV0IGFsaXF1\n" 1271cb0ef41Sopenharmony_ci "aXAgZXggZWEgY29tbW9kbyBjb25zZXF1YXQuIER1aXMgYXV0ZSBpcnVyZSBkb2xvciBp\n" 1281cb0ef41Sopenharmony_ci "biByZXByZWhlbmRlcml0IGluIHZvbHVwdGF0ZSB2ZWxpdCBlc3NlIGNpbGx1bSBkb2xv\n" 1291cb0ef41Sopenharmony_ci "cmUgZXUgZnVnaWF0IG51bGxhIHBhcmlhdHVyLiBFeGNlcHRldXIgc2ludCBvY2NhZWNh\n" 1301cb0ef41Sopenharmony_ci "dCBjdXBpZGF0YXQgbm9uIHByb2lkZW50LCBzdW50IGluIGN1bHBhIHF1aSBvZmZpY2lh\n" 1311cb0ef41Sopenharmony_ci "IGRlc2VydW50IG1vbGxpdCBhbmltIGlkIGVzdCBsYWJvcnVtLg==", text); 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_ci test("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2Npbmcg\n" 1341cb0ef41Sopenharmony_ci "ZWxpdCwgc2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFib3JlIGV0\n" 1351cb0ef41Sopenharmony_ci "IGRvbG9yZSBtYWduYSBhbGlxdWEuIFV0IGVuaW0gYWQgbWluaW0gdmVuaWFtLCBxdWlz\n" 1361cb0ef41Sopenharmony_ci "IG5vc3RydWQgZXhlcmNpdGF0aW9uIHVsbGFtY28gbGFib3JpcyBuaXNpIHV0IGFsaXF1\n" 1371cb0ef41Sopenharmony_ci "aXAgZXggZWEgY29tbW9kbyBjb25zZXF1YXQuIER1aXMgYXV0ZSBpcnVyZSBkb2xvciBp\n" 1381cb0ef41Sopenharmony_ci "biByZXByZWhlbmRlcml0IGluIHZvbHVwdGF0ZSB2ZWxpdCBlc3NlIGNpbGx1bSBkb2xv\n" 1391cb0ef41Sopenharmony_ci "cmUgZXUgZnVnaWF0IG51bGxhIHBhcmlhdHVyLiBFeGNlcHRldXIgc2ludCBvY2NhZWNh\n" 1401cb0ef41Sopenharmony_ci "dCBjdXBpZGF0YXQgbm9uIHByb2lkZW50LCBzdW50IGluIGN1bHBhIHF1aSBvZmZpY2lh\n" 1411cb0ef41Sopenharmony_ci "IGRlc2VydW50IG1vbGxpdCBhbmltIGlkIGVzdCBsYWJvcnVtLg", text); 1421cb0ef41Sopenharmony_ci} 143