@@ -2789,6 +2789,8 @@ void CheckTLVByteSpan(nlTestSuite * inSuite, void * inContext)
2789
2789
NL_TEST_ASSERT (inSuite, memcmp (readerSpan.data (), bytesBuffer, sizeof (bytesBuffer)) == 0 );
2790
2790
}
2791
2791
2792
+ #define IS1_CHAR " \x1F "
2793
+
2792
2794
void CheckTLVCharSpan (nlTestSuite * inSuite, void * inContext)
2793
2795
{
2794
2796
struct CharSpanTestCase
@@ -2799,12 +2801,14 @@ void CheckTLVCharSpan(nlTestSuite * inSuite, void * inContext)
2799
2801
2800
2802
// clang-format off
2801
2803
static CharSpanTestCase sCharSpanTestCases [] = {
2802
- // Test String Expected String from Get()
2803
- // =========================================================================================
2804
- { " This is a test case #0" , " This is a test case #0" },
2805
- { " This is a test case #1\x1f Test Localized String Identifier" , " This is a test case #1" },
2806
- { " This is a test case #2 \x1f abc \x1f def" , " This is a test case #2 " },
2807
- { " This is a test case #3\x1f " , " This is a test case #3" },
2804
+ // Test String Expected String from Get()
2805
+ // ==================================================================================================
2806
+ { " This is a test case #0" , " This is a test case #0" },
2807
+ { " This is a test case #1" IS1_CHAR " Test Localized String Identifier" , " This is a test case #1" },
2808
+ { " This is a test case #2 " IS1_CHAR " abc" IS1_CHAR " def" , " This is a test case #2 " },
2809
+ { " This is a test case #3" IS1_CHAR, " This is a test case #3" },
2810
+ { " Thé" IS1_CHAR, " Thé" },
2811
+ { IS1_CHAR " abc " IS1_CHAR " def" , " " },
2808
2812
};
2809
2813
// clang-format on
2810
2814
@@ -2836,6 +2840,110 @@ void CheckTLVCharSpan(nlTestSuite * inSuite, void * inContext)
2836
2840
}
2837
2841
}
2838
2842
2843
+ void CheckTLVGetLocalizedStringIdentifier (nlTestSuite * inSuite, void * inContext)
2844
+ {
2845
+ struct CharSpanTestCase
2846
+ {
2847
+ const char * testString;
2848
+ Optional<LocalizedStringIdentifier> expectedLSID;
2849
+ CHIP_ERROR expectedResult;
2850
+ };
2851
+
2852
+ // clang-format off
2853
+ static CharSpanTestCase sCharSpanTestCases [] = {
2854
+ // Test String Expected LocalizedStringIdentifier from Get() Expected Return
2855
+ // =============================================================================================================================================
2856
+ { " This is a test case #0" , chip::Optional<LocalizedStringIdentifier>(), CHIP_NO_ERROR },
2857
+ { " This is a test case #1" IS1_CHAR " 0123" , chip::Optional<LocalizedStringIdentifier>(), CHIP_ERROR_INVALID_TLV_ELEMENT },
2858
+ { " This is a test case #2" IS1_CHAR " 123" IS1_CHAR " 3210" , chip::Optional<LocalizedStringIdentifier>(0x123 ), CHIP_NO_ERROR },
2859
+ { " This is a test case #3" IS1_CHAR " 012" , chip::Optional<LocalizedStringIdentifier>(), CHIP_ERROR_INVALID_TLV_ELEMENT },
2860
+ { " This is a test case #3" IS1_CHAR " 12" , chip::Optional<LocalizedStringIdentifier>(0x12 ), CHIP_NO_ERROR },
2861
+ { " Thé" IS1_CHAR " " , chip::Optional<LocalizedStringIdentifier>(), CHIP_NO_ERROR },
2862
+ { " Thé" IS1_CHAR " 7" , chip::Optional<LocalizedStringIdentifier>(0x7 ), CHIP_NO_ERROR },
2863
+ { " Thé" IS1_CHAR " 1FA" , chip::Optional<LocalizedStringIdentifier>(0x1FA ), CHIP_NO_ERROR },
2864
+ { " " IS1_CHAR " 1FA" , chip::Optional<LocalizedStringIdentifier>(0x1FA ), CHIP_NO_ERROR },
2865
+ { " Thé" IS1_CHAR " 1FAB" , chip::Optional<LocalizedStringIdentifier>(0x1FAB ), CHIP_NO_ERROR },
2866
+ { " Thé" IS1_CHAR " 1FAb" , chip::Optional<LocalizedStringIdentifier>(), CHIP_ERROR_INVALID_TLV_ELEMENT },
2867
+ { " Thé" IS1_CHAR " 1FABC" , chip::Optional<LocalizedStringIdentifier>(), CHIP_ERROR_INVALID_TLV_ELEMENT },
2868
+ { " Thé" IS1_CHAR " 1FA" IS1_CHAR " " , chip::Optional<LocalizedStringIdentifier>(0x1FA ), CHIP_NO_ERROR },
2869
+ { " Thé" IS1_CHAR " 1FA" IS1_CHAR " F8sa===" , chip::Optional<LocalizedStringIdentifier>(0x1FA ), CHIP_NO_ERROR },
2870
+ };
2871
+ // clang-format on
2872
+
2873
+ for (auto & testCase : sCharSpanTestCases )
2874
+ {
2875
+ uint8_t backingStore[100 ];
2876
+ TLVWriter writer;
2877
+ TLVReader reader;
2878
+ CHIP_ERROR err = CHIP_NO_ERROR;
2879
+
2880
+ writer.Init (backingStore);
2881
+
2882
+ err = writer.PutString (ProfileTag (TestProfile_1, 1 ), testCase.testString );
2883
+ NL_TEST_ASSERT (inSuite, err == CHIP_NO_ERROR);
2884
+
2885
+ err = writer.Finalize ();
2886
+ NL_TEST_ASSERT (inSuite, err == CHIP_NO_ERROR);
2887
+
2888
+ reader.Init (backingStore, writer.GetLengthWritten ());
2889
+ err = reader.Next ();
2890
+ NL_TEST_ASSERT (inSuite, err == CHIP_NO_ERROR);
2891
+
2892
+ Optional<LocalizedStringIdentifier> readerLSID;
2893
+ err = reader.Get (readerLSID);
2894
+ NL_TEST_ASSERT (inSuite, testCase.expectedResult == err);
2895
+ NL_TEST_ASSERT (inSuite, testCase.expectedLSID == readerLSID);
2896
+ }
2897
+
2898
+ // Error case: A case of TLVReader buffer underrun.
2899
+ // Expected error after Next() call is: CHIP_ERROR_TLV_UNDERRUN
2900
+ {
2901
+ uint8_t backingStore[100 ];
2902
+ TLVWriter writer;
2903
+ TLVReader reader;
2904
+ CHIP_ERROR err = CHIP_NO_ERROR;
2905
+
2906
+ writer.Init (backingStore);
2907
+
2908
+ err = writer.PutString (ProfileTag (TestProfile_1, 1 ), sCharSpanTestCases [2 ].testString );
2909
+ NL_TEST_ASSERT (inSuite, err == CHIP_NO_ERROR);
2910
+
2911
+ err = writer.Finalize ();
2912
+ NL_TEST_ASSERT (inSuite, err == CHIP_NO_ERROR);
2913
+
2914
+ reader.Init (backingStore, writer.GetLengthWritten () - 1 );
2915
+ err = reader.Next ();
2916
+ NL_TEST_ASSERT (inSuite, err == CHIP_ERROR_TLV_UNDERRUN);
2917
+ }
2918
+
2919
+ // Error case: the reader is on a bytestring, not utf-8 string.
2920
+ // Expected error after Get(Optional<LocalizedStringIdentifier> &) call is: CHIP_ERROR_WRONG_TLV_TYPE
2921
+ {
2922
+ uint8_t backingStore[100 ];
2923
+ TLVWriter writer;
2924
+ TLVReader reader;
2925
+ CHIP_ERROR err = CHIP_NO_ERROR;
2926
+
2927
+ writer.Init (backingStore);
2928
+
2929
+ err = writer.PutBytes (ProfileTag (TestProfile_1, 1 ), reinterpret_cast <const uint8_t *>(sCharSpanTestCases [2 ].testString ),
2930
+ static_cast <uint32_t >(strlen (sCharSpanTestCases [2 ].testString )));
2931
+ NL_TEST_ASSERT (inSuite, err == CHIP_NO_ERROR);
2932
+
2933
+ err = writer.Finalize ();
2934
+ NL_TEST_ASSERT (inSuite, err == CHIP_NO_ERROR);
2935
+
2936
+ reader.Init (backingStore, writer.GetLengthWritten ());
2937
+ err = reader.Next ();
2938
+ NL_TEST_ASSERT (inSuite, err == CHIP_NO_ERROR);
2939
+
2940
+ Optional<LocalizedStringIdentifier> readerLSID;
2941
+ err = reader.Get (readerLSID);
2942
+ NL_TEST_ASSERT (inSuite, err == CHIP_ERROR_WRONG_TLV_TYPE);
2943
+ NL_TEST_ASSERT (inSuite, readerLSID == Optional<LocalizedStringIdentifier>());
2944
+ }
2945
+ }
2946
+
2839
2947
void CheckTLVSkipCircular (nlTestSuite * inSuite, void * inContext)
2840
2948
{
2841
2949
const size_t bufsize = 40 ; // large enough s.t. 2 elements fit, 3rd causes eviction
@@ -4514,6 +4622,7 @@ static const nlTest sTests[] =
4514
4622
NL_TEST_DEF (" CHIP TLV Skip non-contiguous" , CheckTLVSkipCircular),
4515
4623
NL_TEST_DEF (" CHIP TLV ByteSpan" , CheckTLVByteSpan),
4516
4624
NL_TEST_DEF (" CHIP TLV CharSpan" , CheckTLVCharSpan),
4625
+ NL_TEST_DEF (" CHIP TLV Get LocalizedStringIdentifier" , CheckTLVGetLocalizedStringIdentifier),
4517
4626
NL_TEST_DEF (" CHIP TLV Scoped Buffer" , CheckTLVScopedBuffer),
4518
4627
NL_TEST_DEF (" CHIP TLV Check reserve" , CheckCloseContainerReserve),
4519
4628
NL_TEST_DEF (" CHIP TLV Reader Fuzz Test" , TLVReaderFuzzTest),
0 commit comments