1*4b9c6d91SCole Faust /* test_util.h 2*4b9c6d91SCole Faust * Copyright 2021 The ChromiumOS Authors 3*4b9c6d91SCole Faust * Use of this source code is governed by a BSD-style license that can be 4*4b9c6d91SCole Faust * found in the LICENSE file. 5*4b9c6d91SCole Faust * 6*4b9c6d91SCole Faust * Utility functions in testing. 7*4b9c6d91SCole Faust */ 8*4b9c6d91SCole Faust 9*4b9c6d91SCole Faust #ifndef _TEST_UTIL_H_ 10*4b9c6d91SCole Faust #define _TEST_UTIL_H_ 11*4b9c6d91SCole Faust 12*4b9c6d91SCole Faust #include <stdio.h> 13*4b9c6d91SCole Faust 14*4b9c6d91SCole Faust #include <memory> 15*4b9c6d91SCole Faust #include <string> 16*4b9c6d91SCole Faust 17*4b9c6d91SCole Faust #include "config_parser.h" 18*4b9c6d91SCole Faust 19*4b9c6d91SCole Faust namespace mj { 20*4b9c6d91SCole Faust 21*4b9c6d91SCole Faust namespace internal { 22*4b9c6d91SCole Faust 23*4b9c6d91SCole Faust // Functor for |ScopedFILE| (below). 24*4b9c6d91SCole Faust struct ScopedFILECloser { operatorScopedFILECloser25*4b9c6d91SCole Faust inline void operator()(FILE *x) const { 26*4b9c6d91SCole Faust if (x) { 27*4b9c6d91SCole Faust fclose(x); 28*4b9c6d91SCole Faust } 29*4b9c6d91SCole Faust } 30*4b9c6d91SCole Faust }; 31*4b9c6d91SCole Faust 32*4b9c6d91SCole Faust // Functor for |ScopedConfigEntry| (below). 33*4b9c6d91SCole Faust struct ScopedConfigEntryDeleter { operatorScopedConfigEntryDeleter34*4b9c6d91SCole Faust inline void operator()(config_entry *entry) const { 35*4b9c6d91SCole Faust if (entry) { 36*4b9c6d91SCole Faust free(entry); 37*4b9c6d91SCole Faust } 38*4b9c6d91SCole Faust } 39*4b9c6d91SCole Faust }; 40*4b9c6d91SCole Faust 41*4b9c6d91SCole Faust } // namespace internal 42*4b9c6d91SCole Faust 43*4b9c6d91SCole Faust } // namespace mj 44*4b9c6d91SCole Faust 45*4b9c6d91SCole Faust using ScopedFILE = std::unique_ptr<FILE, mj::internal::ScopedFILECloser>; 46*4b9c6d91SCole Faust using ScopedConfigEntry = 47*4b9c6d91SCole Faust std::unique_ptr<config_entry, mj::internal::ScopedConfigEntryDeleter>; 48*4b9c6d91SCole Faust 49*4b9c6d91SCole Faust /* 50*4b9c6d91SCole Faust * write_to_pipe: write a string as the file content into a pipe based 51*4b9c6d91SCole Faust * file handle. This is particularly useful when testing with temporary data 52*4b9c6d91SCole Faust * files, without dealing with complexities such as relative file path, file 53*4b9c6d91SCole Faust * permission and etc. However, a pipe has limited capacity so write_to_pipe 54*4b9c6d91SCole Faust * will hang when a big enough string is written. This is for use in testing 55*4b9c6d91SCole Faust * only. 56*4b9c6d91SCole Faust * 57*4b9c6d91SCole Faust * Returns a FILE* that contains @content. 58*4b9c6d91SCole Faust */ 59*4b9c6d91SCole Faust 60*4b9c6d91SCole Faust FILE *write_to_pipe(const std::string& content); 61*4b9c6d91SCole Faust 62*4b9c6d91SCole Faust /* 63*4b9c6d91SCole Faust * source_path: return the path to a test fixture located in the current 64*4b9c6d91SCole Faust * source tree. This uses the `SRC` environment variable as the root of the 65*4b9c6d91SCole Faust * tree, falling back to the current directory. 66*4b9c6d91SCole Faust */ 67*4b9c6d91SCole Faust std::string source_path(const std::string& file); 68*4b9c6d91SCole Faust 69*4b9c6d91SCole Faust #endif /* _TEST_UTIL_H_ */ 70