예제 #1
0
        public void StringGivenString()
        {
            ushort LangCodeEn        = 0x656e; // "en"
            ushort LangCodeIs        = 0x6973; // "is"
            ushort EncodingIdUtf8    = 106;
            ushort EncodingIdUtf16BE = 1013;
            ushort BaseA             = 0x0100;
            ushort BaseB             = 0x0100;
            //
            String             str       = "ambds\u2022nbdas\u00FEdlka\U00012004slkda";
            ServiceElement     element_  = new ServiceElement(ElementType.TextString, str);
            ServiceAttributeId id        = ServiceRecord.CreateLanguageBasedAttributeId(UniversalAttributeId.ServiceName, (ServiceAttributeId)BaseA);
            ServiceAttribute   attribute = new ServiceAttribute(id, element_);
            ServiceRecord      record    = CreateRecord(attribute);
            //
            LanguageBaseItem langBaseEn = new LanguageBaseItem(LangCodeEn, EncodingIdUtf8, BaseA);
            LanguageBaseItem langBaseIs = new LanguageBaseItem(LangCodeIs, EncodingIdUtf16BE, BaseB);

            Assert.AreEqual(str, record.GetMultiLanguageStringAttributeById(
                                InTheHand.Net.Bluetooth.AttributeIds.UniversalAttributeId.ServiceName,
                                langBaseEn));
            Assert.AreEqual(str, record.GetMultiLanguageStringAttributeById(
                                InTheHand.Net.Bluetooth.AttributeIds.UniversalAttributeId.ServiceName,
                                langBaseIs));
            //
            ServiceElement element = record.GetAttributeById(UniversalAttributeId.ServiceName, langBaseIs).Value;

            Assert.AreEqual(ElementTypeDescriptor.TextString, element.ElementTypeDescriptor);
            Assert.AreEqual(ElementType.TextString, element.ElementType);
            Assert.IsInstanceOfType(typeof(String), element.Value);
            Assert.AreEqual(str, element.GetValueAsStringUtf8());
            Assert.AreEqual(str, element.GetValueAsString(Encoding.ASCII));
            Assert.AreEqual(str, element.GetValueAsString(Encoding.Unicode));
        }
예제 #2
0
        public void EnIsEncodingsRecordGetStringByIdAndLang()
        {
            String expectedEn = "ab\u2020cd";
            String expectedIs = "ab\u00D0c\u00DEe";

            byte[]        buffer = RecordBytesEnIsEncodings;
            ServiceRecord record = new ServiceRecordParser().Parse(buffer);

            LanguageBaseItem[] langList = record.GetLanguageBaseList();
            //
            LanguageBaseItem langIs = langList[0];

            Assert.AreEqual("is", langIs.NaturalLanguage);
            LanguageBaseItem langEn = langList[1];

            Assert.AreEqual("en", langEn.NaturalLanguage);
            //
            // Here's the stuff really tested here!!!!
            String resultEn = record.GetMultiLanguageStringAttributeById(0, langEn);
            String resultIs = record.GetMultiLanguageStringAttributeById(0, langIs);

            Assert.AreEqual(expectedEn, resultEn);
            Assert.AreEqual(expectedIs, resultIs);
            String resultEnPrimary = record.GetPrimaryMultiLanguageStringAttributeById(0);

            Assert.AreEqual(expectedEn, resultEnPrimary);
        }
예제 #3
0
        public void EnIsSimple()
        {
            String expectedEn = "abcd";
            String expectedIs = "abce";

            byte[]        buffer = RecordBytesEnIs;
            ServiceRecord record = new ServiceRecordParser().Parse(buffer);

            LanguageBaseItem[] langList = record.GetLanguageBaseList();
            //
            LanguageBaseItem langIs = langList[0];

            Assert.AreEqual("is", langIs.NaturalLanguage);
            LanguageBaseItem langEn = langList[1];

            Assert.AreEqual("en", langEn.NaturalLanguage);
            //
            ServiceAttribute attrEn = record.GetAttributeByIndex(1);
            ServiceAttribute attrIs = record.GetAttributeByIndex(2);

            Assert.AreEqual((ServiceAttributeId)0x0100, attrEn.Id);
            Assert.AreEqual((ServiceAttributeId)0x0110, attrIs.Id);
            String resultEn = attrEn.Value.GetValueAsString(langEn);
            String resultIs = attrIs.Value.GetValueAsString(langIs);

            Assert.AreEqual(expectedEn, resultEn);
            Assert.AreEqual(expectedIs, resultIs);
        }
