<commit_before>/**
 * This file is part of the CernVM File System.
 */

#include <gtest/gtest.h>

#include <unistd.h>

#include "authz/authz.h"
#include "authz/authz_fetch.h"
#include "authz/authz_session.h"
#include "platform.h"
#include "statistics.h"


class TestAuthzFetcher : public AuthzFetcher {
 public:
  virtual AuthzStatus Fetch(
    const QueryInfo &query_info,
    AuthzToken *authz_token,
    unsigned *ttl)
  {
    *ttl = next_ttl;
    *authz_token = next_token;
    return next_status;
  }

  unsigned next_ttl;
  AuthzToken next_token;
  AuthzStatus next_status;
};


class T_AuthzSession : public ::testing::Test {
 protected:
  virtual void SetUp() {
    authz_session_mgr_ =
      AuthzSessionManager::Create(&authz_fetcher_, &statistics);
  }

  virtual void TearDown() {
    delete authz_session_mgr_;
  }

  AuthzSessionManager *authz_session_mgr_;
  TestAuthzFetcher authz_fetcher_;
  perf::Statistics statistics;
};


TEST_F(T_AuthzSession, GetPidInfo) {
  AuthzSessionManager::PidKey pid_key;
  EXPECT_FALSE(authz_session_mgr_->GetPidInfo(-1, &pid_key));

  EXPECT_TRUE(authz_session_mgr_->GetPidInfo(getpid(), &pid_key));
  EXPECT_EQ(getpid(), pid_key.pid);
  EXPECT_EQ(getuid(), pid_key.uid);
  EXPECT_EQ(getgid(), pid_key.gid);
  EXPECT_EQ(getsid(0), pid_key.sid);
  EXPECT_EQ(0U, pid_key.deadline);

  AuthzSessionManager::PidKey init_pid_key;
  EXPECT_TRUE(authz_session_mgr_->GetPidInfo(1, &init_pid_key));
  EXPECT_EQ(1, init_pid_key.pid);
  EXPECT_EQ(0U, init_pid_key.uid);
  EXPECT_EQ(0U, init_pid_key.gid);
  EXPECT_EQ(1, init_pid_key.sid);
  EXPECT_EQ(0U, init_pid_key.deadline);

  EXPECT_GE(pid_key.pid_bday, init_pid_key.pid_bday);
}


TEST_F(T_AuthzSession, LookupSessionKey) {
  uint64_t now = platform_monotonic_time();
  EXPECT_EQ(0U, authz_session_mgr_->pid2session_.size());
  AuthzSessionManager::SessionKey session_key;
  AuthzSessionManager::PidKey pid_key;
  EXPECT_FALSE(
    authz_session_mgr_->LookupSessionKey(-1, &pid_key, &session_key));

  EXPECT_TRUE(authz_session_mgr_->LookupSessionKey(
    getpid(), &pid_key, &session_key));
  EXPECT_EQ(getpid(), pid_key.pid);
  EXPECT_EQ(getsid(0), pid_key.sid);<commit_msg>fix authz unit test<commit_after>/**
 * This file is part of the CernVM File System.
 */

#include <gtest/gtest.h>

#include <unistd.h>

#include "authz/authz.h"
#include "authz/authz_fetch.h"
#include "authz/authz_session.h"
#include "platform.h"
#include "statistics.h"


class TestAuthzFetcher : public AuthzFetcher {
 public:
  virtual AuthzStatus Fetch(
    const QueryInfo &query_info,
    AuthzToken *authz_token,
    unsigned *ttl)
  {
    *ttl = next_ttl;
    *authz_token = next_token;
    return next_status;
  }

  unsigned next_ttl;
  AuthzToken next_token;
  AuthzStatus next_status;
};


class T_AuthzSession : public ::testing::Test {
 protected:
  virtual void SetUp() {
    authz_session_mgr_ =
      AuthzSessionManager::Create(&authz_fetcher_, &statistics);
  }

  virtual void TearDown() {
    delete authz_session_mgr_;
  }

  AuthzSessionManager *authz_session_mgr_;
  TestAuthzFetcher authz_fetcher_;
  perf::Statistics statistics;
};


TEST_F(T_AuthzSession, GetPidInfo) {
  AuthzSessionManager::PidKey pid_key;
  EXPECT_FALSE(authz_session_mgr_->GetPidInfo(-1, &pid_key));

  EXPECT_TRUE(authz_session_mgr_->GetPidInfo(getpid(), &pid_key));
  EXPECT_EQ(getpid(), pid_key.pid);
  EXPECT_EQ(getuid(), pid_key.uid);
  EXPECT_EQ(getgid(), pid_key.gid);
  EXPECT_EQ(getsid(0), pid_key.sid);
  EXPECT_EQ(0U, pid_key.deadline);

  AuthzSessionManager::PidKey init_pid_key;
  EXPECT_TRUE(authz_session_mgr_->GetPidInfo(1, &init_pid_key));
  EXPECT_EQ(1, init_pid_key.pid);
  // On docker, the following two lines are not necessarily true
  // EXPECT_EQ(0U, init_pid_key.uid);
  // EXPECT_EQ(0U, init_pid_key.gid);
  EXPECT_EQ(1, init_pid_key.sid);
  EXPECT_EQ(0U, init_pid_key.deadline);

  EXPECT_GE(pid_key.pid_bday, init_pid_key.pid_bday);
}


TEST_F(T_AuthzSession, LookupSessionKey) {
  uint64_t now = platform_monotonic_time();
  EXPECT_EQ(0U, authz_session_mgr_->pid2session_.size());
  AuthzSessionManager::SessionKey session_key;
  AuthzSessionManager::PidKey pid_key;
  EXPECT_FALSE(
    authz_session_mgr_->LookupSessionKey(-1, &pid_key, &session_key));

  EXPECT_TRUE(authz_session_mgr_->LookupSessionKey(
    getpid(), &pid_key, &session_key));
  EXPECT_EQ(getpid(), pid_key.pid);
  EXPECT_EQ(getsid(0), pid_key.sid);
{'fraction_non_alphanumeric': 0.12197329712604661, 'fraction_numerical': 0.006788866259334691, 'mean_word_length': 5.471449487554905, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7200823', 'n_tokens_mistral': 1724, 'n_tokens_neox': 1752, 'n_words': 272}