|
1 | 1 | /* Reverse Engineer's Hex Editor |
2 | | - * Copyright (C) 2017-2024 Daniel Collins <solemnwarning@solemnwarning.net> |
| 2 | + * Copyright (C) 2017-2025 Daniel Collins <solemnwarning@solemnwarning.net> |
3 | 3 | * |
4 | 4 | * This program is free software; you can redistribute it and/or modify it |
5 | 5 | * under the terms of the GNU General Public License version 2 as published by |
|
33 | 33 | #define UNIT_TEST |
34 | 34 | #include "../src/buffer.hpp" |
35 | 35 |
|
36 | | -#define TMPFILE "tests/.tmpfile" |
37 | | -#define TMPFILE2 "tests/.tmpfile2" |
38 | | - |
39 | 36 | #define TEST_BUFFER_MANIP(buffer_manip_code) \ |
40 | 37 | { \ |
41 | | - write_file(TMPFILE, BEGIN_DATA); \ |
42 | | - REHex::Buffer b(TMPFILE, 8); \ |
| 38 | + TempFile tmpfile(BEGIN_DATA.data(), BEGIN_DATA.size()); \ |
| 39 | + REHex::Buffer b(tmpfile.tmpfile, 8); \ |
43 | 40 | buffer_manip_code; \ |
44 | 41 | std::vector<unsigned char> got_data = b.read_data(0, 1024); \ |
45 | 42 | EXPECT_EQ(got_data, END_DATA) << "Buffer::read_data() returns correct data"; \ |
46 | 43 | } \ |
47 | 44 | { \ |
48 | | - write_file(TMPFILE, BEGIN_DATA); \ |
49 | | - REHex::Buffer b(TMPFILE, 8); \ |
| 45 | + TempFile tmpfile(BEGIN_DATA.data(), BEGIN_DATA.size()); \ |
| 46 | + REHex::Buffer b(tmpfile.tmpfile, 8); \ |
50 | 47 | buffer_manip_code; \ |
51 | 48 | b.write_inplace(); \ |
52 | | - std::vector<unsigned char> got_data = read_file(TMPFILE); \ |
| 49 | + std::vector<unsigned char> got_data = read_file(tmpfile.tmpfile); \ |
53 | 50 | EXPECT_EQ(got_data, END_DATA) << "write_inplace() produces file with correct data"; \ |
54 | 51 | } \ |
55 | 52 | { \ |
56 | | - write_file(TMPFILE, BEGIN_DATA); \ |
57 | | - REHex::Buffer b(TMPFILE, 8); \ |
| 53 | + TempFile tmpfile(BEGIN_DATA.data(), BEGIN_DATA.size()); \ |
| 54 | + REHex::Buffer b(tmpfile.tmpfile, 8); \ |
58 | 55 | buffer_manip_code; \ |
59 | | - b.write_copy(TMPFILE2); \ |
60 | | - std::vector<unsigned char> got_data = read_file(TMPFILE2); \ |
| 56 | + TempFilename tmpfile2; \ |
| 57 | + b.write_copy(tmpfile2.tmpfile); \ |
| 58 | + std::vector<unsigned char> got_data = read_file(tmpfile2.tmpfile); \ |
61 | 59 | EXPECT_EQ(got_data, END_DATA) << "write_copy() produces file with correct data"; \ |
62 | 60 | } \ |
63 | 61 | { \ |
64 | | - write_file(TMPFILE, BEGIN_DATA); \ |
65 | | - REHex::Buffer b(TMPFILE, 8); \ |
| 62 | + TempFile tmpfile(BEGIN_DATA.data(), BEGIN_DATA.size()); \ |
| 63 | + REHex::Buffer b(tmpfile.tmpfile, 8); \ |
66 | 64 | buffer_manip_code; \ |
67 | | - b.write_inplace(TMPFILE); \ |
68 | | - std::vector<unsigned char> got_data = read_file(TMPFILE); \ |
| 65 | + b.write_inplace(tmpfile.tmpfile); \ |
| 66 | + std::vector<unsigned char> got_data = read_file(tmpfile.tmpfile); \ |
69 | 67 | EXPECT_EQ(got_data, END_DATA) << "write_inplace(<same file>) produces file with correct data"; \ |
70 | 68 | } \ |
71 | 69 | { \ |
72 | | - write_file(TMPFILE, BEGIN_DATA); \ |
73 | | - REHex::Buffer b(TMPFILE, 8); \ |
| 70 | + TempFile tmpfile(BEGIN_DATA.data(), BEGIN_DATA.size()); \ |
| 71 | + REHex::Buffer b(tmpfile.tmpfile, 8); \ |
74 | 72 | buffer_manip_code; \ |
75 | | - if(unlink(TMPFILE2) != 0 && errno != ENOENT) { throw std::runtime_error("Unable to unlink temporary file"); } \ |
76 | | - b.write_inplace(TMPFILE2); \ |
77 | | - std::vector<unsigned char> got_data = read_file(TMPFILE2); \ |
| 73 | + TempFilename tmpfile2; \ |
| 74 | + if(unlink(tmpfile2.tmpfile) != 0 && errno != ENOENT) { throw std::runtime_error("Unable to unlink temporary file"); } \ |
| 75 | + b.write_inplace(tmpfile2.tmpfile); \ |
| 76 | + std::vector<unsigned char> got_data = read_file(tmpfile2.tmpfile); \ |
78 | 77 | EXPECT_EQ(got_data, END_DATA) << "write_inplace(<new file>) produces file with correct data"; \ |
79 | 78 | } \ |
80 | 79 | if(END_DATA.size() > 0) \ |
81 | 80 | { \ |
82 | | - write_file(TMPFILE, BEGIN_DATA); \ |
83 | | - REHex::Buffer b(TMPFILE, 8); \ |
| 81 | + TempFile tmpfile(BEGIN_DATA.data(), BEGIN_DATA.size()); \ |
| 82 | + REHex::Buffer b(tmpfile.tmpfile, 8); \ |
84 | 83 | buffer_manip_code; \ |
85 | 84 | std::vector<unsigned char> tf2data((END_DATA.size() - 1), 0xFF); \ |
86 | | - write_file(TMPFILE2, tf2data); \ |
87 | | - b.write_inplace(TMPFILE2); \ |
88 | | - std::vector<unsigned char> got_data = read_file(TMPFILE2); \ |
| 85 | + TempFile tmpfile2(tf2data.data(), tf2data.size()); \ |
| 86 | + b.write_inplace(tmpfile2.tmpfile); \ |
| 87 | + std::vector<unsigned char> got_data = read_file(tmpfile2.tmpfile); \ |
89 | 88 | EXPECT_EQ(got_data, END_DATA) << "write_inplace(<smaller file>) produces file with correct data"; \ |
90 | 89 | } \ |
91 | 90 | { \ |
92 | | - write_file(TMPFILE, BEGIN_DATA); \ |
93 | | - REHex::Buffer b(TMPFILE, 8); \ |
| 91 | + TempFile tmpfile(BEGIN_DATA.data(), BEGIN_DATA.size()); \ |
| 92 | + REHex::Buffer b(tmpfile.tmpfile, 8); \ |
94 | 93 | buffer_manip_code; \ |
95 | 94 | std::vector<unsigned char> tf2data((END_DATA.size() + 1), 0xFF); \ |
96 | | - write_file(TMPFILE2, tf2data); \ |
97 | | - b.write_inplace(TMPFILE2); \ |
98 | | - std::vector<unsigned char> got_data = read_file(TMPFILE2); \ |
| 95 | + TempFile tmpfile2(tf2data.data(), tf2data.size()); \ |
| 96 | + b.write_inplace(tmpfile2.tmpfile); \ |
| 97 | + std::vector<unsigned char> got_data = read_file(tmpfile2.tmpfile); \ |
99 | 98 | EXPECT_EQ(got_data, END_DATA) << "write_inplace(<larger file>) produces file with correct data"; \ |
100 | 99 | } |
101 | 100 |
|
|
0 commit comments