tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestClearKeyUtils.cpp (2545B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include <stdint.h>
      8 
      9 #include "../ClearKeyBase64.cpp"
     10 #include "gtest/gtest.h"
     11 
     12 using namespace std;
     13 
     14 struct B64Test {
     15  string b64;
     16  vector<uint8_t> raw;
     17  bool shouldPass;
     18 };
     19 
     20 MOZ_RUNINIT const B64Test tests[] = {
     21    {"AAAAADk4AU4AAAAAAAAAAA",
     22     {0x0, 0x0, 0x0, 0x0, 0x39, 0x38, 0x1, 0x4e, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
     23      0x0, 0x0},
     24     true},
     25    {"h2mqp1zAJjDIC34YXEXBxA==",
     26     {0x87, 0x69, 0xaa, 0xa7, 0x5c, 0xc0, 0x26, 0x30, 0xc8, 0xb, 0x7e, 0x18,
     27      0x5c, 0x45, 0xc1, 0xc4},
     28     true},
     29    {"flcdA35XHQN-Vx0DflcdAw",
     30     {0x7e, 0x57, 0x1d, 0x3, 0x7e, 0x57, 0x1d, 0x3, 0x7e, 0x57, 0x1d, 0x3, 0x7e,
     31      0x57, 0x1d, 0x3},
     32     true},
     33    {
     34        "flczM35XMzN-VzMzflczMw",
     35        {0x7e, 0x57, 0x33, 0x33, 0x7e, 0x57, 0x33, 0x33, 0x7e, 0x57, 0x33, 0x33,
     36         0x7e, 0x57, 0x33, 0x33},
     37        true,
     38    },
     39    {"flcdBH5XHQR-Vx0EflcdBA",
     40     {0x7e, 0x57, 0x1d, 0x4, 0x7e, 0x57, 0x1d, 0x4, 0x7e, 0x57, 0x1d, 0x4, 0x7e,
     41      0x57, 0x1d, 0x4},
     42     true},
     43    {"fldERH5XRER-V0REfldERA",
     44     {0x7e, 0x57, 0x44, 0x44, 0x7e, 0x57, 0x44, 0x44, 0x7e, 0x57, 0x44, 0x44,
     45      0x7e, 0x57, 0x44, 0x44},
     46     true},
     47    {"fuzzbiz=", {0x7e, 0xec, 0xf3, 0x6e, 0x2c}, true},
     48    {"fuzzbizfuzzbizfuzzbizfuzzbizfuzzbizfuzzbizfuzzbizfuzzbiz",
     49     {0x7e, 0xec, 0xf3, 0x6e, 0x2c, 0xdf, 0xbb, 0x3c, 0xdb, 0x8b, 0x37,
     50      0xee, 0xcf, 0x36, 0xe2, 0xcd, 0xfb, 0xb3, 0xcd, 0xb8, 0xb3, 0x7e,
     51      0xec, 0xf3, 0x6e, 0x2c, 0xdf, 0xbb, 0x3c, 0xdb, 0x8b, 0x37, 0xee,
     52      0xcf, 0x36, 0xe2, 0xcd, 0xfb, 0xb3, 0xcd, 0xb8, 0xb3},
     53     true},
     54    {"", {}, true},
     55    {"00", {0xd3}, true},
     56    {"000", {0xd3, 0x4d}, true},
     57 
     58    {"invalid", {0x8a, 0x7b, 0xda, 0x96, 0x27}, true},
     59    {"invalic", {0x8a, 0x7b, 0xda, 0x96, 0x27}, true},
     60 
     61    // Failure tests
     62    {"A", {}, false},  // 1 character is too few.
     63    {"_", {}, false},  // 1 character is too few.
     64 };
     65 
     66 TEST(ClearKey, DecodeBase64)
     67 {
     68  for (const B64Test& test : tests) {
     69    vector<uint8_t> v;
     70    bool rv = DecodeBase64(string(test.b64), v);
     71    EXPECT_EQ(test.shouldPass, rv);
     72    if (test.shouldPass) {
     73      EXPECT_EQ(test.raw.size(), v.size());
     74      for (size_t k = 0; k < test.raw.size(); k++) {
     75        EXPECT_EQ(test.raw[k], v[k]);
     76      }
     77    }
     78  }
     79 }