1 /* Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
10 #include <sys/ioctl.h>
14 #include "gtest/gtest.h"
16 #include "nacl_io/fifo_char.h"
17 #include "nacl_io/socket/fifo_packet.h"
18 #include "nacl_io/socket/packet.h"
20 #include "ppapi_simple/ps.h"
22 using namespace nacl_io
;
25 const size_t kTestSize
= 32;
26 const size_t kHalfSize
= kTestSize
/ 2;
27 const size_t kQuarterSize
= kTestSize
/ 4;
31 * Test writes that start a wrapped location. We had
32 * a bug where writes that wrapped around were fine but
33 * bytes that started at a wrapped location were being
34 * written to the wrong loction.
36 TEST(FIFOChar
, WriteWrapped
) {
37 char temp_wr
[kTestSize
* 2];
38 char temp_rd
[kTestSize
* 2];
42 memset(temp_rd
, 0, sizeof(temp_rd
));
43 for (size_t index
= 0; index
< sizeof(temp_wr
); index
++)
44 temp_wr
[index
] = index
;
46 FIFOChar
fifo(kTestSize
);
49 wr_offs
+= fifo
.Write(temp_wr
+ wr_offs
, kTestSize
);
50 EXPECT_EQ(kTestSize
, wr_offs
);
53 rd_offs
+= fifo
.Read(temp_rd
+ rd_offs
, kHalfSize
);
54 EXPECT_EQ(kHalfSize
, rd_offs
);
56 // Write the next two quaters. The second
57 // of these calls to write start at a wrapped
58 // location 1/4 of the way into the fifo
59 wr_offs
+= fifo
.Write(temp_wr
+ wr_offs
, kQuarterSize
);
60 EXPECT_EQ(kTestSize
+ kQuarterSize
, wr_offs
);
61 wr_offs
+= fifo
.Write(temp_wr
+ wr_offs
, kQuarterSize
);
62 EXPECT_EQ(kTestSize
+ kHalfSize
, wr_offs
);
64 // Finally read all the bytes we wrote.
65 rd_offs
+= fifo
.Read(temp_rd
+ rd_offs
, kTestSize
);
66 EXPECT_EQ(kTestSize
+ kHalfSize
, rd_offs
);
68 for (size_t i
= 0; i
< rd_offs
; i
++)
69 ASSERT_EQ((char)i
, temp_rd
[i
]) << "fifo mismatch at pos:" << i
;
72 TEST(FIFOChar
, Wrap
) {
73 char temp_wr
[kTestSize
* 2];
74 char temp_rd
[kTestSize
* 2];
78 FIFOChar
fifo(kTestSize
);
80 memset(temp_rd
, 0, sizeof(temp_rd
));
81 for (size_t index
= 0; index
< sizeof(temp_wr
); index
++)
82 temp_wr
[index
] = index
;
84 EXPECT_TRUE(fifo
.IsEmpty());
85 EXPECT_FALSE(fifo
.IsFull());
87 // Wrap read and write differently, and verify copy is correct
88 EXPECT_EQ(0, fifo
.ReadAvailable());
89 EXPECT_EQ(kTestSize
, fifo
.WriteAvailable());
91 wr_offs
+= fifo
.Write(temp_wr
, kHalfSize
);
92 EXPECT_EQ(kHalfSize
, wr_offs
);
94 EXPECT_FALSE(fifo
.IsEmpty());
95 EXPECT_FALSE(fifo
.IsFull());
97 rd_offs
+= fifo
.Read(temp_rd
, kQuarterSize
);
98 EXPECT_EQ(kQuarterSize
, rd_offs
);
100 EXPECT_FALSE(fifo
.IsEmpty());
101 EXPECT_FALSE(fifo
.IsFull());
103 wr_offs
+= fifo
.Write(&temp_wr
[wr_offs
], kTestSize
);
104 EXPECT_EQ(kTestSize
+ kQuarterSize
, wr_offs
);
106 EXPECT_FALSE(fifo
.IsEmpty());
108 rd_offs
+= fifo
.Read(&temp_rd
[rd_offs
], kTestSize
);
109 EXPECT_EQ(kTestSize
+ kQuarterSize
, rd_offs
);
111 EXPECT_TRUE(fifo
.IsEmpty());
112 EXPECT_FALSE(fifo
.IsFull());
114 for (size_t index
= 0; index
< kQuarterSize
+ kTestSize
; index
++)
115 EXPECT_EQ((char) index
, temp_rd
[index
]);
118 TEST(FIFOPacket
, Packets
) {
119 char temp_wr
[kTestSize
];
120 FIFOPacket
fifo(kTestSize
);
124 pkt0
.Copy(temp_wr
, kHalfSize
, 0);
125 pkt1
.Copy(temp_wr
, kTestSize
, 0);
127 EXPECT_TRUE(fifo
.IsEmpty());
128 EXPECT_FALSE(fifo
.IsFull());
130 EXPECT_EQ(0, fifo
.ReadAvailable());
131 EXPECT_EQ(kTestSize
, fifo
.WriteAvailable());
133 fifo
.WritePacket(&pkt0
);
134 EXPECT_FALSE(fifo
.IsEmpty());
135 EXPECT_FALSE(fifo
.IsFull());
137 EXPECT_EQ(kHalfSize
, fifo
.ReadAvailable());
138 EXPECT_EQ(kHalfSize
, fifo
.WriteAvailable());
140 fifo
.WritePacket(&pkt1
);
141 EXPECT_FALSE(fifo
.IsEmpty());
142 EXPECT_TRUE(fifo
.IsFull());
144 EXPECT_EQ(kHalfSize
+ kTestSize
, fifo
.ReadAvailable());
145 EXPECT_EQ(0, fifo
.WriteAvailable());
147 EXPECT_EQ(&pkt0
, fifo
.ReadPacket());
148 EXPECT_FALSE(fifo
.IsEmpty());
149 EXPECT_TRUE(fifo
.IsFull());
151 EXPECT_EQ(kTestSize
, fifo
.ReadAvailable());
152 EXPECT_EQ(0, fifo
.WriteAvailable());
154 EXPECT_EQ(&pkt1
, fifo
.ReadPacket());
156 EXPECT_TRUE(fifo
.IsEmpty());
157 EXPECT_FALSE(fifo
.IsFull());
159 EXPECT_EQ(0, fifo
.ReadAvailable());
160 EXPECT_EQ(kTestSize
, fifo
.WriteAvailable());