tor-browser

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

util_pkcs11uri_unittest.cc (6673B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=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 file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include <climits>
      8 #include <memory>
      9 #include "pkcs11uri.h"
     10 
     11 #include "gtest/gtest.h"
     12 #include "scoped_ptrs_util.h"
     13 
     14 namespace nss_test {
     15 
     16 class PK11URITest : public ::testing::Test {
     17 public:
     18  bool TestCreate(const PK11URIAttribute *pattrs, size_t num_pattrs,
     19                  const PK11URIAttribute *qattrs, size_t num_qattrs) {
     20    ScopedPK11URI tmp(
     21        PK11URI_CreateURI(pattrs, num_pattrs, qattrs, num_qattrs));
     22    return tmp != nullptr;
     23  }
     24 
     25  void TestCreateRetrieve(const PK11URIAttribute *pattrs, size_t num_pattrs,
     26                          const PK11URIAttribute *qattrs, size_t num_qattrs) {
     27    ScopedPK11URI tmp(
     28        PK11URI_CreateURI(pattrs, num_pattrs, qattrs, num_qattrs));
     29    ASSERT_TRUE(tmp);
     30 
     31    size_t i;
     32    for (i = 0; i < num_pattrs; i++) {
     33      const char *value = PK11URI_GetPathAttribute(tmp.get(), pattrs[i].name);
     34      EXPECT_TRUE(value);
     35      if (value) {
     36        EXPECT_EQ(std::string(value), std::string(pattrs[i].value));
     37      }
     38    }
     39    for (i = 0; i < num_qattrs; i++) {
     40      const char *value = PK11URI_GetQueryAttribute(tmp.get(), qattrs[i].name);
     41      EXPECT_TRUE(value);
     42      if (value) {
     43        EXPECT_EQ(std::string(value), std::string(qattrs[i].value));
     44      }
     45    }
     46  }
     47 
     48  void TestCreateFormat(const PK11URIAttribute *pattrs, size_t num_pattrs,
     49                        const PK11URIAttribute *qattrs, size_t num_qattrs,
     50                        const std::string &formatted) {
     51    ScopedPK11URI tmp(
     52        PK11URI_CreateURI(pattrs, num_pattrs, qattrs, num_qattrs));
     53    ASSERT_TRUE(tmp);
     54    char *out = PK11URI_FormatURI(nullptr, tmp.get());
     55    EXPECT_TRUE(out);
     56    if (out) {
     57      EXPECT_EQ(std::string(out), formatted);
     58    }
     59    PORT_Free(out);
     60  }
     61 
     62  bool TestParse(const std::string &str) {
     63    ScopedPK11URI tmp(PK11URI_ParseURI(str.c_str()));
     64    return tmp != nullptr;
     65  }
     66 
     67  void TestParseRetrieve(const std::string &str, const PK11URIAttribute *pattrs,
     68                         size_t num_pattrs, const PK11URIAttribute *qattrs,
     69                         size_t num_qattrs) {
     70    ScopedPK11URI tmp(PK11URI_ParseURI(str.c_str()));
     71    ASSERT_TRUE(tmp);
     72 
     73    size_t i;
     74    for (i = 0; i < num_pattrs; i++) {
     75      const char *value = PK11URI_GetPathAttribute(tmp.get(), pattrs[i].name);
     76      EXPECT_TRUE(value);
     77      if (value) {
     78        EXPECT_EQ(std::string(value), std::string(pattrs[i].value));
     79      }
     80    }
     81    for (i = 0; i < num_qattrs; i++) {
     82      const char *value = PK11URI_GetQueryAttribute(tmp.get(), qattrs[i].name);
     83      EXPECT_TRUE(value);
     84      if (value) {
     85        EXPECT_EQ(std::string(value), std::string(qattrs[i].value));
     86      }
     87    }
     88  }
     89 
     90  void TestParseFormat(const std::string &str, const std::string &formatted) {
     91    ScopedPK11URI tmp(PK11URI_ParseURI(str.c_str()));
     92    ASSERT_TRUE(tmp);
     93    char *out = PK11URI_FormatURI(nullptr, tmp.get());
     94    EXPECT_TRUE(out);
     95    if (out) {
     96      EXPECT_EQ(std::string(out), formatted);
     97      PORT_Free(out);
     98    }
     99  }
    100 
    101 protected:
    102 };
    103 
    104 const PK11URIAttribute pattrs[] = {
    105    {"token", "aaa"}, {"manufacturer", "bbb"}, {"vendor", "ccc"}};
    106 
    107 const PK11URIAttribute qattrs[] = {{"pin-source", "|grep foo /etc/passwd"},
    108                                   {"pin-value", "secret"},
    109                                   {"vendor", "ddd"}};
    110 
    111 const PK11URIAttribute pattrs_invalid[] = {{"token", "aaa"},
    112                                           {"manufacturer", "bbb"},
    113                                           {"vendor", "ccc"},
    114                                           {"$%*&", "invalid"},
    115                                           {"", "empty"}};
    116 
    117 const PK11URIAttribute qattrs_invalid[] = {
    118    {"pin-source", "|grep foo /etc/passwd"},
    119    {"pin-value", "secret"},
    120    {"vendor", "ddd"},
    121    {"$%*&", "invalid"},
    122    {"", "empty"}};
    123 
    124 TEST_F(PK11URITest, CreateTest) {
    125  EXPECT_TRUE(
    126      TestCreate(pattrs, PR_ARRAY_SIZE(pattrs), qattrs, PR_ARRAY_SIZE(qattrs)));
    127  EXPECT_FALSE(TestCreate(pattrs_invalid, PR_ARRAY_SIZE(pattrs_invalid), qattrs,
    128                          PR_ARRAY_SIZE(qattrs)));
    129  EXPECT_FALSE(TestCreate(pattrs, PR_ARRAY_SIZE(pattrs), qattrs_invalid,
    130                          PR_ARRAY_SIZE(qattrs_invalid)));
    131  EXPECT_FALSE(TestCreate(pattrs_invalid, PR_ARRAY_SIZE(pattrs_invalid),
    132                          qattrs_invalid, PR_ARRAY_SIZE(qattrs_invalid)));
    133 }
    134 
    135 TEST_F(PK11URITest, CreateRetrieveTest) {
    136  TestCreateRetrieve(pattrs, PR_ARRAY_SIZE(pattrs), qattrs,
    137                     PR_ARRAY_SIZE(qattrs));
    138 }
    139 
    140 TEST_F(PK11URITest, CreateFormatTest) {
    141  TestCreateFormat(pattrs, PR_ARRAY_SIZE(pattrs), qattrs, PR_ARRAY_SIZE(qattrs),
    142                   "pkcs11:token=aaa;manufacturer=bbb;vendor=ccc?pin-source=|"
    143                   "grep%20foo%20/etc/passwd&pin-value=secret&vendor=ddd");
    144 }
    145 
    146 TEST_F(PK11URITest, ParseTest) {
    147  EXPECT_FALSE(TestParse("pkcs11:token=aaa;token=bbb"));
    148  EXPECT_FALSE(TestParse("pkcs11:dup=aaa;dup=bbb"));
    149  EXPECT_FALSE(TestParse("pkcs11:?pin-value=aaa&pin-value=bbb"));
    150  EXPECT_FALSE(TestParse("pkcs11:=empty"));
    151  EXPECT_FALSE(TestParse("pkcs11:token=%2;manufacturer=aaa"));
    152 }
    153 
    154 TEST_F(PK11URITest, ParseRetrieveTest) {
    155  TestParseRetrieve(
    156      "pkcs11:token=aaa;manufacturer=bbb;vendor=ccc?pin-source=|"
    157      "grep%20foo%20/etc/passwd&pin-value=secret&vendor=ddd",
    158      pattrs, PR_ARRAY_SIZE(pattrs), qattrs, PR_ARRAY_SIZE(qattrs));
    159 }
    160 
    161 TEST_F(PK11URITest, ParseFormatTest) {
    162  TestParseFormat("pkcs11:", "pkcs11:");
    163  TestParseFormat("PKCS11:", "pkcs11:");
    164  TestParseFormat("pkcs11:token=aaa", "pkcs11:token=aaa");
    165  TestParseFormat("pkcs11:token=aaa;manufacturer=bbb",
    166                  "pkcs11:token=aaa;manufacturer=bbb");
    167  TestParseFormat("pkcs11:manufacturer=bbb;token=aaa",
    168                  "pkcs11:token=aaa;manufacturer=bbb");
    169  TestParseFormat("pkcs11:manufacturer=bbb;token=aaa;vendor2=ddd;vendor1=ccc",
    170                  "pkcs11:token=aaa;manufacturer=bbb;vendor1=ccc;vendor2=ddd");
    171  TestParseFormat("pkcs11:?pin-value=secret", "pkcs11:?pin-value=secret");
    172  TestParseFormat("pkcs11:?dup=aaa&dup=bbb", "pkcs11:?dup=aaa&dup=bbb");
    173  TestParseFormat(
    174      "pkcs11:?pin-source=|grep%20foo%20/etc/passwd&pin-value=secret",
    175      "pkcs11:?pin-source=|grep%20foo%20/etc/passwd&pin-value=secret");
    176  TestParseFormat("pkcs11:token=aaa?pin-value=secret",
    177                  "pkcs11:token=aaa?pin-value=secret");
    178 }
    179 
    180 }  // namespace nss_test