<commit_before>// Copyright 2013-2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// PKCS#11 s11.15: Random number generation functions
//   C_SeedRandom
//   C_GenerateRandom

#include <cstdlib>
#include "pkcs11test.h"

using namespace std;  // So sue me

namespace pkcs11 {
namespace test {

CK_BYTE seed[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};

TEST_F(ReadOnlySessionTest, SeedRandom) {
  CK_RV rv = g_fns->C_SeedRandom(session_, seed, sizeof(seed));
  if (g_token_flags & CKF_RNG) {
    EXPECT_TRUE(rv == CKR_OK ||
      rv == CKR_RANDOM_SEED_NOT_SUPPORTED) << " rv=" << CK_RV_(rv);
  } else {
    EXPECT_TRUE(rv == CKR_RANDOM_NO_RNG) << " rv=" << CK_RV_(rv);
  }
}

TEST(RNG, SeedRandomNoInit) {
  CK_RV rv = g_fns->C_SeedRandom(INVALID_SESSION_HANDLE, seed, sizeof(seed));
  if (g_token_flags & CKF_RNG) {
    EXPECT_TRUE(rv == CKR_CRYPTOKI_NOT_INITIALIZED ||
      rv == CKR_RANDOM_SEED_NOT_SUPPORTED) << " rv=" << CK_RV_(rv);
  } else {
    EXPECT_TRUE(rv == CKR_RANDOM_NO_RNG) << " rv=" << CK_RV_(rv);
  }
}

TEST_F(ReadOnlySessionTest, SeedRandomBadArguments) {
  CK_RV rv = g_fns->C_SeedRandom(session_, nullptr, 1);
  if (g_token_flags & CKF_RNG) {
    EXPECT_TRUE(rv == CKR_ARGUMENTS_BAD ||
      rv == CKR_RANDOM_SEED_NOT_SUPPORTED) << " rv=" << CK_RV_(rv);
  } else {
    EXPECT_TRUE(rv == CKR_RANDOM_NO_RNG) << " rv=" << CK_RV_(rv);
  }
}

TEST_F(PKCS11Test, SeedRandomNoSession) {
  CK_RV rv = g_fns->C_SeedRandom(INVALID_SESSION_HANDLE, seed, sizeof(seed));
  if (g_token_flags & CKF_RNG) {
    EXPECT_TRUE(rv == CKR_SESSION_HANDLE_INVALID ||
      rv == CKR_RANDOM_SEED_NOT_SUPPORTED) << " rv=" << CK_RV_(rv);
  } else {
    EXPECT_TRUE(rv == CKR_RANDOM_NO_RNG) << " rv=" << CK_RV_(rv);
  }
}

TEST_F(ReadOnlySessionTest, GenerateRandom) {
  CK_BYTE data[1024];
  if (g_token_flags & CKF_RNG) {
    EXPECT_CKR_OK(g_fns->C_GenerateRandom(session_, data, sizeof(data)));
    EXPECT_CKR_OK(g_fns->C_GenerateRandom(session_, data, 10));
    EXPECT_CKR_OK(g_fns->C_GenerateRandom(session_, data, 100));
    CK_BYTE data2[100];
    EXPECT_CKR_OK(g_fns->C_GenerateRandom(session_, data2, 100));
    EXPECT_NE(0, memcmp(data, data2, 100));
  } else {
    EXPECT_CKR(CKR_RANDOM_NO_RNG, g_fns->C_GenerateRandom(session_, data, sizeof(data)));
  }
}

TEST(RNG, GenerateRandomNoInit) {
  CK_BYTE data[8];
  EXPECT_CKR(CKR_CRYPTOKI_NOT_INITIALIZED,
    g_fns->C_GenerateRandom(INVALID_SESSION_HANDLE, data, sizeof(data)));
}

TEST_F(ReadOnlySessionTest, GenerateRandomBadArguments) {
  EXPECT_CKR(CKR_ARGUMENTS_BAD, g_fns->C_GenerateRandom(session_, nullptr, 1));
}

TEST_F(ReadOnlySessionTest, GenerateRandomNone) {
  CK_BYTE data[64];
  if (g_token_flags & CKF_RNG) {
    CK_RV rv = g_fns->C_GenerateRandom(session_, data, 0);
    EXPECT_TRUE(rv == CKR_OK || rv == CKR_ARGUMENTS_BAD) << CK_RV_(rv);
  }
}

TEST_F(PKCS11Test, GenerateRandomNoSession) {
  CK_BYTE data[16];
  EXPECT_CKR(CKR_SESSION_HANDLE_INVALID,
    g_fns->C_GenerateRandom(INVALID_SESSION_HANDLE, data, sizeof(data)));
}

}  // namespace test
}  // namespace pkcs11
<commit_msg>rng: GenerateRandom* test for ROUserSessionTest.<commit_after>// Copyright 2013-2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// PKCS#11 s11.15: Random number generation functions
//   C_SeedRandom
//   C_GenerateRandom

#include <cstdlib>
#include "pkcs11test.h"

using namespace std;  // So sue me

