<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.

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

using namespace std;  // So sue me

namespace pkcs11 {
namespace test {

TEST_F(PKCS11Test, ParallelSessionUnsupported) {
  // No CKF_SERIAL_SESSION => not supported
  CK_SESSION_HANDLE session;
  EXPECT_CKR(CKR_SESSION_PARALLEL_NOT_SUPPORTED, g_fns->C_OpenSession(g_slot_id, 0, NULL_PTR, NULL_PTR, &session));
}

TEST_F(ReadOnlySessionTest, SessionInfo) {
  CK_SESSION_INFO session_info;
  EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session_, &session_info));
  if (g_verbose) cout << session_info_description(&session_info) << endl;
  // PKCS#11 s6.7.1: When the session is initially opened, it is in [..] the "R/O Public Session" if the application has
  // no previously open sessions that are logged in
  EXPECT_EQ(CKS_RO_PUBLIC_SESSION, session_info.state);

  // Logging in changes the state.
  EXPECT_CKR_OK(g_fns->C_Login(session_, CKU_USER, (CK_UTF8CHAR_PTR)g_user_pin, strlen(g_user_pin)));
  EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session_, &session_info));
  EXPECT_EQ(CKS_RO_USER_FUNCTIONS, session_info.state);

  // Log out again
  EXPECT_CKR_OK(g_fns->C_Logout(session_));
  EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session_, &session_info));
  EXPECT_EQ(CKS_RO_PUBLIC_SESSION, session_info.state);
}

TEST_F(ReadWriteSessionTest, SessionInfo) {
  CK_SESSION_INFO session_info;
  EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session_, &session_info));
  if (g_verbose) cout << session_info_description(&session_info) << endl;
  // PKCS#11 s6.7.2: When the session is initially opened, it is in [..] the "R/W Public Session" if the application has
  // no previously open sessions that are logged in
  EXPECT_EQ(CKS_RW_PUBLIC_SESSION, session_info.state);

  // Logging in changes the state.
  EXPECT_CKR_OK(g_fns->C_Login(session_, CKU_USER, (CK_UTF8CHAR_PTR)g_user_pin, strlen(g_user_pin)));
  EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session_, &session_info));
  EXPECT_EQ(CKS_RW_USER_FUNCTIONS, session_info.state);

  // Log out again
  EXPECT_CKR_OK(g_fns->C_Logout(session_));
  EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session_, &session_info));
  EXPECT_EQ(CKS_RW_PUBLIC_SESSION, session_info.state);
}

TEST_F(ReadWriteSessionTest, GetSetOperationState) {
  CK_ULONG len;
  CK_RV rv = g_fns->C_GetOperationState(session_, NULL_PTR, &len);
  if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
    TEST_SKIPPED("GetOperationState not supported");
    return;
  }

  // No state => OPERATION_NOT_INITIALIZED
  EXPECT_CKR(CKR_OPERATION_NOT_INITIALIZED, rv);

  // Create some state
  vector<CK_ATTRIBUTE_TYPE> attrs({CKA_ENCRYPT, CKA_DECRYPT});
  SecretKey key(session_, attrs);

  CK_MECHANISM mechanism = {CKM_DES_ECB, NULL_PTR, 0};
  rv = g_fns->C_EncryptInit(session_, &mechanism, key.handle());
  ASSERT_CKR_OK(rv);

  rv = g_fns->C_GetOperationState(session_, NULL_PTR, &len);
  if (rv != CKR_STATE_UNSAVEABLE) {<commit_msg>Cope with alternative authentication paths<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.

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

using namespace std;  // So sue me

namespace pkcs11 {
namespace test {

TEST_F(PKCS11Test, ParallelSessionUnsupported) {
  // No CKF_SERIAL_SESSION => not supported
  CK_SESSION_HANDLE session;
  EXPECT_CKR(CKR_SESSION_PARALLEL_NOT_SUPPORTED, g_fns->C_OpenSession(g_slot_id, 0, NULL_PTR, NULL_PTR, &session));
}

TEST_F(ReadOnlySessionTest, SessionInfo) {
  CK_SESSION_INFO session_info;
  EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session_, &session_info));
  if (g_verbose) cout << session_info_description(&session_info) << endl;
  CK_STATE original_state = session_info.state;

  if (!(g_token_flags & CKF_PROTECTED_AUTHENTICATION_PATH)) {
    // PKCS#11 s6.7.1: When the session is initially opened, it is in [..] the "R/O Public Session" if the application
    // has no previously open sessions that are logged in
    EXPECT_EQ(CKS_RO_PUBLIC_SESSION, session_info.state);
  } else {
    // PKCS#11 s9.2: Token has a "protected authentication path", whereby a user can log into the token without passing
    // a PIN through the Cryptoki library, so initial state might be logged in.
  }

  // Logging in changes the state.
  EXPECT_CKR_OK(g_fns->C_Login(session_, CKU_USER, (CK_UTF8CHAR_PTR)g_user_pin, strlen(g_user_pin)));
  EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session_, &session_info));
  EXPECT_EQ(CKS_RO_USER_FUNCTIONS, session_info.state);

  // Log out again
  EXPECT_CKR_OK(g_fns->C_Logout(session_));
  EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session_, &session_info));
  EXPECT_EQ(original_state, session_info.state);
}

