OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef NET_CURVECP_TEST_DATA_STREAM_H_ | |
6 #define NET_CURVECP_TEST_DATA_STREAM_H_ | |
7 #pragma once | |
8 | |
9 #include <string.h> // for memcpy() | |
10 #include <algorithm> | |
11 | |
12 // This is a test class for generating an infinite stream of data which can | |
13 // be verified independently to be the correct stream of data. | |
14 | |
15 namespace net { | |
16 | |
17 class TestDataStream { | |
18 public: | |
19 TestDataStream() { | |
20 Reset(); | |
21 } | |
22 | |
23 // Fill |buffer| with |length| bytes of data from the stream. | |
24 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 | |
35 // Verify that |buffer| contains the expected next |length| bytes from the | |
36 // stream. Returns true if correct, false otherwise. | |
37 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 | |
50 void Reset() { | |
51 index_ = 0; | |
52 bytes_remaining_ = 0; | |
53 buffer_ptr_ = buffer_; | |
54 } | |
55 | |
56 private: | |
57 // If there is no data spilled over from the previous index, advance the | |
58 // index and fill the buffer. | |
59 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 | |
71 // Consume data from the spill buffer. | |
72 void Consume(int bytes) { | |
73 bytes_remaining_ -= bytes; | |
74 if (bytes_remaining_) | |
75 buffer_ptr_ += bytes; | |
76 else | |
77 buffer_ptr_ = buffer_; | |
78 } | |
79 | |
80 int index_; | |
81 int bytes_remaining_; | |
82 char buffer_[16]; | |
83 char* buffer_ptr_; | |
84 }; | |
85 | |
86 } // namespace net | |
87 | |
88 #endif // NET_CURVECP_TEST_DATA_STREAM_H_ | |
OLD | NEW |