TEST(ByteArraySuite, copy) { const shared_int SIZE = 100; char buffer[SIZE]; // Copy ByteArray copyFrom; ByteArray copyTo; EXPECT_TRUE(copyFrom.init(&buffer[0], SIZE)); EXPECT_TRUE(copyTo.load(copyFrom)); EXPECT_EQ((shared_int)copyTo.getBufferSize(), SIZE); EXPECT_TRUE(copyTo.load(copyFrom)); EXPECT_EQ((shared_int)copyTo.getBufferSize(), 2*SIZE); // Copy too large ByteArray tooBig; if (tooBig.getMaxBufferSize()-1 <= std::numeric_limits<shared_int>::max()) { shared_int TOO_BIG = tooBig.getMaxBufferSize()-1; char bigBuffer[TOO_BIG]; EXPECT_TRUE(tooBig.init(&bigBuffer[0], TOO_BIG)); EXPECT_FALSE(copyTo.load(tooBig)); // A failed load should not change the buffer. EXPECT_EQ((shared_int)copyTo.getBufferSize(), 2*SIZE); } else std::cout << std::string(15, ' ') << "ByteArray.MaxSize==INT_MAX. Skipping TOO_BIG tests" << std::endl; }
bool SimpleSocket::receiveBytes(ByteArray & buffer, shared_int num_bytes) { int rc = this->SOCKET_FAIL; bool rtn = false; // Reset the buffer (this is not required since the buffer length should // ensure that we don't read any of the garbage that may be left over from // a previous read), but it is good practice. memset(&this->buffer_, 0, sizeof(this->buffer_)); // Doing a sanity check to determine if the byte array buffer is larger than // what can be sent in the socket. This should not happen and might be indicative // of some code synchronization issues between the client and server base. if (this->MAX_BUFFER_SIZE < (int)buffer.getMaxBufferSize()) { LOG_WARN("Socket buffer max size: %u, is larger than byte array buffer: %u", this->MAX_BUFFER_SIZE, buffer.getMaxBufferSize()); } if (this->isConnected()) { rc = rawReceiveBytes(this->buffer_, num_bytes); if (this->SOCKET_FAIL != rc) { if (rc > 0) { LOG_COMM("Byte array receive, bytes read: %u", rc); buffer.init(&this->buffer_[0], rc); rtn = true; } else { LOG_WARN("Recieved zero bytes: %u", rc); rtn = false; } } else { this->logSocketError("Socket received failed", rc); rtn = false; } } else { rtn = false; LOG_WARN("Not connected, bytes not sent"); } if (!rtn) { this->setConnected(false); } return rtn; }
TEST(ByteArraySuite, init) { const shared_int SIZE = 100; ByteArray bytes; char buffer[SIZE]; // Valid byte arrays EXPECT_TRUE(bytes.init(&buffer[0], SIZE)); EXPECT_EQ((shared_int)bytes.getBufferSize(), SIZE); // Invalid init (too big) if (bytes.getMaxBufferSize() < std::numeric_limits<shared_int>::max()) { shared_int TOO_BIG = bytes.getMaxBufferSize()+1; char bigBuffer[TOO_BIG]; EXPECT_FALSE(bytes.init(&bigBuffer[0], TOO_BIG)); } else std::cout << std::string(15, ' ') << "ByteArray.MaxSize==INT_MAX. Skipping TOO_BIG tests" << std::endl; }