summaryrefslogtreecommitdiffstats
path: root/device/usb/usb_device_handle_unittest.cc
blob: 4cb4dff3e778a5a148f32007f20952aca58416c0 (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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "device/test/usb_test_gadget.h"
#include "device/usb/usb_device.h"
#include "device/usb/usb_device_handle.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace device {

namespace {

class UsbDeviceHandleTest : public ::testing::Test {
 public:
  void SetUp() override {
    if (!UsbTestGadget::IsTestEnabled()) {
      return;
    }

    message_loop_.reset(new base::MessageLoopForIO);

    gadget_ = UsbTestGadget::Claim();
    ASSERT_TRUE(gadget_.get());

    ASSERT_TRUE(gadget_->SetType(UsbTestGadget::ECHO));

    handle_ = gadget_->GetDevice()->Open();
    ASSERT_TRUE(handle_.get());
  }

  void TearDown() override {
    if (handle_.get()) {
      handle_->Close();
    }
    gadget_.reset(NULL);
    message_loop_.reset(NULL);
  }

 protected:
  scoped_refptr<UsbDeviceHandle> handle_;

 private:
  scoped_ptr<UsbTestGadget> gadget_;
  scoped_ptr<base::MessageLoop> message_loop_;
};

class TestCompletionCallback {
 public:
  TestCompletionCallback()
      : callback_(base::Bind(&TestCompletionCallback::SetResult,
                             base::Unretained(this))) {}

  void SetResult(UsbTransferStatus status,
                 scoped_refptr<net::IOBuffer> buffer,
                 size_t transferred) {
    status_ = status;
    transferred_ = transferred;
    run_loop_.Quit();
  }

  void WaitForResult() { run_loop_.Run(); }

  const UsbTransferCallback& callback() const { return callback_; }
  UsbTransferStatus status() const { return status_; }
  size_t transferred() const { return transferred_; }

 private:
  const UsbTransferCallback callback_;
  base::RunLoop run_loop_;
  UsbTransferStatus status_;
  size_t transferred_;
};

TEST_F(UsbDeviceHandleTest, InterruptTransfer) {
  if (!handle_.get()) {
    return;
  }

  ASSERT_TRUE(handle_->ClaimInterface(0));

  scoped_refptr<net::IOBufferWithSize> in_buffer(new net::IOBufferWithSize(64));
  TestCompletionCallback in_completion;
  handle_->InterruptTransfer(USB_DIRECTION_INBOUND,
                             0x81,
                             in_buffer.get(),
                             in_buffer->size(),
                             5000,  // 5 second timeout
                             in_completion.callback());

  scoped_refptr<net::IOBufferWithSize> out_buffer(
      new net::IOBufferWithSize(in_buffer->size()));
  TestCompletionCallback out_completion;
  for (int i = 0; i < out_buffer->size(); ++i) {
    out_buffer->data()[i] = i;
  }

  handle_->InterruptTransfer(USB_DIRECTION_OUTBOUND,
                             0x01,
                             out_buffer.get(),
                             out_buffer->size(),
                             5000,  // 5 second timeout
                             out_completion.callback());
  out_completion.WaitForResult();
  ASSERT_EQ(USB_TRANSFER_COMPLETED, out_completion.status());
  EXPECT_EQ(static_cast<size_t>(out_buffer->size()),
            out_completion.transferred());

  in_completion.WaitForResult();
  ASSERT_EQ(USB_TRANSFER_COMPLETED, in_completion.status());
  EXPECT_EQ(static_cast<size_t>(in_buffer->size()),
            in_completion.transferred());
  for (size_t i = 0; i < in_completion.transferred(); ++i) {
    EXPECT_EQ(out_buffer->data()[i], in_buffer->data()[i]);
  }
}

TEST_F(UsbDeviceHandleTest, BulkTransfer) {
  if (!handle_.get()) {
    return;
  }

  ASSERT_TRUE(handle_->ClaimInterface(1));

  scoped_refptr<net::IOBufferWithSize> in_buffer(
      new net::IOBufferWithSize(512));
  TestCompletionCallback in_completion;
  handle_->BulkTransfer(USB_DIRECTION_INBOUND, 0x82, in_buffer.get(),
                        in_buffer->size(),
                        5000,  // 5 second timeout
                        in_completion.callback());

  scoped_refptr<net::IOBufferWithSize> out_buffer(
      new net::IOBufferWithSize(in_buffer->size()));
  TestCompletionCallback out_completion;
  for (int i = 0; i < out_buffer->size(); ++i) {
    out_buffer->data()[i] = i;
  }

  handle_->BulkTransfer(USB_DIRECTION_OUTBOUND, 0x02, out_buffer.get(),
                        out_buffer->size(),
                        5000,  // 5 second timeout
                        out_completion.callback());
  out_completion.WaitForResult();
  ASSERT_EQ(USB_TRANSFER_COMPLETED, out_completion.status());
  EXPECT_EQ(static_cast<size_t>(out_buffer->size()),
            out_completion.transferred());

  in_completion.WaitForResult();
  ASSERT_EQ(USB_TRANSFER_COMPLETED, in_completion.status());
  EXPECT_EQ(static_cast<size_t>(in_buffer->size()),
            in_completion.transferred());
  for (size_t i = 0; i < in_completion.transferred(); ++i) {
    EXPECT_EQ(out_buffer->data()[i], in_buffer->data()[i]);
  }
}

}  // namespace

}  // namespace device