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. 4 */ 5 6 #include <errno.h> 7 #include <fcntl.h> 8 #include <pthread.h> 9 #include <stdio.h> 10 #include <sys/ioctl.h> 11 #include <sys/stat.h> 12 #include <sys/time.h> 13 14 #include "gtest/gtest.h" 15 16 #include "nacl_io/fifo_char.h" 17 #include "nacl_io/socket/fifo_packet.h" 18 #include "nacl_io/socket/packet.h" 19 20 #include "ppapi_simple/ps.h" 21 22 using namespace nacl_io; 23 24 namespace { 25 const size_t kTestSize = 32; 26 const size_t kHalfSize = kTestSize / 2; 27 const size_t kQuarterSize = kTestSize / 4; 28 }; 29 30 /** 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. 35 */ 36 TEST(FIFOChar, WriteWrapped) { 37 char temp_wr[kTestSize * 2]; 38 char temp_rd[kTestSize * 2]; 39 size_t wr_offs = 0; 40 size_t rd_offs = 0; 41 42 memset(temp_rd, 0, sizeof(temp_rd)); 43 for (size_t index = 0; index < sizeof(temp_wr); index++) 44 temp_wr[index] = index; 45 46 FIFOChar fifo(kTestSize); 47 48 // Fill the fifo 49 wr_offs += fifo.Write(temp_wr + wr_offs, kTestSize); 50 EXPECT_EQ(kTestSize, wr_offs); 51 52 // Read 1/2 of it 53 rd_offs += fifo.Read(temp_rd + rd_offs, kHalfSize); 54 EXPECT_EQ(kHalfSize, rd_offs); 55 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); 63 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); 67 68 for (size_t i = 0; i < rd_offs; i++) 69 ASSERT_EQ((char)i, temp_rd[i]) << "fifo mismatch at pos:" << i; 70 } 71 72 TEST(FIFOChar, Wrap) { 73 char temp_wr[kTestSize * 2]; 74 char temp_rd[kTestSize * 2]; 75 size_t wr_offs = 0; 76 size_t rd_offs = 0; 77 78 FIFOChar fifo(kTestSize); 79 80 memset(temp_rd, 0, sizeof(temp_rd)); 81 for (size_t index = 0; index < sizeof(temp_wr); index++) 82 temp_wr[index] = index; 83 84 EXPECT_TRUE(fifo.IsEmpty()); 85 EXPECT_FALSE(fifo.IsFull()); 86 87 // Wrap read and write differently, and verify copy is correct 88 EXPECT_EQ(0, fifo.ReadAvailable()); 89 EXPECT_EQ(kTestSize, fifo.WriteAvailable()); 90 91 wr_offs += fifo.Write(temp_wr, kHalfSize); 92 EXPECT_EQ(kHalfSize, wr_offs); 93 94 EXPECT_FALSE(fifo.IsEmpty()); 95 EXPECT_FALSE(fifo.IsFull()); 96 97 rd_offs += fifo.Read(temp_rd, kQuarterSize); 98 EXPECT_EQ(kQuarterSize, rd_offs); 99 100 EXPECT_FALSE(fifo.IsEmpty()); 101 EXPECT_FALSE(fifo.IsFull()); 102 103 wr_offs += fifo.Write(&temp_wr[wr_offs], kTestSize); 104 EXPECT_EQ(kTestSize + kQuarterSize, wr_offs); 105 106 EXPECT_FALSE(fifo.IsEmpty()); 107 108 rd_offs += fifo.Read(&temp_rd[rd_offs], kTestSize); 109 EXPECT_EQ(kTestSize + kQuarterSize, rd_offs); 110 111 EXPECT_TRUE(fifo.IsEmpty()); 112 EXPECT_FALSE(fifo.IsFull()); 113 114 for (size_t index = 0; index < kQuarterSize + kTestSize; index++) 115 EXPECT_EQ((char) index, temp_rd[index]); 116 } 117 118 TEST(FIFOPacket, Packets) { 119 char temp_wr[kTestSize]; 120 FIFOPacket fifo(kTestSize); 121 122 Packet pkt0(NULL); 123 Packet pkt1(NULL); 124 pkt0.Copy(temp_wr, kHalfSize, 0); 125 pkt1.Copy(temp_wr, kTestSize, 0); 126 127 EXPECT_TRUE(fifo.IsEmpty()); 128 EXPECT_FALSE(fifo.IsFull()); 129 130 EXPECT_EQ(0, fifo.ReadAvailable()); 131 EXPECT_EQ(kTestSize, fifo.WriteAvailable()); 132 133 fifo.WritePacket(&pkt0); 134 EXPECT_FALSE(fifo.IsEmpty()); 135 EXPECT_FALSE(fifo.IsFull()); 136 137 EXPECT_EQ(kHalfSize, fifo.ReadAvailable()); 138 EXPECT_EQ(kHalfSize, fifo.WriteAvailable()); 139 140 fifo.WritePacket(&pkt1); 141 EXPECT_FALSE(fifo.IsEmpty()); 142 EXPECT_TRUE(fifo.IsFull()); 143 144 EXPECT_EQ(kHalfSize + kTestSize, fifo.ReadAvailable()); 145 EXPECT_EQ(0, fifo.WriteAvailable()); 146 147 EXPECT_EQ(&pkt0, fifo.ReadPacket()); 148 EXPECT_FALSE(fifo.IsEmpty()); 149 EXPECT_TRUE(fifo.IsFull()); 150 151 EXPECT_EQ(kTestSize, fifo.ReadAvailable()); 152 EXPECT_EQ(0, fifo.WriteAvailable()); 153 154 EXPECT_EQ(&pkt1, fifo.ReadPacket()); 155 156 EXPECT_TRUE(fifo.IsEmpty()); 157 EXPECT_FALSE(fifo.IsFull()); 158 159 EXPECT_EQ(0, fifo.ReadAvailable()); 160 EXPECT_EQ(kTestSize, fifo.WriteAvailable()); 161 } 162