benc_serializeString_test.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #define string_strcmp
  16. #include "util/platform/libc/string.h"
  17. #include "memory/Allocator.h"
  18. #include "memory/MallocAllocator.h"
  19. #include "io/Reader.h"
  20. #include "io/ArrayReader.h"
  21. #include "io/Writer.h"
  22. #include "io/ArrayWriter.h"
  23. #include "benc/String.h"
  24. #include "benc/serialization/BencSerializer.h"
  25. #include "benc/serialization/standard/StandardBencSerializer.h"
  26. #include "util/Assert.h"
  27. #include <stdio.h>
  28. static int expect(char* str, struct Writer* writer, struct Reader* reader)
  29. {
  30. int ret = 0;
  31. char buffer[32];
  32. Writer_write(writer, "\0", 1);
  33. Reader_read(reader, buffer, strlen(str) + 1);
  34. if (strcmp(str, buffer) != 0) {
  35. printf("Expected %s\n Got %s\n", str, buffer);
  36. return -1;
  37. }
  38. return ret;
  39. }
  40. static void testSerialize(struct Writer* writer, struct Reader* reader)
  41. {
  42. Assert_always(!StandardBencSerializer_get()->serializeString(writer, String_CONST("hello")));
  43. Assert_always(!expect("5:hello", writer, reader));
  44. Assert_always(!StandardBencSerializer_get()->serializeString(writer, String_CONST("")));
  45. Assert_always(!expect("0:", writer, reader));
  46. }
  47. static void testParse(struct Writer* w, struct Reader* r, struct Allocator* alloc)
  48. {
  49. char* badBenc = "d2:aq21:RouterModule_pingNode4:argsd4:path39:fcd9:6a75:6c9c7:timeouti4000ee"
  50. "6:cookie0:4:hash64:09c6bcd1482df339757c99bbc5e796192968a28562f701fb53a57ed6"
  51. "e26b15511:q4:auth4:txid19:43866780dc455e15619e";
  52. Writer_write(w, badBenc, strlen(badBenc)+1);
  53. Dict dict;
  54. Assert_always(StandardBencSerializer_get()->parseDictionary(r, alloc, &dict));
  55. }
  56. int main()
  57. {
  58. char out[512];
  59. struct Allocator* alloc = MallocAllocator_new(1<<20);
  60. struct Writer* writer = ArrayWriter_new(out, 512, alloc);
  61. struct Reader* reader = ArrayReader_new(out, 512, alloc);
  62. testSerialize(writer, reader);
  63. testParse(writer, reader, alloc);
  64. Allocator_free(alloc);
  65. return 0;
  66. }