Stun Server  Compliant with the latest RFCs including 5389, 5769, and 5780
discover the local host's own external IP address
testbuilder.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2011 John Selbie
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 
18 
19 #include "commonincludes.hpp"
20 
21 #include "stuncore.h"
22 
23 #include "testbuilder.h"
24 
26 {
27  HRESULT hr = S_OK;
28  Chk(Test1())
29  Chk(Test2());
30 Cleanup:
31  return hr;
32 }
33 
34 // The goal of this test is to just validate that we can create a message from CStunMessageBuilder and have it's output parsed correctly by CStunMessageReader
35 // Also helps validate CSocketAddress
37 {
38  HRESULT hr = S_OK;
39  CStunMessageBuilder builder;
40  CStunMessageReader reader;
41  StunAttribute attrib;
42  CRefCountedBuffer spBuffer;
43  CRefCountedBuffer spBufferReader;
44  CSocketAddress addrValidate(0,0);
45  StunTransactionId transid = {};
46  uint32_t ipvalidate = 0;
47 
48 
49  CSocketAddress addr(0x7f000001, 9999);
50  CSocketAddress addrOrigin(0xAABBCCDD, 8888);
51  CSocketAddress addrOther(0x11223344, 7777);
52 
53 
54  ChkA(builder.AddBindingRequestHeader());
55  ChkA(builder.AddRandomTransactionId(&transid));
57  ChkA(builder.AddMappedAddress(addr));
58  ChkA(builder.AddXorMappedAddress(addr));
59  ChkA(builder.AddOtherAddress(addrOther));
60  ChkA(builder.AddResponseOriginAddress(addrOrigin));
61  ChkA(builder.AddFingerprintAttribute());
62  ChkA(builder.GetResult(&spBuffer));
63 
64  ChkIfA(CStunMessageReader::BodyValidated != reader.AddBytes(spBuffer->GetData(), spBuffer->GetSize()), E_FAIL);
65 
66  ChkIfA(reader.HasFingerprintAttribute() == false, E_FAIL);
67 
68  ChkIfA(reader.IsFingerprintAttributeValid() == false, E_FAIL);
69 
71 
73 
74  ChkA(reader.GetBuffer(&spBufferReader));
75 
77 
79 
80  ChkIfA(0 != ::strncmp("FOOBAR", (const char*)(spBufferReader->GetData() + attrib.offset), attrib.size), E_FAIL);
81 
82  ChkA(reader.GetXorMappedAddress(&addrValidate));
83  ChkIf(addrValidate.IsSameIP_and_Port(addr) == false, E_FAIL);
84  ChkIfA(addrValidate.GetIPLength() != 4, E_FAIL);
85 
86  addrValidate = CSocketAddress(0,0);
87  ChkA(reader.GetMappedAddress(&addrValidate));
88  ChkIfA(addrValidate.GetPort() != 9999, E_FAIL);
89  ChkIfA(addrValidate.GetIPLength() != 4, E_FAIL);
90  ChkIfA(4 != addrValidate.GetIP(&ipvalidate, 4), E_FAIL);
91  ChkIfA(ipvalidate != 0x7f000001, E_FAIL);
92 
93  addrValidate = CSocketAddress(0,0);
94  ipvalidate = 0;
95  reader.GetOtherAddress(&addrValidate);
96  ChkIfA(addrValidate.GetPort() != 7777, E_FAIL);
97  ChkIfA(addrValidate.GetIPLength() != 4, E_FAIL);
98  ChkIfA(4 != addrValidate.GetIP(&ipvalidate, 4), E_FAIL);
99  ChkIf(ipvalidate != 0x11223344, E_FAIL);
100 
101 
102 Cleanup:
103  return hr;
104 }
105 
106 
107 // this test validates that IPV6 addresses work fine with CStunMessageBuilder and CStunMessageReader
109 {
110  HRESULT hr = S_OK;
111  CSocketAddress addr(0,0);
112  CSocketAddress addrValidate(0,0);
113  const char* ip6addr = "ABCDEFGHIJKLMNOP";
114  sockaddr_in6 addr6 = {};
115  CStunMessageReader reader;
116  StunTransactionId transid;
117  CStunMessageBuilder builder;
118  CRefCountedBuffer spBuffer;
119 
120  addr6.sin6_family = AF_INET6;
121  addr6.sin6_port = htons(9999);
122  memcpy(addr6.sin6_addr.s6_addr, ip6addr, 16);
123  addr = CSocketAddress(addr6);
124 
126  ChkA(builder.AddRandomTransactionId(&transid));
127  ChkA(builder.AddMappedAddress(addr));
128  ChkA(builder.AddXorMappedAddress(addr));
129  ChkA(builder.GetResult(&spBuffer));
130 
131  ChkIfA(CStunMessageReader::BodyValidated != reader.AddBytes(spBuffer->GetData(), spBuffer->GetSize()), E_FAIL);
132 
133  ChkA(reader.GetXorMappedAddress(&addrValidate));
134 
135  ChkIf(addrValidate.IsSameIP_and_Port(addr) == false, E_FAIL);
136 
137 Cleanup:
138  return hr;
139 }
140 
141 
#define S_OK
Definition: hresult.h:46
uint16_t offset
Definition: stuntypes.h:112
HRESULT AddStringAttribute(uint16_t attribType, const char *pstr)
uint16_t GetMessageType()
Definition: stunreader.cpp:839
HRESULT AddRandomTransactionId(StunTransactionId *pTransId)
Definition: stunbuilder.cpp:93
HRESULT GetAttributeByType(uint16_t attributeType, StunAttribute *pAttribute)
Definition: stunreader.cpp:362
uint16_t GetIPLength() const
bool IsSameIP_and_Port(const CSocketAddress &other) const
#define Chk(expr)
Definition: chkmacros.h:53
HRESULT AddXorMappedAddress(const CSocketAddress &addr)
HRESULT GetBuffer(CRefCountedBuffer *pRefBuffer)
Definition: stunreader.cpp:844
HRESULT AddBindingRequestHeader()
Definition: stunbuilder.cpp:76
bool HasFingerprintAttribute()
Definition: stunreader.cpp:93
HRESULT GetMappedAddress(CSocketAddress *pAddress)
Definition: stunreader.cpp:505
virtual HRESULT Run()
Definition: testbuilder.cpp:25
#define ChkIf(expr, hrerror)
Definition: chkmacros.h:63
ReaderParseState AddBytes(const uint8_t *pData, uint32_t size)
Definition: stunreader.cpp:750
HRESULT AddMappedAddress(const CSocketAddress &addr)
HRESULT GetOtherAddress(CSocketAddress *pAddress)
Definition: stunreader.cpp:513
HRESULT AddHeader(StunMessageType msgType, StunMessageClass msgClass)
Definition: stunbuilder.cpp:55
HRESULT AddResponseOriginAddress(const CSocketAddress &other)
uint16_t attributeType
Definition: stuntypes.h:110
HRESULT AddFingerprintAttribute()
int32_t HRESULT
Definition: hresult.h:22
HRESULT Test2()
const uint16_t STUN_ATTRIBUTE_SOFTWARE
Definition: stuntypes.h:64
HRESULT GetResult(CRefCountedBuffer *pspBuffer)
bool IsFingerprintAttributeValid()
Definition: stunreader.cpp:99
#define E_FAIL
Definition: hresult.h:56
StunMessageClass GetMessageClass()
Definition: stunreader.cpp:834
#define ChkA(expr)
Definition: chkmacros.h:73
boost::shared_ptr< CBuffer > CRefCountedBuffer
Definition: buffer.h:65
#define ChkIfA(expr, hrerror)
Definition: chkmacros.h:84
HRESULT Test1()
Definition: testbuilder.cpp:36
uint16_t size
Definition: stuntypes.h:111
HRESULT GetXorMappedAddress(CSocketAddress *pAddress)
Definition: stunreader.cpp:529
HRESULT AddOtherAddress(const CSocketAddress &other)