Debian Codegraph @

sal/qa/osl/file/test_cpy_wrt_file.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20 #include <sal/types.h>
21 #include <cppunit/TestFixture.h>
22 #include <cppunit/extensions/HelperMacros.h>
23 #include <osl/file.hxx>
24 #include <rtl/ustring.hxx>
25
26 using namespace osl;
27
28 class test_osl_writeFile : public CppUnit::TestFixture
29 {
30 public:
31 void wrt_file()
32 {
33 FileBase::RC err;
34
35 //create a tempfile
36 OUString aTmpFile;
37 err = FileBase::createTempFile(nullptr, nullptr, &aTmpFile);
38 CPPUNIT_ASSERT_EQUAL_MESSAGE("temp File creation failed", osl::FileBase::E_None, err);
39
40 //now attempt to open with Create flag an existing file, should get E_EXIST
41 File tmp_file(aTmpFile);
42 err = tmp_file.open(osl_File_OpenFlag_Write | osl_File_OpenFlag_Create);
43
44 OString sErrorMsg = "Expected that '" +
45 OUStringToOString(aTmpFile, RTL_TEXTENCODING_ASCII_US) +
46 "' would exist!";
47 CPPUNIT_ASSERT_EQUAL_MESSAGE(sErrorMsg.getStr(), FileBase::E_EXIST, err);
48
49 char buffer[1];
50 sal_uInt64 written = 0;
51 err = tmp_file.write(static_cast<void*>(buffer), sizeof(buffer), written);
52 CPPUNIT_ASSERT_MESSAGE("write on unconnected file should fail",
53 err != osl::FileBase::E_None);
54 CPPUNIT_ASSERT_EQUAL_MESSAGE("write on unconnected file should fail",
55 sal_uInt64(0), written);
56
57 err = tmp_file.sync();
58 CPPUNIT_ASSERT_MESSAGE("sync on unconnected file should fail", err != FileBase::E_None);
59 err = tmp_file.close();
60 CPPUNIT_ASSERT_MESSAGE("close on unconnected file should fail", err != FileBase::E_None);
61
62 err = ::osl::File::remove(aTmpFile);
63 CPPUNIT_ASSERT_EQUAL_MESSAGE("temp file should have existed", FileBase::E_None, err);
64 }
65
66 CPPUNIT_TEST_SUITE(test_osl_writeFile);
67 CPPUNIT_TEST(wrt_file);
68 CPPUNIT_TEST_SUITE_END();
69 };
70
71 // register test suites
72 CPPUNIT_TEST_SUITE_REGISTRATION(test_osl_writeFile);
73
74 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */