using System;
#if FX1_1
using IList_ServiceElementSystem.Collections.IList;
using List_ServiceElementSystem.Collections.ArrayList;
using IList_ServiceAttributeSystem.Collections.IList;
using List_ServiceAttributeSystem.Collections.ArrayList;
#else
using IList_ServiceElementSystem.Collections.Generic.IListInTheHand.Net.Bluetooth.ServiceElement;
using List_ServiceElementSystem.Collections.Generic.ListInTheHand.Net.Bluetooth.ServiceElement;
using IList_ServiceAttributeSystem.Collections.Generic.IListInTheHand.Net.Bluetooth.ServiceAttribute;
using List_ServiceAttributeSystem.Collections.Generic.ListInTheHand.Net.Bluetooth.ServiceAttribute;
#endif
using System.Text;
using NUnit.Framework;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Tests.Sdp2;
using InTheHand.Net.Bluetooth.AttributeIds;
namespace InTheHand.Net.Tests.Sdp2{
[TestFixture]
public class SdpHelperTests
{
[Test]
public void GetChannelElement_AttrNotExists()
{
ServiceRecord rcd = new ServiceRecord();
ServiceElement element = ServiceRecordHelper.GetRfcommChannelElement(rcd);
Assert.IsNull(element);
}
[Test]
public void GetChannelByte_AttrNotExists()
{
ServiceRecord rcd = new ServiceRecord();
int value = ServiceRecordHelper.GetRfcommChannelNumber(rcd);
Assert.AreEqual(-1, value);
}
[Test]
public void GetChannelElement_Opp()
{
ServiceRecord rcd = ServiceRecord.CreateServiceRecordFromBytes(Data_CompleteThirdPartyRecords.PalmOsOpp);
ServiceElement element = ServiceRecordHelper.GetRfcommChannelElement(rcd);
Assert.IsNotNull(element);
Assert.AreEqual(ElementType.UInt8, element.ElementType);
Assert.AreEqual(Data_CompleteThirdPartyRecords.PalmOsOpp_RfcommChannelNumber, element.Value);
}
[Test]
public void GetChannelByte_Opp()
{
ServiceRecord rcd = ServiceRecord.CreateServiceRecordFromBytes(Data_CompleteThirdPartyRecords.PalmOsOpp);
int value = ServiceRecordHelper.GetRfcommChannelNumber(rcd);
Assert.AreNotEqual(-1, value);
Assert.AreEqual(Data_CompleteThirdPartyRecords.PalmOsOpp_RfcommChannelNumber, (byte)value);
}
[Test]
public void GetChannelElement_SdpNone()
{
ServiceRecord rcd = ServiceRecord.CreateServiceRecordFromBytes(Data_CompleteThirdPartyRecords.XpB_0of2Sdp);
ServiceElement element = ServiceRecordHelper.GetRfcommChannelElement(rcd);
Assert.IsNull(element);
}
[Test]
public void GetChannelByte_SdpNone()
{
ServiceRecord rcd = ServiceRecord.CreateServiceRecordFromBytes(Data_CompleteThirdPartyRecords.XpB_0of2Sdp);
int value = ServiceRecordHelper.GetRfcommChannelNumber(rcd);
Assert.AreEqual(-1, value);
}
[Test]
public void GetChannelByte_PdlHasUuid32s()
{
ServiceRecord rcd = ServiceRecord.CreateServiceRecordFromBytes(Data_CompleteThirdPartyRecords.KingSt_d2r1);
int value = ServiceRecordHelper.GetRfcommChannelNumber(rcd);
Assert.AreEqual(2, value);
}
[Test]
public void GetChannelByte_PdlHasUuid128s()
{
ServiceRecord rcd = ServiceRecord.CreateServiceRecordFromBytes(Data_CompleteThirdPartyRecords.KingSt_d2r1_withPdlUuid128s);
int value = ServiceRecordHelper.GetRfcommChannelNumber(rcd);
Assert.AreEqual(2, value);
}
//------------------------
public const string CrLf = "\r\n";
public const string ExpectedRfcommDump
= "AttrId: 0x0004 -- ProtocolDescriptorList" + CrLf
+ "ElementSequence" + CrLf
+ " ElementSequence" + CrLf
+ " Uuid16: 0x100 -- L2CapProtocol" + CrLf
+ " ElementSequence" + CrLf
+ " Uuid16: 0x3 -- RFCommProtocol" + CrLf
+ " UInt8: 0x0" + CrLf
+ "( ( L2Cap ), ( Rfcomm, ChannelNumber=0 ) )" + CrLf
;
public const string ExpectedGoepDump
= "AttrId: 0x0004 -- ProtocolDescriptorList" + CrLf
+ "ElementSequence" + CrLf
+ " ElementSequence" + CrLf
+ " Uuid16: 0x100 -- L2CapProtocol" + CrLf
+ " ElementSequence" + CrLf
+ " Uuid16: 0x3 -- RFCommProtocol" + CrLf
+ " UInt8: 0x0" + CrLf
+ " ElementSequence" + CrLf
+ " Uuid16: 0x8 -- ObexProtocol" + CrLf
+ "( ( L2Cap ), ( Rfcomm, ChannelNumber=0 ), ( Obex ) )" + CrLf
;
[Test]
public void CreateRfcomm()
{
ServiceElement element = ServiceRecordHelper.CreateRfcommProtocolDescriptorList();
//
List_ServiceAttribute attrs = new List_ServiceAttribute();
attrs.Add(new ServiceAttribute(UniversalAttributeId.ProtocolDescriptorList, element));
ServiceRecord record = new ServiceRecord(attrs);
String dump = ServiceRecordUtilities.Dump(record);
Assert.AreEqual(ExpectedRfcommDump, dump, "RFCOMM ProtoDL dump");
}
[Test]
public void CreateGoep()
{
ServiceElement element = ServiceRecordHelper.CreateGoepProtocolDescriptorList();
//
List_ServiceAttribute attrs = new List_ServiceAttribute();
attrs.Add(new ServiceAttribute(UniversalAttributeId.ProtocolDescriptorList, element));
ServiceRecord record = new ServiceRecord(attrs);
String dump = ServiceRecordUtilities.Dump(record);
Assert.AreEqual(ExpectedGoepDump, dump, "GOEP ProtoDL dump");
}
[Test]
public void PalmOsOppCompleteRecord()
{
ServiceRecord record = CreatePalmOsOppCompleteRecord();
ServiceRecordHelper.SetRfcommChannelNumber(record, 1);
byte[] buf = new byte[256];
int length = new ServiceRecordCreator().CreateServiceRecord(record, buf);
//ServiceRecordUtilities.Dump(Console.Out, record);
Assert2.AreEqualBuffers(Data_CompleteThirdPartyRecords.PalmOsOpp_HackMadeFirstLengthOneByteField, buf, length);
}
public ServiceRecord CreatePalmOsOppCompleteRecord()
{
List_ServiceAttribute attrs = new List_ServiceAttribute();
ServiceElement element;
List_ServiceElement list;
//
attrs.Add(new ServiceAttribute(UniversalAttributeId.ServiceRecordHandle,
new ServiceElement(ElementType.UInt32, (UInt32)0x10001)));
//
element = new ServiceElement(ElementType.Uuid16, (UInt16)0x1105);
list = new List_ServiceElement();
list.Add(element);
element = new ServiceElement(ElementType.ElementSequence, list);
attrs.Add(new ServiceAttribute(UniversalAttributeId.ServiceClassIdList, element));
//
element = ServiceRecordHelper.CreateGoepProtocolDescriptorList();
attrs.Add(new ServiceAttribute(UniversalAttributeId.ProtocolDescriptorList, element));
//
const UInt16 Windows1252EncodingId = 2252;
LanguageBaseItem[] languages = {
new LanguageBaseItem("en", Windows1252EncodingId, LanguageBaseItem.PrimaryLanguageBaseAttributeId)
};
element = LanguageBaseItem.CreateElementSequenceFromList(languages);
attrs.Add(new ServiceAttribute(UniversalAttributeId.LanguageBaseAttributeIdList, element));
//
// PalmOs really uses Windows-1252, but since the string is ASCII, UTF-8 is equivalent.
element = new ServiceElement(ElementType.TextString, "OBEX Object Push");
attrs.Add(new ServiceAttribute(
ServiceRecord.CreateLanguageBasedAttributeId(UniversalAttributeId.ServiceName,
LanguageBaseItem.PrimaryLanguageBaseAttributeId),
element));
//
list = new List_ServiceElement();
list.Add(new ServiceElement(ElementType.UInt8, (byte)0x1));
list.Add(new ServiceElement(ElementType.UInt8, (byte)0x2));
list.Add(new ServiceElement(ElementType.UInt8, (byte)0x3));
list.Add(new ServiceElement(ElementType.UInt8, (byte)0xFF));
element = new ServiceElement(ElementType.ElementSequence, list);
attrs.Add(new ServiceAttribute(ObexAttributeId.SupportedFormatsList, element));
//
ServiceRecord record = new ServiceRecord(attrs);
return record;
}
}//class
}
|