| 1 | #include <opustags.h> |
| 2 | #include "tap.h" |
| 3 | |
| 4 | #include <string.h> |
| 5 | #include <unistd.h> |
| 6 | |
| 7 | void check_partial_files() |
| 8 | { |
| 9 | static const char* result = "partial_file.test"; |
| 10 | std::string name; |
| 11 | { |
| 12 | ot::partial_file bad_tmp; |
| 13 | try { |
| 14 | bad_tmp.open("/dev/null"); |
| 15 | throw failure("opening a device as a partial file should fail"); |
| 16 | } catch (const ot::status& rc) { |
| 17 | is(rc, ot::st::standard_error, "opening a device as a partial file fails"); |
| 18 | } |
| 19 | |
| 20 | bad_tmp.open(result); |
| 21 | name = bad_tmp.name(); |
| 22 | if (name.size() != strlen(result) + 12 || |
| 23 | name.compare(0, strlen(result), result) != 0) |
| 24 | throw failure("the temporary name is surprising: " + name); |
| 25 | } |
| 26 | is(access(name.c_str(), F_OK), -1, "expect the temporary file is deleted"); |
| 27 | |
| 28 | ot::partial_file good_tmp; |
| 29 | good_tmp.open(result); |
| 30 | name = good_tmp.name(); |
| 31 | good_tmp.commit(); |
| 32 | is(access(name.c_str(), F_OK), -1, "expect the temporary file is deleted"); |
| 33 | is(access(result, F_OK), 0, "expect the final result file"); |
| 34 | is(remove(result), 0, "remove the result file"); |
| 35 | } |
| 36 | |
| 37 | void check_slurp() |
| 38 | { |
| 39 | static const ot::byte_string_view pixel = ""_bsv |
| 40 | "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d" |
| 41 | "\x49\x48\x44\x52\x00\x00\x00\x01\x00\x00\x00\x01" |
| 42 | "\x08\x02\x00\x00\x00\x90\x77\x53\xde\x00\x00\x00" |
| 43 | "\x0c\x49\x44\x41\x54\x08\xd7\x63\xf8\xff\xff\x3f" |
| 44 | "\x00\x05\xfe\x02\xfe\xdc\xcc\x59\xe7\x00\x00\x00" |
| 45 | "\x00\x49\x45\x4e\x44\xae\x42\x60\x82"; |
| 46 | opaque_is(ot::slurp_binary_file("pixel.png"), pixel, "loads a whole file"); |
| 47 | } |
| 48 | |
| 49 | void check_converter() |
| 50 | { |
| 51 | setlocale(LC_ALL, ""); |
| 52 | is(ot::decode_utf8(ot::encode_utf8("Éphémère")), "Éphémère", "decode_utf8 reverts encode_utf8"); |
| 53 | opaque_is(ot::encode_utf8(ot::decode_utf8(u8"Éphémère")), u8"Éphémère", |
| 54 | "encode_utf8 reverts decode_utf8"); |
| 55 | |
| 56 | try { |
| 57 | ot::decode_utf8((char8_t*) "\xFF\xFF"); |
| 58 | throw failure("conversion from bad UTF-8 did not fail"); |
| 59 | } catch (const ot::status&) {} |
| 60 | } |
| 61 | |
| 62 | void check_shell_esape() |
| 63 | { |
| 64 | is(ot::shell_escape("foo"), "'foo'", "simple string"); |
| 65 | is(ot::shell_escape("a'b"), "'a'\\''b'", "string with a simple quote"); |
| 66 | is(ot::shell_escape("a!b"), "'a'\\!'b'", "string with a bang"); |
| 67 | is(ot::shell_escape("a!b'c!d'e"), "'a'\\!'b'\\''c'\\!'d'\\''e'", "string with a bang"); |
| 68 | } |
| 69 | |
| 70 | int main(int argc, char **argv) |
| 71 | { |
| 72 | plan(4); |
| 73 | run(check_partial_files, "test partial files"); |
| 74 | run(check_slurp, "file slurping"); |
| 75 | run(check_converter, "test encoding converter"); |
| 76 | run(check_shell_esape, "test shell escaping"); |
| 77 | return 0; |
| 78 | } |