1 // Copyright 2015 The Kyua Authors.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 //   notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 //   notice, this list of conditions and the following disclaimer in the
12 //   documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 //   may be used to endorse or promote products derived from this software
15 //   without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 /// \file integration/helpers/race.cpp
30 /// Creates a file and reads it back, looking for races.
31 ///
32 /// This program should fail with high chances if it is called multiple times at
33 /// once with TEST_ENV_shared_file pointing to the same file.
34 
35 extern "C" {
36 #include <sys/types.h>
37 
38 #include <unistd.h>
39 }
40 
41 #include <cstdlib>
42 #include <fstream>
43 #include <iostream>
44 
45 #include "utils/format/macros.hpp"
46 #include "utils/fs/operations.hpp"
47 #include "utils/fs/path.hpp"
48 #include "utils/env.hpp"
49 #include "utils/optional.ipp"
50 #include "utils/stream.hpp"
51 
52 namespace fs = utils::fs;
53 
54 using utils::optional;
55 
56 
57 /// Entry point to the helper test program.
58 ///
59 /// \return EXIT_SUCCESS if no race is detected; EXIT_FAILURE otherwise.
60 int
61 main(void)
62 {
63     const optional< std::string > shared_file = utils::getenv(
64         "TEST_ENV_shared_file");
65     if (!shared_file) {
66         std::cerr << "Environment variable TEST_ENV_shared_file not defined\n";
67         std::exit(EXIT_FAILURE);
68     }
69     const fs::path shared_path(shared_file.get());
70 
71     if (fs::exists(shared_path)) {
72         std::cerr << "Shared file already exists; created by a concurrent "
73             "test?";
74         std::exit(EXIT_FAILURE);
75     }
76 
77     const std::string contents = F("%s") % ::getpid();
78 
79     std::ofstream output(shared_path.c_str());
80     if (!output) {
81         std::cerr << "Failed to create shared file; conflict with a concurrent "
82             "test?";
83         std::exit(EXIT_FAILURE);
84     }
85     output << contents;
86     output.close();
87 
88     ::usleep(10000);
89 
90     const std::string read_contents = utils::read_file(shared_path);
91     if (read_contents != contents) {
92         std::cerr << "Shared file contains unexpected contents; modified by a "
93             "concurrent test?";
94         std::exit(EXIT_FAILURE);
95     }
96 
97     fs::unlink(shared_path);
98     std::exit(EXIT_SUCCESS);
99 }
100