summaryrefslogtreecommitdiff
path: root/chromium/third_party/nearby/src/presence/implementation/advertisement_decoder_test.cc
blob: 445c8ea1754c74a72a2dedaa4eb762f2fdd3900c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "presence/implementation/advertisement_decoder.h"

#include <string>
#include <vector>

#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "absl/strings/escaping.h"
#include "presence/data_element.h"
#include "presence/implementation/credential_manager_impl.h"

namespace nearby {
namespace presence {

namespace {
using ::testing::ElementsAre;
using ::testing::Return;
using ::testing::UnorderedElementsAre;
using ::testing::status::StatusIs;

class MockCredentialManager : public CredentialManagerImpl {
 public:
  MOCK_METHOD(absl::StatusOr<std::string>, DecryptDataElements,
              (absl::string_view metadata_key, absl::string_view salt,
               absl::string_view data_elements),
              (override));
};

TEST(AdvertisementDecoder, DecodeBaseNpPrivateAdvertisement) {
  const std::string salt = "AB";
  const std::string metadata =
      absl::HexStringToBytes("1011121314151617181920212223");
  MockCredentialManager credential_manager;
  EXPECT_CALL(
      credential_manager,
      DecryptDataElements(metadata, salt, absl::HexStringToBytes("5051525354")))
      .WillOnce(Return(absl::HexStringToBytes("18CD29EEFF")));

  AdvertisementDecoder decoder(&credential_manager);

  const absl::StatusOr<std::vector<DataElement>> result =
      decoder.DecodeAdvertisement(absl::HexStringToBytes(
          "00204142e110111213141516171819202122235051525354"));

  ASSERT_OK(result);
  EXPECT_THAT(
      *result,
      ElementsAre(DataElement(DataElement::kSaltFieldType, salt),
                  DataElement(DataElement::kPrivateIdentityFieldType, metadata),
                  DataElement(8, absl::HexStringToBytes("CD")),
                  DataElement(9, absl::HexStringToBytes("EEFF"))));
}

TEST(AdvertisementDecoder, DecodeBaseNpPublicAdvertisement) {
  const std::string salt = "AB";
  MockCredentialManager credential_manager;
  AdvertisementDecoder decoder(&credential_manager);

  const absl::StatusOr<std::vector<DataElement>> result =
      decoder.DecodeAdvertisement(
          absl::HexStringToBytes("002041420318CD29EEFF"));

  ASSERT_OK(result);
  EXPECT_THAT(
      *result,
      ElementsAre(DataElement(DataElement::kSaltFieldType, salt),
                  DataElement(DataElement::kPublicIdentityFieldType, ""),
                  DataElement(8, absl::HexStringToBytes("CD")),
                  DataElement(9, absl::HexStringToBytes("EEFF"))));
}

TEST(AdvertisementDecoder, DecodeBaseNpPWithActionField) {
  std::string salt = "AB";
  MockCredentialManager credential_manager;
  AdvertisementDecoder decoder(&credential_manager);

  auto result =
      decoder.DecodeAdvertisement(absl::HexStringToBytes("002041420326B840"));

  EXPECT_OK(result);
  EXPECT_THAT(*result,
              UnorderedElementsAre(
                  DataElement(DataElement::kSaltFieldType, salt),
                  DataElement(DataElement::kPublicIdentityFieldType, ""),
                  DataElement(DataElement::kContextTimestampFieldType,
                              absl::HexStringToBytes("0B")),
                  DataElement(DataElement::kActionFieldType,
                              action::kTapToTransferAction),
                  DataElement(DataElement::kActionFieldType,
                              action::kNearbyShareAction)));
}

TEST(AdvertisementDecoder, InvalidAdvertisementFieldTooShort) {
  MockCredentialManager credential_manager;
  AdvertisementDecoder decoder(&credential_manager);

  // 0x50 header means 5 bytes long salt but only 4 bytes follow.
  EXPECT_THAT(
      decoder.DecodeAdvertisement(absl::HexStringToBytes("0050A0A1A2A3")),
      StatusIs(absl::StatusCode::kOutOfRange));
}

TEST(AdvertisementDecoder, ZeroLengthPayload) {
  MockCredentialManager credential_manager;
  AdvertisementDecoder decoder(&credential_manager);

  // A action with type 0xA and no payload
  const absl::StatusOr<std::vector<DataElement>> result =
      decoder.DecodeAdvertisement(absl::HexStringToBytes("000A"));

  ASSERT_OK(result);
  EXPECT_THAT(*result, ElementsAre(DataElement(0xA, "")));
}

TEST(AdvertisementDecoder, EmptyAdvertisement) {
  MockCredentialManager credential_manager;
  AdvertisementDecoder decoder(&credential_manager);

  EXPECT_THAT(decoder.DecodeAdvertisement(""),
              StatusIs(absl::StatusCode::kOutOfRange));
}

TEST(AdvertisementDecoder, InvalidEncryptedContent) {
  const std::string salt = "AB";
  const std::string metadata =
      absl::HexStringToBytes("1011121314151617181920212223");
  MockCredentialManager credential_manager;
  // 0x28CD is an invalid DE, 0x28 means a 2 byte payload with type 8 alas only
  // one byte is given (0xCD)
  EXPECT_CALL(
      credential_manager,
      DecryptDataElements(metadata, salt, absl::HexStringToBytes("5051525354")))
      .WillOnce(Return(absl::HexStringToBytes("28CD")));
  AdvertisementDecoder decoder(&credential_manager);

  EXPECT_THAT(decoder.DecodeAdvertisement(absl::HexStringToBytes(
                  "00204142e110111213141516171819202122235051525354")),
              StatusIs(absl::StatusCode::kOutOfRange));
}

TEST(AdvertisementDecoder, UnsupportedAdvertisementVersion) {
  MockCredentialManager credential_manager;
  AdvertisementDecoder decoder(&credential_manager);

  EXPECT_THAT(decoder.DecodeAdvertisement(
                  absl::HexStringToBytes("012041420318CD29EEFF")),
              StatusIs(absl::StatusCode::kUnimplemented));
}

}  // namespace
}  // namespace presence
}  // namespace nearby