summaryrefslogtreecommitdiffhomepage
path: root/tests/test-elf.cpp
blob: 737ba65502fd04be0e1e5d47e26742924908ac12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "elf.h"
#include "minicc.h"

#include <boost/algorithm/string.hpp>

#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include <algorithm>
#include <cctype>
#include <deque>
#include <map>
#include <memory>
#include <string>
#include <system_error>
#include <utility>
#include <vector>

using namespace std::string_literals;
namespace fs = std::filesystem;

class ElfTest: public ::testing::Test
{
protected:
 ElfTest() {
  //debug = true;
 }
 ~ElfTest() {
 }
 fs::path TempFilename(){return "tempfile.txt";}

 void SetUp(){
  std::error_code ec;
  fs::remove(TempFilename(), ec);
 }
 void TearDown(){
  std::error_code ec;
  fs::remove(TempFilename(), ec);
 }
};

#if 0
TEST_F(ElfTest, read) {
}
#endif

TEST_F(ElfTest, write_code) {
 Elf::Write(TempFilename(),
            {
            0x48, 0xc7, 0xc0, 0x3c, 0x00, 0x00, 0x00, // mov    $0x3c,%rax     # syscall 60
            0x48, 0x31, 0xff,                         // xor    %rdi,%rdi      # exit code 0
            0x0f, 0x05,                               // syscall
            }, {});

 ASSERT_TRUE(fs::exists(TempFilename()));
 ASSERT_GT(fs::file_size(TempFilename()), size_t(0));
}

TEST_F(ElfTest, write_code_data) {
 Elf::Write(TempFilename(),
            {
            0x48, 0xc7, 0xc0, 0x3c, 0x00, 0x00, 0x00, // mov    $0x3c,%rax     # syscall 60
            0x48, 0x8b, 0x3c, 0x25, 0x00, 0x20, 0x40, // mov    0x402000,%rdi  # use value from data segment as exit code
            0,
            0x0f, 0x05,                               // syscall
            },
            {1, 0, 0, 0, 0, 0, 0, 0});

 ASSERT_TRUE(fs::exists(TempFilename()));
 ASSERT_GT(fs::file_size(TempFilename()), size_t(0));
}