<commit_before>
require 'test/unit'
require 'rbdaemon/daemon'
require 'tempfile'

class TC_Daemon < Test::Unit::TestCase
  def test_smoke
    assert_equal(0, Process.uid)
    tmpfile = ::Tempfile.new('tc_daemon', '/tmp')
    File::chmod(0666, tmpfile.path)
    Process.fork do 
      RBDaemon::Daemon.new({:user => 'nobody', :groups => 'nobody',
                            :change_root => '/tmp'},
                           3, "ok\n", tmpfile.path) do |daemon, times, str, path|
        tmpfile = File.new(File.basename(path), 'w')
        times.times do
          tmpfile.write(str)
        end
        tmpfile.flush
        loop do
          sleep(3)
        end
      end
    end
    # let the daemon process run
    sleep(1)

    # check pid
    pid_file = "/var/run/#{File.basename($0).split(/\./)[0]}.pid"
    assert(File.exist?(pid_file))
    pid = IO.read(pid_file).rstrip.to_i
    assert_nothing_raised{Process::kill(0, pid)}
    proc_root = "/proc/#{pid}"
    status = {}
    IO.foreach("#{proc_root}/status") do |l|
      k, v = l.rstrip.split(/:\s*/)
      status[k] = v
    end
    assert_equal(pid, status['Pid'].to_i)

    stat = IO.read("#{proc_root}/stat").rstrip.split(/\s+/)
    assert_equal(pid, stat[0].to_i)
    # the current ppid should 1
    assert_equal(1, stat[3].to_i)
    # must not be the group leader
    assert_not_equal(pid, stat[4].to_i)
    # a different session id
    assert_not_equal(IO.read("/proc/self/stat").rstrip.split(/\s+/)[5], stat[5])
    # pgid == sid
    assert_equal(stat[4], stat[5])

    # check daemon's work
    lines = IO.readlines(tmpfile.path)
    assert_equal(3, lines.length)
    assert_equal("ok\n", lines[0])

    # check uid and gid
    uid = Etc.getpwnam('nobody').uid
    gid = Etc.getgrnam('nobody').gid
    status['Uid'].split(/\s+/).each do |s|
      assert_equal(uid, s.to_i)<commit_msg>Use daemon.daemon instead of nobody.nobody for testing<commit_after>
require 'test/unit'
require 'rbdaemon/daemon'
require 'tempfile'

class TC_Daemon < Test::Unit::TestCase
  def test_smoke
    assert_equal(0, Process.uid)
    tmpfile = ::Tempfile.new('tc_daemon', '/tmp')
    File::chmod(0666, tmpfile.path)
    Process.fork do 
      RBDaemon::Daemon.new({:user => 'daemon', :groups => ['daemon'],
                            :change_root => '/tmp'},
                           3, "ok\n", tmpfile.path) do |daemon, times, str, path|
        tmpfile = File.new(File.basename(path), 'w')
        times.times do
          tmpfile.write(str)
        end
        tmpfile.flush
        loop do
          sleep(3)
        end
      end
    end
    # let the daemon process run
    sleep(1)

    # check pid
    pid_file = "/var/run/#{File.basename($0).split(/\./)[0]}.pid"
    assert(File.exist?(pid_file))
    pid = IO.read(pid_file).rstrip.to_i
    assert_nothing_raised{Process::kill(0, pid)}
    proc_root = "/proc/#{pid}"
    status = {}
    IO.foreach("#{proc_root}/status") do |l|
      k, v = l.rstrip.split(/:\s*/)
      status[k] = v
    end
    assert_equal(pid, status['Pid'].to_i)

    stat = IO.read("#{proc_root}/stat").rstrip.split(/\s+/)
    assert_equal(pid, stat[0].to_i)
    # the current ppid should 1
    assert_equal(1, stat[3].to_i)
    # must not be the group leader
    assert_not_equal(pid, stat[4].to_i)
    # a different session id
    assert_not_equal(IO.read("/proc/self/stat").rstrip.split(/\s+/)[5], stat[5])
    # pgid == sid
    assert_equal(stat[4], stat[5])

    # check daemon's work
    lines = IO.readlines(tmpfile.path)
    assert_equal(3, lines.length)
    assert_equal("ok\n", lines[0])

    # check uid and gid
    uid = Etc.getpwnam('daemon').uid
    gid = Etc.getgrnam('daemon').gid
    status['Uid'].split(/\s+/).each do |s|
      assert_equal(uid, s.to_i)
{'fraction_non_alphanumeric': 0.16458055925432757, 'fraction_numerical': 0.011717709720372836, 'mean_word_length': 2.526760563380282, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '6465796', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1362, 'n_words': 263}