namespace pkcs11 {
namespace test {

CK_BYTE seed[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};

TEST_F(ReadOnlySessionTest, SeedRandom) {
  CK_RV rv = g_fns->C_SeedRandom(session_, seed, sizeof(seed));
  if (g_token_flags & CKF_RNG) {
    EXPECT_TRUE(rv == CKR_OK ||
      rv == CKR_RANDOM_SEED_NOT_SUPPORTED) << " rv=" << CK_RV_(rv);
  } else {
    EXPECT_TRUE(rv == CKR_RANDOM_NO_RNG) << " rv=" << CK_RV_(rv);
  }
}

TEST(RNG, SeedRandomNoInit) {
  CK_RV rv = g_fns->C_SeedRandom(INVALID_SESSION_HANDLE, seed, sizeof(seed));
  if (g_token_flags & CKF_RNG) {
    EXPECT_TRUE(rv == CKR_CRYPTOKI_NOT_INITIALIZED ||
      rv == CKR_RANDOM_SEED_NOT_SUPPORTED) << " rv=" << CK_RV_(rv);
  } else {
    EXPECT_TRUE(rv == CKR_RANDOM_NO_RNG) << " rv=" << CK_RV_(rv);
  }
}

TEST_F(ReadOnlySessionTest, SeedRandomBadArguments) {
  CK_RV rv = g_fns->C_SeedRandom(session_, nullptr, 1);
  if (g_token_flags & CKF_RNG) {
    EXPECT_TRUE(rv == CKR_ARGUMENTS_BAD ||
      rv == CKR_RANDOM_SEED_NOT_SUPPORTED) << " rv=" << CK_RV_(rv);
  } else {
    EXPECT_TRUE(rv == CKR_RANDOM_NO_RNG) << " rv=" << CK_RV_(rv);
  }
}

TEST_F(PKCS11Test, SeedRandomNoSession) {
  CK_RV rv = g_fns->C_SeedRandom(INVALID_SESSION_HANDLE, seed, sizeof(seed));
  if (g_token_flags & CKF_RNG) {
    EXPECT_TRUE(rv == CKR_SESSION_HANDLE_INVALID ||
      rv == CKR_RANDOM_SEED_NOT_SUPPORTED) << " rv=" << CK_RV_(rv);
  } else {
    EXPECT_TRUE(rv == CKR_RANDOM_NO_RNG) << " rv=" << CK_RV_(rv);
  }
}

void GenerateRandom(CK_SESSION_HANDLE session) {
  CK_BYTE data[1024];
  if (g_token_flags & CKF_RNG) {
    EXPECT_CKR_OK(g_fns->C_GenerateRandom(session, data, sizeof(data)));
    EXPECT_CKR_OK(g_fns->C_GenerateRandom(session, data, 10));
    EXPECT_CKR_OK(g_fns->C_GenerateRandom(session, data, 100));
    CK_BYTE data2[100];
    EXPECT_CKR_OK(g_fns->C_GenerateRandom(session, data2, 100));
    EXPECT_NE(0, memcmp(data, data2, 100));
  } else {
    EXPECT_CKR(CKR_RANDOM_NO_RNG, g_fns->C_GenerateRandom(session, data, sizeof(data)));
  }
}

TEST_F(ReadOnlySessionTest, GenerateRandom) {
  GenerateRandom(session_);
}

TEST_F(ROUserSessionTest, GenerateRandom) {
  GenerateRandom(session_);
}

TEST(RNG, GenerateRandomNoInit) {
  CK_BYTE data[8];
  EXPECT_CKR(CKR_CRYPTOKI_NOT_INITIALIZED,
    g_fns->C_GenerateRandom(INVALID_SESSION_HANDLE, data, sizeof(data)));
}

void GenerateRandomBadArguments(CK_SESSION_HANDLE session) {
  EXPECT_CKR(CKR_ARGUMENTS_BAD, g_fns->C_GenerateRandom(session, nullptr, 1));
}

TEST_F(ReadOnlySessionTest, GenerateRandomBadArguments) {
  GenerateRandomBadArguments(session_);
}

TEST_F(ROUserSessionTest, GenerateRandomBadArguments) {
  GenerateRandomBadArguments(session_);
}

void GenerateRandomNone(CK_SESSION_HANDLE session) {
  CK_BYTE data[64];
  if (g_token_flags & CKF_RNG) {
    CK_RV rv = g_fns->C_GenerateRandom(session, data, 0);
    EXPECT_TRUE(rv == CKR_OK || rv == CKR_ARGUMENTS_BAD) << CK_RV_(rv);
  }
}

TEST_F(ReadOnlySessionTest, GenerateRandomNone) {
  GenerateRandomNone(session_);
}

TEST_F(ROUserSessionTest, GenerateRandomNone) {
  GenerateRandomNone(session_);
}

TEST_F(PKCS11Test, GenerateRandomNoSession) {
  CK_BYTE data[16];
  EXPECT_CKR(CKR_SESSION_HANDLE_INVALID,
    g_fns->C_GenerateRandom(INVALID_SESSION_HANDLE, data, sizeof(data)));
}

}  // namespace test
}  // namespace pkcs11

{'fraction_non_alphanumeric': 0.12854393842206543, 'fraction_numerical': 0.02129570237331623, 'mean_word_length': 4.7876763177431325, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1278972', 'n_tokens_mistral': 3109, 'n_tokens_neox': 2976, 'n_words': 575}