From 6e5b70888b9a1bc4bb1c1d443402f9e7987e31d1 Mon Sep 17 00:00:00 2001 From: Steve Brokenshire Date: Sun, 6 Dec 2015 01:24:17 +0000 Subject: [PATCH] Added code and unit tests to check that input file has the correct format for vCard3/vCard4 --- source/convert.cpp | 48 +++++++++++++++++++++++++++++++++ source/convert.h | 2 ++ source/tests/xestiaab_convert.h | 27 ++++++++++++++----- 3 files changed, 70 insertions(+), 7 deletions(-) diff --git a/source/convert.cpp b/source/convert.cpp index 2da7e45..ae0e4db 100644 --- a/source/convert.cpp +++ b/source/convert.cpp @@ -17,6 +17,7 @@ // with Xestia Address Book. If not, see #include "convert.h" +#include "vcard/vcard.h" ConvertResult ConvertContact(wxString InputFormat, wxString OutputFormat, wxString InputFilename, wxString OutputFilename){ @@ -72,6 +73,14 @@ ConvertResult ConvertContact(wxString InputFormat, wxString OutputFormat, } + // Check that the input filename is not empty. + + if (InputFilename.empty()){ + + return CONVERTRESULT_INPUTFILEEMPTY; + + } + // Check that the input file given exists. if (!wxFileExists(InputFilename)){ @@ -92,6 +101,7 @@ ConvertResult ConvertContact(wxString InputFormat, wxString OutputFormat, // Check that the output file can be opened. + bool OutputPipe = false; wxFile OutputFile; if (!OutputFilename.IsEmpty()){ @@ -100,6 +110,44 @@ ConvertResult ConvertContact(wxString InputFormat, wxString OutputFormat, return CONVERTRESULT_OUTPUTFILEERROR; } + } else { + + OutputPipe = true; + + } + + // Check that the file has the correct file format. + + if (InputFormat == "vCard4"){ + + // Read from the file. + + wxString InputFileData; + InputFile.ReadAll(&InputFileData, wxConvAuto()); + + vCard InputvCard; + + InputvCard.LoadString(InputFileData); + + if (InputvCard.Get("VERSION") != "4.0"){ + return CONVERTRESULT_INPUTFILEINVALIDFORMAT; + } + + } else if (InputFormat == "vCard3"){ + + // Read from the file. + + wxString InputFileData; + InputFile.ReadAll(&InputFileData, wxConvAuto()); + + vCard InputvCard; + + InputvCard.LoadString(InputFileData); + + if (InputvCard.Get("VERSION") != "3.0"){ + return CONVERTRESULT_INPUTFILEINVALIDFORMAT; + } + } return CONVERTRESULT_OK; diff --git a/source/convert.h b/source/convert.h index 7f71162..a1e4b40 100644 --- a/source/convert.h +++ b/source/convert.h @@ -31,6 +31,8 @@ enum ConvertResult { CONVERTRESULT_INVALIDINPUTFORMAT, CONVERTRESULT_INVALIDOUTPUTFORMAT, CONVERTRESULT_INPUTFILEMISSING, + CONVERTRESULT_INPUTFILEEMPTY, + CONVERTRESULT_INPUTFILEINVALIDFORMAT, CONVERTRESULT_INPUTFILEERROR, CONVERTRESULT_OUTPUTFILEERROR }; diff --git a/source/tests/xestiaab_convert.h b/source/tests/xestiaab_convert.h index 7108715..4077998 100644 --- a/source/tests/xestiaab_convert.h +++ b/source/tests/xestiaab_convert.h @@ -29,7 +29,7 @@ TEST(ConvertCmdLine, ConvertTests){ wxT("Temp-LoadCheck-Load1-v3Conv.vcf"))); ASSERT_EQ(CONVERTRESULT_OK, ConvertContact(wxT("vCard3"), wxT("vCard4"), - wxT("LoadCheck-Load1.vcf"), + wxT("LoadCheck-Load1-v3.vcf"), wxT("Temp-LoadCheck-Load1-v4Conv.vcf"))); // Test that a matching input and output formats return @@ -46,12 +46,12 @@ TEST(ConvertCmdLine, ConvertTests){ wxT("vCard3"), wxT("LoadCheck-Load1.vcf"), wxT("Temp-LoadCheck-Load1-v3Conv.vcf"))); - - // Test that an invalid output format return an error. + + // Test that an empty input filename was given. - ASSERT_EQ(CONVERTRESULT_INVALIDOUTPUTFORMAT, ConvertContact(wxT("vCard3"), - wxT("vCardNope"), - wxT("LoadCheck-Load1.vcf"), + ASSERT_EQ(CONVERTRESULT_INPUTFILEEMPTY, ConvertContact(wxT("vCard3"), + wxT("vCard4"), + wxT(""), wxT("Temp-LoadCheck-Load1-v3Conv.vcf"))); // Check that the input file given exists. @@ -73,6 +73,19 @@ TEST(ConvertCmdLine, ConvertTests){ ASSERT_EQ(CONVERTRESULT_OUTPUTFILEERROR, ConvertContact(wxT("vCard3"), wxT("vCard4"), wxT("LoadCheck-Load1.vcf"), - wxT("InvalidPermissions.vcf"))); + wxT("InvalidPermissions.vcf"))); + + // Check that the input file has the correct format. + // Tests for: vCard 4.0, vCard 3.0. + + ASSERT_EQ(CONVERTRESULT_INPUTFILEINVALIDFORMAT, ConvertContact(wxT("vCard4"), + wxT("vCard3"), + wxT("LoadCheck-Load1-v3.vcf"), + wxT("Temp-LoadCheck-Load1.vcf"))); + + ASSERT_EQ(CONVERTRESULT_INPUTFILEINVALIDFORMAT, ConvertContact(wxT("vCard3"), + wxT("vCard4"), + wxT("LoadCheck-Load1.vcf"), + wxT("Temp-LoadCheck-Load1.vcf"))); } \ No newline at end of file -- 2.39.2