TEST_F(ReadWriteSessionTest, SessionInfo) {
  CK_SESSION_INFO session_info;
  EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session_, &session_info));
  if (g_verbose) cout << session_info_description(&session_info) << endl;
  CK_STATE original_state = session_info.state;
  if (!(g_token_flags & CKF_PROTECTED_AUTHENTICATION_PATH)) {
    // PKCS#11 s6.7.2: When the session is initially opened, it is in [..] the "R/W Public Session" if the application
    // has no previously open sessions that are logged in
    EXPECT_EQ(CKS_RW_PUBLIC_SESSION, session_info.state);
  } else {
    // PKCS#11 s9.2: Token has a "protected authentication path", whereby a user can log into the token without passing
    // a PIN through the Cryptoki library, so initial state might be logged in.
  }

  // Logging in changes the state.
  EXPECT_CKR_OK(g_fns->C_Login(session_, CKU_USER, (CK_UTF8CHAR_PTR)g_user_pin, strlen(g_user_pin)));
  EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session_, &session_info));
  EXPECT_EQ(CKS_RW_USER_FUNCTIONS, session_info.state);

  // Log out again
  EXPECT_CKR_OK(g_fns->C_Logout(session_));
  EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session_, &session_info));
  EXPECT_EQ(original_state, session_info.state);
}

TEST_F(ReadWriteSessionTest, GetSetOperationState) {
  CK_ULONG len;
  CK_RV rv = g_fns->C_GetOperationState(session_, NULL_PTR, &len);
  if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
    TEST_SKIPPED("GetOperationState not supported");
    return;
  }

  // No state => OPERATION_NOT_INITIALIZED
  EXPECT_CKR(CKR_OPERATION_NOT_INITIALIZED, rv);

  // Create some state
  vector<CK_ATTRIBUTE_TYPE> attrs({CKA_ENCRYPT, CKA_DECRYPT});
  SecretKey key(session_, attrs);

  CK_MECHANISM mechanism = {CKM_DES_ECB, NULL_PTR, 0};
  rv = g_fns->C_EncryptInit(session_, &mechanism, key.handle());
  ASSERT_CKR_OK(rv);

  rv = g_fns->C_GetOperationState(session_, NULL_PTR, &len);
  if (rv != CKR_STATE_UNSAVEABLE) {
{'fraction_non_alphanumeric': 0.1041231732776618, 'fraction_numerical': 0.009394572025052192, 'mean_word_length': 5.1124401913875595, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 39, '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': '6286540', 'n_tokens_mistral': 2743, 'n_tokens_neox': 2683, 'n_words': 646}