// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Mobius Forensic Toolkit
// Copyright (C) 2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018 Eduardo Aguiar
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#include <mobius/unittest.h>
#include <mobius/imagefile/imagefile.h>

static void
testcase_imagefile_null ()
{
  mobius::unittest test ("mobius::imagefile::imagefile (null)");
  
  mobius::imagefile::imagefile imagefile;

  // url
  test.ASSERT_EQUAL (imagefile.get_url (), std::string ());

  // get_type
  ASSERT_EXCEPTION (test, imagefile.get_type (), std::runtime_error);

  // get_size
  ASSERT_EXCEPTION (test, imagefile.get_size (), std::runtime_error);

  // get_sectors  
  ASSERT_EXCEPTION (test, imagefile.get_sectors (), std::runtime_error);

  // get_sector_size
  ASSERT_EXCEPTION (test, imagefile.get_sector_size (), std::runtime_error);

  // get_segments
  ASSERT_EXCEPTION (test, imagefile.get_segments (), std::runtime_error);

  // get_segment_sizr
  ASSERT_EXCEPTION (test, imagefile.get_segment_size (), std::runtime_error);

  // get_drive_model
  ASSERT_EXCEPTION (test, imagefile.get_drive_model (), std::runtime_error);

  // get_drive_serial_number
  ASSERT_EXCEPTION (test, imagefile.get_drive_serial_number (), std::runtime_error);

  // get_hash_md5
  ASSERT_EXCEPTION (test, imagefile.get_hash_md5 (), std::runtime_error);

  // get_acquisition_user
  ASSERT_EXCEPTION (test, imagefile.get_acquisition_user (), std::runtime_error);

  // get_acquisition_time
  ASSERT_EXCEPTION (test, imagefile.get_acquisition_time (), std::runtime_error);
  
  // get_acquisition_tool
  ASSERT_EXCEPTION (test, imagefile.get_acquisition_tool (), std::runtime_error);

  // get_acquisition_platform
  ASSERT_EXCEPTION (test, imagefile.get_acquisition_platform (), std::runtime_error);

  // new_reader
  ASSERT_EXCEPTION (test, imagefile.new_reader (), std::runtime_error);

  test.end ();
}

static void
testcase_imagefile_raw ()
{
  mobius::unittest test ("mobius::imagefile::imagefile (raw)");
  
  std::string url = "file:///etc/hosts";
  mobius::imagefile::imagefile imagefile (url);

  // url
  test.ASSERT_EQUAL (imagefile.get_url (), url);

  // get_type
  test.ASSERT_EQUAL (imagefile.get_type (), "raw");

  // get_size
  test.ASSERT_NOT_EQUAL (imagefile.get_size (), 0);

  // get_sectors  
  test.ASSERT_NOT_EQUAL (imagefile.get_sectors (), 0);

  // get_sector_size
  test.ASSERT_EQUAL (imagefile.get_sector_size (), 512);

  // get_segments
  test.ASSERT_EQUAL (imagefile.get_segments (), 1);

  // get_segment_size
  test.ASSERT_EQUAL (imagefile.get_segment_size (), imagefile.get_size ());

  // get_drive_model
  test.ASSERT_EQUAL (imagefile.get_drive_model (), std::string ());

  // get_drive_serial_number
  test.ASSERT_EQUAL (imagefile.get_drive_serial_number (), std::string ());

  // get_hash_md5
  test.ASSERT_EQUAL (imagefile.get_hash_md5 (), std::string ());

  // get_acquisition_user
  test.ASSERT_NOT_EQUAL (imagefile.get_acquisition_user (), std::string ());

  // get_acquisition_time
  test.ASSERT_TRUE (imagefile.get_acquisition_time ());
  
  // get_acquisition_tool
  test.ASSERT_EQUAL (imagefile.get_acquisition_tool (), std::string ());

  // get_acquisition_platform
  test.ASSERT_EQUAL (imagefile.get_acquisition_platform (), std::string ());

  // new_reader
  auto reader = imagefile.new_reader ();
  auto data = reader.read (2);
  test.ASSERT_EQUAL (data.size (), 2);

  test.end ();
}

static void
testcase_imagefile_split ()
{
  mobius::unittest test ("mobius::imagefile::imagefile (split)");
  const std::string url = "file:///tmp/unittest.001";

  // create imagefile
  {
    mobius::imagefile::imagefile imagefile (url, "split");
    imagefile.set_segment_size (100000);

    auto writer = imagefile.new_writer ();
    writer.seek (100);
    writer.write ("abc\r\n");

    writer.seek (0);
    writer.write ("def");

    writer.seek (99998);
    writer.write ("1234");

    writer.seek (3);
    writer.write ("ghi");
  }
  
  // read imagefile created
  mobius::imagefile::imagefile imagefile (url);

  // url
  test.ASSERT_EQUAL (imagefile.get_url (), url);

  // get_type
  test.ASSERT_EQUAL (imagefile.get_type (), "split");

  // get_size
  test.ASSERT_EQUAL (imagefile.get_size (), 100002);

  // get_sectors  
  test.ASSERT_EQUAL (imagefile.get_sectors (), 196);

  // get_sector_size
  test.ASSERT_EQUAL (imagefile.get_sector_size (), 512);

  // get_segments
  test.ASSERT_EQUAL (imagefile.get_segments (), 2);

  // get_segment_size
  test.ASSERT_EQUAL (imagefile.get_segment_size (), 100000);

  // get_drive_model
  test.ASSERT_EQUAL (imagefile.get_drive_model (), std::string ());

  // get_drive_serial_number
  test.ASSERT_EQUAL (imagefile.get_drive_serial_number (), std::string ());

  // get_hash_md5
  test.ASSERT_EQUAL (imagefile.get_hash_md5 (), std::string ());

  // get_acquisition_user
  test.ASSERT_NOT_EQUAL (imagefile.get_acquisition_user (), std::string ());

  // get_acquisition_time
  test.ASSERT_TRUE (imagefile.get_acquisition_time ());
  
  // get_acquisition_tool
  test.ASSERT_EQUAL (imagefile.get_acquisition_tool (), std::string ());

  // get_acquisition_platform
  test.ASSERT_EQUAL (imagefile.get_acquisition_platform (), std::string ());

  // data
  auto reader = imagefile.new_reader ();

  mobius::bytearray b1 = reader.read (6);
  test.ASSERT_EQUAL (b1, "defghi");

  reader.seek (100);
  mobius::bytearray b2 = reader.read (5);
  test.ASSERT_EQUAL (b2, "abc\r\n");

  reader.seek (99998);
  mobius::bytearray b3 = reader.read (4);
  test.ASSERT_EQUAL (b3, "1234");

  test.end ();
}

void
unittest_imagefile ()
{
  testcase_imagefile_null ();
  testcase_imagefile_raw ();
  testcase_imagefile_split ();
}
