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/shared/gio/gio.h"
11 #include "native_client/src/shared/gio/gio_test_base.h"
12 #include "gtest/gtest.h"
13
14 namespace {
15
16 // Premade files set from argv. These are only read (not written to).
17 // Must be a file with contents ['A'+0, 'A'+1, ..., 'A'+31]
18 char* g_premade_textfile = NULL;
19 // Must be a file with contents [0, 1, 2, ..., 31]
20 char* g_premade_binfile = NULL;
21 char* g_temp_file = NULL;
22
23 /** Parse arguments for input files and checks that there are enough args. */
parse_args(int argc,char * argv[])24 void parse_args(int argc, char *argv[]) {
25 int opt;
26 while (-1 != (opt = getopt(argc, argv, "t:x:b:"))) {
27 switch (opt) {
28 default:
29 printf("Unknown commandline option %c", opt);
30 FAIL();
31 case 'x':
32 g_premade_textfile = optarg;
33 break;
34 case 'b':
35 g_premade_binfile = optarg;
36 break;
37 case 't':
38 g_temp_file = optarg;
39 break;
40 }
41 }
42
43 printf("Testing with textfile %s\n", g_premade_textfile);
44 printf("Testing with binfile %s\n", g_premade_binfile);
45 printf("Testing with temp file %s\n", g_temp_file);
46
47 ASSERT_NE(reinterpret_cast<char*>(NULL), g_temp_file);
48 ASSERT_NE(reinterpret_cast<char*>(NULL), g_premade_textfile);
49 ASSERT_NE(reinterpret_cast<char*>(NULL), g_premade_binfile);
50 }
51
52
TEST(GioTest,ReadTest)53 TEST(GioTest, ReadTest) {
54 struct GioFile my_file;
55 int ret_code;
56
57 ret_code = GioFileCtor(&my_file, g_premade_textfile, "r");
58 EXPECT_EQ(1, ret_code);
59 GioReadTestWithOffset(&my_file.base, 'A');
60 GioCloseTest(&my_file.base);
61
62 ret_code = GioFileCtor(&my_file, g_premade_binfile, "rb");
63 EXPECT_EQ(1, ret_code);
64 GioReadTestWithOffset(&my_file.base, 0);
65 GioCloseTest(&my_file.base);
66 }
67
68
MakeTempFile(struct GioFile * my_file)69 void MakeTempFile(struct GioFile* my_file) {
70 int ret_code = GioFileCtor(my_file, g_temp_file, "w+b");
71 EXPECT_EQ(1, ret_code);
72 }
73
CloseAndCleanTempFile(struct GioFile * gf)74 void CloseAndCleanTempFile(struct GioFile* gf) {
75 GioCloseTest(&gf->base);
76
77 // Since we pass in the name of the temp file from Scons, and Scons does not
78 // know when it is opened/closed to delete the temp file. Delete it here.
79 ASSERT_EQ(0, remove(g_temp_file));
80 }
81
TEST(GioTest,WriteTest)82 TEST(GioTest, WriteTest) {
83 struct GioFile my_file;
84
85 MakeTempFile(&my_file);
86 GioWriteTest(&my_file.base, false);
87 CloseAndCleanTempFile(&my_file);
88 }
89
TEST(GioTest,SeekTest)90 TEST(GioTest, SeekTest) {
91 struct GioFile my_file;
92 int ret_code;
93
94 ret_code = GioFileCtor(&my_file, g_premade_textfile, "r");
95 EXPECT_EQ(1, ret_code);
96 GioSeekTestWithOffset(&my_file.base, 'A', false);
97 GioCloseTest(&my_file.base);
98
99 ret_code = GioFileCtor(&my_file, g_premade_binfile, "rb");
100 EXPECT_EQ(1, ret_code);
101 GioSeekTestWithOffset(&my_file.base, 0, false);
102 GioCloseTest(&my_file.base);
103 }
104
105 } // namespace
106
main(int argc,char * argv[])107 int main(int argc, char* argv[]) {
108 testing::InitGoogleTest(&argc, argv);
109 parse_args(argc, argv);
110 return RUN_ALL_TESTS();
111 }
112