@@ -470,6 +470,266 @@ func TestGetDefaultCmsisPackRoot(t *testing.T) {
470470 }
471471}
472472
473+ func TestUpdatePublicIndexIfOnline (t * testing.T ) {
474+ assert := assert .New (t )
475+
476+ t .Run ("test with existing index and recent update.cfg" , func (t * testing.T ) {
477+ localTestingDir := "test-update-online-no-update-needed"
478+ assert .Nil (installer .SetPackRoot (localTestingDir , CreatePackRoot ))
479+ installer .UnlockPackRoot ()
480+ defer removePackRoot (localTestingDir )
481+
482+ // Create a test server
483+ server := NewServer ()
484+ defer server .httpsServer .Close ()
485+
486+ // Create a public index file
487+ publicIndexContent , err := os .ReadFile (samplePublicIndex )
488+ assert .Nil (err )
489+
490+ // Add route for the public index
491+ server .AddRoute ("index.pidx" , publicIndexContent )
492+
493+ // Update the Installation to use the test server
494+ installer .Installation .PublicIndexXML .URL = server .URL () + "index.pidx"
495+ installer .ActualPublicIndex = server .URL () + "index.pidx"
496+
497+ // Create an existing index file
498+ assert .Nil (utils .TouchFile (installer .Installation .PublicIndex ))
499+
500+ // Create a recent update.cfg file (less than one day old)
501+ updateCfgPath := filepath .Join (installer .Installation .WebDir , "update.cfg" )
502+ recentDate := time .Now ().Format ("2-1-2006" )
503+ updateCfgContent := "Date: " + recentDate + "\n Auto: true\n "
504+ assert .Nil (os .WriteFile (updateCfgPath , []byte (updateCfgContent ), 0600 ))
505+
506+ // Get modification time before
507+ statBefore , err := os .Stat (installer .Installation .PublicIndex )
508+ assert .Nil (err )
509+ modTimeBefore := statBefore .ModTime ()
510+
511+ // Call UpdatePublicIndexIfOnline - should not update because update.cfg is recent
512+ err = installer .UpdatePublicIndexIfOnline ()
513+ // May fail internally due to connection check, but always returns nil
514+ assert .Nil (err )
515+
516+ // public index should not be modified since update.cfg is recent
517+ statAfter , err2 := os .Stat (installer .Installation .PublicIndex )
518+ assert .Nil (err2 )
519+ modTimeAfter := statAfter .ModTime ()
520+
521+ // File modification times should be very close (within filesystem precision)
522+ // If the index was updated, the mod time would be significantly different
523+ timeDiff := modTimeAfter .Sub (modTimeBefore ).Abs ()
524+ assert .True (timeDiff < 2 * time .Second , "Index should not be updated when update.cfg is recent (time diff: %v)" , timeDiff )
525+ })
526+
527+ t .Run ("test with existing index and old update.cfg" , func (t * testing.T ) {
528+ localTestingDir := "test-update-online-old-updatecfg"
529+ assert .Nil (installer .SetPackRoot (localTestingDir , CreatePackRoot ))
530+ installer .UnlockPackRoot ()
531+ defer removePackRoot (localTestingDir )
532+
533+ // Create a test server
534+ server := NewServer ()
535+ defer server .httpsServer .Close ()
536+
537+ // Create a public index file
538+ publicIndexContent , err := os .ReadFile (samplePublicIndex )
539+ assert .Nil (err )
540+
541+ // Add route for the public index
542+ server .AddRoute ("index.pidx" , publicIndexContent )
543+
544+ // Update the Installation to use the test server
545+ installer .Installation .PublicIndexXML .URL = server .URL () + "index.pidx"
546+ installer .ActualPublicIndex = server .URL () + "index.pidx"
547+
548+ // Create an existing index file
549+ assert .Nil (utils .TouchFile (installer .Installation .PublicIndex ))
550+
551+ // Create an old update.cfg file (more than one day old)
552+ updateCfgPath := filepath .Join (installer .Installation .WebDir , "update.cfg" )
553+ oldDate := time .Now ().AddDate (0 , 0 , - 2 ).Format ("2-1-2006" )
554+ updateCfgContent := "Date: " + oldDate + "\n Auto: true\n "
555+ assert .Nil (os .WriteFile (updateCfgPath , []byte (updateCfgContent ), 0600 ))
556+
557+ // Call UpdatePublicIndexIfOnline - may fail depending on connection but should return nil
558+ err = installer .UpdatePublicIndexIfOnline ()
559+ // May fail internally due to connection check, but always returns nil
560+ assert .Nil (err )
561+ })
562+
563+ t .Run ("test with missing index file" , func (t * testing.T ) {
564+ localTestingDir := "test-update-missing-index"
565+ assert .Nil (installer .SetPackRoot (localTestingDir , CreatePackRoot ))
566+ installer .UnlockPackRoot ()
567+ defer removePackRoot (localTestingDir )
568+
569+ // Create a test server
570+ server := NewServer ()
571+ defer server .httpsServer .Close ()
572+
573+ // Create a public index file
574+ publicIndexContent , err := os .ReadFile (samplePublicIndex )
575+ assert .Nil (err )
576+
577+ // Add route for the public index
578+ server .AddRoute ("index.pidx" , publicIndexContent )
579+
580+ // Update the Installation to use the test server
581+ installer .Installation .PublicIndexXML .URL = server .URL () + "index.pidx"
582+ installer .ActualPublicIndex = server .URL () + "index.pidx"
583+
584+ // Ensure index file does not exist
585+ if utils .FileExists (installer .Installation .PublicIndex ) {
586+ os .Remove (installer .Installation .PublicIndex )
587+ }
588+
589+ // Call UpdatePublicIndexIfOnline - should try to download
590+ err = installer .UpdatePublicIndexIfOnline ()
591+ // Should always succeed
592+ assert .Nil (err )
593+
594+ // Verify that index file was created
595+ assert .True (utils .FileExists (installer .Installation .PublicIndex ))
596+
597+ // Verify that update.cfg was created
598+ updateCfgPath := filepath .Join (installer .Installation .WebDir , "update.cfg" )
599+ assert .True (utils .FileExists (updateCfgPath ))
600+ })
601+
602+ t .Run ("test with missing index and download failure" , func (t * testing.T ) {
603+ localTestingDir := "test-update-missing-index-download-failure"
604+ assert .Nil (installer .SetPackRoot (localTestingDir , CreatePackRoot ))
605+ installer .UnlockPackRoot ()
606+ defer removePackRoot (localTestingDir )
607+
608+ // Create a test server that returns 404
609+ server := NewServer ()
610+ defer server .httpsServer .Close ()
611+
612+ // Add route that returns nil (causes 404)
613+ server .AddRoute ("index.pidx" , nil )
614+
615+ // Update the Installation to use the test server
616+ installer .Installation .PublicIndexXML .URL = server .URL () + "index.pidx"
617+ installer .ActualPublicIndex = server .URL () + "index.pidx"
618+
619+ // Ensure index file does not exist
620+ if utils .FileExists (installer .Installation .PublicIndex ) {
621+ os .Remove (installer .Installation .PublicIndex )
622+ }
623+
624+ // Call UpdatePublicIndexIfOnline
625+ err := installer .UpdatePublicIndexIfOnline ()
626+ // Should not return error because WarningInsteadOfErrors is true
627+ assert .Nil (err )
628+ })
629+
630+ t .Run ("test with corrupted update.cfg file" , func (t * testing.T ) {
631+ localTestingDir := "test-update-corrupted-updatecfg"
632+ assert .Nil (installer .SetPackRoot (localTestingDir , CreatePackRoot ))
633+ installer .UnlockPackRoot ()
634+ defer removePackRoot (localTestingDir )
635+
636+ // Create a test server
637+ server := NewServer ()
638+ defer server .httpsServer .Close ()
639+
640+ // Create a public index file
641+ publicIndexContent , err := os .ReadFile (samplePublicIndex )
642+ assert .Nil (err )
643+
644+ // Add route for the public index
645+ server .AddRoute ("index.pidx" , publicIndexContent )
646+
647+ // Update the Installation to use the test server
648+ installer .Installation .PublicIndexXML .URL = server .URL () + "index.pidx"
649+ installer .ActualPublicIndex = server .URL () + "index.pidx"
650+
651+ // Create an existing index file
652+ assert .Nil (utils .TouchFile (installer .Installation .PublicIndex ))
653+
654+ // Create a corrupted update.cfg file (invalid date format)
655+ updateCfgPath := filepath .Join (installer .Installation .WebDir , "update.cfg" )
656+ corruptedContent := "Date: invalid-date-format\n Auto: true\n "
657+ assert .Nil (os .WriteFile (updateCfgPath , []byte (corruptedContent ), 0600 ))
658+
659+ // Call UpdatePublicIndexIfOnline - should handle corrupted file gracefully
660+ err = installer .UpdatePublicIndexIfOnline ()
661+ // May fail internally due to connection check, but always returns nil
662+ assert .Nil (err )
663+ })
664+
665+ t .Run ("test with missing update.cfg file" , func (t * testing.T ) {
666+ localTestingDir := "test-update-missing-updatecfg"
667+ assert .Nil (installer .SetPackRoot (localTestingDir , CreatePackRoot ))
668+ installer .UnlockPackRoot ()
669+ defer removePackRoot (localTestingDir )
670+
671+ // Create a test server
672+ server := NewServer ()
673+ defer server .httpsServer .Close ()
674+
675+ // Create a public index file
676+ publicIndexContent , err := os .ReadFile (samplePublicIndex )
677+ assert .Nil (err )
678+
679+ // Add route for the public index
680+ server .AddRoute ("index.pidx" , publicIndexContent )
681+
682+ // Update the Installation to use the test server
683+ installer .Installation .PublicIndexXML .URL = server .URL () + "index.pidx"
684+ installer .ActualPublicIndex = server .URL () + "index.pidx"
685+
686+ // Create an existing index file
687+ assert .Nil (utils .TouchFile (installer .Installation .PublicIndex ))
688+
689+ // Ensure update.cfg does not exist
690+ updateCfgPath := filepath .Join (installer .Installation .WebDir , "update.cfg" )
691+ if utils .FileExists (updateCfgPath ) {
692+ os .Remove (updateCfgPath )
693+ }
694+
695+ // Call UpdatePublicIndexIfOnline - should try to update because update.cfg is missing
696+ err = installer .UpdatePublicIndexIfOnline ()
697+ // May fail internally due to connection check, but always returns nil
698+ assert .Nil (err )
699+ })
700+
701+ t .Run ("test with invalid XML response" , func (t * testing.T ) {
702+ localTestingDir := "test-update-invalid-xml"
703+ assert .Nil (installer .SetPackRoot (localTestingDir , CreatePackRoot ))
704+ installer .UnlockPackRoot ()
705+ defer removePackRoot (localTestingDir )
706+
707+ // Create a test server
708+ server := NewServer ()
709+ defer server .httpsServer .Close ()
710+
711+ // Create invalid XML content
712+ invalidIndexContent := []byte ("This is not valid XML content" )
713+
714+ // Add route for the public index
715+ server .AddRoute ("index.pidx" , invalidIndexContent )
716+
717+ // Update the Installation to use the test server
718+ installer .Installation .PublicIndexXML .URL = server .URL () + "index.pidx"
719+ installer .ActualPublicIndex = server .URL () + "index.pidx"
720+
721+ // Ensure index file does not exist
722+ if utils .FileExists (installer .Installation .PublicIndex ) {
723+ os .Remove (installer .Installation .PublicIndex )
724+ }
725+
726+ // Call UpdatePublicIndexIfOnline
727+ err := installer .UpdatePublicIndexIfOnline ()
728+ // Should handle gracefully with warning (WarningInsteadOfErrors is true)
729+ assert .Nil (err )
730+ })
731+ }
732+
473733func TestUpdatePublicIndex (t * testing.T ) {
474734
475735 assert := assert .New (t )
0 commit comments