[ 
https://issues.apache.org/jira/browse/DISPATCH-1568?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17039010#comment-17039010
 ] 

ASF GitHub Bot commented on DISPATCH-1568:
------------------------------------------

jdanekrh commented on pull request #684: WIP: (Work In Progress) DISPATCH-1568: 
using doctest 
URL: https://github.com/apache/qpid-dispatch/pull/684#discussion_r380620641
 
 

 ##########
 File path: src/router_core/terminus.c
 ##########
 @@ -75,25 +76,47 @@ void qdr_terminus_free(qdr_terminus_t *term)
     free_qdr_terminus_t(term);
 }
 
+#ifdef TESTING
+__attribute__((weak))
+int mocked_vsnprintf(char *str, size_t size, const char *format, ...){return 
-1;}
+#endif
 
 // DISPATCH-1461: snprintf() is evil - it returns >= size on overflow.  This
 // wrapper will never return >= size, even if truncated.  This makes it safe to
 // do pointer & length arithmetic without overflowing the destination buffer in
 // qdr_terminus_format()
-//
-static inline int safe_snprintf(char *str, size_t size, const char *format, 
...)
-{
+// not static to be used  unit-tested
+size_t safe_snprintf(char *str, size_t size, const char *format, ...) {
+    // max size allowed must be INT_MAX (since vsnprintf reutrns an int)
+    if (size == 0 || size > INT_MAX) {
+        //TODO log a warning somewhere?
+        return 0;
+    }
+    int max_possible_return_value = (int)(size - 1);
     va_list ap;
     va_start(ap, format);
+#ifdef TESTING
+    int rc = mocked_vsnprintf(str, size, format, ap);
 
 Review comment:
   I was thinking about this and I don't like this much. I understand that in 
C, there isn't many options to replace vsnprintf calls in tests, but this 
"pollutes" the production code a lot.
   
   In the C++ world, they seem to call these techniques "seams", [1] [2] [3]. 
Dispatch already uses the linker seam in two places, to mock 
`qd_server_timeout` and `qd_timer_now`.
   
   I generally like using the linker better, it seems cleaner to me.
   
   [1] http://www.informit.com/articles/article.aspx?p=359417&seqNum=3
   [2] https://accu.org/index.php/journals/1927 Refactoring Towards Seams in C++
   [3] https://cute-test.com/guides/mocking-with-cute/
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> add c-unittest support
> ----------------------
>
>                 Key: DISPATCH-1568
>                 URL: https://issues.apache.org/jira/browse/DISPATCH-1568
>             Project: Qpid Dispatch
>          Issue Type: Improvement
>          Components: Router Node
>            Reporter: Nicolas
>            Priority: Major
>   Original Estimate: 336h
>  Remaining Estimate: 336h
>
> Right now we are not using any framework for easily write c unit-test when 
> developing (or later).
> The idea is to research available libraries, pick one and try to include in 
> our workflow.
> preliminary candidates:
> [https://github.com/google/googletest]
> https://github.com/catchorg/Catch2
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@qpid.apache.org
For additional commands, e-mail: dev-h...@qpid.apache.org

Reply via email to