1 : /*
2 : * Copyright (c) 2011 The Native Client Authors. All rights reserved.
3 : * Use of this source code is governed by a BSD-style license that can be
4 : * found in the LICENSE file.
5 : */
6 :
7 : #include <stdio.h>
8 :
9 : #include "native_client/src/include/portability.h"
10 : #include "native_client/src/include/portability_io.h"
11 : #include "native_client/src/shared/gio/gio.h"
12 : #include "native_client/src/shared/gio/gio_test_base.h"
13 : #include "gtest/gtest.h"
14 :
15 : namespace {
16 :
17 : // Premade files set from argv. These are only read (not written to).
18 : // Must be a file with contents ['A'+0, 'A'+1, ..., 'A'+31]
19 : char* g_premade_textfile = NULL;
20 : // Must be a file with contents [0, 1, 2, ..., 31]
21 : char* g_premade_binfile = NULL;
22 : char* g_temp_file = NULL;
23 :
24 : /** Parse arguments for input files and checks that there are enough args. */
25 1 : void parse_args(int argc, char *argv[]) {
26 : int opt;
27 1 : while (-1 != (opt = getopt(argc, argv, "t:x:b:"))) {
28 1 : switch (opt) {
29 : default:
30 0 : printf("Unknown commandline option %c", opt);
31 0 : FAIL();
32 : case 'x':
33 1 : g_premade_textfile = optarg;
34 1 : break;
35 : case 'b':
36 1 : g_premade_binfile = optarg;
37 1 : break;
38 : case 't':
39 1 : g_temp_file = optarg;
40 : break;
41 : }
42 1 : }
43 :
44 1 : printf("Testing with textfile %s\n", g_premade_textfile);
45 1 : printf("Testing with binfile %s\n", g_premade_binfile);
46 1 : printf("Testing with temp file %s\n", g_temp_file);
47 :
48 1 : ASSERT_NE(reinterpret_cast<char*>(NULL), g_temp_file);
49 1 : ASSERT_NE(reinterpret_cast<char*>(NULL), g_premade_textfile);
50 1 : ASSERT_NE(reinterpret_cast<char*>(NULL), g_premade_binfile);
51 1 : }
52 :
53 :
54 1 : static int OpenGioPioFile(struct GioPio *gio, const char* name, int oflag) {
55 1 : int fd = OPEN(name, oflag, 0666);
56 1 : EXPECT_NE(-1, fd);
57 1 : return GioPioCtor(gio, fd);
58 1 : }
59 :
60 :
61 3 : TEST(GioPioTest, ReadTest) {
62 : struct GioPio my_file;
63 : int ret_code;
64 :
65 1 : ret_code = OpenGioPioFile(&my_file, g_premade_textfile, O_RDONLY);
66 1 : EXPECT_EQ(1, ret_code);
67 1 : GioReadTestWithOffset(&my_file.base, 'A');
68 1 : GioCloseTest(&my_file.base);
69 :
70 1 : ret_code = OpenGioPioFile(&my_file, g_premade_binfile, O_RDONLY | _O_BINARY);
71 1 : EXPECT_EQ(1, ret_code);
72 1 : GioReadTestWithOffset(&my_file.base, 0);
73 1 : GioCloseTest(&my_file.base);
74 1 : }
75 :
76 :
77 1 : void MakeTempFile(struct GioPio* my_file) {
78 : int ret_code;
79 :
80 1 : ret_code = OpenGioPioFile(my_file, g_temp_file, O_RDWR | O_CREAT | _O_BINARY);
81 1 : EXPECT_EQ(1, ret_code);
82 1 : }
83 :
84 1 : void CloseAndCleanTempFile(struct GioPio* gf) {
85 1 : GioCloseTest(&gf->base);
86 :
87 : // Since we pass in the name of the temp file from Scons, and Scons does not
88 : // know when it is opened/closed to delete the temp file. Delete it here.
89 1 : ASSERT_EQ(0, remove(g_temp_file));
90 1 : }
91 :
92 3 : TEST(GioPioTest, WriteTest) {
93 : struct GioPio my_file;
94 :
95 1 : MakeTempFile(&my_file);
96 1 : GioWriteTest(&my_file.base, false);
97 1 : CloseAndCleanTempFile(&my_file);
98 1 : }
99 :
100 3 : TEST(GioPioTest, SeekTest) {
101 : struct GioPio my_file;
102 : int ret_code;
103 :
104 1 : ret_code = OpenGioPioFile(&my_file, g_premade_textfile, O_RDONLY);
105 1 : EXPECT_EQ(1, ret_code);
106 1 : GioSeekTestWithOffset(&my_file.base, 'A', false);
107 1 : GioCloseTest(&my_file.base);
108 :
109 1 : ret_code = OpenGioPioFile(&my_file, g_premade_binfile, O_RDONLY | _O_BINARY);
110 1 : EXPECT_EQ(1, ret_code);
111 1 : GioSeekTestWithOffset(&my_file.base, 0, false);
112 1 : GioCloseTest(&my_file.base);
113 1 : }
114 :
115 : } // namespace
116 :
117 1 : int main(int argc, char* argv[]) {
118 1 : testing::InitGoogleTest(&argc, argv);
119 1 : parse_args(argc, argv);
120 1 : return RUN_ALL_TESTS();
121 1 : }
|