예제 #4
0
        private static ServiceElement CreateEnglishUtf8PrimaryLanguageServiceElement()
        {
            ServiceElement englishUtf8PrimaryLanguage = LanguageBaseItem.CreateElementSequenceFromList(
                new LanguageBaseItem[] { LanguageBaseItem.CreateEnglishUtf8PrimaryLanguageItem() });

            return(englishUtf8PrimaryLanguage);
        }
예제 #5
0
        /// <summary>
        /// Check that the expected Encoding is mapped from the given IETF charset id,
        /// as are used in the LanguageBaseList Attribute.
        /// </summary>
        private void DoTest(Encoding expectedEncoding, ushort ietfCharsetId)
        {
            LanguageBaseItem item =
                new LanguageBaseItem(/*"en"*/ Data_LanguageBaseList.LangEn, ietfCharsetId, (ServiceAttributeId)0x0100);
            Encoding encResult = item.GetEncoding();

            Assert.AreEqual(expectedEncoding, encResult);
        }
예제 #6
0
        public void ContainsByIdAndLangFake()
        {
            ServiceRecord record = RecordAccess_Data.CreateRecordWithZeroItems();
            //
            LanguageBaseItem langFake = new LanguageBaseItem(1, 2, 3);

            Assert.IsFalse(record.Contains(UniversalAttributeId.ServiceRecordHandle, langFake));
        }
예제 #7
0
        public void FromNumberA()
        {
            LanguageBaseItem item = new LanguageBaseItem(Data_LanguageBaseList.LangEn, 2252, LanguageBaseItem.PrimaryLanguageBaseAttributeId);

            Assert.AreEqual(Data_LanguageBaseList.LangEn, item.NaturalLanguageAsUInt16);
            Assert.AreEqual((Int16)Data_LanguageBaseList.LangEn, item.NaturalLanguageAsInt16);
            Assert.AreEqual(Data_LanguageBaseList.LangStringEn, item.NaturalLanguage);
        }
예제 #8
0
        private static ServiceElement CreateEnglishUtf8PrimaryLanguageServiceElement()
        {
            ServiceElement englishUtf8PrimaryLanguage = LanguageBaseItem.CreateElementSequenceFromList(
                new LanguageBaseItem[] {
                new LanguageBaseItem("en", LanguageBaseItem.Utf8EncodingId, LanguageBaseItem.PrimaryLanguageBaseAttributeId)
            });

            return(englishUtf8PrimaryLanguage);
        }
예제 #9
0
        public void AttrByIdAndLangFake()
        {
            ServiceAttribute attr;
            ServiceRecord    record = RecordAccess_Data.CreateRecordWithZeroItems();
            //
            LanguageBaseItem langFake = new LanguageBaseItem(1, 2, 3);

            attr = record.GetAttributeById(UniversalAttributeId.ServiceRecordHandle, langFake);
        }
예제 #10
0
        public void NoSuchAttributeInRecord()
        {
            IList_ServiceAttribute list
                = new List_ServiceAttribute(0);
            ServiceRecord    record = new ServiceRecord(list);
            LanguageBaseItem item   = record.GetPrimaryLanguageBaseItem();

            Assert.IsNull(item, "IsNull--item");
        }
예제 #11
0
        public void LangAttrNoPrimary0100Item()
        {
            IList_ServiceAttribute list = new List_ServiceAttribute();

            list.Add(Data_LanguageBaseList.AttrOneItemBadBaseZero);
            ServiceRecord    record = new ServiceRecord(list);
            LanguageBaseItem item   = record.GetPrimaryLanguageBaseItem();

            Assert.IsNull(item, "GetPrimaryLanguageBaseItem() should be null.");
        }
예제 #12
0
        public void TwoItemsAsArrayToParse()
        {
            ServiceRecord    record = new ServiceRecordParser().Parse(Data_LanguageBaseList.RecordTwoItemsAsBytes);
            LanguageBaseItem item   = record.GetPrimaryLanguageBaseItem();

            //
            Assert.AreEqual(Data_LanguageBaseList.LangStringEn, item.NaturalLanguage, "NaturalLanguage");
            Assert.AreEqual(Data_LanguageBaseList.EncUtf8, item.EncodingId, "EncodingId");
            Assert.AreEqual((ServiceAttributeId)0x0100, item.AttributeIdBase, "AttributeIdBase");
        }
