<p>Vadim Yanitskiy has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/14184">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">gsm0408/gsm0408_test.c: introduce BCD number encoding / decoding test<br><br>So far, both gsm48_encode_bcd_number() and gsm48_decode_bcd_number2()<br>were uncovered by unit tests. Let's fill this gap by testing the<br>following scenarios:<br><br>  - encoding / decoding of a regular 6-digit MSISDN;<br>  - encoding / decoding of a long 15-digit MSISDN;<br>  - encoding / decoding of a MSISDN to a buffer:<br>    - with exactly matching size,<br>    - with lower size (truncation);<br>  - decoding LV buffer with incorrect length.<br><br>As it turns out, gsm48_decode_bcd_number2() does not properly<br>handle encoded LV if the output buffer size is equal to the<br>original MSISDN length + 1 (\0-terminator): one digit is lost.<br><br>For example, decoding of 15-digit long MSISDN to a buffer of size<br>16 (15 digits + 1 for \0) would give us only 14 digits. This is<br>reflected in the unit test output:<br><br>  Decoding HEX (buffer limit=16) '0821436587092143f5'...<br>    Expected: (rc=0) '123456789012345'<br>      Actual: (rc=0) '12345678901234'<br><br>Moreover, if the output buffer is shorter than decoded number,<br>gsm48_decode_bcd_number2() silently truncates it and returns 0,<br>while its description states, that the rc should reflect this.<br><br>To be fixed in the follow-up patches.<br><br>Change-Id: I4b2c330cf8ffe4427c0bee7d5f3b74be56ecd85d<br>Related: OS#4025<br>---<br>M tests/gsm0408/gsm0408_test.c<br>M tests/gsm0408/gsm0408_test.ok<br>2 files changed, 179 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/84/14184/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/tests/gsm0408/gsm0408_test.c b/tests/gsm0408/gsm0408_test.c</span><br><span>index 2d60b84..169010e 100644</span><br><span>--- a/tests/gsm0408/gsm0408_test.c</span><br><span>+++ b/tests/gsm0408/gsm0408_test.c</span><br><span>@@ -610,6 +610,150 @@</span><br><span>     printf("\n");</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static const struct bcd_number_test {</span><br><span style="color: hsl(120, 100%, 40%);">+       /* Human-readable test name */</span><br><span style="color: hsl(120, 100%, 40%);">+        const char *test_name;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      /* To be encoded number in ASCII */</span><br><span style="color: hsl(120, 100%, 40%);">+   const char *enc_ascii;</span><br><span style="color: hsl(120, 100%, 40%);">+        /* Expected encoding result in HEX */</span><br><span style="color: hsl(120, 100%, 40%);">+ const char *enc_hex;</span><br><span style="color: hsl(120, 100%, 40%);">+  /* Expected return code */</span><br><span style="color: hsl(120, 100%, 40%);">+    int enc_rc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* To be decoded buffer in HEX */</span><br><span style="color: hsl(120, 100%, 40%);">+     const char *dec_hex;</span><br><span style="color: hsl(120, 100%, 40%);">+  /* Expected decoding result in ASCII */</span><br><span style="color: hsl(120, 100%, 40%);">+       const char *dec_ascii;</span><br><span style="color: hsl(120, 100%, 40%);">+        /* Expected return code */</span><br><span style="color: hsl(120, 100%, 40%);">+    int dec_rc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Encoding buffer limit (0 means unlimited) */</span><br><span style="color: hsl(120, 100%, 40%);">+       size_t enc_buf_lim;</span><br><span style="color: hsl(120, 100%, 40%);">+   /* Decoding buffer limit (0 means unlimited) */</span><br><span style="color: hsl(120, 100%, 40%);">+       size_t dec_buf_lim;</span><br><span style="color: hsl(120, 100%, 40%);">+} bcd_number_test_set[] = {</span><br><span style="color: hsl(120, 100%, 40%);">+      {</span><br><span style="color: hsl(120, 100%, 40%);">+             .test_name = "regular 6-digit MSISDN",</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+            /* Encoding test */</span><br><span style="color: hsl(120, 100%, 40%);">+           .enc_ascii = "123456",</span><br><span style="color: hsl(120, 100%, 40%);">+              .enc_hex   = "03214365",</span><br><span style="color: hsl(120, 100%, 40%);">+            .enc_rc    = 4,</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+             /* Decoding test */</span><br><span style="color: hsl(120, 100%, 40%);">+           .dec_hex   = "03214365",</span><br><span style="color: hsl(120, 100%, 40%);">+            .dec_ascii = "123456",</span><br><span style="color: hsl(120, 100%, 40%);">+      },</span><br><span style="color: hsl(120, 100%, 40%);">+    {</span><br><span style="color: hsl(120, 100%, 40%);">+             .test_name = "long 15-digit (maximum) MSISDN",</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+            /* Encoding test */</span><br><span style="color: hsl(120, 100%, 40%);">+           .enc_ascii = "123456789012345",</span><br><span style="color: hsl(120, 100%, 40%);">+             .enc_hex   = "0821436587092143f5",</span><br><span style="color: hsl(120, 100%, 40%);">+          .enc_rc    = 9,</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+             /* Decoding test */</span><br><span style="color: hsl(120, 100%, 40%);">+           .dec_hex   = "0821436587092143f5",</span><br><span style="color: hsl(120, 100%, 40%);">+          .dec_ascii = "123456789012345",</span><br><span style="color: hsl(120, 100%, 40%);">+     },</span><br><span style="color: hsl(120, 100%, 40%);">+    {</span><br><span style="color: hsl(120, 100%, 40%);">+             .test_name = "long 15-digit (maximum) MSISDN, limited buffer",</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+            /* Encoding test */</span><br><span style="color: hsl(120, 100%, 40%);">+           .enc_ascii = "123456789012345",</span><br><span style="color: hsl(120, 100%, 40%);">+             .enc_hex   = "0821436587092143f5",</span><br><span style="color: hsl(120, 100%, 40%);">+          .enc_rc    = 9,</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+             /* Decoding test */</span><br><span style="color: hsl(120, 100%, 40%);">+           .dec_hex   = "0821436587092143f5",</span><br><span style="color: hsl(120, 100%, 40%);">+          .dec_ascii = "123456789012345",</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           /* Buffer length limitations */</span><br><span style="color: hsl(120, 100%, 40%);">+               .dec_buf_lim = 15 + 1,</span><br><span style="color: hsl(120, 100%, 40%);">+                .enc_buf_lim = 9,</span><br><span style="color: hsl(120, 100%, 40%);">+     },</span><br><span style="color: hsl(120, 100%, 40%);">+    {</span><br><span style="color: hsl(120, 100%, 40%);">+             .test_name = "to be truncated 20-digit MSISDN",</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           /* Encoding test (not enough room in buffer) */</span><br><span style="color: hsl(120, 100%, 40%);">+               .enc_ascii = "12345678901234567890",</span><br><span style="color: hsl(120, 100%, 40%);">+                .enc_hex   = "", /* nothing */</span><br><span style="color: hsl(120, 100%, 40%);">+              .enc_rc    = -EIO,</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+          /* Decoding test (one 5 digits do not fit) */</span><br><span style="color: hsl(120, 100%, 40%);">+         .dec_hex   = "0a21436587092143658709",</span><br><span style="color: hsl(120, 100%, 40%);">+              .dec_ascii = "123456789012345",</span><br><span style="color: hsl(120, 100%, 40%);">+             .dec_rc    = 0,</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+             /* Buffer length limitations */</span><br><span style="color: hsl(120, 100%, 40%);">+               .dec_buf_lim = 15 + 1, /* 5 digits less */</span><br><span style="color: hsl(120, 100%, 40%);">+            .enc_buf_lim = 9,</span><br><span style="color: hsl(120, 100%, 40%);">+     },</span><br><span style="color: hsl(120, 100%, 40%);">+    {</span><br><span style="color: hsl(120, 100%, 40%);">+             .test_name = "LV incorrect length",</span><br><span style="color: hsl(120, 100%, 40%);">+         .dec_hex   = "05214365", /* should be 0x03 */</span><br><span style="color: hsl(120, 100%, 40%);">+               .dec_ascii = "(none)",</span><br><span style="color: hsl(120, 100%, 40%);">+              .dec_rc    = -EIO,</span><br><span style="color: hsl(120, 100%, 40%);">+    },</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void test_bcd_number_encode_decode()</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      const struct bcd_number_test *test;</span><br><span style="color: hsl(120, 100%, 40%);">+   uint8_t buf_enc[0xff] = { 0 };</span><br><span style="color: hsl(120, 100%, 40%);">+        char buf_dec[0xff] = { 0 };</span><br><span style="color: hsl(120, 100%, 40%);">+   size_t buf_len, i;</span><br><span style="color: hsl(120, 100%, 40%);">+    int rc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     printf("BSD number encoding / decoding test\n");</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  for (i = 0; i < ARRAY_SIZE(bcd_number_test_set); i++) {</span><br><span style="color: hsl(120, 100%, 40%);">+            test = &bcd_number_test_set[i];</span><br><span style="color: hsl(120, 100%, 40%);">+           printf("- Running test: %s\n", test->test_name);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+               if (test->enc_ascii) {</span><br><span style="color: hsl(120, 100%, 40%);">+                     if (test->enc_buf_lim)</span><br><span style="color: hsl(120, 100%, 40%);">+                             buf_len = test->enc_buf_lim;</span><br><span style="color: hsl(120, 100%, 40%);">+                       else</span><br><span style="color: hsl(120, 100%, 40%);">+                          buf_len = sizeof(buf_enc);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                  printf("  - Encoding ASCII (buffer limit=%zu) '%s'...\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                          test->enc_buf_lim, test->enc_ascii);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                   rc = gsm48_encode_bcd_number(buf_enc, buf_len,</span><br><span style="color: hsl(120, 100%, 40%);">+                                                     0, test->enc_ascii);</span><br><span style="color: hsl(120, 100%, 40%);">+                  printf("    - Expected: (rc=%d) '%s'\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                           test->enc_rc, test->enc_hex);</span><br><span style="color: hsl(120, 100%, 40%);">+                    printf("    -   Actual: (rc=%d) '%s'\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                           rc, osmo_hexdump_nospc(buf_enc, rc >= 0 ? rc : 0));</span><br><span style="color: hsl(120, 100%, 40%);">+         }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           if (test->dec_hex) {</span><br><span style="color: hsl(120, 100%, 40%);">+                       /* Parse a HEX string */</span><br><span style="color: hsl(120, 100%, 40%);">+                      rc = osmo_hexparse(test->dec_hex, buf_enc, sizeof(buf_enc));</span><br><span style="color: hsl(120, 100%, 40%);">+                       OSMO_ASSERT(rc >= 0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                    if (test->dec_buf_lim)</span><br><span style="color: hsl(120, 100%, 40%);">+                             buf_len = test->dec_buf_lim;</span><br><span style="color: hsl(120, 100%, 40%);">+                       else</span><br><span style="color: hsl(120, 100%, 40%);">+                          buf_len = sizeof(buf_dec);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                  printf("  - Decoding HEX (buffer limit=%zu) '%s'...\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                            test->dec_buf_lim, test->dec_hex);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                     rc = gsm48_decode_bcd_number2(buf_dec, buf_len,</span><br><span style="color: hsl(120, 100%, 40%);">+                                                     buf_enc, rc, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+                        printf("    - Expected: (rc=%d) '%s'\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                           test->dec_rc, test->dec_ascii);</span><br><span style="color: hsl(120, 100%, 40%);">+                  printf("    -   Actual: (rc=%d) '%s'\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                           rc, rc == 0 ? buf_dec : "(none)");</span><br><span style="color: hsl(120, 100%, 40%);">+           }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   printf("\n");</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> struct {</span><br><span>     int range;</span><br><span>   int arfcns_num;</span><br><span>@@ -949,6 +1093,7 @@</span><br><span>       test_mid_from_imsi();</span><br><span>        test_mid_encode_decode();</span><br><span>    test_mid_decode_zero_length();</span><br><span style="color: hsl(120, 100%, 40%);">+        test_bcd_number_encode_decode();</span><br><span>     test_ra_cap();</span><br><span>       test_lai_encode_decode();</span><br><span> </span><br><span>diff --git a/tests/gsm0408/gsm0408_test.ok b/tests/gsm0408/gsm0408_test.ok</span><br><span>index 0bd101d..2875a7e 100644</span><br><span>--- a/tests/gsm0408/gsm0408_test.ok</span><br><span>+++ b/tests/gsm0408/gsm0408_test.ok</span><br><span>@@ -139,6 +139,40 @@</span><br><span>     rc=1</span><br><span>     returned empty string</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+BSD number encoding / decoding test</span><br><span style="color: hsl(120, 100%, 40%);">+- Running test: regular 6-digit MSISDN</span><br><span style="color: hsl(120, 100%, 40%);">+  - Encoding ASCII (buffer limit=0) '123456'...</span><br><span style="color: hsl(120, 100%, 40%);">+    - Expected: (rc=4) '03214365'</span><br><span style="color: hsl(120, 100%, 40%);">+    -   Actual: (rc=4) '03214365'</span><br><span style="color: hsl(120, 100%, 40%);">+  - Decoding HEX (buffer limit=0) '03214365'...</span><br><span style="color: hsl(120, 100%, 40%);">+    - Expected: (rc=0) '123456'</span><br><span style="color: hsl(120, 100%, 40%);">+    -   Actual: (rc=0) '123456'</span><br><span style="color: hsl(120, 100%, 40%);">+- Running test: long 15-digit (maximum) MSISDN</span><br><span style="color: hsl(120, 100%, 40%);">+  - Encoding ASCII (buffer limit=0) '123456789012345'...</span><br><span style="color: hsl(120, 100%, 40%);">+    - Expected: (rc=9) '0821436587092143f5'</span><br><span style="color: hsl(120, 100%, 40%);">+    -   Actual: (rc=9) '0821436587092143f5'</span><br><span style="color: hsl(120, 100%, 40%);">+  - Decoding HEX (buffer limit=0) '0821436587092143f5'...</span><br><span style="color: hsl(120, 100%, 40%);">+    - Expected: (rc=0) '123456789012345'</span><br><span style="color: hsl(120, 100%, 40%);">+    -   Actual: (rc=0) '123456789012345'</span><br><span style="color: hsl(120, 100%, 40%);">+- Running test: long 15-digit (maximum) MSISDN, limited buffer</span><br><span style="color: hsl(120, 100%, 40%);">+  - Encoding ASCII (buffer limit=9) '123456789012345'...</span><br><span style="color: hsl(120, 100%, 40%);">+    - Expected: (rc=9) '0821436587092143f5'</span><br><span style="color: hsl(120, 100%, 40%);">+    -   Actual: (rc=9) '0821436587092143f5'</span><br><span style="color: hsl(120, 100%, 40%);">+  - Decoding HEX (buffer limit=16) '0821436587092143f5'...</span><br><span style="color: hsl(120, 100%, 40%);">+    - Expected: (rc=0) '123456789012345'</span><br><span style="color: hsl(120, 100%, 40%);">+    -   Actual: (rc=0) '12345678901234'</span><br><span style="color: hsl(120, 100%, 40%);">+- Running test: to be truncated 20-digit MSISDN</span><br><span style="color: hsl(120, 100%, 40%);">+  - Encoding ASCII (buffer limit=9) '12345678901234567890'...</span><br><span style="color: hsl(120, 100%, 40%);">+    - Expected: (rc=-5) ''</span><br><span style="color: hsl(120, 100%, 40%);">+    -   Actual: (rc=-5) ''</span><br><span style="color: hsl(120, 100%, 40%);">+  - Decoding HEX (buffer limit=16) '0a21436587092143658709'...</span><br><span style="color: hsl(120, 100%, 40%);">+    - Expected: (rc=0) '123456789012345'</span><br><span style="color: hsl(120, 100%, 40%);">+    -   Actual: (rc=0) '12345678901234'</span><br><span style="color: hsl(120, 100%, 40%);">+- Running test: LV incorrect length</span><br><span style="color: hsl(120, 100%, 40%);">+  - Decoding HEX (buffer limit=0) '05214365'...</span><br><span style="color: hsl(120, 100%, 40%);">+    - Expected: (rc=-5) '(none)'</span><br><span style="color: hsl(120, 100%, 40%);">+    -   Actual: (rc=-5) '(none)'</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> Constructed RA:</span><br><span> 077-121-666-5</span><br><span> MCC+MNC in BCD: 70 17 21 </span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/14184">change 14184</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/14184"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: libosmocore </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: I4b2c330cf8ffe4427c0bee7d5f3b74be56ecd85d </div>
<div style="display:none"> Gerrit-Change-Number: 14184 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Vadim Yanitskiy <axilirator@gmail.com> </div>