VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/testcase/tstDevPhy.cpp

Last change on this file was 103410, checked in by vboxsync, 3 months ago

Devices/Network/testcase: Fix some harmless dangling pointer warnings, bugref:3409

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
Line 
1/* $Id: tstDevPhy.cpp 103410 2024-02-19 07:11:27Z vboxsync $ */
2/** @file
3 * PHY MDIO unit tests.
4 */
5
6/*
7 * Copyright (C) 2007-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#ifdef USE_CPPUNIT
33# include <cppunit/ui/text/TestRunner.h>
34# include <cppunit/extensions/HelperMacros.h>
35#else
36# include "CppUnitEmulation.h"
37#endif
38
39#include "../DevE1000Phy.h"
40
41
42/**
43 * Test fixture for PHY MDIO/MDC interface emulation.
44 */
45class PhyTest
46#ifdef USE_CPPUNIT
47 : public CppUnit::TestFixture
48#endif
49{
50 CPPUNIT_TEST_SUITE( tstDevPhy );
51
52 CPPUNIT_TEST(testSize);
53 CPPUNIT_TEST(testReadPID);
54 CPPUNIT_TEST(testReadEPID);
55 CPPUNIT_TEST(testWriteANA);
56
57 CPPUNIT_TEST_SUITE_END();
58
59private:
60 enum Op
61 {
62 WRITE_OP = 0x1,
63 READ_OP = 0x2
64 };
65
66#define PHYADR_VAL (uint16_t)0
67#define ST_VAL (uint16_t)1
68#define TA_VAL (uint16_t)2
69#define PREAMBLE_VAL 0xFFFFFFFF
70
71 enum BitWidths {
72 ST_BITS = 2,
73 OP_BITS = 2,
74 PHYADR_BITS = 5,
75 REGADR_BITS = 5,
76 TA_BITS = 2,
77 DATA_BITS = 16,
78 PREAMBLE_BITS = 32
79 };
80
81 PPHY phy;
82
83 // Helper methods
84 void shiftOutBits(uint32_t data, uint16_t count);
85 uint16_t shiftInBits(uint16_t count);
86 int readAt(uint16_t addr);
87 void writeTo(uint16_t addr, uint32_t value);
88
89public:
90 void setUp()
91 {
92 phy = new PHY;
93 Phy::init(phy, 0, PHY_EPID_M881000);
94 }
95
96 void tearDown()
97 {
98 delete phy;
99 phy = NULL;
100 }
101
102 void testSize()
103 {
104 CPPUNIT_ASSERT_EQUAL(32, ST_BITS+OP_BITS+PHYADR_BITS+REGADR_BITS+TA_BITS+DATA_BITS);
105 }
106
107 void testReadPID()
108 {
109 CPPUNIT_ASSERT_EQUAL(0x0141, readAt(2));
110 }
111
112 void testReadEPID()
113 {
114 CPPUNIT_ASSERT_EQUAL(0x0141, readAt(2));
115 CPPUNIT_ASSERT_EQUAL(PHY_EPID_M881000, readAt(3));
116 }
117
118 void testWriteANA()
119 {
120 writeTo(4, 0xBEEF);
121 CPPUNIT_ASSERT_EQUAL(0xBEEF, readAt(4));
122 }
123
124};
125
126/**
127 * shiftOutBits - Shift data bits our to MDIO
128 **/
129void PhyTest::shiftOutBits(uint32_t data, uint16_t count) {
130 uint32_t mask = 0x01 << (count - 1);
131
132 do {
133 Phy::writeMDIO(phy, data & mask, NULL /*pDevIns*/);
134 mask >>= 1;
135 } while (mask);
136}
137
138/**
139 * shiftInBits - Shift data bits in from MDIO
140 **/
141uint16_t PhyTest::shiftInBits(uint16_t count)
142{
143 uint16_t data = 0;
144
145 while (count--)
146 {
147 data <<= 1;
148 data |= Phy::readMDIO(phy) ? 1 : 0;
149 }
150
151 return data;
152}
153
154int PhyTest::readAt(uint16_t addr)
155{
156 shiftOutBits(PREAMBLE_VAL, PREAMBLE_BITS);
157
158 shiftOutBits(ST_VAL, ST_BITS);
159 shiftOutBits(READ_OP, OP_BITS);
160 shiftOutBits(PHYADR_VAL, PHYADR_BITS);
161 shiftOutBits(addr, REGADR_BITS);
162
163 CPPUNIT_ASSERT_EQUAL((uint16_t)0, shiftInBits(1));
164 uint16_t u16Data = shiftInBits(DATA_BITS);
165 shiftInBits(1);
166 return u16Data;
167}
168
169void PhyTest::writeTo(uint16_t addr, uint32_t value)
170{
171 shiftOutBits(PREAMBLE_VAL, PREAMBLE_BITS);
172
173 shiftOutBits(ST_VAL, ST_BITS);
174 shiftOutBits(WRITE_OP, OP_BITS);
175 shiftOutBits(PHYADR_VAL, PHYADR_BITS);
176 shiftOutBits(addr, REGADR_BITS);
177 shiftOutBits(TA_VAL, TA_BITS);
178 shiftOutBits(value, DATA_BITS);
179}
180
181// Create text test runner and run all tests.
182int main()
183{
184#ifdef USE_CPPUNIT
185 CppUnit::TextUi::TestRunner runner;
186 runner.addTest( PhyTest::suite() );
187 return runner.run() ? 0 : 1;
188#else
189 PhyTest Test;
190 return Test.run();
191#endif
192}
193
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use