| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_CURVECP_TEST_DATA_STREAM_H_ | 5 #ifndef NET_BASE_TEST_DATA_STREAM_H_ |
| 6 #define NET_CURVECP_TEST_DATA_STREAM_H_ | 6 #define NET_BASE_TEST_DATA_STREAM_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string.h> // for memcpy() | 9 #include <string.h> // for memcpy(). |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include "net/base/net_api.h" |
| 11 | 12 |
| 12 // This is a test class for generating an infinite stream of data which can | 13 // This is a class for generating an infinite stream of data which can be |
| 13 // be verified independently to be the correct stream of data. | 14 // verified independently to be the correct stream of data. |
| 14 | 15 |
| 15 namespace net { | 16 namespace net { |
| 16 | 17 |
| 17 class TestDataStream { | 18 class NET_API TestDataStream { |
| 18 public: | 19 public: |
| 19 TestDataStream() { | 20 TestDataStream(); |
| 20 Reset(); | |
| 21 } | |
| 22 | 21 |
| 23 // Fill |buffer| with |length| bytes of data from the stream. | 22 // Fill |buffer| with |length| bytes of data from the stream. |
| 24 void GetBytes(char* buffer, int length) { | 23 void GetBytes(char* buffer, int length); |
| 25 while (length) { | |
| 26 AdvanceIndex(); | |
| 27 int bytes_to_copy = std::min(length, bytes_remaining_); | |
| 28 memcpy(buffer, buffer_ptr_, bytes_to_copy); | |
| 29 buffer += bytes_to_copy; | |
| 30 Consume(bytes_to_copy); | |
| 31 length -= bytes_to_copy; | |
| 32 } | |
| 33 } | |
| 34 | 24 |
| 35 // Verify that |buffer| contains the expected next |length| bytes from the | 25 // Verify that |buffer| contains the expected next |length| bytes from the |
| 36 // stream. Returns true if correct, false otherwise. | 26 // stream. Returns true if correct, false otherwise. |
| 37 bool VerifyBytes(const char *buffer, int length) { | 27 bool VerifyBytes(const char *buffer, int length); |
| 38 while (length) { | |
| 39 AdvanceIndex(); | |
| 40 int bytes_to_compare = std::min(length, bytes_remaining_); | |
| 41 if (memcmp(buffer, buffer_ptr_, bytes_to_compare)) | |
| 42 return false; | |
| 43 Consume(bytes_to_compare); | |
| 44 length -= bytes_to_compare; | |
| 45 buffer += bytes_to_compare; | |
| 46 } | |
| 47 return true; | |
| 48 } | |
| 49 | 28 |
| 50 void Reset() { | 29 // Resets all the data. |
| 51 index_ = 0; | 30 void Reset(); |
| 52 bytes_remaining_ = 0; | |
| 53 buffer_ptr_ = buffer_; | |
| 54 } | |
| 55 | 31 |
| 56 private: | 32 private: |
| 57 // If there is no data spilled over from the previous index, advance the | 33 // If there is no data spilled over from the previous index, advance the |
| 58 // index and fill the buffer. | 34 // index and fill the buffer. |
| 59 void AdvanceIndex() { | 35 void AdvanceIndex(); |
| 60 if (bytes_remaining_ == 0) { | |
| 61 // Convert it to ascii, but don't bother to reverse it. | |
| 62 // (e.g. 12345 becomes "54321") | |
| 63 int val = index_++; | |
| 64 do { | |
| 65 buffer_[bytes_remaining_++] = (val % 10) + '0'; | |
| 66 } while ((val /= 10) > 0); | |
| 67 buffer_[bytes_remaining_++] = '.'; | |
| 68 } | |
| 69 } | |
| 70 | 36 |
| 71 // Consume data from the spill buffer. | 37 // Consume data from the spill buffer. |
| 72 void Consume(int bytes) { | 38 void Consume(int bytes); |
| 73 bytes_remaining_ -= bytes; | |
| 74 if (bytes_remaining_) | |
| 75 buffer_ptr_ += bytes; | |
| 76 else | |
| 77 buffer_ptr_ = buffer_; | |
| 78 } | |
| 79 | 39 |
| 80 int index_; | 40 int index_; |
| 81 int bytes_remaining_; | 41 int bytes_remaining_; |
| 82 char buffer_[16]; | 42 char buffer_[16]; |
| 83 char* buffer_ptr_; | 43 char* buffer_ptr_; |
| 84 }; | 44 }; |
| 85 | 45 |
| 86 } // namespace net | 46 } // namespace net |
| 87 | 47 |
| 88 #endif // NET_CURVECP_TEST_DATA_STREAM_H_ | 48 #endif // NET_BASE_TEST_DATA_STREAM_H_ |
| OLD | NEW |