summaryrefslogtreecommitdiffstats
path: root/security/nss/gtests/util_gtest/util_select_unittest.cc
blob: f62357c9b3f2c81798708a8502fd9c61562fd25b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "gtest/gtest.h"
#include "scoped_ptrs_util.h"

namespace nss_test {

class SelectTest : public ::testing::Test {
 protected:
  void test_select(std::vector<uint8_t> &dest, const std::vector<uint8_t> &src0,
                   const std::vector<uint8_t> &src1, unsigned char b) {
    EXPECT_EQ(src0.size(), src1.size());
    EXPECT_GE(dest.size(), src0.size());
    return NSS_SecureSelect(dest.data(), src0.data(), src1.data(), src0.size(),
                            b);
  };
};

TEST_F(SelectTest, TestSelectZero) {
  std::vector<uint8_t> dest(32, 255);
  std::vector<uint8_t> src0(32, 0);
  std::vector<uint8_t> src1(32, 1);
  test_select(dest, src0, src1, 0);
  EXPECT_EQ(dest, src0);
}

TEST_F(SelectTest, TestSelectOne) {
  std::vector<uint8_t> dest(32, 255);
  std::vector<uint8_t> src0(32, 0);
  std::vector<uint8_t> src1(32, 1);
  test_select(dest, src0, src1, 1);
  EXPECT_EQ(dest, src1);
}

TEST_F(SelectTest, TestSelect170) {
  std::vector<uint8_t> dest(32, 255);
  std::vector<uint8_t> src0(32, 0);
  std::vector<uint8_t> src1(32, 1);
  test_select(dest, src0, src1, 170);
  EXPECT_EQ(dest, src1);
}

TEST_F(SelectTest, TestSelect255) {
  std::vector<uint8_t> dest(32, 255);
  std::vector<uint8_t> src0(32, 0);
  std::vector<uint8_t> src1(32, 1);
  test_select(dest, src0, src1, 255);
  EXPECT_EQ(dest, src1);
}

}  // namespace nss_test