OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "components/proximity_auth/wire_message.h" | 5 #include "components/proximity_auth/wire_message.h" |
6 | 6 |
7 #include "base/base64.h" | |
8 #include "base/json/json_reader.h" | |
9 #include "base/logging.h" | |
10 #include "base/macros.h" | |
11 #include "base/values.h" | |
12 | |
13 // The wire messages have a simple format: | |
14 // [ message version ] [ body length ] [ JSON body ] | |
Tim Song
2014/09/12 00:25:21
Mention that the JSON object has the data and perm
Ilya Sherman
2014/09/12 01:24:26
Done.
| |
15 // 1 byte 2 bytes body length | |
16 | |
7 namespace proximity_auth { | 17 namespace proximity_auth { |
18 namespace { | |
19 | |
20 // The length of the message header, in bytes. | |
21 const size_t kHeaderLength = 3; | |
22 | |
23 // The protocol version of the message format. | |
24 const int kExpectedMessageFormatVersion = 3; | |
25 | |
26 const char kPayloadKey[] = "payload"; | |
27 const char kPermitIdKey[] = "permit_id"; | |
28 | |
29 // Parses the |serialized_message|'s header. Returns |true| iff the message has | |
30 // a valid header, is complete, and is well-formed according to the header. Sets | |
31 // |is_incomplete_message| to true iff the message does not have enough data to | |
32 // parse the header, or if the message length encoded in the message header | |
33 // exceeds the size of the |serialized_message|. | |
34 bool ParseHeader(const std::string& serialized_message, | |
35 bool* is_incomplete_message) { | |
36 *is_incomplete_message = false; | |
37 if (serialized_message.size() < kHeaderLength) { | |
38 *is_incomplete_message = true; | |
39 return false; | |
40 } | |
41 | |
42 COMPILE_ASSERT(kHeaderLength > 2, header_length_too_small); | |
43 size_t version = serialized_message[0]; | |
44 if (version != kExpectedMessageFormatVersion) { | |
45 VLOG(1) << "Error: Invalid message version. Got " << version | |
46 << ", expected " << kExpectedMessageFormatVersion; | |
47 return false; | |
48 } | |
49 | |
50 size_t expected_body_length = | |
51 (static_cast<size_t>(serialized_message[1]) << 8) | | |
52 static_cast<size_t>(serialized_message[2]); | |
Tim Song
2014/09/12 00:25:21
nit: alignment
Ilya Sherman
2014/09/12 01:24:26
How would you like me to change the alignment?
Tim Song
2014/09/12 01:38:21
nudge it right one character.
Ilya Sherman
2014/09/12 17:36:10
Hmm, that would make it appear to be within the pa
Tim Song
2014/09/12 23:54:25
Yeah, I see your point. Maybe add a left shift zer
Ilya Sherman
2014/09/15 22:25:13
Done.
| |
53 size_t expected_message_length = kHeaderLength + expected_body_length; | |
54 if (serialized_message.size() < expected_message_length) { | |
55 *is_incomplete_message = true; | |
56 return false; | |
57 } | |
58 if (serialized_message.size() != expected_message_length) { | |
59 VLOG(1) << "Error: Invalid message length. Got " | |
60 << serialized_message.size() << ", expected " | |
61 << expected_message_length; | |
62 return false; | |
63 } | |
64 | |
65 return true; | |
66 } | |
67 | |
68 } // namespace | |
8 | 69 |
9 WireMessage::~WireMessage() { | 70 WireMessage::~WireMessage() { |
10 } | 71 } |
11 | 72 |
12 // static | 73 // static |
13 bool WireMessage::IsCompleteMessage(const std::string& serialized_message) { | 74 scoped_ptr<WireMessage> WireMessage::Deserialize( |
14 // TODO(isherman): Implement. | 75 const std::string& serialized_message, |
15 return false; | 76 bool* is_incomplete_message) { |
77 if (!ParseHeader(serialized_message, is_incomplete_message)) | |
78 return scoped_ptr<WireMessage>(); | |
79 | |
80 scoped_ptr<base::Value> body_value( | |
81 base::JSONReader::Read(serialized_message.substr(kHeaderLength))); | |
82 if (!body_value || !body_value->IsType(base::Value::TYPE_DICTIONARY)) { | |
83 VLOG(1) << "Error: Unable to parse message as JSON."; | |
84 return scoped_ptr<WireMessage>(); | |
85 } | |
86 | |
87 base::DictionaryValue* body; | |
88 bool success = body_value->GetAsDictionary(&body); | |
89 DCHECK(success); | |
90 | |
91 std::string permit_id, payload_base64; | |
92 if (!body->GetString(kPermitIdKey, &permit_id) || permit_id.empty()) { | |
Tim Song
2014/09/12 00:25:21
The permit_id is optional. It's fine if the messag
Ilya Sherman
2014/09/12 01:24:26
Done.
| |
93 VLOG(1) << "Error: Missing permit id."; | |
94 return scoped_ptr<WireMessage>(); | |
95 } | |
96 if (!body->GetString(kPayloadKey, &payload_base64) || | |
97 payload_base64.empty()) { | |
98 VLOG(1) << "Error: Missing payload."; | |
99 return scoped_ptr<WireMessage>(); | |
100 } | |
101 | |
102 std::string payload; | |
103 if (!base::Base64Decode(payload_base64, &payload)) { | |
104 VLOG(1) << "Error: Invalid base64 encoding for payload."; | |
105 return scoped_ptr<WireMessage>(); | |
106 } | |
107 | |
108 return scoped_ptr<WireMessage>(new WireMessage(permit_id, payload)); | |
16 } | 109 } |
17 | 110 |
18 // static | 111 WireMessage::WireMessage(const std::string& permit_id, |
19 scoped_ptr<WireMessage> WireMessage::Deserialize( | 112 const std::string& payload) |
20 const std::string& serialized_message) { | 113 : permit_id_(permit_id), |
21 // TODO(isherman): Implement. | 114 payload_(payload) { |
22 return scoped_ptr<WireMessage>(); | |
23 } | |
24 | |
25 WireMessage::WireMessage() { | |
26 // TODO(isherman): Implement. | |
27 } | 115 } |
28 | 116 |
29 } // namespace proximity_auth | 117 } // namespace proximity_auth |
OLD | NEW |