예제 #13
0
 public void OneItem()
 {
     LanguageBaseItem[] items = LanguageBaseItem.ParseListFromElementSequence(Data_LanguageBaseList.AttrOneItem.Value);
     Assert.AreEqual(1, items.Length);
     Assert.AreEqual(Data_LanguageBaseList.LangStringEn, items[0].NaturalLanguage, "NaturalLanguage");
     Assert.AreEqual(Data_LanguageBaseList.LangEn, items[0].NaturalLanguageAsUInt16, "NaturalLanguageUInt16");
     Assert.AreEqual(Data_LanguageBaseList.EncUtf8, items[0].EncodingId, "EncodingId");
     Assert.AreEqual(Data_LanguageBaseList.EncUtf8, items[0].EncodingIdAsInt16, "EncodingId");
     Assert.AreEqual((ServiceAttributeId)0x0100, items[0].AttributeIdBase, "AttributeIdBase");
 }
예제 #14
0
        public void LangAttrInvalid()
        {
            IList_ServiceAttribute list = new List_ServiceAttribute();

            list.Add(Data_LanguageBaseList.AttrFourElements);
            ServiceRecord    record = new ServiceRecord(list);
            LanguageBaseItem item   = record.GetPrimaryLanguageBaseItem();

            Assert.IsNull(item, "GetPrimaryLanguageBaseItem() should be null.");
        }
예제 #15
0
        public String GetPrimaryMultiLanguageStringAttributeById(ServiceAttributeId id)
        {
            LanguageBaseItem lang = this.GetPrimaryLanguageBaseItem();

            if (lang == null)
            {
                lang = LanguageBaseItem.CreateEnglishUtf8PrimaryLanguageItem();
            }
            return(GetMultiLanguageStringAttributeById(id, lang));
        }
예제 #16
0
        //[ExpectedException(typeof(System.Net.ProtocolViolationException), "LanguageBaseList attribute not of type ElementSequence.")]
        public void LangAttrNotElementTypeSeq()
        {
            List_ServiceAttribute list = new List_ServiceAttribute();

            list.Add(new ServiceAttribute(UniversalAttributeId.LanguageBaseAttributeIdList,
                                          new ServiceElement(ElementType.TextString, new byte[] { (byte)'f', (byte)'o', (byte)'o', })));
            ServiceRecord    record = new ServiceRecord(list);
            LanguageBaseItem item   = record.GetPrimaryLanguageBaseItem();

            Assert.IsNull(item, "GetPrimaryLanguageBaseItem() should be null.");
        }
예제 #17
0
 public void BadStringEncodingNotAscii()
 {
     byte[]        buffer = RecordBytesEnIsEncodings;
     ServiceRecord record = new ServiceRecordParser().Parse(buffer);
     //
     ServiceAttribute attr      = record.GetAttributeById((ServiceAttributeId)0x0100);
     const ushort     langEn    = 0x656e;
     const ushort     ietfAscii = 3;
     LanguageBaseItem langBase  = new LanguageBaseItem(langEn, ietfAscii, (ServiceAttributeId)0x0999);
     String           x         = attr.Value.GetValueAsString(langBase);
 }
예제 #18
0
        //--------------------------------------------------------------
        /// <summary>
        /// Determines whether a TextString service attribute with the specified ID
        /// and natural language
        /// is in the List.
        /// </summary>
        /// -
        /// <param name="id">The id of the service attribute to locate, as a
        /// <see cref="T:InTheHand.Net.Bluetooth.ServiceAttributeId"/>.</param>
        /// <param name="language">
        /// Which multi-language version of the string attribute to locate.
        /// </param>
        /// -
        /// <returns>true if item is found in the record; otherwise, false. </returns>
        public bool Contains(ServiceAttributeId id, LanguageBaseItem language)
        {
            if (language == null)
            {
                throw new ArgumentNullException("language");
            }
            ServiceAttributeId actualId = CreateLanguageBasedAttributeId(id, language.AttributeIdBase);
            bool found = TryGetAttributeById(actualId, out var tmp);

            return(found && (tmp.Value.ElementType == ElementType.TextString));
        }
