hardened upload function with test cases