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 <cppunit/plugin/TestPlugIn.h>
24 #include <osl/file.hxx>
25 #include <osl/thread.h>
26 #include <rtl/ustring.hxx>
27 
28 using namespace osl;
29 
30 class test_osl_writeFile : public CppUnit::TestFixture
31 {
32 public:
wrt_file()33     void wrt_file()
34     {
35         FileBase::RC err;
36 
37         //create a tempfile
38         OUString aTmpFile;
39         err = FileBase::createTempFile(nullptr, nullptr, &aTmpFile);
40         CPPUNIT_ASSERT_EQUAL_MESSAGE("temp File creation failed", osl::FileBase::E_None, err);
41 
42         //now attempt to open with Create flag an existing file, should get E_EXIST
43         File tmp_file(aTmpFile);
44         err = tmp_file.open(osl_File_OpenFlag_Write | osl_File_OpenFlag_Create);
45 
46         OString sErrorMsg = "Expected that '" +
47             OUStringToOString(aTmpFile, RTL_TEXTENCODING_ASCII_US) +
48             "' would exist!";
49         CPPUNIT_ASSERT_EQUAL_MESSAGE(sErrorMsg.getStr(), FileBase::E_EXIST, err);
50 
51         char buffer[1];
52         sal_uInt64 written = 0;
53         err = tmp_file.write(static_cast<void*>(buffer), sizeof(buffer), written);
54         CPPUNIT_ASSERT_MESSAGE("write on unconnected file should fail",
55             err != osl::FileBase::E_None);
56         CPPUNIT_ASSERT_EQUAL_MESSAGE("write on unconnected file should fail",
57             sal_uInt64(0), written);
58 
59         err = tmp_file.sync();
60         CPPUNIT_ASSERT_MESSAGE("sync on unconnected file should fail", err != FileBase::E_None);
61         err = tmp_file.close();
62         CPPUNIT_ASSERT_MESSAGE("close on unconnected file should fail", err != FileBase::E_None);
63 
64         err = ::osl::File::remove(aTmpFile);
65         CPPUNIT_ASSERT_EQUAL_MESSAGE("temp file should have existed", FileBase::E_None, err);
66     }
67 
68     CPPUNIT_TEST_SUITE(test_osl_writeFile);
69     CPPUNIT_TEST(wrt_file);
70     CPPUNIT_TEST_SUITE_END();
71 };
72 
73 // register test suites
74 CPPUNIT_TEST_SUITE_REGISTRATION(test_osl_writeFile);
75 
76 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
77