예제 #19
0
        /// <summary>
        /// Returns the attribute with the given ID and natural language.
        /// </summary>
        /// -
        /// <param name="id">The id of the service attribute to locate, as a
        /// <see cref="T:InTheHand.Net.Bluetooth.ServiceAttributeId"/>.</param>
        /// <param name="language">
        /// Which multi-language version of the string attribute to locate.
        /// </param>
        /// -
        /// <returns>A <see cref="T:InTheHand.Net.Bluetooth.ServiceAttribute"/> holding
        /// the attribute with the specified ID and language.
        /// Is never <see langword="null"/>.
        /// </returns>
        /// -
        /// <exception cref="T:System.Collections.Generic.KeyNotFoundException">
        /// There is no attribute with the given Id with the given language base in the record.
        /// </exception>
        public ServiceAttribute GetAttributeById(ServiceAttributeId id, LanguageBaseItem language)
        {
            if (language == null)
            {
                throw new ArgumentNullException("language");
            }
            ServiceAttributeId actualId = CreateLanguageBasedAttributeId(id, language.AttributeIdBase);
            ServiceAttribute   attr     = GetAttributeById(actualId);

            System.Diagnostics.Debug.Assert(attr != null);
            return(attr);
        }
예제 #20
0
 public void TwoItems()
 {
     LanguageBaseItem[] items = LanguageBaseItem.ParseListFromElementSequence(Data_LanguageBaseList.AttrTwoItems.Value);
     Assert.AreEqual(2, items.Length);
     Assert.AreEqual(Data_LanguageBaseList.LangStringEn, items[0].NaturalLanguage, "NaturalLanguage");
     Assert.AreEqual(Data_LanguageBaseList.LangEn, items[0].NaturalLanguageAsUInt16, "NaturalLanguageUInt16");
     Assert.AreEqual(Data_LanguageBaseList.EncUtf8, items[0].EncodingId, "EncodingId");
     Assert.AreEqual((ServiceAttributeId)0x0100, items[0].AttributeIdBase, "AttributeIdBase");
     //
     Assert.AreEqual(Data_LanguageBaseList.LangStringFr, items[1].NaturalLanguage, "NaturalLanguage");
     Assert.AreEqual(Data_LanguageBaseList.LangFr, items[1].NaturalLanguageAsUInt16, "NaturalLanguageUInt16");
     Assert.AreEqual(Data_LanguageBaseList.EncWindows1252, items[1].EncodingId, "EncodingId");
     Assert.AreEqual((ServiceAttributeId)0x0110, items[1].AttributeIdBase, "AttributeIdBase");
 }
예제 #21
0
        public String GetMultiLanguageStringAttributeById(ServiceAttributeId id, LanguageBaseItem language)
        {
            if (language == null)
            {
                throw new ArgumentNullException("language");
            }
            ServiceAttributeId actualId = CreateLanguageBasedAttributeId(id, language.AttributeIdBase);
            ServiceAttribute   attr     = GetAttributeById(actualId);
            ServiceElement     element  = attr.Value;
            // (No need to check that element is of type TextString, that's handled inside the following).
            String str = element.GetValueAsString(language);

            return(str);
        }
예제 #22
0
        public void BadNonStringAttributePrimary()
        {
            byte[]        buffer = Data_CompleteThirdPartyRecords.Xp1Sdp;
            ServiceRecord record = new ServiceRecordParser().Parse(buffer);

            LanguageBaseItem[] langList = record.GetLanguageBaseList();
            //
            LanguageBaseItem langEn = langList[0];

            Assert.AreEqual("en", langEn.NaturalLanguage);
            //
            // There's a attribute 0x0201 of type UInt32, accessing it should fail
            String resultEn = record.GetPrimaryMultiLanguageStringAttributeById((ServiceAttributeId)0x101);

            Assert.Fail("should have thrown!");
        }
예제 #23
0
        public void TestAllDefinedCharsetMappingRows()
        {
            int    numberSuccessful, numberFailed;
            String resultText =
                LanguageBaseItem.TestAllDefinedEncodingMappingRows(out numberSuccessful, out numberFailed);
            int successExpected = 16;
            int failedExpected  = 3;

#if NETCF
            successExpected = 10;
            failedExpected  = 9;
#endif
            Assert.AreEqual(successExpected, numberSuccessful, "numberSuccessful");
            Assert.AreEqual(failedExpected, numberFailed, "numberFailed");
            //-Console.WriteLine(resultText);
        }
예제 #24
0
        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);
        }
