#include <Arduino.h>
#include <unity.h>
#include "html_util.h"

static void test_html_escape_basic() {
  TEST_ASSERT_EQUAL_STRING("", html_escape("").c_str());
  TEST_ASSERT_EQUAL_STRING("plain", html_escape("plain").c_str());
  TEST_ASSERT_EQUAL_STRING("a&amp;b", html_escape("a&b").c_str());
  TEST_ASSERT_EQUAL_STRING("&lt;tag&gt;", html_escape("<tag>").c_str());
  TEST_ASSERT_EQUAL_STRING("&quot;hi&quot;", html_escape("\"hi\"").c_str());
  TEST_ASSERT_EQUAL_STRING("it&#39;s", html_escape("it's").c_str());
  TEST_ASSERT_EQUAL_STRING("&amp;&lt;&gt;&quot;&#39;", html_escape("&<>\"'").c_str());
}

static void test_sanitize_device_id() {
  String out;
  TEST_ASSERT_TRUE(sanitize_device_id("F19C", out));
  TEST_ASSERT_EQUAL_STRING("dd3-F19C", out.c_str());
  TEST_ASSERT_TRUE(sanitize_device_id("dd3-f19c", out));
  TEST_ASSERT_EQUAL_STRING("dd3-F19C", out.c_str());
  TEST_ASSERT_FALSE(sanitize_device_id("F19G", out));
  TEST_ASSERT_FALSE(sanitize_device_id("dd3-12", out));
  TEST_ASSERT_FALSE(sanitize_device_id("dd3-12345", out));
  TEST_ASSERT_FALSE(sanitize_device_id("../F19C", out));
  TEST_ASSERT_FALSE(sanitize_device_id("dd3-%2f", out));
  TEST_ASSERT_FALSE(sanitize_device_id("dd3-12/3", out));
  TEST_ASSERT_FALSE(sanitize_device_id("dd3-12\\3", out));
}

void setup() {
  UNITY_BEGIN();
  RUN_TEST(test_html_escape_basic);
  RUN_TEST(test_sanitize_device_id);
  UNITY_END();
}

void loop() {}