예제 #25
0
        /// <summary>
        /// Get the value of the <see cref="F:InTheHand.Net.Bluetooth.ElementTypeDescriptor.TextString"/>,
        /// when it is encoded as specified by the given IETF Charset identifer.
        /// </summary>
        /// -
        /// <remarks>
        /// Note that a strict decoding of the string is carried out
        /// (except on the NETCF where it is not supported).
        /// Thus if the value is not in the specified encoding, or has been
        /// encoded incorrectly, then an error will occur.
        /// </remarks>
        /// -
        /// <returns>
        /// A <see cref="T:System.String"/> holding the value of the
        /// <see cref="F:InTheHand.Net.Bluetooth.ElementTypeDescriptor.TextString"/>
        /// from the service element.
        /// </returns>
        /// -
        /// <exception cref="T:System.InvalidOperationException">
        /// The service element is not of type
        /// <see cref="F:InTheHand.Net.Bluetooth.ElementTypeDescriptor.TextString"/>.
        /// </exception>
        /// <exception cref="T:System.Text.DecoderFallbackException">
        /// If the value in the service element is not a valid string in the given encoding.
        /// </exception>
        public String GetValueAsString(LanguageBaseItem languageBase)
        {
            if (languageBase == null)
            {
                throw new ArgumentNullException("languageBase");
            }
            Encoding enc = languageBase.GetEncoding();

            if (_strictStringDecoding)
            {
                enc = (Encoding)enc.Clone();                          //not in NETCFv1
                enc.DecoderFallback = new DecoderExceptionFallback(); // not in NETCF.
                // Not intended for encoding, but set it anyway.
                enc.EncoderFallback = new EncoderExceptionFallback(); // not in NETCF.
            }

            return(GetValueAsString(enc));
        }
예제 #26
0
        //--------------------------------------------------------------

        /// <summary>
        /// Gets the list of LanguageBaseAttributeId items in the service record.
        /// </summary>
        /// -
        /// <remarks>
        /// See also <see cref="M:InTheHand.Net.Bluetooth.ServiceRecord.GetPrimaryLanguageBaseItem"/>.
        /// </remarks>
        /// -
        /// <returns>
        /// An array of <see cref="T:InTheHand.Net.Bluetooth.LanguageBaseItem"/>.
        /// An array of length zero is returned if the service record contains no such attribute.
        /// </returns>
        /// -
        /// <seealso cref="M:InTheHand.Net.Bluetooth.ServiceRecord.GetPrimaryLanguageBaseItem"/>
        public LanguageBaseItem[] GetLanguageBaseList()
        {
            if (!Contains(InTheHand.Net.Bluetooth.AttributeIds.UniversalAttributeId.LanguageBaseAttributeIdList))
            {
                return(new LanguageBaseItem[0]);
            }
            ServiceAttribute attr = GetAttributeById(InTheHand.Net.Bluetooth.AttributeIds.UniversalAttributeId.LanguageBaseAttributeIdList);

            if (attr.Value.ElementType != ElementType.ElementSequence)
            {
                return(new LanguageBaseItem[0]);
            }
            LanguageBaseItem[] langList;
            try {
                langList = LanguageBaseItem.ParseListFromElementSequence(attr.Value);
            } catch (System.Net.ProtocolViolationException) {
                return(new LanguageBaseItem[0]);
            }
            return(langList);
        }
예제 #27
0
 public void FourElements()
 {
     LanguageBaseItem[] items = LanguageBaseItem.ParseListFromElementSequence(Data_LanguageBaseList.AttrFourElements.Value);
 }
예제 #28
0
 public void OneItemBadNotSeq()
 {
     LanguageBaseItem[] items = LanguageBaseItem.ParseListFromElementSequence(Data_LanguageBaseList.AttrOneItemBadNotSeq.Value);
 }
예제 #29
0
        /// <summary>
        /// Get a list of installed services.
        /// </summary>
        /// <returns>
        /// The installed services.
        /// </returns>
        public Dictionary <Guid, string> GetInstalledServices()
        {
            var services = new Dictionary <Guid, string>();

            foreach (var service in this.InstalledServices)
            {
                var serviceNames = string.Empty;
                var records      = this.GetServiceRecords(service);
                foreach (var record in records.Where(record => record.Contains(UniversalAttributeId.ServiceName) && record.AttributeIds.Contains(UniversalAttributeId.ServiceName)))
                {
                    try
                    {
                        var serviceName = record.GetMultiLanguageStringAttributeById(UniversalAttributeId.ServiceName, LanguageBaseItem.CreateEnglishUtf8PrimaryLanguageItem());
                        serviceNames += (serviceNames.Length > 0 ? ", " : "") + serviceName;
                    }
                    catch (KeyNotFoundException)
                    {
                        // Apparently the check is not enough
                    }
                }

                services.Add(service, serviceNames);
            }

            return(services);
        }
예제 #30
0
        public void Element_GetStringLNull()
        {
            LanguageBaseItem enc = null;

            CreateInt16ServiceElement().GetValueAsString(enc);
        }